blob: 82ca6d2bfdc7797a13646ff01271ceb803b0a973 [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"
Elijah Newren0b027f62023-03-21 06:25:58 +00008#include "abspath.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07009#include "config.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000010#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +000011#include "hex.h"
Elijah Newren87bed172023-04-11 00:41:53 -070012#include "object-file.h"
Elijah Newrena034e912023-05-16 06:34:06 +000013#include "object-store-ll.h"
Peter Eriksen8e440252006-04-02 14:44:09 +020014#include "blob.h"
Adam Robend8ee4832008-05-23 16:19:38 +020015#include "quote.h"
Dmitry Potapov548601a2008-08-03 18:36:20 +040016#include "parse-options.h"
Elijah Newrene38da482023-03-21 06:26:05 +000017#include "setup.h"
Elijah Newrena034e912023-05-16 06:34:06 +000018#include "strbuf.h"
Elijah Newrend48be352023-03-21 06:26:07 +000019#include "write-or-die.h"
Bryan Larsen7672db22005-07-08 16:51:55 -070020
Junio C Hamano5ba9a932014-09-11 13:14:51 -070021/*
22 * This is to create corrupt objects for debugging and as such it
23 * needs to bypass the data conversion performed by, and the type
24 * limitation imposed by, index_fd() and its callees.
25 */
Patryk Obaraeab8bf22017-08-20 22:09:26 +020026static int hash_literally(struct object_id *oid, int fd, const char *type, unsigned flags)
Junio C Hamano5ba9a932014-09-11 13:14:51 -070027{
28 struct strbuf buf = STRBUF_INIT;
29 int ret;
30
31 if (strbuf_read(&buf, fd, 4096) < 0)
32 ret = -1;
Junio C Hamano5ba9a932014-09-11 13:14:51 -070033 else
Ævar Arnfjörð Bjarmason0ff7b4f2022-02-05 00:48:31 +010034 ret = write_object_file_literally(buf.buf, buf.len, type, oid,
Patryk Obara1752cbb2018-01-28 01:13:22 +010035 flags);
Jeff King590b6362023-01-18 20:57:56 -050036 close(fd);
Junio C Hamano5ba9a932014-09-11 13:14:51 -070037 strbuf_release(&buf);
38 return ret;
39}
40
41static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
42 int literally)
Bryan Larsen7672db22005-07-08 16:51:55 -070043{
Bryan Larsen7672db22005-07-08 16:51:55 -070044 struct stat st;
Patryk Obaraeab8bf22017-08-20 22:09:26 +020045 struct object_id oid;
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -070046
Dmitry Potapov43df4f82008-08-03 08:39:16 +040047 if (fstat(fd, &st) < 0 ||
Junio C Hamano5ba9a932014-09-11 13:14:51 -070048 (literally
Patryk Obaraeab8bf22017-08-20 22:09:26 +020049 ? hash_literally(&oid, fd, type, flags)
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +070050 : index_fd(the_repository->index, &oid, fd, &st,
51 type_from_string(type), path, flags)))
Junio C Hamano17b787f2014-09-11 12:44:05 -070052 die((flags & HASH_WRITE_OBJECT)
Bryan Larsen7672db22005-07-08 16:51:55 -070053 ? "Unable to add %s to database"
54 : "Unable to hash %s", path);
Patryk Obaraeab8bf22017-08-20 22:09:26 +020055 printf("%s\n", oid_to_hex(&oid));
Adam Robend8ee4832008-05-23 16:19:38 +020056 maybe_flush_or_die(stdout, "hash to stdout");
Bryan Larsen7672db22005-07-08 16:51:55 -070057}
58
Junio C Hamano17b787f2014-09-11 12:44:05 -070059static void hash_object(const char *path, const char *type, const char *vpath,
Junio C Hamano5ba9a932014-09-11 13:14:51 -070060 unsigned flags, int literally)
Daniel Barkalow024510c2005-12-10 17:25:24 -050061{
Dmitry Potapov43df4f82008-08-03 08:39:16 +040062 int fd;
René Scharfe66e905b2021-08-25 22:16:46 +020063 fd = xopen(path, O_RDONLY);
Junio C Hamano5ba9a932014-09-11 13:14:51 -070064 hash_fd(fd, type, vpath, flags, literally);
Daniel Barkalow024510c2005-12-10 17:25:24 -050065}
66
Junio C Hamano5ba9a932014-09-11 13:14:51 -070067static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
68 int literally)
Adam Robend8ee4832008-05-23 16:19:38 +020069{
Jeff King0d4cc1b2016-01-31 06:25:26 -050070 struct strbuf buf = STRBUF_INIT;
71 struct strbuf unquoted = STRBUF_INIT;
Adam Robend8ee4832008-05-23 16:19:38 +020072
Junio C Hamanoc0353c72015-10-28 13:56:23 -070073 while (strbuf_getline(&buf, stdin) != EOF) {
Adam Robend8ee4832008-05-23 16:19:38 +020074 if (buf.buf[0] == '"') {
Jeff King0d4cc1b2016-01-31 06:25:26 -050075 strbuf_reset(&unquoted);
76 if (unquote_c_style(&unquoted, buf.buf, NULL))
Adam Robend8ee4832008-05-23 16:19:38 +020077 die("line is badly quoted");
Jeff King0d4cc1b2016-01-31 06:25:26 -050078 strbuf_swap(&buf, &unquoted);
Adam Robend8ee4832008-05-23 16:19:38 +020079 }
Junio C Hamano5ba9a932014-09-11 13:14:51 -070080 hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
81 literally);
Adam Robend8ee4832008-05-23 16:19:38 +020082 }
83 strbuf_release(&buf);
Jeff King0d4cc1b2016-01-31 06:25:26 -050084 strbuf_release(&unquoted);
Adam Robend8ee4832008-05-23 16:19:38 +020085}
86
Linus Torvaldsb28a1ce2010-01-21 19:50:11 -080087int cmd_hash_object(int argc, const char **argv, const char *prefix)
Bryan Larsen7672db22005-07-08 16:51:55 -070088{
Junio C Hamanob64a9842014-09-11 12:19:54 -070089 static const char * const hash_object_usage[] = {
Ævar Arnfjörð Bjarmason5af8b612022-10-13 17:39:02 +020090 N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters]\n"
Ævar Arnfjörð Bjarmasond9054a12022-10-13 17:39:18 +020091 " [--stdin [--literally]] [--] <file>..."),
92 N_("git hash-object [-t <type>] [-w] --stdin-paths [--no-filters]"),
Junio C Hamanob64a9842014-09-11 12:19:54 -070093 NULL
94 };
95 const char *type = blob_type;
96 int hashstdin = 0;
97 int stdin_paths = 0;
Junio C Hamanob64a9842014-09-11 12:19:54 -070098 int no_filters = 0;
Junio C Hamano5ba9a932014-09-11 13:14:51 -070099 int literally = 0;
Jeff King0e94ee92016-09-12 20:23:17 -0700100 int nongit = 0;
Junio C Hamano17b787f2014-09-11 12:44:05 -0700101 unsigned flags = HASH_FORMAT_CHECK;
Junio C Hamanob64a9842014-09-11 12:19:54 -0700102 const char *vpath = NULL;
Ævar Arnfjörð Bjarmasond17294a2022-02-05 01:04:29 +0100103 char *vpath_free = NULL;
Junio C Hamanob64a9842014-09-11 12:19:54 -0700104 const struct option hash_object_options[] = {
105 OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
Junio C Hamano17b787f2014-09-11 12:44:05 -0700106 OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
107 HASH_WRITE_OBJECT),
Junio C Hamanob64a9842014-09-11 12:19:54 -0700108 OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
109 OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
110 OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")),
Junio C Hamano5ba9a932014-09-11 13:14:51 -0700111 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 -0700112 OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")),
113 OPT_END()
114 };
Bryan Larsen7672db22005-07-08 16:51:55 -0700115 int i;
Dmitry Potapov548601a2008-08-03 18:36:20 +0400116 const char *errstr = NULL;
117
Jeff Kingd64db5b2019-05-09 17:28:51 -0400118 argc = parse_options(argc, argv, prefix, hash_object_options,
Stephen Boyd37782922009-05-23 11:53:12 -0700119 hash_object_usage, 0);
Adam Robend8ee4832008-05-23 16:19:38 +0200120
Jeff King0e94ee92016-09-12 20:23:17 -0700121 if (flags & HASH_WRITE_OBJECT)
Dmitry Potapov548601a2008-08-03 18:36:20 +0400122 prefix = setup_git_directory();
Jeff King0e94ee92016-09-12 20:23:17 -0700123 else
124 prefix = setup_git_directory_gently(&nongit);
125
Ævar Arnfjörð Bjarmasond17294a2022-02-05 01:04:29 +0100126 if (vpath && prefix) {
127 vpath_free = prefix_filename(prefix, vpath);
128 vpath = vpath_free;
129 }
Gerrit Pape8a2f5e52008-02-21 10:06:47 +0000130
Elijah Newren272459a2009-02-28 12:56:49 -0700131 git_config(git_default_config, NULL);
132
Dmitry Potapov548601a2008-08-03 18:36:20 +0400133 if (stdin_paths) {
134 if (hashstdin)
135 errstr = "Can't use --stdin-paths with --stdin";
136 else if (argc)
137 errstr = "Can't specify files with --stdin-paths";
Dmitry Potapov39702432008-08-03 18:36:21 +0400138 else if (vpath)
139 errstr = "Can't use --stdin-paths with --path";
Dmitry Potapov548601a2008-08-03 18:36:20 +0400140 }
Dmitry Potapov4a3d85d2008-08-03 18:36:22 +0400141 else {
142 if (hashstdin > 1)
143 errstr = "Multiple --stdin arguments are not supported";
144 if (vpath && no_filters)
145 errstr = "Can't use --path with --no-filters";
146 }
Adam Robend8ee4832008-05-23 16:19:38 +0200147
Dmitry Potapov548601a2008-08-03 18:36:20 +0400148 if (errstr) {
Daniel Lowe42fc1132008-11-10 16:07:52 -0500149 error("%s", errstr);
Dmitry Potapov548601a2008-08-03 18:36:20 +0400150 usage_with_options(hash_object_usage, hash_object_options);
151 }
152
153 if (hashstdin)
Junio C Hamano5ba9a932014-09-11 13:14:51 -0700154 hash_fd(0, type, vpath, flags, literally);
Dmitry Potapov548601a2008-08-03 18:36:20 +0400155
156 for (i = 0 ; i < argc; i++) {
157 const char *arg = argv[i];
Jeff Kinga1be47e2017-03-20 21:20:42 -0400158 char *to_free = NULL;
Dmitry Potapov548601a2008-08-03 18:36:20 +0400159
Jeff King116fb642017-03-20 21:22:28 -0400160 if (prefix)
Jeff Kinge4da43b2017-03-20 21:28:49 -0400161 arg = to_free = prefix_filename(prefix, arg);
Junio C Hamano17b787f2014-09-11 12:44:05 -0700162 hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
Junio C Hamano5ba9a932014-09-11 13:14:51 -0700163 flags, literally);
Jeff Kinga1be47e2017-03-20 21:20:42 -0400164 free(to_free);
Bryan Larsen7672db22005-07-08 16:51:55 -0700165 }
Adam Robend8ee4832008-05-23 16:19:38 +0200166
167 if (stdin_paths)
Junio C Hamano5ba9a932014-09-11 13:14:51 -0700168 hash_stdin_paths(type, no_filters, flags, literally);
Adam Robend8ee4832008-05-23 16:19:38 +0200169
Ævar Arnfjörð Bjarmasond17294a2022-02-05 01:04:29 +0100170 free(vpath_free);
171
Bryan Larsen7672db22005-07-08 16:51:55 -0700172 return 0;
173}