use declarations from builtin.h for builtin commands
[git/kirr.git] / builtin-cat-file.c
blob814fb0743f7a453114991af6c28bb69ef44d4883
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"
12 static void flush_buffer(const char *buf, unsigned long size)
14 while (size > 0) {
15 long ret = xwrite(1, buf, size);
16 if (ret < 0) {
17 /* Ignore epipe */
18 if (errno == EPIPE)
19 break;
20 die("git-cat-file: %s", strerror(errno));
21 } else if (!ret) {
22 die("git-cat-file: disk full?");
24 size -= ret;
25 buf += ret;
29 static int pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
31 /* the parser in tag.c is useless here. */
32 const char *endp = buf + size;
33 const char *cp = buf;
35 while (cp < endp) {
36 char c = *cp++;
37 if (c != '\n')
38 continue;
39 if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
40 const char *tagger = cp;
42 /* Found the tagger line. Copy out the contents
43 * of the buffer so far.
45 flush_buffer(buf, cp - buf);
48 * Do something intelligent, like pretty-printing
49 * the date.
51 while (cp < endp) {
52 if (*cp++ == '\n') {
53 /* tagger to cp is a line
54 * that has ident and time.
56 const char *sp = tagger;
57 char *ep;
58 unsigned long date;
59 long tz;
60 while (sp < cp && *sp != '>')
61 sp++;
62 if (sp == cp) {
63 /* give up */
64 flush_buffer(tagger,
65 cp - tagger);
66 break;
68 while (sp < cp &&
69 !('0' <= *sp && *sp <= '9'))
70 sp++;
71 flush_buffer(tagger, sp - tagger);
72 date = strtoul(sp, &ep, 10);
73 tz = strtol(ep, NULL, 10);
74 sp = show_date(date, tz);
75 flush_buffer(sp, strlen(sp));
76 xwrite(1, "\n", 1);
77 break;
80 break;
82 if (cp < endp && *cp == '\n')
83 /* end of header */
84 break;
86 /* At this point, we have copied out the header up to the end of
87 * the tagger line and cp points at one past \n. It could be the
88 * next header line after the tagger line, or it could be another
89 * \n that marks the end of the headers. We need to copy out the
90 * remainder as is.
92 if (cp < endp)
93 flush_buffer(cp, endp - cp);
94 return 0;
97 int cmd_cat_file(int argc, const char **argv, const char *prefix)
99 unsigned char sha1[20];
100 char type[20];
101 void *buf;
102 unsigned long size;
103 int opt;
105 git_config(git_default_config);
106 if (argc != 3)
107 usage("git-cat-file [-t|-s|-e|-p|<type>] <sha1>");
108 if (get_sha1(argv[2], sha1))
109 die("Not a valid object name %s", argv[2]);
111 opt = 0;
112 if ( argv[1][0] == '-' ) {
113 opt = argv[1][1];
114 if ( !opt || argv[1][2] )
115 opt = -1; /* Not a single character option */
118 buf = NULL;
119 switch (opt) {
120 case 't':
121 if (!sha1_object_info(sha1, type, NULL)) {
122 printf("%s\n", type);
123 return 0;
125 break;
127 case 's':
128 if (!sha1_object_info(sha1, type, &size)) {
129 printf("%lu\n", size);
130 return 0;
132 break;
134 case 'e':
135 return !has_sha1_file(sha1);
137 case 'p':
138 if (sha1_object_info(sha1, type, NULL))
139 die("Not a valid object name %s", argv[2]);
141 /* custom pretty-print here */
142 if (!strcmp(type, tree_type))
143 return cmd_ls_tree(2, argv + 1, NULL);
145 buf = read_sha1_file(sha1, type, &size);
146 if (!buf)
147 die("Cannot read object %s", argv[2]);
148 if (!strcmp(type, tag_type))
149 return pprint_tag(sha1, buf, size);
151 /* otherwise just spit out the data */
152 break;
153 case 0:
154 buf = read_object_with_reference(sha1, argv[1], &size, NULL);
155 break;
157 default:
158 die("git-cat-file: unknown option: %s\n", argv[1]);
161 if (!buf)
162 die("git-cat-file %s: bad file", argv[2]);
164 flush_buffer(buf, size);
165 return 0;