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