tree/diff header cleanup.
[git/dscho.git] / cat-file.c
blob761111eb0f1f3266fe6bd0e9e561ee3d3d6c21d4
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "exec_cmd.h"
9 static void flush_buffer(const char *buf, unsigned long size)
11 while (size > 0) {
12 long ret = xwrite(1, buf, size);
13 if (ret < 0) {
14 /* Ignore epipe */
15 if (errno == EPIPE)
16 break;
17 die("git-cat-file: %s", strerror(errno));
18 } else if (!ret) {
19 die("git-cat-file: disk full?");
21 size -= ret;
22 buf += ret;
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;
30 const char *cp = buf;
32 while (cp < endp) {
33 char c = *cp++;
34 if (c != '\n')
35 continue;
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
46 * the date.
48 while (cp < endp) {
49 if (*cp++ == '\n') {
50 /* tagger to cp is a line
51 * that has ident and time.
53 const char *sp = tagger;
54 char *ep;
55 unsigned long date;
56 long tz;
57 while (sp < cp && *sp != '>')
58 sp++;
59 if (sp == cp) {
60 /* give up */
61 flush_buffer(tagger,
62 cp - tagger);
63 break;
65 while (sp < cp &&
66 !('0' <= *sp && *sp <= '9'))
67 sp++;
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));
73 xwrite(1, "\n", 1);
74 break;
77 break;
79 if (cp < endp && *cp == '\n')
80 /* end of header */
81 break;
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
87 * remainder as is.
89 if (cp < endp)
90 flush_buffer(cp, endp - cp);
91 return 0;
94 int main(int argc, char **argv)
96 unsigned char sha1[20];
97 char type[20];
98 void *buf;
99 unsigned long size;
100 int opt;
102 setup_git_directory();
103 git_config(git_default_config);
104 if (argc != 3 || get_sha1(argv[2], sha1))
105 usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
107 opt = 0;
108 if ( argv[1][0] == '-' ) {
109 opt = argv[1][1];
110 if ( !opt || argv[1][2] )
111 opt = -1; /* Not a single character option */
114 buf = NULL;
115 switch (opt) {
116 case 't':
117 if (!sha1_object_info(sha1, type, NULL)) {
118 printf("%s\n", type);
119 return 0;
121 break;
123 case 's':
124 if (!sha1_object_info(sha1, type, &size)) {
125 printf("%lu\n", size);
126 return 0;
128 break;
130 case 'e':
131 return !has_sha1_file(sha1);
133 case 'p':
134 if (get_sha1(argv[2], sha1) ||
135 sha1_object_info(sha1, type, NULL))
136 die("Not a valid object name %s", argv[2]);
138 /* custom pretty-print here */
139 if (!strcmp(type, "tree"))
140 return execl_git_cmd("ls-tree", argv[2], NULL);
142 buf = read_sha1_file(sha1, type, &size);
143 if (!buf)
144 die("Cannot read object %s", argv[2]);
145 if (!strcmp(type, "tag"))
146 return pprint_tag(sha1, buf, size);
148 /* otherwise just spit out the data */
149 break;
150 case 0:
151 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
152 break;
154 default:
155 die("git-cat-file: unknown option: %s\n", argv[1]);
158 if (!buf)
159 die("git-cat-file %s: bad file", argv[2]);
161 flush_buffer(buf, size);
162 return 0;