2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
12 static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
14 /* the parser in tag.c is useless here. */
15 const char *endp = buf + size;
22 if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
23 const char *tagger = cp;
25 /* Found the tagger line. Copy out the contents
26 * of the buffer so far.
28 write_or_die(1, buf, cp - buf);
31 * Do something intelligent, like pretty-printing
36 /* tagger to cp is a line
37 * that has ident and time.
39 const char *sp = tagger;
43 while (sp < cp && *sp != '>')
47 write_or_die(1, tagger,
52 !('0' <= *sp && *sp <= '9'))
54 write_or_die(1, tagger, sp - tagger);
55 date = strtoul(sp, &ep, 10);
56 tz = strtol(ep, NULL, 10);
57 sp = show_date(date, tz, 0);
58 write_or_die(1, sp, strlen(sp));
65 if (cp < endp && *cp == '\n')
69 /* At this point, we have copied out the header up to the end of
70 * the tagger line and cp points at one past \n. It could be the
71 * next header line after the tagger line, or it could be another
72 * \n that marks the end of the headers. We need to copy out the
76 write_or_die(1, cp, endp - cp);
79 static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
81 unsigned char sha1[20];
82 enum object_type type;
86 if (get_sha1(obj_name, sha1))
87 die("Not a valid object name %s", obj_name);
92 type = sha1_object_info(sha1, NULL);
94 printf("%s\n", typename(type));
100 type = sha1_object_info(sha1, &size);
102 printf("%lu\n", size);
108 return !has_sha1_file(sha1);
111 type = sha1_object_info(sha1, NULL);
113 die("Not a valid object name %s", obj_name);
115 /* custom pretty-print here */
116 if (type == OBJ_TREE) {
117 const char *ls_args[3] = {"ls-tree", obj_name, NULL};
118 return cmd_ls_tree(2, ls_args, NULL);
121 buf = read_sha1_file(sha1, &type, &size);
123 die("Cannot read object %s", obj_name);
124 if (type == OBJ_TAG) {
125 pprint_tag(sha1, buf, size);
129 /* otherwise just spit out the data */
132 buf = read_object_with_reference(sha1, exp_type, &size, NULL);
136 die("git-cat-file: unknown option: %s\n", exp_type);
140 die("git-cat-file %s: bad file", obj_name);
142 write_or_die(1, buf, size);
146 static int batch_one_object(const char *obj_name, int print_contents)
148 unsigned char sha1[20];
149 enum object_type type;
151 void *contents = contents;
156 if (get_sha1(obj_name, sha1)) {
157 printf("%s missing\n", obj_name);
162 contents = read_sha1_file(sha1, &type, &size);
164 type = sha1_object_info(sha1, &size);
169 printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
172 if (print_contents) {
173 write_or_die(1, contents, size);
181 static int batch_objects(int print_contents)
185 strbuf_init(&buf, 0);
186 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
187 int error = batch_one_object(buf.buf, print_contents);
195 static const char cat_file_usage[] = "git-cat-file [ [-t|-s|-e|-p|<type>] <sha1> | [--batch|--batch-check] < <list_of_sha1s> ]";
197 int cmd_cat_file(int argc, const char **argv, const char *prefix)
199 int i, opt = 0, batch = 0, batch_check = 0;
200 const char *exp_type = NULL, *obj_name = NULL;
202 git_config(git_default_config);
204 for (i = 1; i < argc; ++i) {
205 const char *arg = argv[i];
206 int is_batch = 0, is_batch_check = 0;
208 is_batch = !strcmp(arg, "--batch");
210 is_batch_check = !strcmp(arg, "--batch-check");
212 if (is_batch || is_batch_check) {
214 error("git-cat-file: Can't use %s with -%c", arg, opt);
215 usage(cat_file_usage);
216 } else if (exp_type) {
217 error("git-cat-file: Can't use %s when a type (\"%s\") is specified", arg, exp_type);
218 usage(cat_file_usage);
219 } else if (obj_name) {
220 error("git-cat-file: Can't use %s when an object (\"%s\") is specified", arg, obj_name);
221 usage(cat_file_usage);
224 if ((is_batch && batch_check) || (is_batch_check && batch)) {
225 error("git-cat-file: Can't use %s with %s", arg, is_batch ? "--batch-check" : "--batch");
226 usage(cat_file_usage);
237 if (!strcmp(arg, "-t") || !strcmp(arg, "-s") || !strcmp(arg, "-e") || !strcmp(arg, "-p")) {
238 if (batch || batch_check) {
239 error("git-cat-file: Can't use %s with %s", arg, batch ? "--batch" : "--batch-check");
240 usage(cat_file_usage);
249 usage(cat_file_usage);
252 if (batch || batch_check) {
253 error("git-cat-file: Can't specify a type (\"%s\") with %s", arg, batch ? "--batch" : "--batch-check");
254 usage(cat_file_usage);
262 usage(cat_file_usage);
264 // We should have hit one of the earlier if (batch || batch_check) cases before
267 assert(!batch_check);
273 if (batch || batch_check)
274 return batch_objects(batch);
276 if (!exp_type || !obj_name)
277 usage(cat_file_usage);
279 return cat_one_file(opt, exp_type, obj_name);