Skip to content

Commit 0cc56dd

Browse files
committed
object-name: fix resolution of object names containing curly braces
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 cce91a2 (Change 'master@noon' syntax to 'master@{noon}'., 2006-05-19) presumed that curly braces would always come after an '@' or '^' and be paired, causing e.g. '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 either a '@' or '^' character appears. Add tests for this, as well as for a few other test cases that initial versions of this patch broke: * 'foo@@{...}' * 'foo^{/${SEARCH_TEXT_WITH_COLON}}:${PATH}' Reported-by: Gabriel Amaral <[email protected]> Helped-by: Michael Haggerty <[email protected]> Signed-off-by: Elijah Newren <[email protected]>
1 parent 92999a4 commit 0cc56dd

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

object-name.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,12 +2087,14 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
20872087
return -1;
20882088
}
20892089
for (cp = name, bracket_depth = 0; *cp; cp++) {
2090-
if (*cp == '{')
2090+
if (*(cp+1) == '{' && (*cp == '@' || *cp == '^')) {
2091+
cp++;
20912092
bracket_depth++;
2092-
else if (bracket_depth && *cp == '}')
2093+
} else if (bracket_depth && *cp == '}') {
20932094
bracket_depth--;
2094-
else if (!bracket_depth && *cp == ':')
2095+
} else if (!bracket_depth && *cp == ':') {
20952096
break;
2097+
}
20962098
}
20972099
if (*cp == ':') {
20982100
struct object_id tree_oid;

t/t1006-cat-file.sh

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ test_expect_success "setup" '
241241
git config extensions.objectformat $test_hash_algo &&
242242
git config extensions.compatobjectformat $test_compat_hash_algo &&
243243
echo_without_newline "$hello_content" > hello &&
244-
git update-index --add hello
244+
git update-index --add hello &&
245+
git commit -m "add hello file"
245246
'
246247

247248
run_blob_tests () {
@@ -603,6 +604,34 @@ test_expect_success FUNNYNAMES '--batch-check, -Z with newline in input' '
603604
test_cmp expect actual
604605
'
605606

607+
test_expect_success 'setup with curly braches in input' '
608+
git branch "foo{bar" HEAD &&
609+
git branch "foo@" HEAD
610+
'
611+
612+
test_expect_success 'object reference with curly brace' '
613+
git cat-file -p "foo{bar:hello" >actual &&
614+
git cat-file -p HEAD:hello >expect &&
615+
test_cmp expect actual
616+
'
617+
618+
test_expect_success 'object reference with at-sign' '
619+
git cat-file -p "foo@@{0}:hello" >actual &&
620+
git cat-file -p HEAD:hello >expect &&
621+
test_cmp expect actual
622+
'
623+
624+
test_expect_success 'setup with commit with colon' '
625+
git commit-tree -m "testing: just a bunch of junk" HEAD^{tree} >out &&
626+
git branch other $(cat out)
627+
'
628+
629+
test_expect_success 'object reference via commit text search' '
630+
git cat-file -p "other^{/testing:}:hello" >actual &&
631+
git cat-file -p HEAD:hello >expect &&
632+
test_cmp expect actual
633+
'
634+
606635
test_expect_success 'setup blobs which are likely to delta' '
607636
test-tool genrandom foo 10240 >foo &&
608637
{ cat foo && echo plus; } >foo-plus &&

0 commit comments

Comments
 (0)