2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 #include "parse-options.h"
14 #include "streaming.h"
19 static void pprint_tag(const unsigned char *sha1
, const char *buf
, unsigned long size
)
21 /* the parser in tag.c is useless here. */
22 const char *endp
= buf
+ size
;
29 if (7 <= endp
- cp
&& !memcmp("tagger ", cp
, 7)) {
30 const char *tagger
= cp
;
32 /* Found the tagger line. Copy out the contents
33 * of the buffer so far.
35 write_or_die(1, buf
, cp
- buf
);
38 * Do something intelligent, like pretty-printing
43 /* tagger to cp is a line
44 * that has ident and time.
46 const char *sp
= tagger
;
50 while (sp
< cp
&& *sp
!= '>')
54 write_or_die(1, tagger
,
59 !('0' <= *sp
&& *sp
<= '9'))
61 write_or_die(1, tagger
, sp
- tagger
);
62 date
= strtoul(sp
, &ep
, 10);
63 tz
= strtol(ep
, NULL
, 10);
64 sp
= show_date(date
, tz
, 0);
65 write_or_die(1, sp
, strlen(sp
));
72 if (cp
< endp
&& *cp
== '\n')
76 /* At this point, we have copied out the header up to the end of
77 * the tagger line and cp points at one past \n. It could be the
78 * next header line after the tagger line, or it could be another
79 * \n that marks the end of the headers. We need to copy out the
83 write_or_die(1, cp
, endp
- cp
);
86 static int cat_one_file(int opt
, const char *exp_type
, const char *obj_name
)
88 unsigned char sha1
[20];
89 enum object_type type
;
92 struct object_context obj_context
;
94 if (get_sha1_with_context(obj_name
, 0, sha1
, &obj_context
))
95 die("Not a valid object name %s", obj_name
);
100 type
= sha1_object_info(sha1
, NULL
);
102 printf("%s\n", typename(type
));
108 type
= sha1_object_info(sha1
, &size
);
110 printf("%lu\n", size
);
116 return !has_sha1_file(sha1
);
119 if (!obj_context
.path
[0])
120 die("git cat-file --textconv %s: <object> must be <sha1:path>",
123 if (textconv_object(obj_context
.path
, obj_context
.mode
, sha1
, 1, &buf
, &size
))
127 type
= sha1_object_info(sha1
, NULL
);
129 die("Not a valid object name %s", obj_name
);
131 /* custom pretty-print here */
132 if (type
== OBJ_TREE
) {
133 const char *ls_args
[3] = { NULL
};
134 ls_args
[0] = "ls-tree";
135 ls_args
[1] = obj_name
;
136 return cmd_ls_tree(2, ls_args
, NULL
);
139 if (type
== OBJ_BLOB
)
140 return stream_blob_to_fd(1, sha1
, NULL
, 0);
141 buf
= read_sha1_file(sha1
, &type
, &size
);
143 die("Cannot read object %s", obj_name
);
144 if (type
== OBJ_TAG
) {
145 pprint_tag(sha1
, buf
, size
);
149 /* otherwise just spit out the data */
153 if (type_from_string(exp_type
) == OBJ_BLOB
) {
154 unsigned char blob_sha1
[20];
155 if (sha1_object_info(sha1
, NULL
) == OBJ_TAG
) {
156 enum object_type type
;
158 char *buffer
= read_sha1_file(sha1
, &type
, &size
);
159 if (memcmp(buffer
, "object ", 7) ||
160 get_sha1_hex(buffer
+ 7, blob_sha1
))
161 die("%s not a valid tag", sha1_to_hex(sha1
));
164 hashcpy(blob_sha1
, sha1
);
166 if (sha1_object_info(blob_sha1
, NULL
) == OBJ_BLOB
)
167 return stream_blob_to_fd(1, blob_sha1
, NULL
, 0);
169 * we attempted to dereference a tag to a blob
170 * and failed; there may be new dereference
171 * mechanisms this code is not aware of.
172 * fall-back to the usual case.
175 buf
= read_object_with_reference(sha1
, exp_type
, &size
, NULL
);
179 die("git cat-file: unknown option: %s", exp_type
);
183 die("git cat-file %s: bad file", obj_name
);
185 write_or_die(1, buf
, size
);
189 static int batch_one_object(const char *obj_name
, int print_contents
)
191 unsigned char sha1
[20];
192 enum object_type type
= 0;
194 void *contents
= NULL
;
199 if (get_sha1(obj_name
, sha1
)) {
200 printf("%s missing\n", obj_name
);
205 if (print_contents
== BATCH
)
206 contents
= read_sha1_file(sha1
, &type
, &size
);
208 type
= sha1_object_info(sha1
, &size
);
211 printf("%s missing\n", obj_name
);
213 if (print_contents
== BATCH
)
218 printf("%s %s %lu\n", sha1_to_hex(sha1
), typename(type
), size
);
221 if (print_contents
== BATCH
) {
222 write_or_die(1, contents
, size
);
231 static int batch_objects(int print_contents
)
233 struct strbuf buf
= STRBUF_INIT
;
235 while (strbuf_getline(&buf
, stdin
, '\n') != EOF
) {
236 int error
= batch_one_object(buf
.buf
, print_contents
);
244 static const char * const cat_file_usage
[] = {
245 N_("git cat-file (-t|-s|-e|-p|<type>|--textconv) <object>"),
246 N_("git cat-file (--batch|--batch-check) < <list_of_objects>"),
250 static int git_cat_file_config(const char *var
, const char *value
, void *cb
)
252 if (userdiff_config(var
, value
) < 0)
255 return git_default_config(var
, value
, cb
);
258 int cmd_cat_file(int argc
, const char **argv
, const char *prefix
)
260 int opt
= 0, batch
= 0;
261 const char *exp_type
= NULL
, *obj_name
= NULL
;
263 const struct option options
[] = {
264 OPT_GROUP(N_("<type> can be one of: blob, tree, commit, tag")),
265 OPT_SET_INT('t', NULL
, &opt
, N_("show object type"), 't'),
266 OPT_SET_INT('s', NULL
, &opt
, N_("show object size"), 's'),
267 OPT_SET_INT('e', NULL
, &opt
,
268 N_("exit with zero when there's no error"), 'e'),
269 OPT_SET_INT('p', NULL
, &opt
, N_("pretty-print object's content"), 'p'),
270 OPT_SET_INT(0, "textconv", &opt
,
271 N_("for blob objects, run textconv on object's content"), 'c'),
272 OPT_SET_INT(0, "batch", &batch
,
273 N_("show info and content of objects fed from the standard input"),
275 OPT_SET_INT(0, "batch-check", &batch
,
276 N_("show info about objects fed from the standard input"),
281 git_config(git_cat_file_config
, NULL
);
283 if (argc
!= 3 && argc
!= 2)
284 usage_with_options(cat_file_usage
, options
);
286 argc
= parse_options(argc
, argv
, prefix
, options
, cat_file_usage
, 0);
292 usage_with_options(cat_file_usage
, options
);
294 if (!opt
&& !batch
) {
299 usage_with_options(cat_file_usage
, options
);
301 if (batch
&& (opt
|| argc
)) {
302 usage_with_options(cat_file_usage
, options
);
306 return batch_objects(batch
);
308 return cat_one_file(opt
, exp_type
, obj_name
);