@matheusbsbs / matheus-bagliano / commits / 76a44c5c0b
Fork 0

show-branch --reflog: show the reflog message at the top.

This changes the output so the list at the top shows the reflog message, along with their relative timestamps. You can use --reflog=<n> to show <n> most recent log entries, or use --reflog=<n>,<b> to show <n> entries going back from the entry <b>. <b> can be either a number (so --reflog=4,20 shows 4 records starting from @{20}) or a timestamp (e.g. --reflog='4,1 day'). Here is a sample output (with --list option): $ git show-branch --reflog=10 --list jc/show-reflog [jc/show-reflog@{0}] (3 minutes ago) commit (amend): show-branch --ref [jc/show-reflog@{1}] (5 minutes ago) reset HEAD^ [jc/show-reflog@{2}] (14 minutes ago) commit: show-branch --reflog: sho [jc/show-reflog@{3}] (14 minutes ago) commit: show-branch --reflog: sho [jc/show-reflog@{4}] (18 minutes ago) commit (amend): Extend read_ref_a [jc/show-reflog@{5}] (18 minutes ago) commit (amend): Extend read_ref_a [jc/show-reflog@{6}] (18 minutes ago) commit (amend): Extend read_ref_a [jc/show-reflog@{7}] (18 minutes ago) am: read_ref_at(): allow retrievi [jc/show-reflog@{8}] (18 minutes ago) reset --hard HEAD~4 [jc/show-reflog@{9}] (61 minutes ago) commit: show-branch --reflog: use This shows what I did more cleanly: $ git show-branch --reflog=10 jc/show-reflog ! [jc/show-reflog@{0}] (3 minutes ago) commit (amend): show-branch --ref ! [jc/show-reflog@{1}] (5 minutes ago) reset HEAD^ ! [jc/show-reflog@{2}] (14 minutes ago) commit: show-branch --reflog: ! [jc/show-reflog@{3}] (14 minutes ago) commit: show-branch --reflog: ! [jc/show-reflog@{4}] (18 minutes ago) commit (amend): Extend read_ ! [jc/show-reflog@{5}] (18 minutes ago) commit (amend): Extend read ! [jc/show-reflog@{6}] (18 minutes ago) commit (amend): Extend rea ! [jc/show-reflog@{7}] (18 minutes ago) am: read_ref_at(): allow ! [jc/show-reflog@{8}] (18 minutes ago) reset --hard HEAD~4 ! [jc/show-reflog@{9}] (61 minutes ago) commit: show-branch --r ---------- + [jc/show-reflog@{0}] show-branch --reflog: show the reflog + [jc/show-reflog@{2}] show-branch --reflog: show the reflog +++ [jc/show-reflog@{1}] show-branch --reflog: show the reflog +++++ [jc/show-reflog@{4}] Extend read_ref_at() to be usable fro + [jc/show-reflog@{5}] Extend read_ref_at() to be usable fro + [jc/show-reflog@{6}] Extend read_ref_at() to be usable fro + [jc/show-reflog@{7}] read_ref_at(): allow retrieving the r + [jc/show-reflog@{9}] show-branch --reflog: use updated rea + [jc/show-reflog@{9}^] read_ref_at(): allow reporting the c + [jc/show-reflog@{9}~2] show-branch --reflog: show the refl + [jc/show-reflog@{9}~3] read_ref_at(): allow retrieving the ++++++++++ [jc/show-reflog@{8}] dwim_ref(): Separate name-to-ref DWIM At @{9}, I had a commit to complete 5 patch series, but I wanted to consolidate two commits that enhances read_ref_at() into one (they were @{9}^ and @{9}~3), and another two that touch show-branch into one (@{9} and @{9}~2). I first saved them with "format-patch -4", and then did a reset at @{8}. At @{7}, I applied one of them with "am", and then used "git-apply" on the other one, and amended the commit at @{6} (so @{6} and @{7} has the same parent). I did not like the log message, so I amended again at @{5}. Then I cherry-picked @{9}~2 to create @{3} (the log message shows that it needs to learn to set GIT_REFLOG_ACTION -- it uses "git-commit" and the log entry is attributed for it). Another cherry-pick built @{2} out of @{9}, but what I wanted to do was to squash these two into one, so I did a "reset HEAD^" at @{1} and then made the final commit by amending what was at the top. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jan 19, 2007 at 01:20 UTC 76a44c5c0b25b1dbde56d17353d5b0821b7c6981
3 files changed +109 -33
Documentation/git-show-branch.txt
+12 -3
@@ -11,7 +11,7 @@ SYNOPSIS
11 'git-show-branch' [--all] [--remotes] [--topo-order] [--current]
12 [--more=<n> | --list | --independent | --merge-base]
13 [--no-name | --sha1-name] [--topics] [<rev> | <glob>]...
14 -'git-show-branch' --reflog[=<n>] <ref>
14 +'git-show-branch' --reflog[=<n>[,<base>]] [--list] <ref>
15
16 DESCRIPTION
17 -----------
@@ -97,8 +97,10 @@ OPTIONS
97 will show the revisions given by "git rev-list {caret}master
98 topic1 topic2"
99
100 ---reflog[=<n>] <ref>::
101 - Shows <n> most recent ref-log entries for the given ref.
100 +--reflog[=<n>[,<base>]] <ref>::
101 + Shows <n> most recent ref-log entries for the given
102 + ref. If <base> is given, <n> entries going back from
103 + that entry. <base> can be specified as count or date
104
105
106 Note that --more, --list, --independent and --merge-base options
@@ -165,6 +167,13 @@ With this, `git show-branch` without extra parameters would show
167 only the primary branches. In addition, if you happen to be on
168 your topic branch, it is shown as well.
169
170 +------------
171 +$ git show-branch --reflog='10,1 hour ago' --list master
172 +------------
173 +
174 +shows 10 reflog entries going back from the tip as of 1 hour ago.
175 +Without `--list`, the output also shows how these tips are
176 +topologically related with each other.
177
178
179 Author
builtin-show-branch.c
+95 -27
@@ -346,18 +346,21 @@ static void sort_ref_range(int bottom, int top)
346 compare_ref_name);
347 }
348
349 -static int append_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
349 +static int append_ref(const char *refname, const unsigned char *sha1,
350 + int allow_dups)
351 {
352 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
353 int i;
354
355 if (!commit)
356 return 0;
356 - /* Avoid adding the same thing twice */
357 - for (i = 0; i < ref_name_cnt; i++)
358 - if (!strcmp(refname, ref_name[i]))
359 - return 0;
357
358 + if (!allow_dups) {
359 + /* Avoid adding the same thing twice */
360 + for (i = 0; i < ref_name_cnt; i++)
361 + if (!strcmp(refname, ref_name[i]))
362 + return 0;
363 + }
364 if (MAX_REVS <= ref_name_cnt) {
365 fprintf(stderr, "warning: ignoring %s; "
366 "cannot handle more than %d refs\n",
@@ -380,7 +383,7 @@ static int append_head_ref(const char *refname, const unsigned char *sha1, int f
383 */
384 if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1))
385 ofs = 5;
383 - return append_ref(refname + ofs, sha1, flag, cb_data);
386 + return append_ref(refname + ofs, sha1, 0);
387 }
388
389 static int append_remote_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
@@ -394,14 +397,14 @@ static int append_remote_ref(const char *refname, const unsigned char *sha1, int
397 */
398 if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1))
399 ofs = 5;
397 - return append_ref(refname + ofs, sha1, flag, cb_data);
400 + return append_ref(refname + ofs, sha1, 0);
401 }
402
403 static int append_tag_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
404 {
405 if (strncmp(refname, "refs/tags/", 10))
406 return 0;
404 - return append_ref(refname + 5, sha1, flag, cb_data);
407 + return append_ref(refname + 5, sha1, 0);
408 }
409
410 static const char *match_ref_pattern = NULL;
@@ -434,7 +437,7 @@ static int append_matching_ref(const char *refname, const unsigned char *sha1, i
437 return append_head_ref(refname, sha1, flag, cb_data);
438 if (!strncmp("refs/tags/", refname, 10))
439 return append_tag_ref(refname, sha1, flag, cb_data);
437 - return append_ref(refname, sha1, flag, cb_data);
440 + return append_ref(refname, sha1, 0);
441 }
442
443 static void snarf_refs(int head, int remotes)
@@ -507,7 +510,7 @@ static void append_one_rev(const char *av)
510 {
511 unsigned char revkey[20];
512 if (!get_sha1(av, revkey)) {
510 - append_ref(av, revkey, 0, NULL);
513 + append_ref(av, revkey, 0);
514 return;
515 }
516 if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) {
@@ -562,9 +565,24 @@ static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
565 return 0;
566 }
567
568 +static void parse_reflog_param(const char *arg, int *cnt, const char **base)
569 +{
570 + char *ep;
571 + *cnt = strtoul(arg, &ep, 10);
572 + if (*ep == ',')
573 + *base = ep + 1;
574 + else if (*ep)
575 + die("unrecognized reflog param '%s'", arg + 9);
576 + else
577 + *base = NULL;
578 + if (*cnt <= 0)
579 + *cnt = DEFAULT_REFLOG;
580 +}
581 +
582 int cmd_show_branch(int ac, const char **av, const char *prefix)
583 {
584 struct commit *rev[MAX_REVS], *commit;
585 + char *reflog_msg[MAX_REVS];
586 struct commit_list *list = NULL, *seen = NULL;
587 unsigned int rev_mask[MAX_REVS];
588 int num_rev, i, extra = 0;
@@ -585,6 +603,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
603 int topics = 0;
604 int dense = 1;
605 int reflog = 0;
606 + const char *reflog_base = NULL;
607
608 git_config(git_show_branch_config);
609
@@ -631,21 +650,27 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
650 else if (!strcmp(arg, "--reflog")) {
651 reflog = DEFAULT_REFLOG;
652 }
634 - else if (!strncmp(arg, "--reflog=", 9)) {
635 - char *end;
636 - reflog = strtoul(arg + 9, &end, 10);
637 - if (*end != '\0')
638 - die("unrecognized reflog count '%s'", arg + 9);
639 - }
653 + else if (!strncmp(arg, "--reflog=", 9))
654 + parse_reflog_param(arg + 9, &reflog, &reflog_base);
655 else
656 usage(show_branch_usage);
657 ac--; av++;
658 }
659 ac--; av++;
660
646 - /* Only one of these is allowed */
647 - if (1 < independent + merge_base + (extra != 0) + (!!reflog))
648 - usage(show_branch_usage);
661 + if (!!extra || !!reflog) {
662 + /* "listing" mode is incompatible with
663 + * independent nor merge-base modes.
664 + */
665 + if (independent || merge_base)
666 + usage(show_branch_usage);
667 + if (!!reflog && (0 < extra))
668 + /*
669 + * Asking for --more in reflog mode does not
670 + * make sense.
671 + */
672 + usage(show_branch_usage);
673 + }
674
675 /* If nothing is specified, show all branches by default */
676 if (ac + all_heads + all_remotes == 0)
@@ -654,14 +679,51 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
679 if (all_heads + all_remotes)
680 snarf_refs(all_heads, all_remotes);
681 if (reflog) {
657 - int reflen;
658 - if (!ac)
682 + unsigned char sha1[20];
683 + char nth_desc[256];
684 + char *ref;
685 + int base = 0;
686 + if (ac != 1)
687 die("--reflog option needs one branch name");
660 - reflen = strlen(*av);
688 + if (!dwim_ref(*av, strlen(*av), sha1, &ref))
689 + die("No such ref %s", *av);
690 +
691 + /* Has the base been specified? */
692 + if (reflog_base) {
693 + char *ep;
694 + base = strtoul(reflog_base, &ep, 10);
695 + if (*ep) {
696 + /* Ah, that is a date spec... */
697 + unsigned long at;
698 + at = approxidate(reflog_base);
699 + read_ref_at(ref, at, -1, sha1, NULL,
700 + NULL, NULL, &base);
701 + }
702 + }
703 +
704 for (i = 0; i < reflog; i++) {
662 - char *name = xmalloc(reflen + 20);
663 - sprintf(name, "%s@{%d}", *av, i);
664 - append_one_rev(name);
705 + char *logmsg, *msg, *m;
706 + unsigned long timestamp;
707 + int tz;
708 +
709 + if (read_ref_at(ref, 0, base+i, sha1, &logmsg,
710 + &timestamp, &tz, NULL)) {
711 + reflog = i;
712 + break;
713 + }
714 + msg = strchr(logmsg, '\t');
715 + if (!msg)
716 + msg = "(none)";
717 + else
718 + msg++;
719 + m = xmalloc(strlen(msg) + 200);
720 + sprintf(m, "(%s) %s",
721 + show_date(timestamp, 0, 1),
722 + msg);
723 + reflog_msg[i] = m;
724 + free(logmsg);
725 + sprintf(nth_desc, "%s@{%d}", *av, base+i);
726 + append_ref(nth_desc, sha1, 1);
727 }
728 }
729 else {
@@ -760,8 +822,14 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
822 printf("%c [%s] ",
823 is_head ? '*' : '!', ref_name[i]);
824 }
763 - /* header lines never need name */
764 - show_one_commit(rev[i], 1);
825 +
826 + if (!reflog) {
827 + /* header lines never need name */
828 + show_one_commit(rev[i], 1);
829 + }
830 + else
831 + puts(reflog_msg[i]);
832 +
833 if (is_head)
834 head_at = i;
835 }
refs.c
+2 -3
@@ -1068,6 +1068,7 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
1068 die("Log %s is corrupt.", logfile);
1069 date = strtoul(lastgt + 1, &tz_c, 10);
1070 if (date <= at_time || cnt == 0) {
1071 + tz = strtoul(tz_c, NULL, 10);
1072 if (msg)
1073 *msg = ref_msg(rec, logend);
1074 if (cutoff_time)
@@ -1075,14 +1076,13 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
1076 if (cutoff_tz)
1077 *cutoff_tz = tz;
1078 if (cutoff_cnt)
1078 - *cutoff_cnt = reccnt;
1079 + *cutoff_cnt = reccnt - 1;
1080 if (lastrec) {
1081 if (get_sha1_hex(lastrec, logged_sha1))
1082 die("Log %s is corrupt.", logfile);
1083 if (get_sha1_hex(rec + 41, sha1))
1084 die("Log %s is corrupt.", logfile);
1085 if (hashcmp(logged_sha1, sha1)) {
1085 - tz = strtoul(tz_c, NULL, 10);
1086 fprintf(stderr,
1087 "warning: Log %s has gap after %s.\n",
1088 logfile, show_rfc2822_date(date, tz));
@@ -1096,7 +1096,6 @@ int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned char *
1096 if (get_sha1_hex(rec + 41, logged_sha1))
1097 die("Log %s is corrupt.", logfile);
1098 if (hashcmp(logged_sha1, sha1)) {
1099 - tz = strtoul(tz_c, NULL, 10);
1099 fprintf(stderr,
1100 "warning: Log %s unexpectedly ended on %s.\n",
1101 logfile, show_rfc2822_date(date, tz));