2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 #include "parse-options.h"
16 static void pprint_tag(const unsigned char *sha1
, const char *buf
, unsigned long size
)
18 /* the parser in tag.c is useless here. */
19 const char *endp
= buf
+ size
;
26 if (7 <= endp
- cp
&& !memcmp("tagger ", cp
, 7)) {
27 const char *tagger
= cp
;
29 /* Found the tagger line. Copy out the contents
30 * of the buffer so far.
32 write_or_die(1, buf
, cp
- buf
);
35 * Do something intelligent, like pretty-printing
40 /* tagger to cp is a line
41 * that has ident and time.
43 const char *sp
= tagger
;
47 while (sp
< cp
&& *sp
!= '>')
51 write_or_die(1, tagger
,
56 !('0' <= *sp
&& *sp
<= '9'))
58 write_or_die(1, tagger
, sp
- tagger
);
59 date
= strtoul(sp
, &ep
, 10);
60 tz
= strtol(ep
, NULL
, 10);
61 sp
= show_date(date
, tz
, 0);
62 write_or_die(1, sp
, strlen(sp
));
69 if (cp
< endp
&& *cp
== '\n')
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
80 write_or_die(1, cp
, endp
- cp
);
83 static int cat_one_file(int opt
, const char *exp_type
, const char *obj_name
)
85 unsigned char sha1
[20];
86 enum object_type type
;
90 if (get_sha1(obj_name
, sha1
))
91 die("Not a valid object name %s", obj_name
);
96 type
= sha1_object_info(sha1
, NULL
);
98 printf("%s\n", typename(type
));
104 type
= sha1_object_info(sha1
, &size
);
106 printf("%lu\n", size
);
112 return !has_sha1_file(sha1
);
115 type
= sha1_object_info(sha1
, NULL
);
117 die("Not a valid object name %s", obj_name
);
119 /* custom pretty-print here */
120 if (type
== OBJ_TREE
) {
121 const char *ls_args
[3] = { NULL
};
122 ls_args
[0] = "ls-tree";
123 ls_args
[1] = obj_name
;
124 return cmd_ls_tree(2, ls_args
, NULL
);
127 buf
= read_sha1_file(sha1
, &type
, &size
);
129 die("Cannot read object %s", obj_name
);
130 if (type
== OBJ_TAG
) {
131 pprint_tag(sha1
, buf
, size
);
135 /* otherwise just spit out the data */
138 buf
= read_object_with_reference(sha1
, exp_type
, &size
, NULL
);
142 die("git cat-file: unknown option: %s", exp_type
);
146 die("git cat-file %s: bad file", obj_name
);
148 write_or_die(1, buf
, size
);
152 static int batch_one_object(const char *obj_name
, int print_contents
)
154 unsigned char sha1
[20];
155 enum object_type type
= 0;
157 void *contents
= contents
;
162 if (get_sha1(obj_name
, sha1
)) {
163 printf("%s missing\n", obj_name
);
168 if (print_contents
== BATCH
)
169 contents
= read_sha1_file(sha1
, &type
, &size
);
171 type
= sha1_object_info(sha1
, &size
);
174 printf("%s missing\n", obj_name
);
179 printf("%s %s %lu\n", sha1_to_hex(sha1
), typename(type
), size
);
182 if (print_contents
== BATCH
) {
183 write_or_die(1, contents
, size
);
192 static int batch_objects(int print_contents
)
194 struct strbuf buf
= STRBUF_INIT
;
196 while (strbuf_getline(&buf
, stdin
, '\n') != EOF
) {
197 int error
= batch_one_object(buf
.buf
, print_contents
);
205 static const char * const cat_file_usage
[] = {
206 "git cat-file (-t|-s|-e|-p|<type>) <object>",
207 "git cat-file (--batch|--batch-check) < <list_of_objects>",
211 int cmd_cat_file(int argc
, const char **argv
, const char *prefix
)
213 int opt
= 0, batch
= 0;
214 const char *exp_type
= NULL
, *obj_name
= NULL
;
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
,
224 "show info and content of objects fed from the standard input",
226 OPT_SET_INT(0, "batch-check", &batch
,
227 "show info about objects fed from the standard input",
232 git_config(git_default_config
, NULL
);
234 if (argc
!= 3 && argc
!= 2)
235 usage_with_options(cat_file_usage
, options
);
237 argc
= parse_options(argc
, argv
, prefix
, options
, cat_file_usage
, 0);
243 usage_with_options(cat_file_usage
, options
);
245 if (!opt
&& !batch
) {
250 usage_with_options(cat_file_usage
, options
);
252 if (batch
&& (opt
|| argc
)) {
253 usage_with_options(cat_file_usage
, options
);
257 return batch_objects(batch
);
259 return cat_one_file(opt
, exp_type
, obj_name
);