forked from git/git
-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
object-name: fix resolution of object names containing curly braces #1844
Open
newren
wants to merge
2
commits into
gitgitgadget:master
Choose a base branch
from
newren:object-name-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1271,6 +1271,58 @@ static int peel_onion(struct repository *r, const char *name, int len, | |
return 0; | ||
} | ||
|
||
/* | ||
* Documentation/revisions.txt says: | ||
* '<describeOutput>', e.g. 'v1.7.4.2-679-g3bee7fb':: | ||
* Output from `git describe`; i.e. a closest tag, optionally | ||
* followed by a dash and a number of commits, followed by a dash, a | ||
* 'g', and an abbreviated object name. | ||
* | ||
* which means that the stuff before '-g${HASH}' needs to be a valid | ||
* refname, a dash, and a non-negative integer. This function verifies | ||
* that. | ||
* | ||
* In particular, we do not want to treat | ||
* branchname:path/to/file/named/i-gaffed | ||
* as a request for commit affed. | ||
* | ||
* More generally, we should probably not treat | ||
* 'refs/heads/./../.../ ~^:/?*[////\\\&}/busted.lock-g050e0ef6ead' | ||
* as a request for object 050e0ef6ead either. | ||
* | ||
* We are called with name[len] == '-' and name[len+1] == 'g', i.e. | ||
* we are verifying ${REFNAME}-{INTEGER} part of the name. | ||
*/ | ||
static int ref_and_count_parts_valid(const char *name, int len) | ||
{ | ||
struct strbuf sb; | ||
const char *cp; | ||
int flags = REFNAME_ALLOW_ONELEVEL; | ||
int ret = 1; | ||
|
||
/* Ensure we have at least one digit */ | ||
if (!isxdigit(name[len-1])) | ||
return 0; | ||
|
||
/* Skip over digits backwards until we get to the dash */ | ||
for (cp = name + len - 2; name < cp; cp--) { | ||
if (*cp == '-') | ||
break; | ||
if (!isxdigit(*cp)) | ||
return 0; | ||
} | ||
/* Ensure we found the leading dash */ | ||
if (*cp != '-') | ||
return 0; | ||
|
||
len = cp - name; | ||
strbuf_init(&sb, len); | ||
strbuf_add(&sb, name, len); | ||
ret = !check_refname_format(name, flags); | ||
strbuf_release(&sb); | ||
return ret; | ||
} | ||
|
||
static int get_describe_name(struct repository *r, | ||
const char *name, int len, | ||
struct object_id *oid) | ||
|
@@ -1284,7 +1336,8 @@ static int get_describe_name(struct repository *r, | |
/* We must be looking at g in "SOMETHING-g" | ||
* for it to be describe output. | ||
*/ | ||
if (ch == 'g' && cp[-1] == '-') { | ||
if (ch == 'g' && cp[-1] == '-' && | ||
ref_and_count_parts_valid(name, cp - 1 - name)) { | ||
cp++; | ||
len -= cp - name; | ||
return get_short_oid(r, | ||
|
@@ -2051,12 +2104,14 @@ static enum get_oid_result get_oid_with_context_1(struct repository *repo, | |
return -1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the Git mailing list, Junio C Hamano wrote (reply to this): "Elijah Newren via GitGitGadget" <[email protected]> writes:
> 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 '@' 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
Naïvely, it seems that a solution is to parse from left to right,
i.e., (1) notice there is a colon, (2) see if everything before that
colon resolves to a treeish, and (3) see if everything after it is a
path that appears in the treeish.
- When we are given foo@{some:thing}, if we did that, we realize
that "foo@{some" is not a valid tree-ish object name (since "@{"
cannot appear in a refname) and then can backtrack by realizing
"foo" is a ref, and @{...} could be a reflog reference (most
likely a way to spell some sort of timestamp), and try that.
- Similarly, for foo:path-gaffed, we would notice "foo" is a valid
tree-ish object name, and if path-gaffed is a path in it, we'd be
happy. Or foo may not be a tree-ish, or path-gaffed may not
exist in that tree-ish. In which case, we can backtrack and see
foo:path-g is an allowed prefix in a desribe name.
Now in the above description, I have assumed that an alternative
interpretation kicks in only as a fallback when we backtrack, but
we could make sure we try all possibilities and notice ambiguity if
we wanted to.
In any case, such an updated structure of the parsing code paths
(whether alternative interpretations are treated as fallbacks or
equally plausible candidates subject to disambiguation) would be
a vast departure from what we currently have, so a targeted "fix"
like these two patches attempt would be more appropriate as an
initial approach, I think.
Thanks, will queue, but probably we'd look at in any seriousness
after the 2.48 final gets tagged.
|
||
} | ||
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the Git mailing list, Junio C Hamano wrote (reply to this):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the Git mailing list, Elijah Newren wrote (reply to this):