Skip to content

Commit 79162bb

Browse files
author
Linus Torvalds
committed
git-rev-parse: Allow a "zeroth" parent of a commit - the commit itself.
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.
1 parent e33b2ef commit 79162bb

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

rev-parse.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ static int get_parent(char *name, unsigned char *result, int idx)
9797
return -1;
9898
if (parse_commit(commit))
9999
return -1;
100+
if (!idx) {
101+
memcpy(result, commit->object.sha1, 20);
102+
return 0;
103+
}
100104
p = commit->parents;
101105
while (p) {
102106
if (!--idx) {
@@ -238,7 +242,7 @@ static int get_extended_sha1(char *name, unsigned char *sha1)
238242
int len = strlen(name);
239243

240244
parent = 1;
241-
if (len > 2 && name[len-1] >= '1' && name[len-1] <= '9') {
245+
if (len > 2 && name[len-1] >= '0' && name[len-1] <= '9') {
242246
parent = name[len-1] - '0';
243247
len--;
244248
}

0 commit comments

Comments
 (0)