]>
Commit | Line | Data |
---|---|---|
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" |
0b027f6c | 8 | #include "abspath.h" |
b2141fc1 | 9 | #include "config.h" |
f394e093 | 10 | #include "gettext.h" |
41771fa4 | 11 | #include "hex.h" |
87bed179 | 12 | #include "object-file.h" |
a034e910 | 13 | #include "object-store-ll.h" |
8e440259 | 14 | #include "blob.h" |
d8ee4832 | 15 | #include "quote.h" |
548601ad | 16 | #include "parse-options.h" |
d807c4a0 | 17 | #include "exec-cmd.h" |
e38da487 | 18 | #include "setup.h" |
a034e910 | 19 | #include "strbuf.h" |
d48be35c | 20 | #include "write-or-die.h" |
7672db20 | 21 | |
5ba9a93b JH |
22 | /* |
23 | * This is to create corrupt objects for debugging and as such it | |
24 | * needs to bypass the data conversion performed by, and the type | |
25 | * limitation imposed by, index_fd() and its callees. | |
26 | */ | |
eab8bf29 | 27 | static int hash_literally(struct object_id *oid, int fd, const char *type, unsigned flags) |
5ba9a93b JH |
28 | { |
29 | struct strbuf buf = STRBUF_INIT; | |
30 | int ret; | |
31 | ||
32 | if (strbuf_read(&buf, fd, 4096) < 0) | |
33 | ret = -1; | |
5ba9a93b | 34 | else |
0ff7b4f9 | 35 | ret = write_object_file_literally(buf.buf, buf.len, type, oid, |
1752cbbc | 36 | flags); |
590b6367 | 37 | close(fd); |
5ba9a93b JH |
38 | strbuf_release(&buf); |
39 | return ret; | |
40 | } | |
41 | ||
42 | static void hash_fd(int fd, const char *type, const char *path, unsigned flags, | |
43 | int literally) | |
7672db20 | 44 | { |
7672db20 | 45 | struct stat st; |
eab8bf29 | 46 | struct object_id oid; |
c4ce46fc | 47 | |
43df4f86 | 48 | if (fstat(fd, &st) < 0 || |
5ba9a93b | 49 | (literally |
eab8bf29 | 50 | ? hash_literally(&oid, fd, type, flags) |
f8adbec9 NTND |
51 | : index_fd(the_repository->index, &oid, fd, &st, |
52 | type_from_string(type), path, flags))) | |
17b787f6 | 53 | die((flags & HASH_WRITE_OBJECT) |
7672db20 BL |
54 | ? "Unable to add %s to database" |
55 | : "Unable to hash %s", path); | |
eab8bf29 | 56 | printf("%s\n", oid_to_hex(&oid)); |
d8ee4832 | 57 | maybe_flush_or_die(stdout, "hash to stdout"); |
7672db20 BL |
58 | } |
59 | ||
17b787f6 | 60 | static void hash_object(const char *path, const char *type, const char *vpath, |
5ba9a93b | 61 | unsigned flags, int literally) |
024510c8 | 62 | { |
43df4f86 | 63 | int fd; |
66e905b7 | 64 | fd = xopen(path, O_RDONLY); |
5ba9a93b | 65 | hash_fd(fd, type, vpath, flags, literally); |
024510c8 DB |
66 | } |
67 | ||
5ba9a93b JH |
68 | static void hash_stdin_paths(const char *type, int no_filters, unsigned flags, |
69 | int literally) | |
d8ee4832 | 70 | { |
0d4cc1b4 JK |
71 | struct strbuf buf = STRBUF_INIT; |
72 | struct strbuf unquoted = STRBUF_INIT; | |
d8ee4832 | 73 | |
c0353c78 | 74 | while (strbuf_getline(&buf, stdin) != EOF) { |
d8ee4832 | 75 | if (buf.buf[0] == '"') { |
0d4cc1b4 JK |
76 | strbuf_reset(&unquoted); |
77 | if (unquote_c_style(&unquoted, buf.buf, NULL)) | |
d8ee4832 | 78 | die("line is badly quoted"); |
0d4cc1b4 | 79 | strbuf_swap(&buf, &unquoted); |
d8ee4832 | 80 | } |
5ba9a93b JH |
81 | hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags, |
82 | literally); | |
d8ee4832 AR |
83 | } |
84 | strbuf_release(&buf); | |
0d4cc1b4 | 85 | strbuf_release(&unquoted); |
d8ee4832 AR |
86 | } |
87 | ||
b28a1ce0 | 88 | int cmd_hash_object(int argc, const char **argv, const char *prefix) |
7672db20 | 89 | { |
b64a9846 | 90 | static const char * const hash_object_usage[] = { |
5af8b61c | 91 | N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters]\n" |
d9054a19 ÆAB |
92 | " [--stdin [--literally]] [--] <file>..."), |
93 | N_("git hash-object [-t <type>] [-w] --stdin-paths [--no-filters]"), | |
b64a9846 JH |
94 | NULL |
95 | }; | |
96 | const char *type = blob_type; | |
97 | int hashstdin = 0; | |
98 | int stdin_paths = 0; | |
b64a9846 | 99 | int no_filters = 0; |
5ba9a93b | 100 | int literally = 0; |
0e94ee94 | 101 | int nongit = 0; |
17b787f6 | 102 | unsigned flags = HASH_FORMAT_CHECK; |
b64a9846 | 103 | const char *vpath = NULL; |
d17294a0 | 104 | char *vpath_free = NULL; |
b64a9846 JH |
105 | const struct option hash_object_options[] = { |
106 | OPT_STRING('t', NULL, &type, N_("type"), N_("object type")), | |
17b787f6 JH |
107 | OPT_BIT('w', NULL, &flags, N_("write the object into the object database"), |
108 | HASH_WRITE_OBJECT), | |
b64a9846 JH |
109 | OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")), |
110 | OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")), | |
111 | OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")), | |
5ba9a93b | 112 | OPT_BOOL( 0, "literally", &literally, N_("just hash any random garbage to create corrupt objects for debugging Git")), |
b64a9846 JH |
113 | OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")), |
114 | OPT_END() | |
115 | }; | |
7672db20 | 116 | int i; |
548601ad DP |
117 | const char *errstr = NULL; |
118 | ||
d64db5b3 | 119 | argc = parse_options(argc, argv, prefix, hash_object_options, |
37782920 | 120 | hash_object_usage, 0); |
548601ad | 121 | |
0e94ee94 | 122 | if (flags & HASH_WRITE_OBJECT) |
548601ad | 123 | prefix = setup_git_directory(); |
0e94ee94 JK |
124 | else |
125 | prefix = setup_git_directory_gently(&nongit); | |
126 | ||
d17294a0 ÆAB |
127 | if (vpath && prefix) { |
128 | vpath_free = prefix_filename(prefix, vpath); | |
129 | vpath = vpath_free; | |
130 | } | |
d8ee4832 | 131 | |
272459a3 EN |
132 | git_config(git_default_config, NULL); |
133 | ||
548601ad DP |
134 | if (stdin_paths) { |
135 | if (hashstdin) | |
136 | errstr = "Can't use --stdin-paths with --stdin"; | |
137 | else if (argc) | |
138 | errstr = "Can't specify files with --stdin-paths"; | |
39702431 DP |
139 | else if (vpath) |
140 | errstr = "Can't use --stdin-paths with --path"; | |
4a3d85dc DP |
141 | } |
142 | else { | |
143 | if (hashstdin > 1) | |
144 | errstr = "Multiple --stdin arguments are not supported"; | |
145 | if (vpath && no_filters) | |
146 | errstr = "Can't use --path with --no-filters"; | |
548601ad | 147 | } |
548601ad DP |
148 | |
149 | if (errstr) { | |
42fc1139 | 150 | error("%s", errstr); |
548601ad DP |
151 | usage_with_options(hash_object_usage, hash_object_options); |
152 | } | |
d8ee4832 | 153 | |
8a2f5e5b | 154 | if (hashstdin) |
5ba9a93b | 155 | hash_fd(0, type, vpath, flags, literally); |
548601ad DP |
156 | |
157 | for (i = 0 ; i < argc; i++) { | |
158 | const char *arg = argv[i]; | |
a1be47e4 | 159 | char *to_free = NULL; |
548601ad | 160 | |
116fb64e | 161 | if (prefix) |
e4da43b1 | 162 | arg = to_free = prefix_filename(prefix, arg); |
17b787f6 | 163 | hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg, |
5ba9a93b | 164 | flags, literally); |
a1be47e4 | 165 | free(to_free); |
548601ad DP |
166 | } |
167 | ||
168 | if (stdin_paths) | |
5ba9a93b | 169 | hash_stdin_paths(type, no_filters, flags, literally); |
548601ad | 170 | |
d17294a0 ÆAB |
171 | free(vpath_free); |
172 | ||
7672db20 BL |
173 | return 0; |
174 | } |