2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 static void flush_buffer(const char *buf, unsigned long size)
12 long ret = xwrite(1, buf, size);
17 die("git-cat-file: %s", strerror(errno));
19 die("git-cat-file: disk full?");
26 static int pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
28 /* the parser in tag.c is useless here. */
29 const char *endp = buf + size;
36 if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
37 const char *tagger = cp;
39 /* Found the tagger line. Copy out the contents
40 * of the buffer so far.
42 flush_buffer(buf, cp - buf);
45 * Do something intelligent, like pretty-printing
50 /* tagger to cp is a line
51 * that has ident and time.
53 const char *sp = tagger;
57 while (sp < cp && *sp != '>')
66 !('0' <= *sp && *sp <= '9'))
68 flush_buffer(tagger, sp - tagger);
69 date = strtoul(sp, &ep, 10);
70 tz = strtol(ep, NULL, 10);
71 sp = show_date(date, tz);
72 flush_buffer(sp, strlen(sp));
79 if (cp < endp && *cp == '\n')
83 /* At this point, we have copied out the header up to the end of
84 * the tagger line and cp points at one past \n. It could be the
85 * next header line after the tagger line, or it could be another
86 * \n that marks the end of the headers. We need to copy out the
90 flush_buffer(cp, endp - cp);
94 int main(int argc, char **argv)
96 unsigned char sha1[20];
102 setup_git_directory();
103 if (argc != 3 || get_sha1(argv[2], sha1))
104 usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
107 if ( argv[1][0] == '-' ) {
109 if ( !opt || argv[1][2] )
110 opt = -1; /* Not a single character option */
116 if (!sha1_object_info(sha1, type, NULL)) {
117 printf("%s\n", type);
123 if (!sha1_object_info(sha1, type, &size)) {
124 printf("%lu\n", size);
130 return !has_sha1_file(sha1);
133 if (get_sha1(argv[2], sha1) ||
134 sha1_object_info(sha1, type, NULL))
135 die("Not a valid object name %s", argv[2]);
137 /* custom pretty-print here */
138 if (!strcmp(type, "tree"))
139 return execl_git_cmd("ls-tree", argv[2], NULL);
141 buf = read_sha1_file(sha1, type, &size);
143 die("Cannot read object %s", argv[2]);
144 if (!strcmp(type, "tag"))
145 return pprint_tag(sha1, buf, size);
147 /* otherwise just spit out the data */
150 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
154 die("git-cat-file: unknown option: %s\n", argv[1]);
158 die("git-cat-file %s: bad file", argv[2]);
160 flush_buffer(buf, size);