git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sven Brauch <svenbrauch@googlemail.com>
To: git@vger.kernel.org
Cc: svenbrauch@googlemail.com
Subject: [PATCH] git-cat-file: fix output when format string contains no variables
Date: Tue, 05 Nov 2013 21:03:11 +0100	[thread overview]
Message-ID: <1748846.6BAMC5enOx@localhost.localdomain> (raw)

>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

             reply	other threads:[~2013-11-05 20:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-05 20:03 Sven Brauch [this message]
2013-11-06 18:00 ` [PATCH] git-cat-file: fix output when format string contains no variables Junio C Hamano
2013-11-06 18:31   ` Jeff King
2013-11-06 19:40   ` Sven Brauch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1748846.6BAMC5enOx@localhost.localdomain \
    --to=svenbrauch@googlemail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).