From 4ef8d1dd03d3b8adf81b4b81b2231128e3591f36 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 6 Nov 2013 10:00:57 -0800 Subject: sha1_loose_object_info(): do not return success on missing object Since 052fe5ea (sha1_loose_object_info: make type lookup optional, 2013-07-12), sha1_loose_object_info() returns happily without checking if the object in question exists, which is not what the the caller sha1_object_info_extended() expects; the caller does not even bother checking the existence of the object itself. Noticed-by: Sven Brauch Helped-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/sha1_file.c b/sha1_file.c index 613839d..d17315d 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -2413,15 +2413,18 @@ static int sha1_loose_object_info(const unsigned char *sha1, /* * If we don't care about type or size, then we don't - * need to look inside the object at all. + * need to look inside the object at all. Note that we + * do not optimize out the stat call, even if the + * caller doesn't care about the disk-size, since our + * return value implicitly indicates whether the + * object even exists. */ if (!oi->typep && !oi->sizep) { - if (oi->disk_sizep) { - struct stat st; - if (stat_sha1_file(sha1, &st) < 0) - return -1; + struct stat st; + if (stat_sha1_file(sha1, &st) < 0) + return -1; + if (oi->disk_sizep) *oi->disk_sizep = st.st_size; - } return 0; } diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index 4e911fb..558f0f5 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -179,6 +179,12 @@ test_expect_success "--batch-check for an emtpy line" ' test " missing" = "$(echo | git cat-file --batch-check)" ' +test_expect_success 'empty --batch-check notices missing object' ' + echo "$_z40 missing" >expect && + echo "$_z40" | git cat-file --batch-check="" >actual && + test_cmp expect actual +' + batch_input="$hello_sha1 $commit_sha1 $tag_sha1 -- cgit v0.10.2-6-g49f6