Skip to content

Commit 3c076db

Browse files
Lea Wiemanngitster
authored andcommitted
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>
1 parent 4e44ae4 commit 3c076db

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

builtin-cat-file.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
150150
static int batch_one_object(const char *obj_name, int print_contents)
151151
{
152152
unsigned char sha1[20];
153-
enum object_type type;
153+
enum object_type type = 0;
154154
unsigned long size;
155155
void *contents = contents;
156156

@@ -168,8 +168,11 @@ static int batch_one_object(const char *obj_name, int print_contents)
168168
else
169169
type = sha1_object_info(sha1, &size);
170170

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+
}
173176

174177
printf("%s %s %lu\n", sha1_to_hex(sha1), typename(type), size);
175178
fflush(stdout);

t/t1006-cat-file.sh

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,27 @@ do
174174
'
175175
done
176176

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)"
180198
'
181199

182200
test_expect_success "--batch-check for an emtpy line" '

0 commit comments

Comments
 (0)