Skip to content

Commit 5a88867

Browse files
newrendscho
authored andcommitted
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}' Note that we'd prefer not duplicating the special logic for "@^" characters here, because if get_oid_basic() or interpret_nth_prior_checkout() or get_oid_basic() or similar gain extra methods of using curly braces, then the logic in get_oid_with_context_1() would need to be updated as well. But it's not clear how to refactor all of these to have a simple common callpoint with the specialized logic. Reported-by: Gabriel Amaral <gabriel-amaral@github.com> Helped-by: Michael Haggerty <mhagger@github.com> Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5cc2d6c commit 5a88867

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

object-name.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -2052,12 +2052,14 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo,
20522052
return -1;
20532053
}
20542054
for (cp = name, bracket_depth = 0; *cp; cp++) {
2055-
if (*cp == '{')
2055+
if (strchr("@^", *cp) && cp[1] == '{') {
2056+
cp++;
20562057
bracket_depth++;
2057-
else if (bracket_depth && *cp == '}')
2058+
} else if (bracket_depth && *cp == '}') {
20582059
bracket_depth--;
2059-
else if (!bracket_depth && *cp == ':')
2060+
} else if (!bracket_depth && *cp == ':') {
20602061
break;
2062+
}
20612063
}
20622064
if (*cp == ':') {
20632065
struct object_id tree_oid;

t/t1006-cat-file.sh

+30-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ test_expect_success "setup" '
240240
git config extensions.objectformat $test_hash_algo &&
241241
git config extensions.compatobjectformat $test_compat_hash_algo &&
242242
echo_without_newline "$hello_content" > hello &&
243-
git update-index --add hello
243+
git update-index --add hello &&
244+
git commit -m "add hello file"
244245
'
245246

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

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

0 commit comments

Comments
 (0)