blob: 9028e1fdccea2ad44a76792adc6e335fb44cfb5c [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"
Peter Eriksen8e440252006-04-02 14:44:09 +02008#include "blob.h"
Adam Robend8ee4832008-05-23 16:19:38 +02009#include "quote.h"
Dmitry Potapov548601a2008-08-03 18:36:20 +040010#include "parse-options.h"
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +010011#include "exec_cmd.h"
Bryan Larsen7672db22005-07-08 16:51:55 -070012
Junio C Hamano5ba9a932014-09-11 13:14:51 -070013/*
14 * This is to create corrupt objects for debugging and as such it
15 * needs to bypass the data conversion performed by, and the type
16 * limitation imposed by, index_fd() and its callees.
17 */
18static int hash_literally(unsigned char *sha1, int fd, const char *type, unsigned flags)
19{
20 struct strbuf buf = STRBUF_INIT;
21 int ret;
22
23 if (strbuf_read(&buf, fd, 4096) < 0)
24 ret = -1;
Junio C Hamano5ba9a932014-09-11 13:14:51 -070025 else
Eric Sunshine0c3db672015-05-04 03:25:15 -040026 ret = hash_sha1_file_literally(buf.buf, buf.len, type, sha1, flags);
Junio C Hamano5ba9a932014-09-11 13:14:51 -070027 strbuf_release(&buf);
28 return ret;
29}
30
31static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
32 int literally)
Bryan Larsen7672db22005-07-08 16:51:55 -070033{
Bryan Larsen7672db22005-07-08 16:51:55 -070034 struct stat st;
35 unsigned char sha1[20];
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -070036
Dmitry Potapov43df4f82008-08-03 08:39:16 +040037 if (fstat(fd, &st) < 0 ||
Junio C Hamano5ba9a932014-09-11 13:14:51 -070038 (literally
39 ? hash_literally(sha1, fd, type, flags)
40 : index_fd(sha1, fd, &st, type_from_string(type), path, flags)))
Junio C Hamano17b787f2014-09-11 12:44:05 -070041 die((flags & HASH_WRITE_OBJECT)
Bryan Larsen7672db22005-07-08 16:51:55 -070042 ? "Unable to add %s to database"
43 : "Unable to hash %s", path);
44 printf("%s\n", sha1_to_hex(sha1));
Adam Robend8ee4832008-05-23 16:19:38 +020045 maybe_flush_or_die(stdout, "hash to stdout");
Bryan Larsen7672db22005-07-08 16:51:55 -070046}
47
Junio C Hamano17b787f2014-09-11 12:44:05 -070048static void hash_object(const char *path, const char *type, const char *vpath,
Junio C Hamano5ba9a932014-09-11 13:14:51 -070049 unsigned flags, int literally)
Daniel Barkalow024510c2005-12-10 17:25:24 -050050{
Dmitry Potapov43df4f82008-08-03 08:39:16 +040051 int fd;
52 fd = open(path, O_RDONLY);
53 if (fd < 0)
Thomas Rast0721c312009-06-27 17:58:47 +020054 die_errno("Cannot open '%s'", path);
Junio C Hamano5ba9a932014-09-11 13:14:51 -070055 hash_fd(fd, type, vpath, flags, literally);
Daniel Barkalow024510c2005-12-10 17:25:24 -050056}
57
Junio C Hamano5ba9a932014-09-11 13:14:51 -070058static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
59 int literally)
Adam Robend8ee4832008-05-23 16:19:38 +020060{
Jeff King0d4cc1b2016-01-31 06:25:26 -050061 struct strbuf buf = STRBUF_INIT;
62 struct strbuf unquoted = STRBUF_INIT;
Adam Robend8ee4832008-05-23 16:19:38 +020063
Junio C Hamanoc0353c72015-10-28 13:56:23 -070064 while (strbuf_getline(&buf, stdin) != EOF) {
Adam Robend8ee4832008-05-23 16:19:38 +020065 if (buf.buf[0] == '"') {
Jeff King0d4cc1b2016-01-31 06:25:26 -050066 strbuf_reset(&unquoted);
67 if (unquote_c_style(&unquoted, buf.buf, NULL))
Adam Robend8ee4832008-05-23 16:19:38 +020068 die("line is badly quoted");
Jeff King0d4cc1b2016-01-31 06:25:26 -050069 strbuf_swap(&buf, &unquoted);
Adam Robend8ee4832008-05-23 16:19:38 +020070 }
Junio C Hamano5ba9a932014-09-11 13:14:51 -070071 hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
72 literally);
Adam Robend8ee4832008-05-23 16:19:38 +020073 }
74 strbuf_release(&buf);
Jeff King0d4cc1b2016-01-31 06:25:26 -050075 strbuf_release(&unquoted);
Adam Robend8ee4832008-05-23 16:19:38 +020076}
77
Linus Torvaldsb28a1ce2010-01-21 19:50:11 -080078int cmd_hash_object(int argc, const char **argv, const char *prefix)
Bryan Larsen7672db22005-07-08 16:51:55 -070079{
Junio C Hamanob64a9842014-09-11 12:19:54 -070080 static const char * const hash_object_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070081 N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters] [--stdin] [--] <file>..."),
Junio C Hamano33e8fc82015-10-16 11:27:42 -070082 N_("git hash-object --stdin-paths"),
Junio C Hamanob64a9842014-09-11 12:19:54 -070083 NULL
84 };
85 const char *type = blob_type;
86 int hashstdin = 0;
87 int stdin_paths = 0;
Junio C Hamanob64a9842014-09-11 12:19:54 -070088 int no_filters = 0;
Junio C Hamano5ba9a932014-09-11 13:14:51 -070089 int literally = 0;
Jeff King0e94ee92016-09-12 20:23:17 -070090 int nongit = 0;
Junio C Hamano17b787f2014-09-11 12:44:05 -070091 unsigned flags = HASH_FORMAT_CHECK;
Junio C Hamanob64a9842014-09-11 12:19:54 -070092 const char *vpath = NULL;
93 const struct option hash_object_options[] = {
94 OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
Junio C Hamano17b787f2014-09-11 12:44:05 -070095 OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
96 HASH_WRITE_OBJECT),
Junio C Hamanob64a9842014-09-11 12:19:54 -070097 OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
98 OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
99 OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")),
Junio C Hamano5ba9a932014-09-11 13:14:51 -0700100 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 -0700101 OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")),
102 OPT_END()
103 };
Bryan Larsen7672db22005-07-08 16:51:55 -0700104 int i;
Junio C Hamano706fe6a2005-11-26 00:30:07 -0800105 int prefix_length = -1;
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
116 prefix_length = prefix ? strlen(prefix) : 0;
117 if (vpath && prefix)
118 vpath = prefix_filename(prefix, prefix_length, vpath);
Gerrit Pape8a2f5e52008-02-21 10:06:47 +0000119
Elijah Newren272459a2009-02-28 12:56:49 -0700120 git_config(git_default_config, NULL);
121
Dmitry Potapov548601a2008-08-03 18:36:20 +0400122 if (stdin_paths) {
123 if (hashstdin)
124 errstr = "Can't use --stdin-paths with --stdin";
125 else if (argc)
126 errstr = "Can't specify files with --stdin-paths";
Dmitry Potapov39702432008-08-03 18:36:21 +0400127 else if (vpath)
128 errstr = "Can't use --stdin-paths with --path";
Dmitry Potapov548601a2008-08-03 18:36:20 +0400129 }
Dmitry Potapov4a3d85d2008-08-03 18:36:22 +0400130 else {
131 if (hashstdin > 1)
132 errstr = "Multiple --stdin arguments are not supported";
133 if (vpath && no_filters)
134 errstr = "Can't use --path with --no-filters";
135 }
Adam Robend8ee4832008-05-23 16:19:38 +0200136
Dmitry Potapov548601a2008-08-03 18:36:20 +0400137 if (errstr) {
Daniel Lowe42fc1132008-11-10 16:07:52 -0500138 error("%s", errstr);
Dmitry Potapov548601a2008-08-03 18:36:20 +0400139 usage_with_options(hash_object_usage, hash_object_options);
140 }
141
142 if (hashstdin)
Junio C Hamano5ba9a932014-09-11 13:14:51 -0700143 hash_fd(0, type, vpath, flags, literally);
Dmitry Potapov548601a2008-08-03 18:36:20 +0400144
145 for (i = 0 ; i < argc; i++) {
146 const char *arg = argv[i];
147
148 if (0 <= prefix_length)
149 arg = prefix_filename(prefix, prefix_length, 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);
Bryan Larsen7672db22005-07-08 16:51:55 -0700152 }
Adam Robend8ee4832008-05-23 16:19:38 +0200153
154 if (stdin_paths)
Junio C Hamano5ba9a932014-09-11 13:14:51 -0700155 hash_stdin_paths(type, no_filters, flags, literally);
Adam Robend8ee4832008-05-23 16:19:38 +0200156
Bryan Larsen7672db22005-07-08 16:51:55 -0700157 return 0;
158}