]>
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" |
8e440259 | 8 | #include "blob.h" |
d8ee4832 | 9 | #include "quote.h" |
548601ad | 10 | #include "parse-options.h" |
2fb3f6db | 11 | #include "exec_cmd.h" |
7672db20 | 12 | |
5ba9a93b JH |
13 | /* |
14 | * This is to create corrupt objects for debugging and as such it | |
15 | * needs to bypass the data conversion performed by, and the type | |
16 | * limitation imposed by, index_fd() and its callees. | |
17 | */ | |
18 | static int hash_literally(unsigned char *sha1, int fd, const char *type, unsigned flags) | |
19 | { | |
20 | struct strbuf buf = STRBUF_INIT; | |
21 | int ret; | |
22 | ||
23 | if (strbuf_read(&buf, fd, 4096) < 0) | |
24 | ret = -1; | |
25 | else if (flags & HASH_WRITE_OBJECT) | |
26 | ret = write_sha1_file(buf.buf, buf.len, type, sha1); | |
27 | else | |
28 | ret = hash_sha1_file(buf.buf, buf.len, type, sha1); | |
29 | strbuf_release(&buf); | |
30 | return ret; | |
31 | } | |
32 | ||
33 | static void hash_fd(int fd, const char *type, const char *path, unsigned flags, | |
34 | int literally) | |
7672db20 | 35 | { |
7672db20 BL |
36 | struct stat st; |
37 | unsigned char sha1[20]; | |
c4ce46fc | 38 | |
43df4f86 | 39 | if (fstat(fd, &st) < 0 || |
5ba9a93b JH |
40 | (literally |
41 | ? hash_literally(sha1, fd, type, flags) | |
42 | : index_fd(sha1, fd, &st, type_from_string(type), path, flags))) | |
17b787f6 | 43 | die((flags & HASH_WRITE_OBJECT) |
7672db20 BL |
44 | ? "Unable to add %s to database" |
45 | : "Unable to hash %s", path); | |
46 | printf("%s\n", sha1_to_hex(sha1)); | |
d8ee4832 | 47 | maybe_flush_or_die(stdout, "hash to stdout"); |
7672db20 BL |
48 | } |
49 | ||
17b787f6 | 50 | static void hash_object(const char *path, const char *type, const char *vpath, |
5ba9a93b | 51 | unsigned flags, int literally) |
024510c8 | 52 | { |
43df4f86 DP |
53 | int fd; |
54 | fd = open(path, O_RDONLY); | |
55 | if (fd < 0) | |
0721c314 | 56 | die_errno("Cannot open '%s'", path); |
5ba9a93b | 57 | hash_fd(fd, type, vpath, flags, literally); |
024510c8 DB |
58 | } |
59 | ||
5ba9a93b JH |
60 | static void hash_stdin_paths(const char *type, int no_filters, unsigned flags, |
61 | int literally) | |
d8ee4832 | 62 | { |
f285a2d7 | 63 | struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT; |
d8ee4832 | 64 | |
d8ee4832 AR |
65 | while (strbuf_getline(&buf, stdin, '\n') != EOF) { |
66 | if (buf.buf[0] == '"') { | |
67 | strbuf_reset(&nbuf); | |
68 | if (unquote_c_style(&nbuf, buf.buf, NULL)) | |
69 | die("line is badly quoted"); | |
70 | strbuf_swap(&buf, &nbuf); | |
71 | } | |
5ba9a93b JH |
72 | hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags, |
73 | literally); | |
d8ee4832 AR |
74 | } |
75 | strbuf_release(&buf); | |
76 | strbuf_release(&nbuf); | |
77 | } | |
78 | ||
b28a1ce0 | 79 | int cmd_hash_object(int argc, const char **argv, const char *prefix) |
7672db20 | 80 | { |
b64a9846 JH |
81 | static const char * const hash_object_usage[] = { |
82 | N_("git hash-object [-t <type>] [-w] [--path=<file>|--no-filters] [--stdin] [--] <file>..."), | |
83 | N_("git hash-object --stdin-paths < <list-of-paths>"), | |
84 | NULL | |
85 | }; | |
86 | const char *type = blob_type; | |
87 | int hashstdin = 0; | |
88 | int stdin_paths = 0; | |
b64a9846 | 89 | int no_filters = 0; |
5ba9a93b | 90 | int literally = 0; |
17b787f6 | 91 | unsigned flags = HASH_FORMAT_CHECK; |
b64a9846 JH |
92 | const char *vpath = NULL; |
93 | const struct option hash_object_options[] = { | |
94 | OPT_STRING('t', NULL, &type, N_("type"), N_("object type")), | |
17b787f6 JH |
95 | OPT_BIT('w', NULL, &flags, N_("write the object into the object database"), |
96 | HASH_WRITE_OBJECT), | |
b64a9846 JH |
97 | OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")), |
98 | OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")), | |
99 | OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")), | |
5ba9a93b | 100 | OPT_BOOL( 0, "literally", &literally, N_("just hash any random garbage to create corrupt objects for debugging Git")), |
b64a9846 JH |
101 | OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")), |
102 | OPT_END() | |
103 | }; | |
7672db20 | 104 | int i; |
706fe6ae | 105 | int prefix_length = -1; |
548601ad DP |
106 | const char *errstr = NULL; |
107 | ||
37782920 SB |
108 | argc = parse_options(argc, argv, NULL, hash_object_options, |
109 | hash_object_usage, 0); | |
548601ad | 110 | |
17b787f6 | 111 | if (flags & HASH_WRITE_OBJECT) { |
548601ad DP |
112 | prefix = setup_git_directory(); |
113 | prefix_length = prefix ? strlen(prefix) : 0; | |
39702431 DP |
114 | if (vpath && prefix) |
115 | vpath = prefix_filename(prefix, prefix_length, vpath); | |
7672db20 | 116 | } |
d8ee4832 | 117 | |
272459a3 EN |
118 | git_config(git_default_config, NULL); |
119 | ||
548601ad DP |
120 | if (stdin_paths) { |
121 | if (hashstdin) | |
122 | errstr = "Can't use --stdin-paths with --stdin"; | |
123 | else if (argc) | |
124 | errstr = "Can't specify files with --stdin-paths"; | |
39702431 DP |
125 | else if (vpath) |
126 | errstr = "Can't use --stdin-paths with --path"; | |
4a3d85dc DP |
127 | } |
128 | else { | |
129 | if (hashstdin > 1) | |
130 | errstr = "Multiple --stdin arguments are not supported"; | |
131 | if (vpath && no_filters) | |
132 | errstr = "Can't use --path with --no-filters"; | |
548601ad | 133 | } |
548601ad DP |
134 | |
135 | if (errstr) { | |
42fc1139 | 136 | error("%s", errstr); |
548601ad DP |
137 | usage_with_options(hash_object_usage, hash_object_options); |
138 | } | |
d8ee4832 | 139 | |
8a2f5e5b | 140 | if (hashstdin) |
5ba9a93b | 141 | hash_fd(0, type, vpath, flags, literally); |
548601ad DP |
142 | |
143 | for (i = 0 ; i < argc; i++) { | |
144 | const char *arg = argv[i]; | |
145 | ||
146 | if (0 <= prefix_length) | |
147 | arg = prefix_filename(prefix, prefix_length, arg); | |
17b787f6 | 148 | hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg, |
5ba9a93b | 149 | flags, literally); |
548601ad DP |
150 | } |
151 | ||
152 | if (stdin_paths) | |
5ba9a93b | 153 | hash_stdin_paths(type, no_filters, flags, literally); |
548601ad | 154 | |
7672db20 BL |
155 | return 0; |
156 | } |