@matheusbsbs / matheus-bagliano / commits / 05d5667fec
Fork 0

git-cat-file: Add --batch-check option

This new option allows multiple objects to be specified on stdin. For each object specified, a line of the following form is printed: <sha1> SP <type> SP <size> LF If the object does not exist in the repository, a line of the following form is printed: <object> SP missing LF Signed-off-by: Adam Roben <aroben@apple.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Adam Roben committed Apr 23, 2008 at 15:17 UTC 05d5667fec9650b049f47edd8cca23a43b135365
3 files changed +163 -7
Documentation/git-cat-file.txt
+26 -5
@@ -9,12 +9,16 @@ git-cat-file - Provide content or type/size information for repository objects
9 SYNOPSIS
10 --------
11 'git-cat-file' [-t | -s | -e | -p | <type>] <object>
12 +'git-cat-file' --batch-check < <list-of-objects>
13
14 DESCRIPTION
15 -----------
15 -Provides content or type of objects in the repository. The type
16 -is required unless '-t' or '-p' is used to find the object type,
17 -or '-s' is used to find the object size.
16 +In the first form, provides content or type of objects in the repository. The
17 +type is required unless '-t' or '-p' is used to find the object type, or '-s'
18 +is used to find the object size.
19 +
20 +In the second form, a list of object (separated by LFs) is provided on stdin,
21 +and the SHA1, type, and size of each object is printed on stdout.
22
23 OPTIONS
24 -------
@@ -46,6 +50,10 @@ OPTIONS
50 or to ask for a "blob" with <object> being a tag object that
51 points at it.
52
53 +--batch-check::
54 + Print the SHA1, type, and size of each object provided on stdin. May not be
55 + combined with any other options or arguments.
56 +
57 OUTPUT
58 ------
59 If '-t' is specified, one of the <type>.
@@ -56,9 +64,22 @@ If '-e' is specified, no output.
64
65 If '-p' is specified, the contents of <object> are pretty-printed.
66
59 -Otherwise the raw (though uncompressed) contents of the <object> will
60 -be returned.
67 +If <type> is specified, the raw (though uncompressed) contents of the <object>
68 +will be returned.
69 +
70 +If '--batch-check' is specified, output of the following form is printed for
71 +each object specified fon stdin:
72 +
73 +------------
74 +<sha1> SP <type> SP <size> LF
75 +------------
76 +
77 +Additionally, output of the following form is printed for each object specified
78 +on stdin that does not exist in the repository:
79
80 +------------
81 +<object> SP missing LF
82 +------------
83
84 Author
85 ------
builtin-cat-file.c
+72 -2
@@ -143,11 +143,48 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
143 return 0;
144 }
145
146 -static const char cat_file_usage[] = "git-cat-file [-t|-s|-e|-p|<type>] <sha1>";
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 {
150 - int i, opt = 0;
187 + int i, opt = 0, batch_check = 0;
188 const char *exp_type = NULL, *obj_name = NULL;
189
190 git_config(git_default_config);
@@ -155,7 +192,28 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
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;
@@ -165,6 +223,11 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
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 }
@@ -172,10 +235,17 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
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
t/t1006-cat-file.sh
+65
@@ -67,6 +67,19 @@ run_tests () {
67 false
68 fi
69 '
70 +
71 + test_expect_success "--batch-check output of $type is correct" '
72 + expect="$sha1 $type $size"
73 + actual="$(echo_without_newline $sha1 | git cat-file --batch-check)"
74 + if test "z$expect" = "z$actual"
75 + then
76 + : happy
77 + else
78 + echo "Oops: expected $expect"
79 + echo "but got $actual"
80 + false
81 + fi
82 + '
83 }
84
85 hello_content="Hello World"
@@ -118,4 +131,56 @@ test_expect_success \
131 "Reach a blob from a tag pointing to it" \
132 "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\""
133
134 +for opt in t s e p
135 +do
136 + test_expect_success "Passing -$opt with --batch-check fails" '
137 + test_must_fail git cat-file --batch-check -$opt $hello_sha1
138 + '
139 +
140 + test_expect_success "Passing --batch-check with -$opt fails" '
141 + test_must_fail git cat-file -$opt --batch-check $hello_sha1
142 + '
143 +done
144 +
145 +test_expect_success "Passing <type> with --batch-check fails" '
146 + test_must_fail git cat-file --batch-check blob $hello_sha1
147 +'
148 +
149 +test_expect_success "Passing --batch-check with <type> fails" '
150 + test_must_fail git cat-file blob --batch-check $hello_sha1
151 +'
152 +
153 +test_expect_success "Passing sha1 with --batch-check fails" '
154 + test_must_fail git cat-file --batch-check $hello_sha1
155 +'
156 +
157 +test_expect_success "--batch-check for a non-existent object" '
158 + test "deadbeef missing" = \
159 + "$(echo_without_newline deadbeef | git cat-file --batch-check)"
160 +'
161 +
162 +test_expect_success "--batch-check for an emtpy line" '
163 + test " missing" = "$(echo | git cat-file --batch-check)"
164 +'
165 +
166 +batch_check_input="$hello_sha1
167 +$tree_sha1
168 +$commit_sha1
169 +$tag_sha1
170 +deadbeef
171 +
172 +"
173 +
174 +batch_check_output="$hello_sha1 blob $hello_size
175 +$tree_sha1 tree $tree_size
176 +$commit_sha1 commit $commit_size
177 +$tag_sha1 tag $tag_size
178 +deadbeef missing
179 + missing"
180 +
181 +test_expect_success "--batch-check with multiple sha1s gives correct format" '
182 + test "$batch_check_output" = \
183 + "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)"
184 +'
185 +
186 test_done