git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] git-cat-file: fix output when format string contains no variables
@ 2013-11-05 20:03 Sven Brauch
  2013-11-06 18:00 ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Sven Brauch @ 2013-11-05 20:03 UTC (permalink / raw)
  To: git; +Cc: svenbrauch

>From 2e7b5aed771faeff654a447346bb0b57570d9569 Mon Sep 17 00:00:00 2001
From: Sven Brauch <svenbrauch@googlemail.com>
Date: Tue, 5 Nov 2013 20:06:21 +0100
Subject: [PATCH] git-cat-file: fix output when format string contains no
 variables

When the format string for git-cat-object --batch-check contained no
variables, the function would not actually look for the object on disk,
but just verify that the hash is correct. Thus it would report no error
if asking for objects which did not actually exist on disk if the SHA hash
looked ok.

Example of buggy behaviour:
echo "XYZ" | git hash-object --stdin | git cat-file --batch-check="found"
would report "found" although the manpage claims it would report an error.

Signed-off-by: Sven Brauch <svenbrauch@googlemail.com>
---

Notes:
    This fixes a bug where git-cat-file --batch-check would erroneously tell
    that objects exist while they did in fact not in case the argument to
    --batch-check was just a constant strig (i.e. no %(...) variables).
    The reason was that calling sha1_object_info_extended while requesting no
    properties of the object would not even verify this object existed, or more
    exactly, sha1_loose_object_info would not do that.
    
    I'm entirely unfamiliar with the git codebase; the suggested fix ensures
    that always at least one property is requested. If there's a better way
    to fix this issue, please let me know.

 builtin/cat-file.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index b2ca775..32d0b63 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -238,6 +238,15 @@ static int batch_one_object(const char *obj_name, struct batch_options *opt,
 		return 0;
 	}
 
+	if (!data->info.disk_sizep && !data->info.sizep && !data->info.typep) {
+		/*
+		 * None of the info fields was requested, so we request the cheapest
+		 * one (disk_sizep) to force sha1_object_info_extended to actually
+		 * look for the file.
+		 */
+		data->info.disk_sizep = &data->disk_size;
+	}
+
 	if (sha1_object_info_extended(data->sha1, &data->info) < 0) {
 		printf("%s missing\n", obj_name);
 		fflush(stdout);
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-11-06 19:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-11-05 20:03 [PATCH] git-cat-file: fix output when format string contains no variables Sven Brauch
2013-11-06 18:00 ` Junio C Hamano
2013-11-06 18:31   ` Jeff King
2013-11-06 19:40   ` Sven Brauch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).