Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 5 | * Copyright (C) Junio C Hamano, 2005 |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 6 | */ |
Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 7 | #include "builtin.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 8 | #include "config.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 9 | #include "blob.h" |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 10 | #include "quote.h" |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 11 | #include "parse-options.h" |
Steffen Prohaska | 2fb3f6d | 2009-01-18 13:00:12 +0100 | [diff] [blame] | 12 | #include "exec_cmd.h" |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 13 | |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 14 | /* |
| 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 | */ |
| 19 | static 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 Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 26 | else |
Eric Sunshine | 0c3db67 | 2015-05-04 03:25:15 -0400 | [diff] [blame] | 27 | ret = hash_sha1_file_literally(buf.buf, buf.len, type, sha1, flags); |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 28 | strbuf_release(&buf); |
| 29 | return ret; |
| 30 | } |
| 31 | |
| 32 | static void hash_fd(int fd, const char *type, const char *path, unsigned flags, |
| 33 | int literally) |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 34 | { |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 35 | struct stat st; |
| 36 | unsigned char sha1[20]; |
Junio C Hamano | c4ce46f | 2011-05-08 01:47:33 -0700 | [diff] [blame] | 37 | |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 38 | if (fstat(fd, &st) < 0 || |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 39 | (literally |
| 40 | ? hash_literally(sha1, fd, type, flags) |
| 41 | : index_fd(sha1, fd, &st, type_from_string(type), path, flags))) |
Junio C Hamano | 17b787f | 2014-09-11 12:44:05 -0700 | [diff] [blame] | 42 | die((flags & HASH_WRITE_OBJECT) |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 43 | ? "Unable to add %s to database" |
| 44 | : "Unable to hash %s", path); |
| 45 | printf("%s\n", sha1_to_hex(sha1)); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 46 | maybe_flush_or_die(stdout, "hash to stdout"); |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Junio C Hamano | 17b787f | 2014-09-11 12:44:05 -0700 | [diff] [blame] | 49 | static void hash_object(const char *path, const char *type, const char *vpath, |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 50 | unsigned flags, int literally) |
Daniel Barkalow | 024510c | 2005-12-10 17:25:24 -0500 | [diff] [blame] | 51 | { |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 52 | int fd; |
| 53 | fd = open(path, O_RDONLY); |
| 54 | if (fd < 0) |
Thomas Rast | 0721c31 | 2009-06-27 17:58:47 +0200 | [diff] [blame] | 55 | die_errno("Cannot open '%s'", path); |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 56 | hash_fd(fd, type, vpath, flags, literally); |
Daniel Barkalow | 024510c | 2005-12-10 17:25:24 -0500 | [diff] [blame] | 57 | } |
| 58 | |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 59 | static void hash_stdin_paths(const char *type, int no_filters, unsigned flags, |
| 60 | int literally) |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 61 | { |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 62 | struct strbuf buf = STRBUF_INIT; |
| 63 | struct strbuf unquoted = STRBUF_INIT; |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 64 | |
Junio C Hamano | c0353c7 | 2015-10-28 13:56:23 -0700 | [diff] [blame] | 65 | while (strbuf_getline(&buf, stdin) != EOF) { |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 66 | if (buf.buf[0] == '"') { |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 67 | strbuf_reset(&unquoted); |
| 68 | if (unquote_c_style(&unquoted, buf.buf, NULL)) |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 69 | die("line is badly quoted"); |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 70 | strbuf_swap(&buf, &unquoted); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 71 | } |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 72 | hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags, |
| 73 | literally); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 74 | } |
| 75 | strbuf_release(&buf); |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 76 | strbuf_release(&unquoted); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Linus Torvalds | b28a1ce | 2010-01-21 19:50:11 -0800 | [diff] [blame] | 79 | int cmd_hash_object(int argc, const char **argv, const char *prefix) |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 80 | { |
Junio C Hamano | b64a984 | 2014-09-11 12:19:54 -0700 | [diff] [blame] | 81 | static const char * const hash_object_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 82 | N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters] [--stdin] [--] <file>..."), |
Junio C Hamano | 33e8fc8 | 2015-10-16 11:27:42 -0700 | [diff] [blame] | 83 | N_("git hash-object --stdin-paths"), |
Junio C Hamano | b64a984 | 2014-09-11 12:19:54 -0700 | [diff] [blame] | 84 | NULL |
| 85 | }; |
| 86 | const char *type = blob_type; |
| 87 | int hashstdin = 0; |
| 88 | int stdin_paths = 0; |
Junio C Hamano | b64a984 | 2014-09-11 12:19:54 -0700 | [diff] [blame] | 89 | int no_filters = 0; |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 90 | int literally = 0; |
Jeff King | 0e94ee9 | 2016-09-12 20:23:17 -0700 | [diff] [blame] | 91 | int nongit = 0; |
Junio C Hamano | 17b787f | 2014-09-11 12:44:05 -0700 | [diff] [blame] | 92 | unsigned flags = HASH_FORMAT_CHECK; |
Junio C Hamano | b64a984 | 2014-09-11 12:19:54 -0700 | [diff] [blame] | 93 | const char *vpath = NULL; |
| 94 | const struct option hash_object_options[] = { |
| 95 | OPT_STRING('t', NULL, &type, N_("type"), N_("object type")), |
Junio C Hamano | 17b787f | 2014-09-11 12:44:05 -0700 | [diff] [blame] | 96 | OPT_BIT('w', NULL, &flags, N_("write the object into the object database"), |
| 97 | HASH_WRITE_OBJECT), |
Junio C Hamano | b64a984 | 2014-09-11 12:19:54 -0700 | [diff] [blame] | 98 | 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 Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 101 | OPT_BOOL( 0, "literally", &literally, N_("just hash any random garbage to create corrupt objects for debugging Git")), |
Junio C Hamano | b64a984 | 2014-09-11 12:19:54 -0700 | [diff] [blame] | 102 | OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")), |
| 103 | OPT_END() |
| 104 | }; |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 105 | int i; |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 106 | const char *errstr = NULL; |
| 107 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 108 | argc = parse_options(argc, argv, NULL, hash_object_options, |
| 109 | hash_object_usage, 0); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 110 | |
Jeff King | 0e94ee9 | 2016-09-12 20:23:17 -0700 | [diff] [blame] | 111 | if (flags & HASH_WRITE_OBJECT) |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 112 | prefix = setup_git_directory(); |
Jeff King | 0e94ee9 | 2016-09-12 20:23:17 -0700 | [diff] [blame] | 113 | else |
| 114 | prefix = setup_git_directory_gently(&nongit); |
| 115 | |
Jeff King | 0e94ee9 | 2016-09-12 20:23:17 -0700 | [diff] [blame] | 116 | if (vpath && prefix) |
Jeff King | 116fb64 | 2017-03-20 21:22:28 -0400 | [diff] [blame] | 117 | vpath = xstrdup(prefix_filename(prefix, vpath)); |
Gerrit Pape | 8a2f5e5 | 2008-02-21 10:06:47 +0000 | [diff] [blame] | 118 | |
Elijah Newren | 272459a | 2009-02-28 12:56:49 -0700 | [diff] [blame] | 119 | git_config(git_default_config, NULL); |
| 120 | |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 121 | 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 Potapov | 3970243 | 2008-08-03 18:36:21 +0400 | [diff] [blame] | 126 | else if (vpath) |
| 127 | errstr = "Can't use --stdin-paths with --path"; |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 128 | } |
Dmitry Potapov | 4a3d85d | 2008-08-03 18:36:22 +0400 | [diff] [blame] | 129 | 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 Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 135 | |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 136 | if (errstr) { |
Daniel Lowe | 42fc113 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 137 | error("%s", errstr); |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 138 | usage_with_options(hash_object_usage, hash_object_options); |
| 139 | } |
| 140 | |
| 141 | if (hashstdin) |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 142 | hash_fd(0, type, vpath, flags, literally); |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 143 | |
| 144 | for (i = 0 ; i < argc; i++) { |
| 145 | const char *arg = argv[i]; |
Jeff King | a1be47e | 2017-03-20 21:20:42 -0400 | [diff] [blame] | 146 | char *to_free = NULL; |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 147 | |
Jeff King | 116fb64 | 2017-03-20 21:22:28 -0400 | [diff] [blame] | 148 | if (prefix) |
Jeff King | e4da43b | 2017-03-20 21:28:49 -0400 | [diff] [blame] | 149 | arg = to_free = prefix_filename(prefix, arg); |
Junio C Hamano | 17b787f | 2014-09-11 12:44:05 -0700 | [diff] [blame] | 150 | hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg, |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 151 | flags, literally); |
Jeff King | a1be47e | 2017-03-20 21:20:42 -0400 | [diff] [blame] | 152 | free(to_free); |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 153 | } |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 154 | |
| 155 | if (stdin_paths) |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 156 | hash_stdin_paths(type, no_filters, flags, literally); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 157 | |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 158 | return 0; |
| 159 | } |