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
, 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 type
= sha1_object_info(sha1
, NULL
);
121 die("Not a valid object name %s", obj_name
);
123 /* custom pretty-print here */
124 if (type
== OBJ_TREE
) {
125 const char *ls_args
[3] = { NULL
};
126 ls_args
[0] = "ls-tree";
127 ls_args
[1] = obj_name
;
128 return cmd_ls_tree(2, ls_args
, NULL
);
131 if (type
== OBJ_BLOB
)
132 return stream_blob_to_fd(1, sha1
, NULL
, 0);
133 buf
= read_sha1_file(sha1
, &type
, &size
);
135 die("Cannot read object %s", obj_name
);
136 if (type
== OBJ_TAG
) {
137 pprint_tag(sha1
, buf
, size
);
141 /* otherwise just spit out the data */
145 if (!obj_context
.path
[0])
146 die("git cat-file --textconv %s: <object> must be <sha1:path>",
149 if (!textconv_object(obj_context
.path
, obj_context
.mode
, sha1
, 1, &buf
, &size
))
150 die("git cat-file --textconv: unable to run textconv on %s",
155 if (type_from_string(exp_type
) == OBJ_BLOB
) {
156 unsigned char blob_sha1
[20];
157 if (sha1_object_info(sha1
, NULL
) == OBJ_TAG
) {
158 enum object_type type
;
160 char *buffer
= read_sha1_file(sha1
, &type
, &size
);
161 if (memcmp(buffer
, "object ", 7) ||
162 get_sha1_hex(buffer
+ 7, blob_sha1
))
163 die("%s not a valid tag", sha1_to_hex(sha1
));
166 hashcpy(blob_sha1
, sha1
);
168 if (sha1_object_info(blob_sha1
, NULL
) == OBJ_BLOB
)
169 return stream_blob_to_fd(1, blob_sha1
, NULL
, 0);
171 * we attempted to dereference a tag to a blob
172 * and failed; there may be new dereference
173 * mechanisms this code is not aware of.
174 * fall-back to the usual case.
177 buf
= read_object_with_reference(sha1
, exp_type
, &size
, NULL
);
181 die("git cat-file: unknown option: %s", exp_type
);
185 die("git cat-file %s: bad file", obj_name
);
187 write_or_die(1, buf
, size
);
191 static int batch_one_object(const char *obj_name
, int print_contents
)
193 unsigned char sha1
[20];
194 enum object_type type
= 0;
196 void *contents
= contents
;
201 if (get_sha1(obj_name
, sha1
)) {
202 printf("%s missing\n", obj_name
);
207 if (print_contents
== BATCH
)
208 contents
= read_sha1_file(sha1
, &type
, &size
);
210 type
= sha1_object_info(sha1
, &size
);
213 printf("%s missing\n", obj_name
);
215 if (print_contents
== BATCH
)
220 printf("%s %s %lu\n", sha1_to_hex(sha1
), typename(type
), size
);
223 if (print_contents
== BATCH
) {
224 write_or_die(1, contents
, size
);
233 static int batch_objects(int print_contents
)
235 struct strbuf buf
= STRBUF_INIT
;
237 while (strbuf_getline(&buf
, stdin
, '\n') != EOF
) {
238 int error
= batch_one_object(buf
.buf
, print_contents
);
246 static const char * const cat_file_usage
[] = {
247 "git cat-file (-t|-s|-e|-p|<type>|--textconv) <object>",
248 "git cat-file (--batch|--batch-check) < <list_of_objects>",
252 static int git_cat_file_config(const char *var
, const char *value
, void *cb
)
254 if (userdiff_config(var
, value
) < 0)
257 return git_default_config(var
, value
, cb
);
260 int cmd_cat_file(int argc
, const char **argv
, const char *prefix
)
262 int opt
= 0, batch
= 0;
263 const char *exp_type
= NULL
, *obj_name
= NULL
;
265 const struct option options
[] = {
266 OPT_GROUP("<type> can be one of: blob, tree, commit, tag"),
267 OPT_SET_INT('t', NULL
, &opt
, "show object type", 't'),
268 OPT_SET_INT('s', NULL
, &opt
, "show object size", 's'),
269 OPT_SET_INT('e', NULL
, &opt
,
270 "exit with zero when there's no error", 'e'),
271 OPT_SET_INT('p', NULL
, &opt
, "pretty-print object's content", 'p'),
272 OPT_SET_INT(0, "textconv", &opt
,
273 "for blob objects, run textconv on object's content", 'c'),
274 OPT_SET_INT(0, "batch", &batch
,
275 "show info and content of objects fed from the standard input",
277 OPT_SET_INT(0, "batch-check", &batch
,
278 "show info about objects fed from the standard input",
283 git_config(git_cat_file_config
, NULL
);
285 if (argc
!= 3 && argc
!= 2)
286 usage_with_options(cat_file_usage
, options
);
288 argc
= parse_options(argc
, argv
, prefix
, options
, cat_file_usage
, 0);
294 usage_with_options(cat_file_usage
, options
);
296 if (!opt
&& !batch
) {
301 usage_with_options(cat_file_usage
, options
);
303 if (batch
&& (opt
|| argc
)) {
304 usage_with_options(cat_file_usage
, options
);
308 return batch_objects(batch
);
310 return cat_one_file(opt
, exp_type
, obj_name
);