1/*
2 * GIT - The information manager from hell
3 *
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"
11#include "parse-options.h"
12#include "diff.h"
13#include "userdiff.h"
14
15#define BATCH 1
16#define BATCH_CHECK 2
17
18static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
19{
20 /* the parser in tag.c is useless here. */
21 const char *endp = buf + size;
22 const char *cp = buf;
23
24 while (cp < endp) {
25 char c = *cp++;
26 if (c != '\n')
27 continue;
28 if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
29 const char *tagger = cp;
30
31 /* Found the tagger line. Copy out the contents
32 * of the buffer so far.
33 */
34 write_or_die(1, buf, cp - buf);
35
36 /*
37 * Do something intelligent, like pretty-printing
38 * the date.
39 */
40 while (cp < endp) {
41 if (*cp++ == '\n') {
42 /* tagger to cp is a line
43 * that has ident and time.
44 */
45 const char *sp = tagger;
46 char *ep;
47 unsigned long date;
48 long tz;
49 while (sp < cp && *sp != '>')
50 sp++;
51 if (sp == cp) {
52 /* give up */
53 write_or_die(1, tagger,
54 cp - tagger);
55 break;
56 }
57 while (sp < cp &&
58 !('0' <= *sp && *sp <= '9'))
59 sp++;
60 write_or_die(1, tagger, sp - tagger);
61 date = strtoul(sp, &ep, 10);
62 tz = strtol(ep, NULL, 10);
63 sp = show_date(date, tz, 0);
64 write_or_die(1, sp, strlen(sp));
65 xwrite(1, "\n", 1);
66 break;
67 }
68 }
69 break;
70 }
71 if (cp < endp && *cp == '\n')
72 /* end of header */
73 break;
74 }
75 /* At this point, we have copied out the header up to the end of
76 * the tagger line and cp points at one past \n. It could be the
77 * next header line after the tagger line, or it could be another
78 * \n that marks the end of the headers. We need to copy out the
79 * remainder as is.
80 */
81 if (cp < endp)
82 write_or_die(1, cp, endp - cp);
83}
84
85static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
86{
87 unsigned char sha1[20];
88 enum object_type type;
89 char *buf;
90 unsigned long size;
91 struct object_context obj_context;
92
93 if (get_sha1_with_context(obj_name, sha1, &obj_context))
94 die("Not a valid object name %s", obj_name);
95
96 buf = NULL;
97 switch (opt) {
98 case 't':
99 type = sha1_object_info(sha1, NULL);
100 if (type > 0) {
101 printf("%s\n", typename(type));
102 return 0;
103 }
104 break;
105
106 case 's':
107 type = sha1_object_info(sha1, &size);
108 if (type > 0) {
109 printf("%lu\n", size);
110 return 0;
111 }
112 break;
113
114 case 'e':
115 return !has_sha1_file(sha1);
116
117 case 'p':
118 type = sha1_object_info(sha1, NULL);
119 if (type < 0)
120 die("Not a valid object name %s", obj_name);
121
122 /* custom pretty-print here */
123 if (type == OBJ_TREE) {
124 const char *ls_args[3] = {"ls-tree", obj_name, NULL};
125 return cmd_ls_tree(2, ls_args, NULL);
126 }
127
128 buf = read_sha1_file(sha1, &type, &size);
129 if (!buf)
130 die("Cannot read object %s", obj_name);
131 if (type == OBJ_TAG) {
132 pprint_tag(sha1, buf, size);
133 return 0;
134 }
135
136 /* otherwise just spit out the data */
137 break;
138
139 case 'c':
140 if (!obj_context.path[0])
141 die("git cat-file --textconv %s: <object> must be <sha1:path>",
142 obj_name);
143
144 if (!textconv_object(obj_context.path, sha1, &buf, &size))
145 die("git cat-file --textconv: unable to run textconv on %s",
146 obj_name);
147 break;
148
149 case 0:
150 buf = read_object_with_reference(sha1, exp_type, &size, NULL);
151 break;
152
153 default:
154 die("git cat-file: unknown option: %s", exp_type);
155 }
156
157 if (!buf)
158 die("git cat-file %s: bad file", obj_name);
159
160 write_or_die(1, buf, size);
161 return 0;
162}
163
164static int batch_one_object(const char *obj_name, int print_contents)
165{
166 unsigned char sha1[20];
167 enum object_type type = 0;
168 unsigned long size;
169 void *contents = contents;
170
171 if (!obj_name)
172 return 1;
173
174 if (get_sha1(obj_name, sha1)) {
175 printf("%s missing\n", obj_name);
176 fflush(stdout);
177 return 0;
178 }
179
180 if (print_contents == BATCH)
181 contents = read_sha1_file(sha1, &type, &size);
182 else
183 type = sha1_object_info(sha1, &size);
184
185 if (type <= 0) {
186 printf("%s missing\n", obj_name);
187 fflush(stdout);
188 return 0;
189 }
190
191 printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
192 fflush(stdout);
193
194 if (print_contents == BATCH) {
195 write_or_die(1, contents, size);
196 printf("\n");
197 fflush(stdout);
198 free(contents);
199 }
200
201 return 0;
202}
203
204static int batch_objects(int print_contents)
205{
206 struct strbuf buf = STRBUF_INIT;
207
208 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
209 int error = batch_one_object(buf.buf, print_contents);
210 if (error)
211 return error;
212 }
213
214 return 0;
215}
216
217static const char * const cat_file_usage[] = {
218 "git cat-file (-t|-s|-e|-p|<type>|--textconv) <object>",
219 "git cat-file (--batch|--batch-check) < <list_of_objects>",
220 NULL
221};
222
223static int git_cat_file_config(const char *var, const char *value, void *cb)
224{
225 switch (userdiff_config(var, value)) {
226 case 0:
227 break;
228 case -1:
229 return -1;
230 default:
231 return 0;
232 }
233
234 return git_default_config(var, value, cb);
235}
236
237int cmd_cat_file(int argc, const char **argv, const char *prefix)
238{
239 int opt = 0, batch = 0;
240 const char *exp_type = NULL, *obj_name = NULL;
241
242 const struct option options[] = {
243 OPT_GROUP("<type> can be one of: blob, tree, commit, tag"),
244 OPT_SET_INT('t', NULL, &opt, "show object type", 't'),
245 OPT_SET_INT('s', NULL, &opt, "show object size", 's'),
246 OPT_SET_INT('e', NULL, &opt,
247 "exit with zero when there's no error", 'e'),
248 OPT_SET_INT('p', NULL, &opt, "pretty-print object's content", 'p'),
249 OPT_SET_INT(0, "textconv", &opt,
250 "for blob objects, run textconv on object's content", 'c'),
251 OPT_SET_INT(0, "batch", &batch,
252 "show info and content of objects fed from the standard input",
253 BATCH),
254 OPT_SET_INT(0, "batch-check", &batch,
255 "show info about objects fed from the standard input",
256 BATCH_CHECK),
257 OPT_END()
258 };
259
260 git_config(git_cat_file_config, NULL);
261
262 if (argc != 3 && argc != 2)
263 usage_with_options(cat_file_usage, options);
264
265 argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0);
266
267 if (opt) {
268 if (argc == 1)
269 obj_name = argv[0];
270 else
271 usage_with_options(cat_file_usage, options);
272 }
273 if (!opt && !batch) {
274 if (argc == 2) {
275 exp_type = argv[0];
276 obj_name = argv[1];
277 } else
278 usage_with_options(cat_file_usage, options);
279 }
280 if (batch && (opt || argc)) {
281 usage_with_options(cat_file_usage, options);
282 }
283
284 if (batch)
285 return batch_objects(batch);
286
287 return cat_one_file(opt, exp_type, obj_name);
288}