Subject: [PATCH] object-name: fix resolution of object names containing curly braces

From: "Elijah Newren via GitGitGadget" <[email-masked]>
Date: Wed, 01 Jan 2025 02:53:09 +0000

From: Elijah Newren <[email-masked]>

Given a branch name of 'foo{bar', commands like

    git cat-file -p foo{bar:README.md

should succeed (assuming that branch had a README.md file, of course).
However, the change in cce91a2caef9 (Change 'master@noon' syntax to
'master@{noon}'., 2006-05-19) presumed that curly braces would always
come after an '@' and be paired, causing 'foo{bar:README.md' to
entirely miss the ':' and assume there's no object being referenced.
In short, git would report:

    fatal: Not a valid object name foo{bar:README.md

Change the parsing to only make the assumption of paired curly braces
immediately after a '@' character appears.

Add tests for both this and 'foo@@{...}' cases, which an initial version
of this patch broke.

Reported-by: Gabriel Amaral <[email-masked]>
Helped-by: Michael Haggerty <[email-masked]>
Signed-off-by: Elijah Newren <[email-masked]>
---
    object-name: fix resolution of object names containing curly braces
    
    Maintainer note: this bug dates back to 2006; it is not a regression in
    this cycle.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1844%2Fnewren%2Fobject-name-fix-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1844/newren/object-name-fix-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1844

 object-name.c       |  8 +++++---
 t/t1006-cat-file.sh | 17 +++++++++++++++++
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/object-name.c b/object-name.c
index c892fbe80aa..e92f26b3256 100644
--- a/object-name.c
+++ b/object-name.c
@@ -2087,12 +2087,14 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
 		return -1;
 	}
 	for (cp = name, bracket_depth = 0; *cp; cp++) {
-		if (*cp == '{')
+		if (*cp == '@' && *(cp+1) == '{') {
+			cp++;
 			bracket_depth++;
-		else if (bracket_depth && *cp == '}')
+		} else if (bracket_depth && *cp == '}') {
 			bracket_depth--;
-		else if (!bracket_depth && *cp == ':')
+		} else if (!bracket_depth && *cp == ':') {
 			break;
+		}
 	}
 	if (*cp == ':') {
 		struct object_id tree_oid;
diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh
index d36cd7c0863..252485dac78 100755
--- a/t/t1006-cat-file.sh
+++ b/t/t1006-cat-file.sh
@@ -603,6 +603,23 @@ test_expect_success FUNNYNAMES '--batch-check, -Z with newline in input' '
 	test_cmp expect actual
 '
 
+test_expect_success FUNNYNAMES 'setup with curly braches in input' '
+	git branch "foo{bar" &&
+	git branch "foo@"
+'
+
+test_expect_success FUNNYNAMES 'object reference with curly brace' '
+	git cat-file -p "foo{bar:hello" >actual &&
+	git cat-file -p HEAD:hello >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success FUNNYNAMES 'object reference with at-sign' '
+	git cat-file -p "foo@@{0}:hello" >actual &&
+	git cat-file -p HEAD:hello >expect &&
+	test_cmp expect actual
+'
+
 test_expect_success 'setup blobs which are likely to delta' '
 	test-tool genrandom foo 10240 >foo &&
 	{ cat foo && echo plus; } >foo-plus &&

base-commit: 92999a42db1c5f43f330e4f2bca4026b5b81576f
-- 
gitgitgadget