Skip to content

Commit

Permalink
git-rev-parse: Allow a "zeroth" parent of a commit - the commit itself.
Browse files Browse the repository at this point in the history
This sounds nonsensical, but it's useful to make sure that the result is
a commit.

For example, "git-rev-parse v2.6.12" will return the _tag_ object for
v2.6.12, but "git-rev-parse v2.6.12^0" will return the _commit_ object
associated with that tag (and v2.6.12^1 will return the first parent).

Also, since the "parent" code will actually parse the commit, this,
together with the "--verify" flag, will verify not only that the result
is a single SHA1, but will also have verified that it's a proper commit
that we can see.
  • Loading branch information
Linus Torvalds committed Jul 12, 2005
1 parent e33b2ef commit 79162bb
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion rev-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ static int get_parent(char *name, unsigned char *result, int idx)
return -1;
if (parse_commit(commit))
return -1;
if (!idx) {
memcpy(result, commit->object.sha1, 20);
return 0;
}
p = commit->parents;
while (p) {
if (!--idx) {
Expand Down Expand Up @@ -238,7 +242,7 @@ static int get_extended_sha1(char *name, unsigned char *sha1)
int len = strlen(name);

parent = 1;
if (len > 2 && name[len-1] >= '1' && name[len-1] <= '9') {
if (len > 2 && name[len-1] >= '0' && name[len-1] <= '9') {
parent = name[len-1] - '0';
len--;
}
Expand Down

0 comments on commit 79162bb

Please sign in to comment.