@matheusbsbs / matheus-bagliano / commits / 621ff67594
Fork 0

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~ 45354a57ee7e3e42c7137db6c94fa968c6babe8d 89815cab95268e8f0f58142b848ac4cd5e9cbdcb 45354a57ee7e3e42c7137db6c94fa968c6babe8d 045f5759c97746589a067461e50fad16f60711ac 45354a57ee7e3e42c7137db6c94fa968c6babe8d 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~ 45354a57ee7e3e42c7137db6c94fa968c6babe8d 89815cab95268e8f0f58142b848ac4cd5e9cbdcb 89815cab95268e8f0f58142b848ac4cd5e9cbdcb 045f5759c97746589a067461e50fad16f60711ac 045f5759c97746589a067461e50fad16f60711ac Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Torvalds committed Mar 14, 2008 at 11:49 UTC 621ff6759414e2a723f61b6d8fc04b9805eb0c20
1 file changed +15 -13
sha1_name.c
+15 -13
@@ -408,18 +408,22 @@ static int get_nth_ancestor(const char *name, int len,
408 unsigned char *result, int generation)
409 {
410 unsigned char sha1[20];
411 - int ret = get_sha1_1(name, len, sha1);
411 + struct commit *commit;
412 + int ret;
413 +
414 + ret = get_sha1_1(name, len, sha1);
415 if (ret)
416 return ret;
417 + commit = lookup_commit_reference(sha1);
418 + if (!commit)
419 + return -1;
420
421 while (generation--) {
416 - struct commit *commit = lookup_commit_reference(sha1);
417 -
418 - if (!commit || parse_commit(commit) || !commit->parents)
422 + if (parse_commit(commit) || !commit->parents)
423 return -1;
420 - hashcpy(sha1, commit->parents->item->object.sha1);
424 + commit = commit->parents->item;
425 }
422 - hashcpy(result, sha1);
426 + hashcpy(result, commit->object.sha1);
427 return 0;
428 }
429
@@ -526,9 +530,8 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1)
530 int ret, has_suffix;
531 const char *cp;
532
529 - /* "name~3" is "name^^^",
530 - * "name~" and "name~0" are name -- not "name^0"!
531 - * "name^" is not "name^0"; it is "name^1".
533 + /*
534 + * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
535 */
536 has_suffix = 0;
537 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)
549 cp++;
550 while (cp < name + len)
551 num = num * 10 + *cp++ - '0';
549 - if (has_suffix == '^') {
550 - if (!num && len1 == len - 1)
551 - num = 1;
552 + if (!num && len1 == len - 1)
553 + num = 1;
554 + if (has_suffix == '^')
555 return get_parent(name, len1, sha1, num);
553 - }
556 /* else if (has_suffix == '~') -- goes without saying */
557 return get_nth_ancestor(name, len1, sha1, num);
558 }