cat-file --batch / --batch-check: do not exit if hashes are missing
Previously, cat-file --batch / --batch-check would silently exit if it was passed a non-existent SHA1 on stdin. Now it prints "<SHA1> missing" as in all other cases (and as advertised in the documentation). Note that cat-file --batch-check (but not --batch) will still output "error: unable to find <SHA1>" on stderr if a non-existent SHA1 is passed, but this does not affect parsing its stdout. Also, type <= 0 was previously using the potentially uninitialized type variable (relying on it being 0); it is now being initialized. Signed-off-by: Lea Wiemann <LeWiemann@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Lea Wiemann committed
Jun 9, 2008 at 02:02 UTC
3c076dbe3cffee3b5cbf34759d627c47a417c145
2 files changed
+27
-6
builtin-cat-file.c
+6
-3
@@ -150,7 +150,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
150
static int batch_one_object(const char *obj_name, int print_contents)
151
{
152
unsigned char sha1[20];
153
- enum object_type type;
153
+ enum object_type type = 0;
154
unsigned long size;
155
void *contents = contents;
156
@@ -168,8 +168,11 @@ static int batch_one_object(const char *obj_name, int print_contents)
168
else
169
type = sha1_object_info(sha1, &size);
170
171
- if (type <= 0)
172
- return 1;
171
+ if (type <= 0) {
172
+ printf("%s missing\n", obj_name);
173
+ fflush(stdout);
174
+ return 0;
175
+ }
176
177
printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
178
fflush(stdout);
t/t1006-cat-file.sh
+21
-3
@@ -174,9 +174,27 @@ do
174
'
175
done
176
177
-test_expect_success "--batch-check for a non-existent object" '
178
- test "deadbeef missing" = \
179
- "$(echo_without_newline deadbeef | git cat-file --batch-check)"
177
+test_expect_success "--batch-check for a non-existent named object" '
178
+ test "foobar42 missing
179
+foobar84 missing" = \
180
+ "$( ( echo foobar42; echo_without_newline foobar84; ) | git cat-file --batch-check)"
181
+'
182
+
183
+test_expect_success "--batch-check for a non-existent hash" '
184
+ test "0000000000000000000000000000000000000042 missing
185
+0000000000000000000000000000000000000084 missing" = \
186
+ "$( ( echo 0000000000000000000000000000000000000042;
187
+ echo_without_newline 0000000000000000000000000000000000000084; ) \
188
+ | git cat-file --batch-check)"
189
+'
190
+
191
+test_expect_success "--batch for an existent and a non-existent hash" '
192
+ test "$tag_sha1 tag $tag_size
193
+$tag_content
194
+0000000000000000000000000000000000000000 missing" = \
195
+ "$( ( echo $tag_sha1;
196
+ echo_without_newline 0000000000000000000000000000000000000000; ) \
197
+ | git cat-file --batch)"
198
'
199
200
test_expect_success "--batch-check for an emtpy line" '