]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/hash-object.c
Merge branch 'js/update-index-ignore-removal-for-skip-worktree'
[thirdparty/git.git] / builtin / hash-object.c
CommitLineData
7672db20
BL
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
8e440259 5 * Copyright (C) Junio C Hamano, 2005
7672db20 6 */
c2e86add 7#include "builtin.h"
b2141fc1 8#include "config.h"
cbd53a21 9#include "object-store.h"
8e440259 10#include "blob.h"
d8ee4832 11#include "quote.h"
548601ad 12#include "parse-options.h"
d807c4a0 13#include "exec-cmd.h"
7672db20 14
5ba9a93b
JH
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 */
eab8bf29 20static int hash_literally(struct object_id *oid, int fd, const char *type, unsigned flags)
5ba9a93b
JH
21{
22 struct strbuf buf = STRBUF_INIT;
23 int ret;
24
25 if (strbuf_read(&buf, fd, 4096) < 0)
26 ret = -1;
5ba9a93b 27 else
1752cbbc
PO
28 ret = hash_object_file_literally(buf.buf, buf.len, type, oid,
29 flags);
5ba9a93b
JH
30 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)
7672db20 36{
7672db20 37 struct stat st;
eab8bf29 38 struct object_id oid;
c4ce46fc 39
43df4f86 40 if (fstat(fd, &st) < 0 ||
5ba9a93b 41 (literally
eab8bf29 42 ? hash_literally(&oid, fd, type, flags)
f8adbec9
NTND
43 : index_fd(the_repository->index, &oid, fd, &st,
44 type_from_string(type), path, flags)))
17b787f6 45 die((flags & HASH_WRITE_OBJECT)
7672db20
BL
46 ? "Unable to add %s to database"
47 : "Unable to hash %s", path);
eab8bf29 48 printf("%s\n", oid_to_hex(&oid));
d8ee4832 49 maybe_flush_or_die(stdout, "hash to stdout");
7672db20
BL
50}
51
17b787f6 52static void hash_object(const char *path, const char *type, const char *vpath,
5ba9a93b 53 unsigned flags, int literally)
024510c8 54{
43df4f86
DP
55 int fd;
56 fd = open(path, O_RDONLY);
57 if (fd < 0)
0721c314 58 die_errno("Cannot open '%s'", path);
5ba9a93b 59 hash_fd(fd, type, vpath, flags, literally);
024510c8
DB
60}
61
5ba9a93b
JH
62static void hash_stdin_paths(const char *type, int no_filters, unsigned flags,
63 int literally)
d8ee4832 64{
0d4cc1b4
JK
65 struct strbuf buf = STRBUF_INIT;
66 struct strbuf unquoted = STRBUF_INIT;
d8ee4832 67
c0353c78 68 while (strbuf_getline(&buf, stdin) != EOF) {
d8ee4832 69 if (buf.buf[0] == '"') {
0d4cc1b4
JK
70 strbuf_reset(&unquoted);
71 if (unquote_c_style(&unquoted, buf.buf, NULL))
d8ee4832 72 die("line is badly quoted");
0d4cc1b4 73 strbuf_swap(&buf, &unquoted);
d8ee4832 74 }
5ba9a93b
JH
75 hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags,
76 literally);
d8ee4832
AR
77 }
78 strbuf_release(&buf);
0d4cc1b4 79 strbuf_release(&unquoted);
d8ee4832
AR
80}
81
b28a1ce0 82int cmd_hash_object(int argc, const char **argv, const char *prefix)
7672db20 83{
b64a9846 84 static const char * const hash_object_usage[] = {
9c9b4f2f 85 N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters] [--stdin] [--] <file>..."),
33e8fc87 86 N_("git hash-object --stdin-paths"),
b64a9846
JH
87 NULL
88 };
89 const char *type = blob_type;
90 int hashstdin = 0;
91 int stdin_paths = 0;
b64a9846 92 int no_filters = 0;
5ba9a93b 93 int literally = 0;
0e94ee94 94 int nongit = 0;
17b787f6 95 unsigned flags = HASH_FORMAT_CHECK;
b64a9846
JH
96 const char *vpath = NULL;
97 const struct option hash_object_options[] = {
98 OPT_STRING('t', NULL, &type, N_("type"), N_("object type")),
17b787f6
JH
99 OPT_BIT('w', NULL, &flags, N_("write the object into the object database"),
100 HASH_WRITE_OBJECT),
b64a9846
JH
101 OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")),
102 OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")),
103 OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")),
5ba9a93b 104 OPT_BOOL( 0, "literally", &literally, N_("just hash any random garbage to create corrupt objects for debugging Git")),
b64a9846
JH
105 OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")),
106 OPT_END()
107 };
7672db20 108 int i;
548601ad
DP
109 const char *errstr = NULL;
110
d64db5b3 111 argc = parse_options(argc, argv, prefix, hash_object_options,
37782920 112 hash_object_usage, 0);
548601ad 113
0e94ee94 114 if (flags & HASH_WRITE_OBJECT)
548601ad 115 prefix = setup_git_directory();
0e94ee94
JK
116 else
117 prefix = setup_git_directory_gently(&nongit);
118
0e94ee94 119 if (vpath && prefix)
116fb64e 120 vpath = xstrdup(prefix_filename(prefix, vpath));
d8ee4832 121
272459a3
EN
122 git_config(git_default_config, NULL);
123
548601ad
DP
124 if (stdin_paths) {
125 if (hashstdin)
126 errstr = "Can't use --stdin-paths with --stdin";
127 else if (argc)
128 errstr = "Can't specify files with --stdin-paths";
39702431
DP
129 else if (vpath)
130 errstr = "Can't use --stdin-paths with --path";
4a3d85dc
DP
131 }
132 else {
133 if (hashstdin > 1)
134 errstr = "Multiple --stdin arguments are not supported";
135 if (vpath && no_filters)
136 errstr = "Can't use --path with --no-filters";
548601ad 137 }
548601ad
DP
138
139 if (errstr) {
42fc1139 140 error("%s", errstr);
548601ad
DP
141 usage_with_options(hash_object_usage, hash_object_options);
142 }
d8ee4832 143
8a2f5e5b 144 if (hashstdin)
5ba9a93b 145 hash_fd(0, type, vpath, flags, literally);
548601ad
DP
146
147 for (i = 0 ; i < argc; i++) {
148 const char *arg = argv[i];
a1be47e4 149 char *to_free = NULL;
548601ad 150
116fb64e 151 if (prefix)
e4da43b1 152 arg = to_free = prefix_filename(prefix, arg);
17b787f6 153 hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg,
5ba9a93b 154 flags, literally);
a1be47e4 155 free(to_free);
548601ad
DP
156 }
157
158 if (stdin_paths)
5ba9a93b 159 hash_stdin_paths(type, no_filters, flags, literally);
548601ad 160
7672db20
BL
161 return 0;
162}