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" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 9 | #include "object-store.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 10 | #include "blob.h" |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 11 | #include "quote.h" |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 12 | #include "parse-options.h" |
Stefan Beller | d807c4a | 2018-04-10 14:26:18 -0700 | [diff] [blame] | 13 | #include "exec-cmd.h" |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 14 | |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 15 | /* |
| 16 | * This is to create corrupt objects for debugging and as such it |
| 17 | * needs to bypass the data conversion performed by, and the type |
| 18 | * limitation imposed by, index_fd() and its callees. |
| 19 | */ |
Patryk Obara | eab8bf2 | 2017-08-20 22:09:26 +0200 | [diff] [blame] | 20 | static int hash_literally(struct object_id *oid, int fd, const char *type, unsigned flags) |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 21 | { |
| 22 | struct strbuf buf = STRBUF_INIT; |
| 23 | int ret; |
| 24 | |
| 25 | if (strbuf_read(&buf, fd, 4096) < 0) |
| 26 | ret = -1; |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 27 | else |
Ævar Arnfjörð Bjarmason | 0ff7b4f | 2022-02-05 00:48:31 +0100 | [diff] [blame] | 28 | ret = write_object_file_literally(buf.buf, buf.len, type, oid, |
Patryk Obara | 1752cbb | 2018-01-28 01:13:22 +0100 | [diff] [blame] | 29 | flags); |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 30 | strbuf_release(&buf); |
| 31 | return ret; |
| 32 | } |
| 33 | |
| 34 | static void hash_fd(int fd, const char *type, const char *path, unsigned flags, |
| 35 | int literally) |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 36 | { |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 37 | struct stat st; |
Patryk Obara | eab8bf2 | 2017-08-20 22:09:26 +0200 | [diff] [blame] | 38 | struct object_id oid; |
Junio C Hamano | c4ce46f | 2011-05-08 01:47:33 -0700 | [diff] [blame] | 39 | |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 40 | if (fstat(fd, &st) < 0 || |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 41 | (literally |
Patryk Obara | eab8bf2 | 2017-08-20 22:09:26 +0200 | [diff] [blame] | 42 | ? hash_literally(&oid, fd, type, flags) |
Nguyễn Thái Ngọc Duy | f8adbec | 2019-01-24 15:29:12 +0700 | [diff] [blame] | 43 | : index_fd(the_repository->index, &oid, fd, &st, |
| 44 | type_from_string(type), path, flags))) |
Junio C Hamano | 17b787f | 2014-09-11 12:44:05 -0700 | [diff] [blame] | 45 | die((flags & HASH_WRITE_OBJECT) |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 46 | ? "Unable to add %s to database" |
| 47 | : "Unable to hash %s", path); |
Patryk Obara | eab8bf2 | 2017-08-20 22:09:26 +0200 | [diff] [blame] | 48 | printf("%s\n", oid_to_hex(&oid)); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 49 | maybe_flush_or_die(stdout, "hash to stdout"); |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Junio C Hamano | 17b787f | 2014-09-11 12:44:05 -0700 | [diff] [blame] | 52 | 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] | 53 | unsigned flags, int literally) |
Daniel Barkalow | 024510c | 2005-12-10 17:25:24 -0500 | [diff] [blame] | 54 | { |
Dmitry Potapov | 43df4f8 | 2008-08-03 08:39:16 +0400 | [diff] [blame] | 55 | int fd; |
René Scharfe | 66e905b | 2021-08-25 22:16:46 +0200 | [diff] [blame] | 56 | fd = xopen(path, O_RDONLY); |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 57 | hash_fd(fd, type, vpath, flags, literally); |
Daniel Barkalow | 024510c | 2005-12-10 17:25:24 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 60 | static void hash_stdin_paths(const char *type, int no_filters, unsigned flags, |
| 61 | int literally) |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 62 | { |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 63 | struct strbuf buf = STRBUF_INIT; |
| 64 | struct strbuf unquoted = STRBUF_INIT; |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 65 | |
Junio C Hamano | c0353c7 | 2015-10-28 13:56:23 -0700 | [diff] [blame] | 66 | while (strbuf_getline(&buf, stdin) != EOF) { |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 67 | if (buf.buf[0] == '"') { |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 68 | strbuf_reset(&unquoted); |
| 69 | if (unquote_c_style(&unquoted, buf.buf, NULL)) |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 70 | die("line is badly quoted"); |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 71 | strbuf_swap(&buf, &unquoted); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 72 | } |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 73 | hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags, |
| 74 | literally); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 75 | } |
| 76 | strbuf_release(&buf); |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 77 | strbuf_release(&unquoted); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 78 | } |
| 79 | |
Linus Torvalds | b28a1ce | 2010-01-21 19:50:11 -0800 | [diff] [blame] | 80 | int cmd_hash_object(int argc, const char **argv, const char *prefix) |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 81 | { |
Junio C Hamano | b64a984 | 2014-09-11 12:19:54 -0700 | [diff] [blame] | 82 | static const char * const hash_object_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 83 | N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters] [--stdin] [--] <file>..."), |
Jean-Noël Avila | 959d670 | 2022-01-31 22:07:48 +0000 | [diff] [blame] | 84 | "git hash-object --stdin-paths", |
Junio C Hamano | b64a984 | 2014-09-11 12:19:54 -0700 | [diff] [blame] | 85 | NULL |
| 86 | }; |
| 87 | const char *type = blob_type; |
| 88 | int hashstdin = 0; |
| 89 | int stdin_paths = 0; |
Junio C Hamano | b64a984 | 2014-09-11 12:19:54 -0700 | [diff] [blame] | 90 | int no_filters = 0; |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 91 | int literally = 0; |
Jeff King | 0e94ee9 | 2016-09-12 20:23:17 -0700 | [diff] [blame] | 92 | int nongit = 0; |
Junio C Hamano | 17b787f | 2014-09-11 12:44:05 -0700 | [diff] [blame] | 93 | unsigned flags = HASH_FORMAT_CHECK; |
Junio C Hamano | b64a984 | 2014-09-11 12:19:54 -0700 | [diff] [blame] | 94 | const char *vpath = NULL; |
Ævar Arnfjörð Bjarmason | d17294a | 2022-02-05 01:04:29 +0100 | [diff] [blame] | 95 | char *vpath_free = NULL; |
Junio C Hamano | b64a984 | 2014-09-11 12:19:54 -0700 | [diff] [blame] | 96 | const struct option hash_object_options[] = { |
| 97 | OPT_STRING('t', NULL, &type, N_("type"), N_("object type")), |
Junio C Hamano | 17b787f | 2014-09-11 12:44:05 -0700 | [diff] [blame] | 98 | OPT_BIT('w', NULL, &flags, N_("write the object into the object database"), |
| 99 | HASH_WRITE_OBJECT), |
Junio C Hamano | b64a984 | 2014-09-11 12:19:54 -0700 | [diff] [blame] | 100 | OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")), |
| 101 | OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")), |
| 102 | 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] | 103 | 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] | 104 | OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")), |
| 105 | OPT_END() |
| 106 | }; |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 107 | int i; |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 108 | const char *errstr = NULL; |
| 109 | |
Jeff King | d64db5b | 2019-05-09 17:28:51 -0400 | [diff] [blame] | 110 | argc = parse_options(argc, argv, prefix, hash_object_options, |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 111 | hash_object_usage, 0); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 112 | |
Jeff King | 0e94ee9 | 2016-09-12 20:23:17 -0700 | [diff] [blame] | 113 | if (flags & HASH_WRITE_OBJECT) |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 114 | prefix = setup_git_directory(); |
Jeff King | 0e94ee9 | 2016-09-12 20:23:17 -0700 | [diff] [blame] | 115 | else |
| 116 | prefix = setup_git_directory_gently(&nongit); |
| 117 | |
Ævar Arnfjörð Bjarmason | d17294a | 2022-02-05 01:04:29 +0100 | [diff] [blame] | 118 | if (vpath && prefix) { |
| 119 | vpath_free = prefix_filename(prefix, vpath); |
| 120 | vpath = vpath_free; |
| 121 | } |
Gerrit Pape | 8a2f5e5 | 2008-02-21 10:06:47 +0000 | [diff] [blame] | 122 | |
Elijah Newren | 272459a | 2009-02-28 12:56:49 -0700 | [diff] [blame] | 123 | git_config(git_default_config, NULL); |
| 124 | |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 125 | if (stdin_paths) { |
| 126 | if (hashstdin) |
| 127 | errstr = "Can't use --stdin-paths with --stdin"; |
| 128 | else if (argc) |
| 129 | errstr = "Can't specify files with --stdin-paths"; |
Dmitry Potapov | 3970243 | 2008-08-03 18:36:21 +0400 | [diff] [blame] | 130 | else if (vpath) |
| 131 | errstr = "Can't use --stdin-paths with --path"; |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 132 | } |
Dmitry Potapov | 4a3d85d | 2008-08-03 18:36:22 +0400 | [diff] [blame] | 133 | else { |
| 134 | if (hashstdin > 1) |
| 135 | errstr = "Multiple --stdin arguments are not supported"; |
| 136 | if (vpath && no_filters) |
| 137 | errstr = "Can't use --path with --no-filters"; |
| 138 | } |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 139 | |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 140 | if (errstr) { |
Daniel Lowe | 42fc113 | 2008-11-10 16:07:52 -0500 | [diff] [blame] | 141 | error("%s", errstr); |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 142 | usage_with_options(hash_object_usage, hash_object_options); |
| 143 | } |
| 144 | |
| 145 | if (hashstdin) |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 146 | hash_fd(0, type, vpath, flags, literally); |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 147 | |
| 148 | for (i = 0 ; i < argc; i++) { |
| 149 | const char *arg = argv[i]; |
Jeff King | a1be47e | 2017-03-20 21:20:42 -0400 | [diff] [blame] | 150 | char *to_free = NULL; |
Dmitry Potapov | 548601a | 2008-08-03 18:36:20 +0400 | [diff] [blame] | 151 | |
Jeff King | 116fb64 | 2017-03-20 21:22:28 -0400 | [diff] [blame] | 152 | if (prefix) |
Jeff King | e4da43b | 2017-03-20 21:28:49 -0400 | [diff] [blame] | 153 | arg = to_free = prefix_filename(prefix, arg); |
Junio C Hamano | 17b787f | 2014-09-11 12:44:05 -0700 | [diff] [blame] | 154 | hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg, |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 155 | flags, literally); |
Jeff King | a1be47e | 2017-03-20 21:20:42 -0400 | [diff] [blame] | 156 | free(to_free); |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 157 | } |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 158 | |
| 159 | if (stdin_paths) |
Junio C Hamano | 5ba9a93 | 2014-09-11 13:14:51 -0700 | [diff] [blame] | 160 | hash_stdin_paths(type, no_filters, flags, literally); |
Adam Roben | d8ee483 | 2008-05-23 16:19:38 +0200 | [diff] [blame] | 161 | |
Ævar Arnfjörð Bjarmason | d17294a | 2022-02-05 01:04:29 +0100 | [diff] [blame] | 162 | free(vpath_free); |
| 163 | |
Bryan Larsen | 7672db2 | 2005-07-08 16:51:55 -0700 | [diff] [blame] | 164 | return 0; |
| 165 | } |