git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] git-cat-file: fix output when format string contains no variables

From
SBSven Brauch <svenbrauch@googlemail.com>
Date
Nov 5, 2013, 20:03 UTC
Message-ID
<1748846.6BAMC5enOx@localhost.localdomain>
>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
Next: Junio C Hamano
Message 1 of 4 in “git-cat-file: fix output when format string contains no variables”
  1. git-cat-file: fix output when format string contains no variablesSven Brauch, Nov 5, 2013
  2. Junio C HamanoNov 6, 2013
  3. Jeff KingNov 6, 2013
  4. Sven BrauchNov 6, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.