blob: fbae878c2b951599fcfb82021185c67cc942b5ca [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"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07008#include "config.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07009#include "object-store.h"
Peter Eriksen8e440252006-04-02 14:44:09 +020010#include "blob.h"
Adam Robend8ee4832008-05-23 16:19:38 +020011#include "quote.h"
Dmitry Potapov548601a2008-08-03 18:36:20 +040012#include "parse-options.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -070013#include "exec-cmd.h"
Bryan Larsen7672db22005-07-08 16:51:55 -070014
Junio C Hamano5ba9a932014-09-11 13:14:51 -070015/*
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 Obaraeab8bf22017-08-20 22:09:26 +020020static int hash_literally(struct object_id *oid, int fd, const char *type, unsigned flags)
Junio C Hamano5ba9a932014-09-11 13:14:51 -070021{
22 struct strbuf buf = STRBUF_INIT;
23 int ret;
24
25 if (strbuf_read(&buf, fd, 4096) < 0)
26 ret = -1;
Junio C Hamano5ba9a932014-09-11 13:14:51 -070027 else
Ævar Arnfjörð Bjarmason0ff7b4f2022-02-05 00:48:31 +010028 ret = write_object_file_literally(buf.buf, buf.len, type, oid,
Patryk Obara1752cbb2018-01-28 01:13:22 +010029 flags);
Junio C Hamano5ba9a932014-09-11 13:14:51 -070030 strbuf_release(&buf);
31 return ret;
32}
33
34static void hash_fd(int fd, const char *type, const char *path, unsigned flags,
35 int literally)
Bryan Larsen7672db22005-07-08 16:51:55 -070036{
Bryan Larsen7672db22005-07-08 16:51:55 -070037 struct stat st;
Patryk Obaraeab8bf22017-08-20 22:09:26 +020038 struct object_id oid;
Junio C Hamanoc4ce46f2011-05-08 01:47:33 -070039
Dmitry Potapov43df4f82008-08-03 08:39:16 +040040 if (fstat(fd, &st) < 0 ||
Junio C Hamano5ba9a932014-09-11 13:14:51 -070041 (literally
Patryk Obaraeab8bf22017-08-20 22:09:26 +020042 ? hash_literally(&oid, fd, type, flags)
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +070043 : index_fd(the_repository->index, &oid, fd, &st,
44 type_from_string(type), path, flags)))
Junio C Hamano17b787f2014-09-11 12:44:05 -070045 die((flags & HASH_WRITE_OBJECT)
Bryan Larsen7672db22005-07-08 16:51:55 -070046 ? "Unable to add %s to database"
47 : "Unable to hash %s", path);
Patryk Obaraeab8bf22017-08-20 22:09:26 +020048 printf("%s\n", oid_to_hex(&oid));
Adam Robend8ee4832008-05-23 16:19:38 +020049 maybe_flush_or_die(stdout, "hash to stdout");
Bryan Larsen7672db22005-07-08 16:51:55 -070050}
51
Junio C Hamano17b787f2014-09-11 12:44:05 -070052static void hash_object(const char *path, const char *type, const char *vpath,
Junio C Hamano5ba9a932014-09-11 13:14:51 -070053 unsigned flags, int literally)
Daniel Barkalow024510c2005-12-10 17:25:24 -050054{
Dmitry Potapov43df4f82008-08-03 08:39:16 +040055 int fd;
René Scharfe66e905b2021-08-25 22:16:46 +020056 fd = xopen(path, O_RDONLY);
Junio C Hamano5ba9a932014-09-11 13:14:51 -070057 hash_fd(fd, type, vpath, flags, literally);
Daniel Barkalow024510c2005-12-10 17:25:24 -050058}
59
Junio C Hamano5ba9a932014-09-11 13:14:51 -070060static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
61 int literally)
Adam Robend8ee4832008-05-23 16:19:38 +020062{
Jeff King0d4cc1b2016-01-31 06:25:26 -050063 struct strbuf buf = STRBUF_INIT;
64 struct strbuf unquoted = STRBUF_INIT;
Adam Robend8ee4832008-05-23 16:19:38 +020065
Junio C Hamanoc0353c72015-10-28 13:56:23 -070066 while (strbuf_getline(&buf, stdin) != EOF) {
Adam Robend8ee4832008-05-23 16:19:38 +020067 if (buf.buf[0] == '"') {
Jeff King0d4cc1b2016-01-31 06:25:26 -050068 strbuf_reset(&unquoted);
69 if (unquote_c_style(&unquoted, buf.buf, NULL))
Adam Robend8ee4832008-05-23 16:19:38 +020070 die("line is badly quoted");
Jeff King0d4cc1b2016-01-31 06:25:26 -050071 strbuf_swap(&buf, &unquoted);
Adam Robend8ee4832008-05-23 16:19:38 +020072 }
Junio C Hamano5ba9a932014-09-11 13:14:51 -070073 hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
74 literally);
Adam Robend8ee4832008-05-23 16:19:38 +020075 }
76 strbuf_release(&buf);
Jeff King0d4cc1b2016-01-31 06:25:26 -050077 strbuf_release(&unquoted);
Adam Robend8ee4832008-05-23 16:19:38 +020078}
79
Linus Torvaldsb28a1ce2010-01-21 19:50:11 -080080int cmd_hash_object(int argc, const char **argv, const char *prefix)
Bryan Larsen7672db22005-07-08 16:51:55 -070081{
Junio C Hamanob64a9842014-09-11 12:19:54 -070082 static const char * const hash_object_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -070083 N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters] [--stdin] [--] <file>..."),
Jean-Noël Avila959d6702022-01-31 22:07:48 +000084 "git hash-object --stdin-paths",
Junio C Hamanob64a9842014-09-11 12:19:54 -070085 NULL
86 };
87 const char *type = blob_type;
88 int hashstdin = 0;
89 int stdin_paths = 0;
Junio C Hamanob64a9842014-09-11 12:19:54 -070090 int no_filters = 0;
Junio C Hamano5ba9a932014-09-11 13:14:51 -070091 int literally = 0;
Jeff King0e94ee92016-09-12 20:23:17 -070092 int nongit = 0;
Junio C Hamano17b787f2014-09-11 12:44:05 -070093 unsigned flags = HASH_FORMAT_CHECK;
Junio C Hamanob64a9842014-09-11 12:19:54 -070094 const char *vpath = NULL;
Ævar Arnfjörð Bjarmasond17294a2022-02-05 01:04:29 +010095 char *vpath_free = NULL;
Junio C Hamanob64a9842014-09-11 12:19:54 -070096 const struct option hash_object_options[] = {
97 OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
Junio C Hamano17b787f2014-09-11 12:44:05 -070098 OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
99 HASH_WRITE_OBJECT),
Junio C Hamanob64a9842014-09-11 12:19:54 -0700100 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 Hamano5ba9a932014-09-11 13:14:51 -0700103 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 -0700104 OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")),
105 OPT_END()
106 };
Bryan Larsen7672db22005-07-08 16:51:55 -0700107 int i;
Dmitry Potapov548601a2008-08-03 18:36:20 +0400108 const char *errstr = NULL;
109
Jeff Kingd64db5b2019-05-09 17:28:51 -0400110 argc = parse_options(argc, argv, prefix, hash_object_options,
Stephen Boyd37782922009-05-23 11:53:12 -0700111 hash_object_usage, 0);
Adam Robend8ee4832008-05-23 16:19:38 +0200112
Jeff King0e94ee92016-09-12 20:23:17 -0700113 if (flags & HASH_WRITE_OBJECT)
Dmitry Potapov548601a2008-08-03 18:36:20 +0400114 prefix = setup_git_directory();
Jeff King0e94ee92016-09-12 20:23:17 -0700115 else
116 prefix = setup_git_directory_gently(&nongit);
117
Ævar Arnfjörð Bjarmasond17294a2022-02-05 01:04:29 +0100118 if (vpath && prefix) {
119 vpath_free = prefix_filename(prefix, vpath);
120 vpath = vpath_free;
121 }
Gerrit Pape8a2f5e52008-02-21 10:06:47 +0000122
Elijah Newren272459a2009-02-28 12:56:49 -0700123 git_config(git_default_config, NULL);
124
Dmitry Potapov548601a2008-08-03 18:36:20 +0400125 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 Potapov39702432008-08-03 18:36:21 +0400130 else if (vpath)
131 errstr = "Can't use --stdin-paths with --path";
Dmitry Potapov548601a2008-08-03 18:36:20 +0400132 }
Dmitry Potapov4a3d85d2008-08-03 18:36:22 +0400133 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 Robend8ee4832008-05-23 16:19:38 +0200139
Dmitry Potapov548601a2008-08-03 18:36:20 +0400140 if (errstr) {
Daniel Lowe42fc1132008-11-10 16:07:52 -0500141 error("%s", errstr);
Dmitry Potapov548601a2008-08-03 18:36:20 +0400142 usage_with_options(hash_object_usage, hash_object_options);
143 }
144
145 if (hashstdin)
Junio C Hamano5ba9a932014-09-11 13:14:51 -0700146 hash_fd(0, type, vpath, flags, literally);
Dmitry Potapov548601a2008-08-03 18:36:20 +0400147
148 for (i = 0 ; i < argc; i++) {
149 const char *arg = argv[i];
Jeff Kinga1be47e2017-03-20 21:20:42 -0400150 char *to_free = NULL;
Dmitry Potapov548601a2008-08-03 18:36:20 +0400151
Jeff King116fb642017-03-20 21:22:28 -0400152 if (prefix)
Jeff Kinge4da43b2017-03-20 21:28:49 -0400153 arg = to_free = prefix_filename(prefix, arg);
Junio C Hamano17b787f2014-09-11 12:44:05 -0700154 hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
Junio C Hamano5ba9a932014-09-11 13:14:51 -0700155 flags, literally);
Jeff Kinga1be47e2017-03-20 21:20:42 -0400156 free(to_free);
Bryan Larsen7672db22005-07-08 16:51:55 -0700157 }
Adam Robend8ee4832008-05-23 16:19:38 +0200158
159 if (stdin_paths)
Junio C Hamano5ba9a932014-09-11 13:14:51 -0700160 hash_stdin_paths(type, no_filters, flags, literally);
Adam Robend8ee4832008-05-23 16:19:38 +0200161
Ævar Arnfjörð Bjarmasond17294a2022-02-05 01:04:29 +0100162 free(vpath_free);
163
Bryan Larsen7672db22005-07-08 16:51:55 -0700164 return 0;
165}