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 int cmd_cat_file(int argc, const char **argv, const char *prefix)
81 unsigned char sha1[20];
82 enum object_type type;
86 const char *exp_type, *obj_name;
88 git_config(git_default_config);
90 usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
94 if (get_sha1(obj_name, sha1))
95 die("Not a valid object name %s", obj_name);
98 if ( exp_type[0] == '-' ) {
100 if ( !opt || exp_type[2] )
101 opt = -1; /* Not a single character option */
107 type = sha1_object_info(sha1, NULL);
109 printf("%s\n", typename(type));
115 type = sha1_object_info(sha1, &size);
117 printf("%lu\n", size);
123 return !has_sha1_file(sha1);
126 type = sha1_object_info(sha1, NULL);
128 die("Not a valid object name %s", obj_name);
130 /* custom pretty-print here */
131 if (type == OBJ_TREE) {
132 const char *ls_args[3] = {"ls-tree", obj_name, NULL};
133 return cmd_ls_tree(2, ls_args, NULL);
136 buf = read_sha1_file(sha1, &type, &size);
138 die("Cannot read object %s", obj_name);
139 if (type == OBJ_TAG) {
140 pprint_tag(sha1, buf, size);
144 /* otherwise just spit out the data */
147 buf = read_object_with_reference(sha1, exp_type, &size, NULL);
151 die("git-cat-file: unknown option: %s\n", exp_type);
155 die("git-cat-file %s: bad file", obj_name);
157 write_or_die(1, buf, size);