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