6 #include "parse-options.h"
8 static const char* show_branch_usage[] = {
9 N_("git show-branch [-a|--all] [-r|--remotes] [--topo-order | --date-order]\n"
10 " [--current] [--color[=<when>] | --no-color] [--sparse]\n"
11 " [--more=<n> | --list | --independent | --merge-base]\n"
12 " [--no-name | --sha1-name] [--topics] [(<rev> | <glob>)...]"),
13 N_("git show-branch (-g|--reflog)[=<n>[,<base>]] [--list] [<ref>]"),
17 static int showbranch_use_color = -1;
19 static int default_num;
20 static int default_alloc;
21 static const char **default_arg;
23 #define UNINTERESTING 01
26 #define MAX_REVS (FLAG_BITS - REV_SHIFT) /* should not exceed bits_per_int - REV_SHIFT */
28 #define DEFAULT_REFLOG 4
30 static const char *get_color_code(int idx)
32 if (want_color(showbranch_use_color))
33 return column_colors_ansi[idx % column_colors_ansi_max];
37 static const char *get_color_reset_code(void)
39 if (want_color(showbranch_use_color))
40 return GIT_COLOR_RESET;
44 static struct commit *interesting(struct commit_list *list)
47 struct commit *commit = list->item;
49 if (commit->object.flags & UNINTERESTING)
56 static struct commit *pop_one_commit(struct commit_list **list_p)
58 struct commit *commit;
59 struct commit_list *list;
68 const char *head_name; /* which head's ancestor? */
69 int generation; /* how many parents away from head_name */
72 /* Name the commit as nth generation ancestor of head_name;
73 * we count only the first-parent relationship for naming purposes.
75 static void name_commit(struct commit *commit, const char *head_name, int nth)
77 struct commit_name *name;
79 commit->util = xmalloc(sizeof(struct commit_name));
81 name->head_name = head_name;
82 name->generation = nth;
85 /* Parent is the first parent of the commit. We may name it
86 * as (n+1)th generation ancestor of the same head_name as
87 * commit is nth generation ancestor of, if that generation
88 * number is better than the name it already has.
90 static void name_parent(struct commit *commit, struct commit *parent)
92 struct commit_name *commit_name = commit->util;
93 struct commit_name *parent_name = parent->util;
97 commit_name->generation + 1 < parent_name->generation)
98 name_commit(parent, commit_name->head_name,
99 commit_name->generation + 1);
102 static int name_first_parent_chain(struct commit *c)
111 p = c->parents->item;
123 static void name_commits(struct commit_list *list,
128 struct commit_list *cl;
132 /* First give names to the given heads */
133 for (cl = list; cl; cl = cl->next) {
137 for (i = 0; i < num_rev; i++) {
139 name_commit(c, ref_name[i], 0);
145 /* Then commits on the first parent ancestry chain */
148 for (cl = list; cl; cl = cl->next) {
149 i += name_first_parent_chain(cl->item);
153 /* Finally, any unnamed commits */
156 for (cl = list; cl; cl = cl->next) {
157 struct commit_list *parents;
158 struct commit_name *n;
164 parents = c->parents;
167 struct commit *p = parents->item;
168 struct strbuf newname = STRBUF_INIT;
169 parents = parents->next;
173 switch (n->generation) {
175 strbuf_addstr(&newname, n->head_name);
178 strbuf_addf(&newname, "%s^", n->head_name);
181 strbuf_addf(&newname, "%s~%d",
182 n->head_name, n->generation);
186 strbuf_addch(&newname, '^');
188 strbuf_addf(&newname, "^%d", nth);
189 name_commit(p, strbuf_detach(&newname, NULL), 0);
191 name_first_parent_chain(p);
197 static int mark_seen(struct commit *commit, struct commit_list **seen_p)
199 if (!commit->object.flags) {
200 commit_list_insert(commit, seen_p);
206 static void join_revs(struct commit_list **list_p,
207 struct commit_list **seen_p,
208 int num_rev, int extra)
210 int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
211 int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
214 struct commit_list *parents;
215 int still_interesting = !!interesting(*list_p);
216 struct commit *commit = pop_one_commit(list_p);
217 int flags = commit->object.flags & all_mask;
219 if (!still_interesting && extra <= 0)
222 mark_seen(commit, seen_p);
223 if ((flags & all_revs) == all_revs)
224 flags |= UNINTERESTING;
225 parents = commit->parents;
228 struct commit *p = parents->item;
229 int this_flag = p->object.flags;
230 parents = parents->next;
231 if ((this_flag & flags) == flags)
234 if (mark_seen(p, seen_p) && !still_interesting)
236 p->object.flags |= flags;
237 commit_list_insert_by_date(p, list_p);
242 * Postprocess to complete well-poisoning.
244 * At this point we have all the commits we have seen in
245 * seen_p list. Mark anything that can be reached from
246 * uninteresting commits not interesting.
250 struct commit_list *s;
251 for (s = *seen_p; s; s = s->next) {
252 struct commit *c = s->item;
253 struct commit_list *parents;
255 if (((c->object.flags & all_revs) != all_revs) &&
256 !(c->object.flags & UNINTERESTING))
259 /* The current commit is either a merge base or
260 * already uninteresting one. Mark its parents
261 * as uninteresting commits _only_ if they are
262 * already parsed. No reason to find new ones
265 parents = c->parents;
267 struct commit *p = parents->item;
268 parents = parents->next;
269 if (!(p->object.flags & UNINTERESTING)) {
270 p->object.flags |= UNINTERESTING;
280 static void show_one_commit(struct commit *commit, int no_name)
282 struct strbuf pretty = STRBUF_INIT;
283 const char *pretty_str = "(unavailable)";
284 struct commit_name *name = commit->util;
286 if (commit->object.parsed) {
287 pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty);
288 pretty_str = pretty.buf;
290 if (starts_with(pretty_str, "[PATCH] "))
294 if (name && name->head_name) {
295 printf("[%s", name->head_name);
296 if (name->generation) {
297 if (name->generation == 1)
300 printf("~%d", name->generation);
306 find_unique_abbrev(commit->object.sha1,
310 strbuf_release(&pretty);
313 static char *ref_name[MAX_REVS + 1];
314 static int ref_name_cnt;
316 static const char *find_digit_prefix(const char *s, int *v)
323 '0' <= (ch = *p) && ch <= '9';
325 ver = ver * 10 + ch - '0';
331 static int version_cmp(const char *a, const char *b)
336 a = find_digit_prefix(a, &va);
337 b = find_digit_prefix(b, &vb);
344 if ('0' <= ca && ca <= '9')
346 if ('0' <= cb && cb <= '9')
360 static int compare_ref_name(const void *a_, const void *b_)
362 const char * const*a = a_, * const*b = b_;
363 return version_cmp(*a, *b);
366 static void sort_ref_range(int bottom, int top)
368 qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]),
372 static int append_ref(const char *refname, const unsigned char *sha1,
375 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
382 /* Avoid adding the same thing twice */
383 for (i = 0; i < ref_name_cnt; i++)
384 if (!strcmp(refname, ref_name[i]))
387 if (MAX_REVS <= ref_name_cnt) {
388 warning("ignoring %s; cannot handle more than %d refs",
392 ref_name[ref_name_cnt++] = xstrdup(refname);
393 ref_name[ref_name_cnt] = NULL;
397 static int append_head_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
399 unsigned char tmp[20];
401 if (!starts_with(refname, "refs/heads/"))
403 /* If both heads/foo and tags/foo exists, get_sha1 would
406 if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1))
408 return append_ref(refname + ofs, sha1, 0);
411 static int append_remote_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
413 unsigned char tmp[20];
415 if (!starts_with(refname, "refs/remotes/"))
417 /* If both heads/foo and tags/foo exists, get_sha1 would
420 if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1))
422 return append_ref(refname + ofs, sha1, 0);
425 static int append_tag_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
427 if (!starts_with(refname, "refs/tags/"))
429 return append_ref(refname + 5, sha1, 0);
432 static const char *match_ref_pattern = NULL;
433 static int match_ref_slash = 0;
434 static int count_slash(const char *s)
443 static int append_matching_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
445 /* we want to allow pattern hold/<asterisk> to show all
446 * branches under refs/heads/hold/, and v0.99.9? to show
447 * refs/tags/v0.99.9a and friends.
450 int slash = count_slash(refname);
451 for (tail = refname; *tail && match_ref_slash < slash; )
456 if (wildmatch(match_ref_pattern, tail, 0, NULL))
458 if (starts_with(refname, "refs/heads/"))
459 return append_head_ref(refname, sha1, flag, cb_data);
460 if (starts_with(refname, "refs/tags/"))
461 return append_tag_ref(refname, sha1, flag, cb_data);
462 return append_ref(refname, sha1, 0);
465 static void snarf_refs(int head, int remotes)
468 int orig_cnt = ref_name_cnt;
469 for_each_ref(append_head_ref, NULL);
470 sort_ref_range(orig_cnt, ref_name_cnt);
473 int orig_cnt = ref_name_cnt;
474 for_each_ref(append_remote_ref, NULL);
475 sort_ref_range(orig_cnt, ref_name_cnt);
479 static int rev_is_head(char *head, int headlen, char *name,
480 unsigned char *head_sha1, unsigned char *sha1)
483 (head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
485 if (starts_with(head, "refs/heads/"))
487 if (starts_with(name, "refs/heads/"))
489 else if (starts_with(name, "heads/"))
491 return !strcmp(head, name);
494 static int show_merge_base(struct commit_list *seen, int num_rev)
496 int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
497 int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
501 struct commit *commit = pop_one_commit(&seen);
502 int flags = commit->object.flags & all_mask;
503 if (!(flags & UNINTERESTING) &&
504 ((flags & all_revs) == all_revs)) {
505 puts(sha1_to_hex(commit->object.sha1));
507 commit->object.flags |= UNINTERESTING;
513 static int show_independent(struct commit **rev,
516 unsigned int *rev_mask)
520 for (i = 0; i < num_rev; i++) {
521 struct commit *commit = rev[i];
522 unsigned int flag = rev_mask[i];
524 if (commit->object.flags == flag)
525 puts(sha1_to_hex(commit->object.sha1));
526 commit->object.flags |= UNINTERESTING;
531 static void append_one_rev(const char *av)
533 unsigned char revkey[20];
534 if (!get_sha1(av, revkey)) {
535 append_ref(av, revkey, 0);
538 if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) {
539 /* glob style match */
540 int saved_matches = ref_name_cnt;
541 match_ref_pattern = av;
542 match_ref_slash = count_slash(av);
543 for_each_ref(append_matching_ref, NULL);
544 if (saved_matches == ref_name_cnt &&
545 ref_name_cnt < MAX_REVS)
546 error("no matching refs with %s", av);
547 if (saved_matches + 1 < ref_name_cnt)
548 sort_ref_range(saved_matches, ref_name_cnt);
551 die("bad sha1 reference %s", av);
554 static int git_show_branch_config(const char *var, const char *value, void *cb)
556 if (!strcmp(var, "showbranch.default")) {
558 return config_error_nonbool(var);
560 * default_arg is now passed to parse_options(), so we need to
561 * mimic the real argv a bit better.
565 default_arg = xcalloc(default_alloc, sizeof(*default_arg));
566 default_arg[default_num++] = "show-branch";
567 } else if (default_alloc <= default_num + 1) {
568 default_alloc = default_alloc * 3 / 2 + 20;
569 REALLOC_ARRAY(default_arg, default_alloc);
571 default_arg[default_num++] = xstrdup(value);
572 default_arg[default_num] = NULL;
576 if (!strcmp(var, "color.showbranch")) {
577 showbranch_use_color = git_config_colorbool(var, value);
581 return git_color_default_config(var, value, cb);
584 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
586 /* If the commit is tip of the named branches, do not
588 * Otherwise, if it is a merge that is reachable from only one
589 * tip, it is not that interesting.
592 for (i = 0; i < n; i++)
593 if (rev[i] == commit)
595 flag = commit->object.flags;
596 for (i = count = 0; i < n; i++) {
597 if (flag & (1u << (i + REV_SHIFT)))
605 static int reflog = 0;
607 static int parse_reflog_param(const struct option *opt, const char *arg,
611 const char **base = (const char **)opt->value;
614 reflog = strtoul(arg, &ep, 10);
618 return error("unrecognized reflog param '%s'", arg);
622 reflog = DEFAULT_REFLOG;
626 int cmd_show_branch(int ac, const char **av, const char *prefix)
628 struct commit *rev[MAX_REVS], *commit;
629 char *reflog_msg[MAX_REVS];
630 struct commit_list *list = NULL, *seen = NULL;
631 unsigned int rev_mask[MAX_REVS];
632 int num_rev, i, extra = 0;
633 int all_heads = 0, all_remotes = 0;
634 int all_mask, all_revs;
635 enum rev_sort_order sort_order = REV_SORT_IN_GRAPH_ORDER;
639 unsigned char head_sha1[20];
644 int shown_merge_point = 0;
645 int with_current_branch = 0;
649 const char *reflog_base = NULL;
650 struct option builtin_show_branch_options[] = {
651 OPT_BOOL('a', "all", &all_heads,
652 N_("show remote-tracking and local branches")),
653 OPT_BOOL('r', "remotes", &all_remotes,
654 N_("show remote-tracking branches")),
655 OPT__COLOR(&showbranch_use_color,
656 N_("color '*!+-' corresponding to the branch")),
657 { OPTION_INTEGER, 0, "more", &extra, N_("n"),
658 N_("show <n> more commits after the common ancestor"),
659 PARSE_OPT_OPTARG, NULL, (intptr_t)1 },
660 OPT_SET_INT(0, "list", &extra, N_("synonym to more=-1"), -1),
661 OPT_BOOL(0, "no-name", &no_name, N_("suppress naming strings")),
662 OPT_BOOL(0, "current", &with_current_branch,
663 N_("include the current branch")),
664 OPT_BOOL(0, "sha1-name", &sha1_name,
665 N_("name commits with their object names")),
666 OPT_BOOL(0, "merge-base", &merge_base,
667 N_("show possible merge bases")),
668 OPT_BOOL(0, "independent", &independent,
669 N_("show refs unreachable from any other ref")),
670 OPT_SET_INT(0, "topo-order", &sort_order,
671 N_("show commits in topological order"),
672 REV_SORT_IN_GRAPH_ORDER),
673 OPT_BOOL(0, "topics", &topics,
674 N_("show only commits not on the first branch")),
675 OPT_SET_INT(0, "sparse", &dense,
676 N_("show merges reachable from only one tip"), 0),
677 OPT_SET_INT(0, "date-order", &sort_order,
678 N_("topologically sort, maintaining date order "
680 REV_SORT_BY_COMMIT_DATE),
681 { OPTION_CALLBACK, 'g', "reflog", &reflog_base, N_("<n>[,<base>]"),
682 N_("show <n> most recent ref-log entries starting at "
684 PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP,
685 parse_reflog_param },
689 git_config(git_show_branch_config, NULL);
691 /* If nothing is specified, try the default first */
692 if (ac == 1 && default_num) {
697 ac = parse_options(ac, av, prefix, builtin_show_branch_options,
698 show_branch_usage, PARSE_OPT_STOP_AT_NON_OPTION);
702 if (extra || reflog) {
703 /* "listing" mode is incompatible with
704 * independent nor merge-base modes.
706 if (independent || merge_base)
707 usage_with_options(show_branch_usage,
708 builtin_show_branch_options);
709 if (reflog && ((0 < extra) || all_heads || all_remotes))
711 * Asking for --more in reflog mode does not
712 * make sense. --list is Ok.
714 * Also --all and --remotes do not make sense either.
716 die("--reflog is incompatible with --all, --remotes, "
717 "--independent or --merge-base");
720 /* If nothing is specified, show all branches by default */
721 if (ac + all_heads + all_remotes == 0)
725 unsigned char sha1[20];
729 unsigned int flags = 0;
732 static const char *fake_av[2];
734 fake_av[0] = resolve_refdup("HEAD",
742 die("--reflog option needs one branch name");
744 if (MAX_REVS < reflog)
745 die("Only %d entries can be shown at one time.",
747 if (!dwim_ref(*av, strlen(*av), sha1, &ref))
748 die("No such ref %s", *av);
750 /* Has the base been specified? */
753 base = strtoul(reflog_base, &ep, 10);
755 /* Ah, that is a date spec... */
757 at = approxidate(reflog_base);
758 read_ref_at(ref, flags, at, -1, sha1, NULL,
763 for (i = 0; i < reflog; i++) {
766 unsigned long timestamp;
769 if (read_ref_at(ref, flags, 0, base+i, sha1, &logmsg,
770 ×tamp, &tz, NULL)) {
774 msg = strchr(logmsg, '\t');
779 reflog_msg[i] = xstrfmt("(%s) %s",
780 show_date(timestamp, tz, 1),
783 sprintf(nth_desc, "%s@{%d}", *av, base+i);
784 append_ref(nth_desc, sha1, 1);
788 else if (all_heads + all_remotes)
789 snarf_refs(all_heads, all_remotes);
797 head_p = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
800 head_len = strlen(head_p);
801 memcpy(head, head_p, head_len + 1);
808 if (with_current_branch && head_p) {
810 for (i = 0; !has_head && i < ref_name_cnt; i++) {
811 /* We are only interested in adding the branch
814 if (rev_is_head(head,
821 int offset = starts_with(head, "refs/heads/") ? 11 : 0;
822 append_one_rev(head + offset);
827 fprintf(stderr, "No revs to be shown.\n");
831 for (num_rev = 0; ref_name[num_rev]; num_rev++) {
832 unsigned char revkey[20];
833 unsigned int flag = 1u << (num_rev + REV_SHIFT);
835 if (MAX_REVS <= num_rev)
836 die("cannot handle more than %d revs.", MAX_REVS);
837 if (get_sha1(ref_name[num_rev], revkey))
838 die("'%s' is not a valid ref.", ref_name[num_rev]);
839 commit = lookup_commit_reference(revkey);
841 die("cannot find commit %s (%s)",
842 ref_name[num_rev], revkey);
843 parse_commit(commit);
844 mark_seen(commit, &seen);
846 /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
847 * and so on. REV_SHIFT bits from bit 0 are used for
848 * internal bookkeeping.
850 commit->object.flags |= flag;
851 if (commit->object.flags == flag)
852 commit_list_insert_by_date(commit, &list);
853 rev[num_rev] = commit;
855 for (i = 0; i < num_rev; i++)
856 rev_mask[i] = rev[i]->object.flags;
859 join_revs(&list, &seen, num_rev, extra);
861 commit_list_sort_by_date(&seen);
864 return show_merge_base(seen, num_rev);
867 return show_independent(rev, num_rev, ref_name, rev_mask);
869 /* Show list; --more=-1 means list-only */
870 if (1 < num_rev || extra < 0) {
871 for (i = 0; i < num_rev; i++) {
873 int is_head = rev_is_head(head,
877 rev[i]->object.sha1);
880 is_head ? '*' : ' ', ref_name[i]);
882 for (j = 0; j < i; j++)
884 printf("%s%c%s [%s] ",
887 get_color_reset_code(), ref_name[i]);
891 /* header lines never need name */
892 show_one_commit(rev[i], 1);
901 for (i = 0; i < num_rev; i++)
909 /* Sort topologically */
910 sort_in_topological_order(&seen, sort_order);
912 /* Give names to commits */
913 if (!sha1_name && !no_name)
914 name_commits(seen, rev, ref_name, num_rev);
916 all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
917 all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
920 struct commit *commit = pop_one_commit(&seen);
921 int this_flag = commit->object.flags;
922 int is_merge_point = ((this_flag & all_revs) == all_revs);
924 shown_merge_point |= is_merge_point;
927 int is_merge = !!(commit->parents &&
928 commit->parents->next);
931 (this_flag & (1u << REV_SHIFT)))
933 if (dense && is_merge &&
934 omit_in_dense(commit, rev, num_rev))
936 for (i = 0; i < num_rev; i++) {
938 if (!(this_flag & (1u << (i + REV_SHIFT))))
942 else if (i == head_at)
948 mark, get_color_reset_code());
952 show_one_commit(commit, no_name);
954 if (shown_merge_point && --extra < 0)