git-cat-file: Add --batch-check option
[git] / builtin-cat-file.c
1 /*
2  * GIT - The information manager from hell
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "cache.h"
7 #include "exec_cmd.h"
8 #include "tag.h"
9 #include "tree.h"
10 #include "builtin.h"
11
12 static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
13 {
14         /* the parser in tag.c is useless here. */
15         const char *endp = buf + size;
16         const char *cp = buf;
17
18         while (cp < endp) {
19                 char c = *cp++;
20                 if (c != '\n')
21                         continue;
22                 if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
23                         const char *tagger = cp;
24
25                         /* Found the tagger line.  Copy out the contents
26                          * of the buffer so far.
27                          */
28                         write_or_die(1, buf, cp - buf);
29
30                         /*
31                          * Do something intelligent, like pretty-printing
32                          * the date.
33                          */
34                         while (cp < endp) {
35                                 if (*cp++ == '\n') {
36                                         /* tagger to cp is a line
37                                          * that has ident and time.
38                                          */
39                                         const char *sp = tagger;
40                                         char *ep;
41                                         unsigned long date;
42                                         long tz;
43                                         while (sp < cp && *sp != '>')
44                                                 sp++;
45                                         if (sp == cp) {
46                                                 /* give up */
47                                                 write_or_die(1, tagger,
48                                                              cp - tagger);
49                                                 break;
50                                         }
51                                         while (sp < cp &&
52                                                !('0' <= *sp && *sp <= '9'))
53                                                 sp++;
54                                         write_or_die(1, tagger, sp - tagger);
55                                         date = strtoul(sp, &ep, 10);
56                                         tz = strtol(ep, NULL, 10);
57                                         sp = show_date(date, tz, 0);
58                                         write_or_die(1, sp, strlen(sp));
59                                         xwrite(1, "\n", 1);
60                                         break;
61                                 }
62                         }
63                         break;
64                 }
65                 if (cp < endp && *cp == '\n')
66                         /* end of header */
67                         break;
68         }
69         /* At this point, we have copied out the header up to the end of
70          * the tagger line and cp points at one past \n.  It could be the
71          * next header line after the tagger line, or it could be another
72          * \n that marks the end of the headers.  We need to copy out the
73          * remainder as is.
74          */
75         if (cp < endp)
76                 write_or_die(1, cp, endp - cp);
77 }
78
79 static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
80 {
81         unsigned char sha1[20];
82         enum object_type type;
83         void *buf;
84         unsigned long size;
85
86         if (get_sha1(obj_name, sha1))
87                 die("Not a valid object name %s", obj_name);
88
89         buf = NULL;
90         switch (opt) {
91         case 't':
92                 type = sha1_object_info(sha1, NULL);
93                 if (type > 0) {
94                         printf("%s\n", typename(type));
95                         return 0;
96                 }
97                 break;
98
99         case 's':
100                 type = sha1_object_info(sha1, &size);
101                 if (type > 0) {
102                         printf("%lu\n", size);
103                         return 0;
104                 }
105                 break;
106
107         case 'e':
108                 return !has_sha1_file(sha1);
109
110         case 'p':
111                 type = sha1_object_info(sha1, NULL);
112                 if (type < 0)
113                         die("Not a valid object name %s", obj_name);
114
115                 /* custom pretty-print here */
116                 if (type == OBJ_TREE) {
117                         const char *ls_args[3] = {"ls-tree", obj_name, NULL};
118                         return cmd_ls_tree(2, ls_args, NULL);
119                 }
120
121                 buf = read_sha1_file(sha1, &type, &size);
122                 if (!buf)
123                         die("Cannot read object %s", obj_name);
124                 if (type == OBJ_TAG) {
125                         pprint_tag(sha1, buf, size);
126                         return 0;
127                 }
128
129                 /* otherwise just spit out the data */
130                 break;
131         case 0:
132                 buf = read_object_with_reference(sha1, exp_type, &size, NULL);
133                 break;
134
135         default:
136                 die("git-cat-file: unknown option: %s\n", exp_type);
137         }
138
139         if (!buf)
140                 die("git-cat-file %s: bad file", obj_name);
141
142         write_or_die(1, buf, size);
143         return 0;
144 }
145
146 static int batch_one_object(const char *obj_name)
147 {
148         unsigned char sha1[20];
149         enum object_type type;
150         unsigned long size;
151
152         if (!obj_name)
153            return 1;
154
155         if (get_sha1(obj_name, sha1)) {
156                 printf("%s missing\n", obj_name);
157                 return 0;
158         }
159
160         type = sha1_object_info(sha1, &size);
161         if (type <= 0)
162                 return 1;
163
164         printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
165
166         return 0;
167 }
168
169 static int batch_objects(void)
170 {
171         struct strbuf buf;
172
173         strbuf_init(&buf, 0);
174         while (strbuf_getline(&buf, stdin, '\n') != EOF) {
175                 int error = batch_one_object(buf.buf);
176                 if (error)
177                         return error;
178         }
179
180         return 0;
181 }
182
183 static const char cat_file_usage[] = "git-cat-file [ [-t|-s|-e|-p|<type>] <sha1> | --batch-check < <list_of_sha1s> ]";
184
185 int cmd_cat_file(int argc, const char **argv, const char *prefix)
186 {
187         int i, opt = 0, batch_check = 0;
188         const char *exp_type = NULL, *obj_name = NULL;
189
190         git_config(git_default_config);
191
192         for (i = 1; i < argc; ++i) {
193                 const char *arg = argv[i];
194
195                 if (!strcmp(arg, "--batch-check")) {
196                         if (opt) {
197                                 error("git-cat-file: Can't use --batch-check with -%c", opt);
198                                 usage(cat_file_usage);
199                         } else if (exp_type) {
200                                 error("git-cat-file: Can't use --batch-check when a type (\"%s\") is specified", exp_type);
201                                 usage(cat_file_usage);
202                         } else if (obj_name) {
203                                 error("git-cat-file: Can't use --batch-check when an object (\"%s\") is specified", obj_name);
204                                 usage(cat_file_usage);
205                         }
206
207                         batch_check = 1;
208                         continue;
209                 }
210
211                 if (!strcmp(arg, "-t") || !strcmp(arg, "-s") || !strcmp(arg, "-e") || !strcmp(arg, "-p")) {
212                         if (batch_check) {
213                                 error("git-cat-file: Can't use %s with --batch-check", arg);
214                                 usage(cat_file_usage);
215                         }
216
217                         exp_type = arg;
218                         opt = exp_type[1];
219                         continue;
220                 }
221
222                 if (arg[0] == '-')
223                         usage(cat_file_usage);
224
225                 if (!exp_type) {
226                         if (batch_check) {
227                                 error("git-cat-file: Can't specify a type (\"%s\") with --batch-check", arg);
228                                 usage(cat_file_usage);
229                         }
230
231                         exp_type = arg;
232                         continue;
233                 }
234
235                 if (obj_name)
236                         usage(cat_file_usage);
237
238                 // We should have hit one of the earlier if (batch_check) cases before
239                 // getting here.
240                 assert(!batch_check);
241
242                 obj_name = arg;
243                 break;
244         }
245
246         if (batch_check)
247                 return batch_objects();
248
249         if (!exp_type || !obj_name)
250                 usage(cat_file_usage);
251
252         return cat_one_file(opt, exp_type, obj_name);
253 }