]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/cat-file.c
Merge branch 'tf/commit-list-prefix'
[thirdparty/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"
15d8e565 11#include "parse-options.h"
e5fba602
CP
12#include "diff.h"
13#include "userdiff.h"
15d8e565
MB
14
15#define BATCH 1
16#define BATCH_CHECK 2
a0f15fa5 17
eddd1c8c 18static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
a0f15fa5
JH
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 */
7230e6d0 34 write_or_die(1, buf, cp - buf);
a0f15fa5
JH
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 */
7230e6d0 53 write_or_die(1, tagger,
a0f15fa5
JH
54 cp - tagger);
55 break;
56 }
57 while (sp < cp &&
58 !('0' <= *sp && *sp <= '9'))
59 sp++;
7230e6d0 60 write_or_die(1, tagger, sp - tagger);
a0f15fa5
JH
61 date = strtoul(sp, &ep, 10);
62 tz = strtol(ep, NULL, 10);
9a8e35e9 63 sp = show_date(date, tz, 0);
7230e6d0 64 write_or_die(1, sp, strlen(sp));
a0f15fa5
JH
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)
7230e6d0 82 write_or_die(1, cp, endp - cp);
a0f15fa5 83}
e83c5163 84
9cf71b17 85static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
e83c5163
LT
86{
87 unsigned char sha1[20];
21666f1a 88 enum object_type type;
e5fba602 89 char *buf;
e83c5163 90 unsigned long size;
e5fba602 91 struct object_context obj_context;
2b6854c8 92
e5fba602 93 if (get_sha1_with_context(obj_name, sha1, &obj_context))
2b6854c8 94 die("Not a valid object name %s", obj_name);
7950571a 95
7950571a
PA
96 buf = NULL;
97 switch (opt) {
98 case 't':
21666f1a
NP
99 type = sha1_object_info(sha1, NULL);
100 if (type > 0) {
101 printf("%s\n", typename(type));
f2a06330 102 return 0;
11e7d5c5 103 }
7950571a
PA
104 break;
105
106 case 's':
21666f1a
NP
107 type = sha1_object_info(sha1, &size);
108 if (type > 0) {
7950571a
PA
109 printf("%lu\n", size);
110 return 0;
111 }
112 break;
113
114 case 'e':
115 return !has_sha1_file(sha1);
116
a0f15fa5 117 case 'p':
21666f1a
NP
118 type = sha1_object_info(sha1, NULL);
119 if (type < 0)
2b6854c8 120 die("Not a valid object name %s", obj_name);
a0f15fa5
JH
121
122 /* custom pretty-print here */
2b6854c8 123 if (type == OBJ_TREE) {
66dbfd55
GV
124 const char *ls_args[3] = { NULL };
125 ls_args[0] = "ls-tree";
126 ls_args[1] = obj_name;
2b6854c8
SP
127 return cmd_ls_tree(2, ls_args, NULL);
128 }
a0f15fa5 129
21666f1a 130 buf = read_sha1_file(sha1, &type, &size);
a0f15fa5 131 if (!buf)
2b6854c8 132 die("Cannot read object %s", obj_name);
21666f1a 133 if (type == OBJ_TAG) {
eddd1c8c
DR
134 pprint_tag(sha1, buf, size);
135 return 0;
136 }
a0f15fa5
JH
137
138 /* otherwise just spit out the data */
139 break;
e5fba602
CP
140
141 case 'c':
142 if (!obj_context.path[0])
143 die("git cat-file --textconv %s: <object> must be <sha1:path>",
144 obj_name);
145
90064710 146 if (!textconv_object(obj_context.path, obj_context.mode, sha1, &buf, &size))
e5fba602
CP
147 die("git cat-file --textconv: unable to run textconv on %s",
148 obj_name);
149 break;
150
7950571a 151 case 0:
2b6854c8 152 buf = read_object_with_reference(sha1, exp_type, &size, NULL);
7950571a
PA
153 break;
154
155 default:
d7530708 156 die("git cat-file: unknown option: %s", exp_type);
bf0c6e83
LT
157 }
158
11e7d5c5 159 if (!buf)
34baebce 160 die("git cat-file %s: bad file", obj_name);
11e7d5c5 161
7230e6d0 162 write_or_die(1, buf, size);
bf0c6e83 163 return 0;
e83c5163 164}
9cf71b17 165
a8128ed6 166static int batch_one_object(const char *obj_name, int print_contents)
05d5667f
AR
167{
168 unsigned char sha1[20];
3c076dbe 169 enum object_type type = 0;
05d5667f 170 unsigned long size;
a8128ed6 171 void *contents = contents;
05d5667f
AR
172
173 if (!obj_name)
174 return 1;
175
176 if (get_sha1(obj_name, sha1)) {
177 printf("%s missing\n", obj_name);
422b2063 178 fflush(stdout);
05d5667f
AR
179 return 0;
180 }
181
15d8e565 182 if (print_contents == BATCH)
a8128ed6
AR
183 contents = read_sha1_file(sha1, &type, &size);
184 else
185 type = sha1_object_info(sha1, &size);
186
3c076dbe
LW
187 if (type <= 0) {
188 printf("%s missing\n", obj_name);
189 fflush(stdout);
190 return 0;
191 }
05d5667f
AR
192
193 printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
a8128ed6
AR
194 fflush(stdout);
195
15d8e565 196 if (print_contents == BATCH) {
a8128ed6
AR
197 write_or_die(1, contents, size);
198 printf("\n");
199 fflush(stdout);
5b8a94b1 200 free(contents);
a8128ed6 201 }
05d5667f
AR
202
203 return 0;
204}
205
a8128ed6 206static int batch_objects(int print_contents)
05d5667f 207{
f285a2d7 208 struct strbuf buf = STRBUF_INIT;
05d5667f 209
05d5667f 210 while (strbuf_getline(&buf, stdin, '\n') != EOF) {
a8128ed6 211 int error = batch_one_object(buf.buf, print_contents);
05d5667f
AR
212 if (error)
213 return error;
214 }
215
216 return 0;
217}
218
15d8e565 219static const char * const cat_file_usage[] = {
e5fba602 220 "git cat-file (-t|-s|-e|-p|<type>|--textconv) <object>",
0e5168fd 221 "git cat-file (--batch|--batch-check) < <list_of_objects>",
15d8e565
MB
222 NULL
223};
4814dbe8 224
e5fba602
CP
225static int git_cat_file_config(const char *var, const char *value, void *cb)
226{
227 switch (userdiff_config(var, value)) {
228 case 0:
229 break;
230 case -1:
231 return -1;
232 default:
233 return 0;
234 }
235
236 return git_default_config(var, value, cb);
237}
238
9cf71b17
AR
239int cmd_cat_file(int argc, const char **argv, const char *prefix)
240{
15d8e565 241 int opt = 0, batch = 0;
4814dbe8 242 const char *exp_type = NULL, *obj_name = NULL;
9cf71b17 243
15d8e565
MB
244 const struct option options[] = {
245 OPT_GROUP("<type> can be one of: blob, tree, commit, tag"),
246 OPT_SET_INT('t', NULL, &opt, "show object type", 't'),
247 OPT_SET_INT('s', NULL, &opt, "show object size", 's'),
248 OPT_SET_INT('e', NULL, &opt,
249 "exit with zero when there's no error", 'e'),
250 OPT_SET_INT('p', NULL, &opt, "pretty-print object's content", 'p'),
e5fba602
CP
251 OPT_SET_INT(0, "textconv", &opt,
252 "for blob objects, run textconv on object's content", 'c'),
15d8e565 253 OPT_SET_INT(0, "batch", &batch,
9517e6b8
JH
254 "show info and content of objects fed from the standard input",
255 BATCH),
15d8e565 256 OPT_SET_INT(0, "batch-check", &batch,
9517e6b8 257 "show info about objects fed from the standard input",
15d8e565
MB
258 BATCH_CHECK),
259 OPT_END()
260 };
a8128ed6 261
e5fba602 262 git_config(git_cat_file_config, NULL);
4814dbe8 263
15d8e565
MB
264 if (argc != 3 && argc != 2)
265 usage_with_options(cat_file_usage, options);
4814dbe8 266
37782920 267 argc = parse_options(argc, argv, prefix, options, cat_file_usage, 0);
05d5667f 268
15d8e565
MB
269 if (opt) {
270 if (argc == 1)
271 obj_name = argv[0];
272 else
273 usage_with_options(cat_file_usage, options);
274 }
275 if (!opt && !batch) {
276 if (argc == 2) {
277 exp_type = argv[0];
278 obj_name = argv[1];
279 } else
280 usage_with_options(cat_file_usage, options);
281 }
282 if (batch && (opt || argc)) {
283 usage_with_options(cat_file_usage, options);
9cf71b17
AR
284 }
285
15d8e565 286 if (batch)
a8128ed6 287 return batch_objects(batch);
05d5667f 288
9cf71b17
AR
289 return cat_one_file(opt, exp_type, obj_name);
290}