Skip to content

Commit

Permalink
object-name: fix resolution of object names containing curly braces
Browse files Browse the repository at this point in the history
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 '@' 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 protected]>
Helped-by: Michael Haggerty <[email protected]>
Signed-off-by: Elijah Newren <[email protected]>
  • Loading branch information
newren committed Dec 30, 2024
1 parent 92999a4 commit 1308dfe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
8 changes: 5 additions & 3 deletions object-name.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions t/t1006-cat-file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,23 @@ test_expect_success FUNNYNAMES '--batch-check, -Z with newline in input' '
test_cmp expect actual
'

test_expect_success 'setup with curly braches in input' '
git branch "foo{bar" &&
git branch "foo@"
'

test_expect_success 'object reference with curly brace' '
git cat-file -p "foo{bar:hello" >actual &&
git cat-file -p master:hello >expect &&
test_cmp expect actual
'

test_expect_success 'object reference with at-sign' '
git cat-file -p "foo@@{0}:hello" >actual &&
git cat-file -p master: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 &&
Expand Down

0 comments on commit 1308dfe

Please sign in to comment.