rev-parse: fix meaning of rev~ vs rev~0.

I think it would make more sense for rev~ to have the same guarantees that
rev^ has, namely to always return a commit. I would also suggest that not
giving a number would have the same effect of defaulting to 1, not 0.

Right now it's a bit illogical, but at least it's an _undocumented_
illogical behaviour.

This patch makes '^' and '~' act the same for the default count (i.e. both
default to 1), and also have the same behaviour for a count of zero.

Before (no discernible pattern):

	[torvalds@woody git]$ git rev-parse v1.5.1 v1.5.1^0 v1.5.1~0 v1.5.1^ v1.5.1~
	45354a57ee
	89815cab95
	45354a57ee
	045f5759c9
	45354a57ee

After (fairly logical):

	[torvalds@woody git]$ git rev-parse v1.5.1 v1.5.1^0 v1.5.1~0 v1.5.1^ v1.5.1~
	45354a57ee
	89815cab95
	89815cab95
	045f5759c9
	045f5759c9

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Linus Torvalds 2008-03-14 11:49:40 -07:00 committed by Junio C Hamano
parent ed0b9d4309
commit 621ff67594

View file

@ -408,18 +408,22 @@ static int get_nth_ancestor(const char *name, int len,
unsigned char *result, int generation)
{
unsigned char sha1[20];
int ret = get_sha1_1(name, len, sha1);
struct commit *commit;
int ret;
ret = get_sha1_1(name, len, sha1);
if (ret)
return ret;
commit = lookup_commit_reference(sha1);
if (!commit)
return -1;
while (generation--) {
struct commit *commit = lookup_commit_reference(sha1);
if (!commit || parse_commit(commit) || !commit->parents)
if (parse_commit(commit) || !commit->parents)
return -1;
hashcpy(sha1, commit->parents->item->object.sha1);
commit = commit->parents->item;
}
hashcpy(result, sha1);
hashcpy(result, commit->object.sha1);
return 0;
}
@ -526,9 +530,8 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1)
int ret, has_suffix;
const char *cp;
/* "name~3" is "name^^^",
* "name~" and "name~0" are name -- not "name^0"!
* "name^" is not "name^0"; it is "name^1".
/*
* "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
*/
has_suffix = 0;
for (cp = name + len - 1; name <= cp; cp--) {
@ -546,11 +549,10 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1)
cp++;
while (cp < name + len)
num = num * 10 + *cp++ - '0';
if (has_suffix == '^') {
if (!num && len1 == len - 1)
num = 1;
if (!num && len1 == len - 1)
num = 1;
if (has_suffix == '^')
return get_parent(name, len1, sha1, num);
}
/* else if (has_suffix == '~') -- goes without saying */
return get_nth_ancestor(name, len1, sha1, num);
}