]>
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" |
b2141fc1 | 8 | #include "config.h" |
8e440259 | 9 | #include "blob.h" |
d8ee4832 | 10 | #include "quote.h" |
548601ad | 11 | #include "parse-options.h" |
d807c4a0 | 12 | #include "exec-cmd.h" |
7672db20 | 13 | |
5ba9a93b JH |
14 | /* |
15 | * This is to create corrupt objects for debugging and as such it | |
16 | * needs to bypass the data conversion performed by, and the type | |
17 | * limitation imposed by, index_fd() and its callees. | |
18 | */ | |
eab8bf29 | 19 | static int hash_literally(struct object_id *oid, int fd, const char *type, unsigned flags) |
5ba9a93b JH |
20 | { |
21 | struct strbuf buf = STRBUF_INIT; | |
22 | int ret; | |
23 | ||
24 | if (strbuf_read(&buf, fd, 4096) < 0) | |
25 | ret = -1; | |
5ba9a93b | 26 | else |
1752cbbc PO |
27 | ret = hash_object_file_literally(buf.buf, buf.len, type, oid, |
28 | flags); | |
5ba9a93b JH |
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 | 36 | struct stat st; |
eab8bf29 | 37 | struct object_id oid; |
c4ce46fc | 38 | |
43df4f86 | 39 | if (fstat(fd, &st) < 0 || |
5ba9a93b | 40 | (literally |
eab8bf29 | 41 | ? hash_literally(&oid, fd, type, flags) |
e3506559 | 42 | : index_fd(&oid, 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); | |
eab8bf29 | 46 | printf("%s\n", oid_to_hex(&oid)); |
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 | { |
0d4cc1b4 JK |
63 | struct strbuf buf = STRBUF_INIT; |
64 | struct strbuf unquoted = STRBUF_INIT; | |
d8ee4832 | 65 | |
c0353c78 | 66 | while (strbuf_getline(&buf, stdin) != EOF) { |
d8ee4832 | 67 | if (buf.buf[0] == '"') { |
0d4cc1b4 JK |
68 | strbuf_reset(&unquoted); |
69 | if (unquote_c_style(&unquoted, buf.buf, NULL)) | |
d8ee4832 | 70 | die("line is badly quoted"); |
0d4cc1b4 | 71 | strbuf_swap(&buf, &unquoted); |
d8ee4832 | 72 | } |
5ba9a93b JH |
73 | hash_object(buf.buf, type, no_filters ? NULL : buf.buf, flags, |
74 | literally); | |
d8ee4832 AR |
75 | } |
76 | strbuf_release(&buf); | |
0d4cc1b4 | 77 | strbuf_release(&unquoted); |
d8ee4832 AR |
78 | } |
79 | ||
b28a1ce0 | 80 | int cmd_hash_object(int argc, const char **argv, const char *prefix) |
7672db20 | 81 | { |
b64a9846 | 82 | static const char * const hash_object_usage[] = { |
9c9b4f2f | 83 | N_("git hash-object [-t <type>] [-w] [--path=<file> | --no-filters] [--stdin] [--] <file>..."), |
33e8fc87 | 84 | N_("git hash-object --stdin-paths"), |
b64a9846 JH |
85 | NULL |
86 | }; | |
87 | const char *type = blob_type; | |
88 | int hashstdin = 0; | |
89 | int stdin_paths = 0; | |
b64a9846 | 90 | int no_filters = 0; |
5ba9a93b | 91 | int literally = 0; |
0e94ee94 | 92 | int nongit = 0; |
17b787f6 | 93 | unsigned flags = HASH_FORMAT_CHECK; |
b64a9846 JH |
94 | const char *vpath = NULL; |
95 | const struct option hash_object_options[] = { | |
96 | OPT_STRING('t', NULL, &type, N_("type"), N_("object type")), | |
17b787f6 JH |
97 | OPT_BIT('w', NULL, &flags, N_("write the object into the object database"), |
98 | HASH_WRITE_OBJECT), | |
b64a9846 JH |
99 | OPT_COUNTUP( 0 , "stdin", &hashstdin, N_("read the object from stdin")), |
100 | OPT_BOOL( 0 , "stdin-paths", &stdin_paths, N_("read file names from stdin")), | |
101 | OPT_BOOL( 0 , "no-filters", &no_filters, N_("store file as is without filters")), | |
5ba9a93b | 102 | OPT_BOOL( 0, "literally", &literally, N_("just hash any random garbage to create corrupt objects for debugging Git")), |
b64a9846 JH |
103 | OPT_STRING( 0 , "path", &vpath, N_("file"), N_("process file as it were from this path")), |
104 | OPT_END() | |
105 | }; | |
7672db20 | 106 | int i; |
548601ad DP |
107 | const char *errstr = NULL; |
108 | ||
37782920 SB |
109 | argc = parse_options(argc, argv, NULL, hash_object_options, |
110 | hash_object_usage, 0); | |
548601ad | 111 | |
0e94ee94 | 112 | if (flags & HASH_WRITE_OBJECT) |
548601ad | 113 | prefix = setup_git_directory(); |
0e94ee94 JK |
114 | else |
115 | prefix = setup_git_directory_gently(&nongit); | |
116 | ||
0e94ee94 | 117 | if (vpath && prefix) |
116fb64e | 118 | vpath = xstrdup(prefix_filename(prefix, vpath)); |
d8ee4832 | 119 | |
272459a3 EN |
120 | git_config(git_default_config, NULL); |
121 | ||
548601ad DP |
122 | if (stdin_paths) { |
123 | if (hashstdin) | |
124 | errstr = "Can't use --stdin-paths with --stdin"; | |
125 | else if (argc) | |
126 | errstr = "Can't specify files with --stdin-paths"; | |
39702431 DP |
127 | else if (vpath) |
128 | errstr = "Can't use --stdin-paths with --path"; | |
4a3d85dc DP |
129 | } |
130 | else { | |
131 | if (hashstdin > 1) | |
132 | errstr = "Multiple --stdin arguments are not supported"; | |
133 | if (vpath && no_filters) | |
134 | errstr = "Can't use --path with --no-filters"; | |
548601ad | 135 | } |
548601ad DP |
136 | |
137 | if (errstr) { | |
42fc1139 | 138 | error("%s", errstr); |
548601ad DP |
139 | usage_with_options(hash_object_usage, hash_object_options); |
140 | } | |
d8ee4832 | 141 | |
8a2f5e5b | 142 | if (hashstdin) |
5ba9a93b | 143 | hash_fd(0, type, vpath, flags, literally); |
548601ad DP |
144 | |
145 | for (i = 0 ; i < argc; i++) { | |
146 | const char *arg = argv[i]; | |
a1be47e4 | 147 | char *to_free = NULL; |
548601ad | 148 | |
116fb64e | 149 | if (prefix) |
e4da43b1 | 150 | arg = to_free = prefix_filename(prefix, arg); |
17b787f6 | 151 | hash_object(arg, type, no_filters ? NULL : vpath ? vpath : arg, |
5ba9a93b | 152 | flags, literally); |
a1be47e4 | 153 | free(to_free); |
548601ad DP |
154 | } |
155 | ||
156 | if (stdin_paths) | |
5ba9a93b | 157 | hash_stdin_paths(type, no_filters, flags, literally); |
548601ad | 158 | |
7672db20 BL |
159 | return 0; |
160 | } |