blob: d04baf999a94cfa6a07e74861876d6a9f1c88a6d [file] [log] [blame]
Bryan Larsen7672db22005-07-08 16:51:55 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
Peter Eriksen8e440252006-04-02 14:44:09 +02005 * Copyright (C) Junio C Hamano, 2005
Bryan Larsen7672db22005-07-08 16:51:55 -07006 */
Stephen Boydc2e86ad2011-03-22 00:51:05 -07007#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07008#include "config.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02009#include "blob.h"
Adam Robend8ee4832008-05-23 16:19:38 +020010#include "quote.h"
Dmitry Potapov548601a2008-08-03 18:36:20 +040011#include "parse-options.h"
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +010012#include "exec_cmd.h"
Bryan Larsen7672db22005-07-08 16:51:55 -070013
Junio C Hamano5ba9a932014-09-11 13:14:51 -070014/*
15 * This is to create corrupt objects for debugging and as such it
16 * needs to bypass the data conversion performed by, and the type
17 * limitation imposed by, index_fd() and its callees.
18 */
19static int hash_literally(unsigned char *sha1, int fd, const char *type, unsigned flags)
20{
21 struct strbuf buf = STRBUF_INIT;
22 int ret;
23
24 if (strbuf_read(&buf, fd, 4096) < 0)
25 ret = -1;
Junio C Hamano5ba9a932014-09-11 13:14:51 -070026 else
Eric Sunshine0c3db672015-05-04 03:25:15 -040027 ret = hash_sha1_file_literally(buf.buf, buf.len, type, sha1, flags);
Junio C Hamano5ba9a932014-09-11 13:14:51 -070028 strbuf_release(&buf);
29 return ret;
30}
31
32static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
33 int literally)
Bryan Larsen7672db22005-07-08 16:51:55 -070034{
Bryan Larsen7672db22005-07-08 16:51:55 -070035 struct stat st;
36 unsigned char sha1[20];
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -070037
Dmitry Potapov43df4f82008-08-03 08:39:16 +040038 if (fstat(fd, &st) < 0 ||
Junio C Hamano5ba9a932014-09-11 13:14:51 -070039 (literally
40 ? hash_literally(sha1, fd, type, flags)
41 : index_fd(sha1, fd, &st, type_from_string(type), path, flags)))
Junio C Hamano17b787f2014-09-11 12:44:05 -070042 die((flags & HASH_WRITE_OBJECT)
Bryan Larsen7672db22005-07-08 16:51:55 -070043 ? "Unable to add %s to database"
44 : "Unable to hash %s", path);
45 printf("%s\n", sha1_to_hex(sha1));
Adam Robend8ee4832008-05-23 16:19:38 +020046 maybe_flush_or_die(stdout, "hash to stdout");
Bryan Larsen7672db22005-07-08 16:51:55 -070047}
48
Junio C Hamano17b787f2014-09-11 12:44:05 -070049static void hash_object(const char *path, const char *type, const char *vpath,
Junio C Hamano5ba9a932014-09-11 13:14:51 -070050 unsigned flags, int literally)
Daniel Barkalow024510c2005-12-10 17:25:24 -050051{
Dmitry Potapov43df4f82008-08-03 08:39:16 +040052 int fd;
53 fd = open(path, O_RDONLY);
54 if (fd < 0)
Thomas Rast0721c312009-06-27 17:58:47 +020055 die_errno("Cannot open '%s'", path);
Junio C Hamano5ba9a932014-09-11 13:14:51 -070056 hash_fd(fd, type, vpath, flags, literally);
Daniel Barkalow024510c2005-12-10 17:25:24 -050057}
58
Junio C Hamano5ba9a932014-09-11 13:14:51 -070059static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
60 int literally)
Adam Robend8ee4832008-05-23 16:19:38 +020061{
Jeff King0d4cc1b2016-01-31 06:25:26 -050062 struct strbuf buf = STRBUF_INIT;
63 struct strbuf unquoted = STRBUF_INIT;
Adam Robend8ee4832008-05-23 16:19:38 +020064
Junio C Hamanoc0353c72015-10-28 13:56:23 -070065 while (strbuf_getline(&buf, stdin) != EOF) {
Adam Robend8ee4832008-05-23 16:19:38 +020066 if (buf.buf[0] == '"') {
Jeff King0d4cc1b2016-01-31 06:25:26 -050067 strbuf_reset(&unquoted);
68 if (unquote_c_style(&unquoted, buf.buf, NULL))
Adam Robend8ee4832008-05-23 16:19:38 +020069 die("line is badly quoted");
Jeff King0d4cc1b2016-01-31 06:25:26 -050070 strbuf_swap(&buf, &unquoted);
Adam Robend8ee4832008-05-23 16:19:38 +020071 }
Junio C Hamano5ba9a932014-09-11 13:14:51 -070072 hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
73 literally);
Adam Robend8ee4832008-05-23 16:19:38 +020074 }
75 strbuf_release(&buf);
Jeff King0d4cc1b2016-01-31 06:25:26 -050076 strbuf_release(&unquoted);
Adam Robend8ee4832008-05-23 16:19:38 +020077}
78
Linus Torvaldsb28a1ce2010-01-21 19:50:11 -080079int cmd_hash_object(int argc, const char **argv, const char *prefix)
Bryan Larsen7672db22005-07-08 16:51:55 -070080{
Junio C Hamanob64a9842014-09-11 12:19:54 -070081 static const char * const hash_object_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070082 N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters] [--stdin] [--] <file>..."),
Junio C Hamano33e8fc82015-10-16 11:27:42 -070083 N_("git hash-object --stdin-paths"),
Junio C Hamanob64a9842014-09-11 12:19:54 -070084 NULL
85 };
86 const char *type = blob_type;
87 int hashstdin = 0;
88 int stdin_paths = 0;
Junio C Hamanob64a9842014-09-11 12:19:54 -070089 int no_filters = 0;
Junio C Hamano5ba9a932014-09-11 13:14:51 -070090 int literally = 0;
Jeff King0e94ee92016-09-12 20:23:17 -070091 int nongit = 0;
Junio C Hamano17b787f2014-09-11 12:44:05 -070092 unsigned flags = HASH_FORMAT_CHECK;
Junio C Hamanob64a9842014-09-11 12:19:54 -070093 const char *vpath = NULL;
94 const struct option hash_object_options[] = {
95 OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
Junio C Hamano17b787f2014-09-11 12:44:05 -070096 OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
97 HASH_WRITE_OBJECT),
Junio C Hamanob64a9842014-09-11 12:19:54 -070098 OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
99 OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
100 OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")),
Junio C Hamano5ba9a932014-09-11 13:14:51 -0700101 OPT_BOOL( 0, "literally", &literally, N_("just hash any random garbage to create corrupt objects for debugging Git")),
Junio C Hamanob64a9842014-09-11 12:19:54 -0700102 OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")),
103 OPT_END()
104 };
Bryan Larsen7672db22005-07-08 16:51:55 -0700105 int i;
Dmitry Potapov548601a2008-08-03 18:36:20 +0400106 const char *errstr = NULL;
107
Stephen Boyd37782922009-05-23 11:53:12 -0700108 argc = parse_options(argc, argv, NULL, hash_object_options,
109 hash_object_usage, 0);
Adam Robend8ee4832008-05-23 16:19:38 +0200110
Jeff King0e94ee92016-09-12 20:23:17 -0700111 if (flags & HASH_WRITE_OBJECT)
Dmitry Potapov548601a2008-08-03 18:36:20 +0400112 prefix = setup_git_directory();
Jeff King0e94ee92016-09-12 20:23:17 -0700113 else
114 prefix = setup_git_directory_gently(&nongit);
115
Jeff King0e94ee92016-09-12 20:23:17 -0700116 if (vpath && prefix)
Jeff King116fb642017-03-20 21:22:28 -0400117 vpath = xstrdup(prefix_filename(prefix, vpath));
Gerrit Pape8a2f5e52008-02-21 10:06:47 +0000118
Elijah Newren272459a2009-02-28 12:56:49 -0700119 git_config(git_default_config, NULL);
120
Dmitry Potapov548601a2008-08-03 18:36:20 +0400121 if (stdin_paths) {
122 if (hashstdin)
123 errstr = "Can't use --stdin-paths with --stdin";
124 else if (argc)
125 errstr = "Can't specify files with --stdin-paths";
Dmitry Potapov39702432008-08-03 18:36:21 +0400126 else if (vpath)
127 errstr = "Can't use --stdin-paths with --path";
Dmitry Potapov548601a2008-08-03 18:36:20 +0400128 }
Dmitry Potapov4a3d85d2008-08-03 18:36:22 +0400129 else {
130 if (hashstdin > 1)
131 errstr = "Multiple --stdin arguments are not supported";
132 if (vpath && no_filters)
133 errstr = "Can't use --path with --no-filters";
134 }
Adam Robend8ee4832008-05-23 16:19:38 +0200135
Dmitry Potapov548601a2008-08-03 18:36:20 +0400136 if (errstr) {
Daniel Lowe42fc1132008-11-10 16:07:52 -0500137 error("%s", errstr);
Dmitry Potapov548601a2008-08-03 18:36:20 +0400138 usage_with_options(hash_object_usage, hash_object_options);
139 }
140
141 if (hashstdin)
Junio C Hamano5ba9a932014-09-11 13:14:51 -0700142 hash_fd(0, type, vpath, flags, literally);
Dmitry Potapov548601a2008-08-03 18:36:20 +0400143
144 for (i = 0 ; i < argc; i++) {
145 const char *arg = argv[i];
Jeff Kinga1be47e2017-03-20 21:20:42 -0400146 char *to_free = NULL;
Dmitry Potapov548601a2008-08-03 18:36:20 +0400147
Jeff King116fb642017-03-20 21:22:28 -0400148 if (prefix)
Jeff Kinge4da43b2017-03-20 21:28:49 -0400149 arg = to_free = prefix_filename(prefix, arg);
Junio C Hamano17b787f2014-09-11 12:44:05 -0700150 hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
Junio C Hamano5ba9a932014-09-11 13:14:51 -0700151 flags, literally);
Jeff Kinga1be47e2017-03-20 21:20:42 -0400152 free(to_free);
Bryan Larsen7672db22005-07-08 16:51:55 -0700153 }
Adam Robend8ee4832008-05-23 16:19:38 +0200154
155 if (stdin_paths)
Junio C Hamano5ba9a932014-09-11 13:14:51 -0700156 hash_stdin_paths(type, no_filters, flags, literally);
Adam Robend8ee4832008-05-23 16:19:38 +0200157
Bryan Larsen7672db22005-07-08 16:51:55 -0700158 return 0;
159}