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 '@' 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 <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>
  • Loading branch information
newren authored and gitster committed Jan 4, 2025
1 parent 1b4e9a5 commit 65cd7ad
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
8 changes: 5 additions & 3 deletions object-name.c
Original file line number Diff line number Diff line change
Expand Up @@ -2052,12 +2052,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+1) == '{' && (*cp == '@' || *cp == '^')) {
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
31 changes: 30 additions & 1 deletion t/t1006-cat-file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ test_expect_success "setup" '
git config extensions.objectformat $test_hash_algo &&
git config extensions.compatobjectformat $test_compat_hash_algo &&
echo_without_newline "$hello_content" > hello &&
git update-index --add hello
git update-index --add hello &&
git commit -m "add hello file"
'

run_blob_tests () {
Expand Down Expand Up @@ -602,6 +603,34 @@ 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" HEAD &&
git branch "foo@" HEAD
'

test_expect_success '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 '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 with commit with colon' '
git commit-tree -m "testing: just a bunch of junk" HEAD^{tree} >out &&
git branch other $(cat out)
'

test_expect_success 'object reference via commit text search' '
git cat-file -p "other^{/testing:}: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 &&
Expand Down

0 comments on commit 65cd7ad

Please sign in to comment.