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