cat-file: do not die on --textconv without textconv filters
[git/mingw/j6t.git] / builtin / cat-file.c
blobfbaa1213f16fef0911ff3c1b2bf1cc3350105022
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "exec_cmd.h"
8 #include "tag.h"
9 #include "tree.h"
10 #include "builtin.h"
11 #include "parse-options.h"
12 #include "diff.h"
13 #include "userdiff.h"
14 #include "streaming.h"
16 #define BATCH 1
17 #define BATCH_CHECK 2
19 static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
21 /* the parser in tag.c is useless here. */
22 const char *endp = buf + size;
23 const char *cp = buf;
25 while (cp < endp) {
26 char c = *cp++;
27 if (c != '\n')
28 continue;
29 if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
30 const char *tagger = cp;
32 /* Found the tagger line. Copy out the contents
33 * of the buffer so far.
35 write_or_die(1, buf, cp - buf);
38 * Do something intelligent, like pretty-printing
39 * the date.
41 while (cp < endp) {
42 if (*cp++ == '\n') {
43 /* tagger to cp is a line
44 * that has ident and time.
46 const char *sp = tagger;
47 char *ep;
48 unsigned long date;
49 long tz;
50 while (sp < cp && *sp != '>')
51 sp++;
52 if (sp == cp) {
53 /* give up */
54 write_or_die(1, tagger,
55 cp - tagger);
56 break;
58 while (sp < cp &&
59 !('0' <= *sp && *sp <= '9'))
60 sp++;
61 write_or_die(1, tagger, sp - tagger);
62 date = strtoul(sp, &ep, 10);
63 tz = strtol(ep, NULL, 10);
64 sp = show_date(date, tz, 0);
65 write_or_die(1, sp, strlen(sp));
66 xwrite(1, "\n", 1);
67 break;
70 break;
72 if (cp < endp && *cp == '\n')
73 /* end of header */
74 break;
76 /* At this point, we have copied out the header up to the end of
77 * the tagger line and cp points at one past \n. It could be the
78 * next header line after the tagger line, or it could be another
79 * \n that marks the end of the headers. We need to copy out the
80 * remainder as is.
82 if (cp < endp)
83 write_or_die(1, cp, endp - cp);
86 static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
88 unsigned char sha1[20];
89 enum object_type type;
90 char *buf;
91 unsigned long size;
92 struct object_context obj_context;
94 if (get_sha1_with_context(obj_name, 0, sha1, &obj_context))
95 die("Not a valid object name %s", obj_name);
97 buf = NULL;
98 switch (opt) {
99 case 't':
100 type = sha1_object_info(sha1, NULL);
101 if (type > 0) {
102 printf("%s\n", typename(type));
103 return 0;
105 break;
107 case 's':
108 type = sha1_object_info(sha1, &size);
109 if (type > 0) {
110 printf("%lu\n", size);
111 return 0;
113 break;
115 case 'e':
116 return !has_sha1_file(sha1);
118 case 'c':
119 if (!obj_context.path[0])
120 die("git cat-file --textconv %s: <object> must be <sha1:path>",
121 obj_name);
123 if (textconv_object(obj_context.path, obj_context.mode, sha1, 1, &buf, &size))
124 break;
126 case 'p':
127 type = sha1_object_info(sha1, NULL);
128 if (type < 0)
129 die("Not a valid object name %s", obj_name);
131 /* custom pretty-print here */
132 if (type == OBJ_TREE) {
133 const char *ls_args[3] = { NULL };
134 ls_args[0] = "ls-tree";
135 ls_args[1] = obj_name;
136 return cmd_ls_tree(2, ls_args, NULL);
139 if (type == OBJ_BLOB)
140 return stream_blob_to_fd(1, sha1, NULL, 0);
141 buf = read_sha1_file(sha1, &type, &size);
142 if (!buf)
143 die("Cannot read object %s", obj_name);
144 if (type == OBJ_TAG) {
145 pprint_tag(sha1, buf, size);
146 return 0;
149 /* otherwise just spit out the data */
150 break;
152 case 0:
153 if (type_from_string(exp_type) == OBJ_BLOB) {
154 unsigned char blob_sha1[20];
155 if (sha1_object_info(sha1, NULL) == OBJ_TAG) {
156 enum object_type type;
157 unsigned long size;
158 char *buffer = read_sha1_file(sha1, &type, &size);
159 if (memcmp(buffer, "object ", 7) ||
160 get_sha1_hex(buffer + 7, blob_sha1))
161 die("%s not a valid tag", sha1_to_hex(sha1));
162 free(buffer);
163 } else
164 hashcpy(blob_sha1, sha1);
166 if (sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
167 return stream_blob_to_fd(1, blob_sha1, NULL, 0);
169 * we attempted to dereference a tag to a blob
170 * and failed; there may be new dereference
171 * mechanisms this code is not aware of.
172 * fall-back to the usual case.
175 buf = read_object_with_reference(sha1, exp_type, &size, NULL);
176 break;
178 default:
179 die("git cat-file: unknown option: %s", exp_type);
182 if (!buf)
183 die("git cat-file %s: bad file", obj_name);
185 write_or_die(1, buf, size);
186 return 0;
189 static int batch_one_object(const char *obj_name, int print_contents)
191 unsigned char sha1[20];
192 enum object_type type = 0;
193 unsigned long size;
194 void *contents = NULL;
196 if (!obj_name)
197 return 1;
199 if (get_sha1(obj_name, sha1)) {
200 printf("%s missing\n", obj_name);
201 fflush(stdout);
202 return 0;
205 if (print_contents == BATCH)
206 contents = read_sha1_file(sha1, &type, &size);
207 else
208 type = sha1_object_info(sha1, &size);
210 if (type <= 0) {
211 printf("%s missing\n", obj_name);
212 fflush(stdout);
213 if (print_contents == BATCH)
214 free(contents);
215 return 0;
218 printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
219 fflush(stdout);
221 if (print_contents == BATCH) {
222 write_or_die(1, contents, size);
223 printf("\n");
224 fflush(stdout);
225 free(contents);
228 return 0;
231 static int batch_objects(int print_contents)
233 struct strbuf buf = STRBUF_INIT;
235 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
236 int error = batch_one_object(buf.buf, print_contents);
237 if (error)
238 return error;
241 return 0;
244 static const char * const cat_file_usage[] = {
245 N_("git cat-file (-t|-s|-e|-p|<type>|--textconv) <object>"),
246 N_("git cat-file (--batch|--batch-check) < <list_of_objects>"),
247 NULL
250 static int git_cat_file_config(const char *var, const char *value, void *cb)
252 if (userdiff_config(var, value) < 0)
253 return -1;
255 return git_default_config(var, value, cb);
258 int cmd_cat_file(int argc, const char **argv, const char *prefix)
260 int opt = 0, batch = 0;
261 const char *exp_type = NULL, *obj_name = NULL;
263 const struct option options[] = {
264 OPT_GROUP(N_("<type> can be one of: blob, tree, commit, tag")),
265 OPT_SET_INT('t', NULL, &opt, N_("show object type"), 't'),
266 OPT_SET_INT('s', NULL, &opt, N_("show object size"), 's'),
267 OPT_SET_INT('e', NULL, &opt,
268 N_("exit with zero when there's no error"), 'e'),
269 OPT_SET_INT('p', NULL, &opt, N_("pretty-print object's content"), 'p'),
270 OPT_SET_INT(0, "textconv", &opt,
271 N_("for blob objects, run textconv on object's content"), 'c'),
272 OPT_SET_INT(0, "batch", &batch,
273 N_("show info and content of objects fed from the standard input"),
274 BATCH),
275 OPT_SET_INT(0, "batch-check", &batch,
276 N_("show info about objects fed from the standard input"),
277 BATCH_CHECK),
278 OPT_END()
281 git_config(git_cat_file_config, NULL);
283 if (argc != 3 && argc != 2)
284 usage_with_options(cat_file_usage, options);
286 argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0);
288 if (opt) {
289 if (argc == 1)
290 obj_name = argv[0];
291 else
292 usage_with_options(cat_file_usage, options);
294 if (!opt && !batch) {
295 if (argc == 2) {
296 exp_type = argv[0];
297 obj_name = argv[1];
298 } else
299 usage_with_options(cat_file_usage, options);
301 if (batch && (opt || argc)) {
302 usage_with_options(cat_file_usage, options);
305 if (batch)
306 return batch_objects(batch);
308 return cat_one_file(opt, exp_type, obj_name);