git-svn: Speed up fetch
[git/git.git] / builtin-cat-file.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163 6#include "cache.h"
a0f15fa5 7#include "exec_cmd.h"
8e440259
PE
8#include "tag.h"
9#include "tree.h"
f81daefe 10#include "builtin.h"
a0f15fa5 11
eddd1c8c 12static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
a0f15fa5
JH
13{
14 /* the parser in tag.c is useless here. */
15 const char *endp = buf + size;
16 const char *cp = buf;
17
18 while (cp < endp) {
19 char c = *cp++;
20 if (c != '\n')
21 continue;
22 if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
23 const char *tagger = cp;
24
25 /* Found the tagger line. Copy out the contents
26 * of the buffer so far.
27 */
7230e6d0 28 write_or_die(1, buf, cp - buf);
a0f15fa5
JH
29
30 /*
31 * Do something intelligent, like pretty-printing
32 * the date.
33 */
34 while (cp < endp) {
35 if (*cp++ == '\n') {
36 /* tagger to cp is a line
37 * that has ident and time.
38 */
39 const char *sp = tagger;
40 char *ep;
41 unsigned long date;
42 long tz;
43 while (sp < cp && *sp != '>')
44 sp++;
45 if (sp == cp) {
46 /* give up */
7230e6d0 47 write_or_die(1, tagger,
a0f15fa5
JH
48 cp - tagger);
49 break;
50 }
51 while (sp < cp &&
52 !('0' <= *sp && *sp <= '9'))
53 sp++;
7230e6d0 54 write_or_die(1, tagger, sp - tagger);
a0f15fa5
JH
55 date = strtoul(sp, &ep, 10);
56 tz = strtol(ep, NULL, 10);
9a8e35e9 57 sp = show_date(date, tz, 0);
7230e6d0 58 write_or_die(1, sp, strlen(sp));
a0f15fa5
JH
59 xwrite(1, "\n", 1);
60 break;
61 }
62 }
63 break;
64 }
65 if (cp < endp && *cp == '\n')
66 /* end of header */
67 break;
68 }
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
73 * remainder as is.
74 */
75 if (cp < endp)
7230e6d0 76 write_or_die(1, cp, endp - cp);
a0f15fa5 77}
e83c5163 78
9cf71b17 79static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
e83c5163
LT
80{
81 unsigned char sha1[20];
21666f1a 82 enum object_type type;
e83c5163
LT
83 void *buf;
84 unsigned long size;
2b6854c8
SP
85
86 if (get_sha1(obj_name, sha1))
87 die("Not a valid object name %s", obj_name);
7950571a 88
7950571a
PA
89 buf = NULL;
90 switch (opt) {
91 case 't':
21666f1a
NP
92 type = sha1_object_info(sha1, NULL);
93 if (type > 0) {
94 printf("%s\n", typename(type));
f2a06330 95 return 0;
11e7d5c5 96 }
7950571a
PA
97 break;
98
99 case 's':
21666f1a
NP
100 type = sha1_object_info(sha1, &size);
101 if (type > 0) {
7950571a
PA
102 printf("%lu\n", size);
103 return 0;
104 }
105 break;
106
107 case 'e':
108 return !has_sha1_file(sha1);
109
a0f15fa5 110 case 'p':
21666f1a
NP
111 type = sha1_object_info(sha1, NULL);
112 if (type < 0)
2b6854c8 113 die("Not a valid object name %s", obj_name);
a0f15fa5
JH
114
115 /* custom pretty-print here */
2b6854c8
SP
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);
119 }
a0f15fa5 120
21666f1a 121 buf = read_sha1_file(sha1, &type, &size);
a0f15fa5 122 if (!buf)
2b6854c8 123 die("Cannot read object %s", obj_name);
21666f1a 124 if (type == OBJ_TAG) {
eddd1c8c
DR
125 pprint_tag(sha1, buf, size);
126 return 0;
127 }
a0f15fa5
JH
128
129 /* otherwise just spit out the data */
130 break;
7950571a 131 case 0:
2b6854c8 132 buf = read_object_with_reference(sha1, exp_type, &size, NULL);
7950571a
PA
133 break;
134
135 default:
2b6854c8 136 die("git-cat-file: unknown option: %s\n", exp_type);
bf0c6e83
LT
137 }
138
11e7d5c5 139 if (!buf)
2b6854c8 140 die("git-cat-file %s: bad file", obj_name);
11e7d5c5 141
7230e6d0 142 write_or_die(1, buf, size);
bf0c6e83 143 return 0;
e83c5163 144}
9cf71b17 145
a8128ed6 146static int batch_one_object(const char *obj_name, int print_contents)
05d5667f
AR
147{
148 unsigned char sha1[20];
149 enum object_type type;
150 unsigned long size;
a8128ed6 151 void *contents = contents;
05d5667f
AR
152
153 if (!obj_name)
154 return 1;
155
156 if (get_sha1(obj_name, sha1)) {
157 printf("%s missing\n", obj_name);
158 return 0;
159 }
160
a8128ed6
AR
161 if (print_contents)
162 contents = read_sha1_file(sha1, &type, &size);
163 else
164 type = sha1_object_info(sha1, &size);
165
05d5667f
AR
166 if (type <= 0)
167 return 1;
168
169 printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
a8128ed6
AR
170 fflush(stdout);
171
172 if (print_contents) {
173 write_or_die(1, contents, size);
174 printf("\n");
175 fflush(stdout);
176 }
05d5667f
AR
177
178 return 0;
179}
180
a8128ed6 181static int batch_objects(int print_contents)
05d5667f
AR
182{
183 struct strbuf buf;
184
185 strbuf_init(&buf, 0);
186 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
a8128ed6 187 int error = batch_one_object(buf.buf, print_contents);
05d5667f
AR
188 if (error)
189 return error;
190 }
191
192 return 0;
193}
194
a8128ed6 195static const char cat_file_usage[] = "git-cat-file [ [-t|-s|-e|-p|<type>] <sha1> | [--batch|--batch-check] < <list_of_sha1s> ]";
4814dbe8 196
9cf71b17
AR
197int cmd_cat_file(int argc, const char **argv, const char *prefix)
198{
a8128ed6 199 int i, opt = 0, batch = 0, batch_check = 0;
4814dbe8 200 const char *exp_type = NULL, *obj_name = NULL;
9cf71b17
AR
201
202 git_config(git_default_config);
4814dbe8
AR
203
204 for (i = 1; i < argc; ++i) {
205 const char *arg = argv[i];
a8128ed6
AR
206 int is_batch = 0, is_batch_check = 0;
207
208 is_batch = !strcmp(arg, "--batch");
209 if (!is_batch)
210 is_batch_check = !strcmp(arg, "--batch-check");
4814dbe8 211
a8128ed6 212 if (is_batch || is_batch_check) {
05d5667f 213 if (opt) {
a8128ed6 214 error("git-cat-file: Can't use %s with -%c", arg, opt);
05d5667f
AR
215 usage(cat_file_usage);
216 } else if (exp_type) {
a8128ed6 217 error("git-cat-file: Can't use %s when a type (\"%s\") is specified", arg, exp_type);
05d5667f
AR
218 usage(cat_file_usage);
219 } else if (obj_name) {
a8128ed6 220 error("git-cat-file: Can't use %s when an object (\"%s\") is specified", arg, obj_name);
05d5667f
AR
221 usage(cat_file_usage);
222 }
223
a8128ed6
AR
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);
227 }
228
229 if (is_batch)
230 batch = 1;
231 else
232 batch_check = 1;
233
05d5667f
AR
234 continue;
235 }
236
4814dbe8 237 if (!strcmp(arg, "-t") || !strcmp(arg, "-s") || !strcmp(arg, "-e") || !strcmp(arg, "-p")) {
a8128ed6
AR
238 if (batch || batch_check) {
239 error("git-cat-file: Can't use %s with %s", arg, batch ? "--batch" : "--batch-check");
05d5667f
AR
240 usage(cat_file_usage);
241 }
242
4814dbe8
AR
243 exp_type = arg;
244 opt = exp_type[1];
245 continue;
246 }
247
248 if (arg[0] == '-')
249 usage(cat_file_usage);
250
251 if (!exp_type) {
a8128ed6
AR
252 if (batch || batch_check) {
253 error("git-cat-file: Can't specify a type (\"%s\") with %s", arg, batch ? "--batch" : "--batch-check");
05d5667f
AR
254 usage(cat_file_usage);
255 }
256
4814dbe8
AR
257 exp_type = arg;
258 continue;
259 }
260
261 if (obj_name)
262 usage(cat_file_usage);
263
a8128ed6 264 // We should have hit one of the earlier if (batch || batch_check) cases before
05d5667f 265 // getting here.
a8128ed6 266 assert(!batch);
05d5667f
AR
267 assert(!batch_check);
268
4814dbe8
AR
269 obj_name = arg;
270 break;
9cf71b17
AR
271 }
272
a8128ed6
AR
273 if (batch || batch_check)
274 return batch_objects(batch);
05d5667f 275
4814dbe8
AR
276 if (!exp_type || !obj_name)
277 usage(cat_file_usage);
278
9cf71b17
AR
279 return cat_one_file(opt, exp_type, obj_name);
280}