sha1_name: fix error message for @{<N>}, @{<date>}

Currently, when we try to resolve @{<N>} or @{<date>} when the reflog doesn't go back far enough, we get errors like: # on branch master $ git show @{10000} fatal: Log for '' only has 7 entries. $ git show @{10000.days.ago} warning: Log for '' only goes back to Tue, 21 May 2013 14:14:45 +0530. ... # detached HEAD case $ git show @{10000} fatal: Log for '' only has 2005 entries. $ git show master@{10000} fatal: Log for 'master' only has 7 entries. The empty string '' is confusing and does not convey information about whose logs we are inspecting. Change this so that we get: # on branch master $ git show @{10000} fatal: Log for 'master' only has 7 entries. $ git show @{10000.days.ago} warning: Log for 'master' only goes back to Tue, 21 May 2013 14:14:45 +0530. ... # detached HEAD case $ git show @{10000} fatal: Log for 'HEAD' only has 2005 entries. $ git show master@{10000} fatal: Log for 'master' only has 7 entries. Also one of the message strings given to die() now points into real_ref that was not used in that fashion, so stop freeing the underlying storage for it. Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com> Bug-spotted-and-fixed-by: Thomas Rast Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ramkumar Ramachandra committed May 22, 2013 at 16:09 UTC 305ebea06d5e633e3d648c798b5e6bb2b9abf361
1 file changed +10 -1
sha1_name.c
+10 -1
@@ -517,12 +517,21 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
517 }
518 if (read_ref_at(real_ref, at_time, nth, sha1, NULL,
519 &co_time, &co_tz, &co_cnt)) {
520 + if (!len) {
521 + if (!prefixcmp(real_ref, "refs/heads/")) {
522 + str = real_ref + 11;
523 + len = strlen(real_ref + 11);
524 + } else {
525 + /* detached HEAD */
526 + str = "HEAD";
527 + len = 4;
528 + }
529 + }
530 if (at_time)
531 warning("Log for '%.*s' only goes "
532 "back to %s.", len, str,
533 show_date(co_time, co_tz, DATE_RFC2822));
534 else {
525 - free(real_ref);
535 die("Log for '%.*s' only has %d entries.",
536 len, str, co_cnt);
537 }