8 #include "parse-options.h"
10 #include "commit-slab.h"
13 static const char* show_branch_usage
[] = {
14 N_("git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\n"
15 " [--current] [--color[=<when>] | --no-color] [--sparse]\n"
16 " [--more=<n> | --list | --independent | --merge-base]\n"
17 " [--no-name | --sha1-name] [--topics]\n"
18 " [(<rev> | <glob>)...]"),
19 N_("git show-branch (-g | --reflog)[=<n>[,<base>]] [--list] [<ref>]"),
23 static int showbranch_use_color
= -1;
25 static struct strvec default_args
= STRVEC_INIT
;
28 * TODO: convert this use of commit->object.flags to commit-slab
29 * instead to store a pointer to ref name directly. Then use the same
30 * UNINTERESTING definition from revision.h here.
32 #define UNINTERESTING 01
35 #define MAX_REVS (FLAG_BITS - REV_SHIFT) /* should not exceed bits_per_int - REV_SHIFT */
37 #define DEFAULT_REFLOG 4
39 static const char *get_color_code(int idx
)
41 if (want_color(showbranch_use_color
))
42 return column_colors_ansi
[idx
% column_colors_ansi_max
];
46 static const char *get_color_reset_code(void)
48 if (want_color(showbranch_use_color
))
49 return GIT_COLOR_RESET
;
53 static struct commit
*interesting(struct commit_list
*list
)
56 struct commit
*commit
= list
->item
;
58 if (commit
->object
.flags
& UNINTERESTING
)
66 const char *head_name
; /* which head's ancestor? */
67 int generation
; /* how many parents away from head_name */
70 define_commit_slab(commit_name_slab
, struct commit_name
*);
71 static struct commit_name_slab name_slab
;
73 static struct commit_name
*commit_to_name(struct commit
*commit
)
75 return *commit_name_slab_at(&name_slab
, commit
);
79 /* Name the commit as nth generation ancestor of head_name;
80 * we count only the first-parent relationship for naming purposes.
82 static void name_commit(struct commit
*commit
, const char *head_name
, int nth
)
84 struct commit_name
*name
;
86 name
= *commit_name_slab_at(&name_slab
, commit
);
88 name
= xmalloc(sizeof(*name
));
89 *commit_name_slab_at(&name_slab
, commit
) = name
;
91 name
->head_name
= head_name
;
92 name
->generation
= nth
;
95 /* Parent is the first parent of the commit. We may name it
96 * as (n+1)th generation ancestor of the same head_name as
97 * commit is nth generation ancestor of, if that generation
98 * number is better than the name it already has.
100 static void name_parent(struct commit
*commit
, struct commit
*parent
)
102 struct commit_name
*commit_name
= commit_to_name(commit
);
103 struct commit_name
*parent_name
= commit_to_name(parent
);
107 commit_name
->generation
+ 1 < parent_name
->generation
)
108 name_commit(parent
, commit_name
->head_name
,
109 commit_name
->generation
+ 1);
112 static int name_first_parent_chain(struct commit
*c
)
117 if (!commit_to_name(c
))
121 p
= c
->parents
->item
;
122 if (!commit_to_name(p
)) {
133 static void name_commits(struct commit_list
*list
,
138 struct commit_list
*cl
;
142 /* First give names to the given heads */
143 for (cl
= list
; cl
; cl
= cl
->next
) {
145 if (commit_to_name(c
))
147 for (i
= 0; i
< num_rev
; i
++) {
149 name_commit(c
, ref_name
[i
], 0);
155 /* Then commits on the first parent ancestry chain */
158 for (cl
= list
; cl
; cl
= cl
->next
) {
159 i
+= name_first_parent_chain(cl
->item
);
163 /* Finally, any unnamed commits */
166 for (cl
= list
; cl
; cl
= cl
->next
) {
167 struct commit_list
*parents
;
168 struct commit_name
*n
;
171 if (!commit_to_name(c
))
173 n
= commit_to_name(c
);
174 parents
= c
->parents
;
177 struct commit
*p
= parents
->item
;
178 struct strbuf newname
= STRBUF_INIT
;
179 parents
= parents
->next
;
181 if (commit_to_name(p
))
183 switch (n
->generation
) {
185 strbuf_addstr(&newname
, n
->head_name
);
188 strbuf_addf(&newname
, "%s^", n
->head_name
);
191 strbuf_addf(&newname
, "%s~%d",
192 n
->head_name
, n
->generation
);
196 strbuf_addch(&newname
, '^');
198 strbuf_addf(&newname
, "^%d", nth
);
199 name_commit(p
, strbuf_detach(&newname
, NULL
), 0);
201 name_first_parent_chain(p
);
207 static int mark_seen(struct commit
*commit
, struct commit_list
**seen_p
)
209 if (!commit
->object
.flags
) {
210 commit_list_insert(commit
, seen_p
);
216 static void join_revs(struct commit_list
**list_p
,
217 struct commit_list
**seen_p
,
218 int num_rev
, int extra
)
220 int all_mask
= ((1u << (REV_SHIFT
+ num_rev
)) - 1);
221 int all_revs
= all_mask
& ~((1u << REV_SHIFT
) - 1);
224 struct commit_list
*parents
;
225 int still_interesting
= !!interesting(*list_p
);
226 struct commit
*commit
= pop_commit(list_p
);
227 int flags
= commit
->object
.flags
& all_mask
;
229 if (!still_interesting
&& extra
<= 0)
232 mark_seen(commit
, seen_p
);
233 if ((flags
& all_revs
) == all_revs
)
234 flags
|= UNINTERESTING
;
235 parents
= commit
->parents
;
238 struct commit
*p
= parents
->item
;
239 int this_flag
= p
->object
.flags
;
240 parents
= parents
->next
;
241 if ((this_flag
& flags
) == flags
)
244 if (mark_seen(p
, seen_p
) && !still_interesting
)
246 p
->object
.flags
|= flags
;
247 commit_list_insert_by_date(p
, list_p
);
252 * Postprocess to complete well-poisoning.
254 * At this point we have all the commits we have seen in
255 * seen_p list. Mark anything that can be reached from
256 * uninteresting commits not interesting.
260 struct commit_list
*s
;
261 for (s
= *seen_p
; s
; s
= s
->next
) {
262 struct commit
*c
= s
->item
;
263 struct commit_list
*parents
;
265 if (((c
->object
.flags
& all_revs
) != all_revs
) &&
266 !(c
->object
.flags
& UNINTERESTING
))
269 /* The current commit is either a merge base or
270 * already uninteresting one. Mark its parents
271 * as uninteresting commits _only_ if they are
272 * already parsed. No reason to find new ones
275 parents
= c
->parents
;
277 struct commit
*p
= parents
->item
;
278 parents
= parents
->next
;
279 if (!(p
->object
.flags
& UNINTERESTING
)) {
280 p
->object
.flags
|= UNINTERESTING
;
290 static void show_one_commit(struct commit
*commit
, int no_name
)
292 struct strbuf pretty
= STRBUF_INIT
;
293 const char *pretty_str
= "(unavailable)";
294 struct commit_name
*name
= commit_to_name(commit
);
296 if (commit
->object
.parsed
) {
297 pp_commit_easy(CMIT_FMT_ONELINE
, commit
, &pretty
);
298 pretty_str
= pretty
.buf
;
300 skip_prefix(pretty_str
, "[PATCH] ", &pretty_str
);
303 if (name
&& name
->head_name
) {
304 printf("[%s", name
->head_name
);
305 if (name
->generation
) {
306 if (name
->generation
== 1)
309 printf("~%d", name
->generation
);
315 find_unique_abbrev(&commit
->object
.oid
,
319 strbuf_release(&pretty
);
322 static char *ref_name
[MAX_REVS
+ 1];
323 static int ref_name_cnt
;
325 static const char *find_digit_prefix(const char *s
, int *v
)
332 '0' <= (ch
= *p
) && ch
<= '9';
334 ver
= ver
* 10 + ch
- '0';
340 static int version_cmp(const char *a
, const char *b
)
345 a
= find_digit_prefix(a
, &va
);
346 b
= find_digit_prefix(b
, &vb
);
353 if ('0' <= ca
&& ca
<= '9')
355 if ('0' <= cb
&& cb
<= '9')
369 static int compare_ref_name(const void *a_
, const void *b_
)
371 const char * const*a
= a_
, * const*b
= b_
;
372 return version_cmp(*a
, *b
);
375 static void sort_ref_range(int bottom
, int top
)
377 QSORT(ref_name
+ bottom
, top
- bottom
, compare_ref_name
);
380 static int append_ref(const char *refname
, const struct object_id
*oid
,
383 struct commit
*commit
= lookup_commit_reference_gently(the_repository
,
391 /* Avoid adding the same thing twice */
392 for (i
= 0; i
< ref_name_cnt
; i
++)
393 if (!strcmp(refname
, ref_name
[i
]))
396 if (MAX_REVS
<= ref_name_cnt
) {
397 warning(Q_("ignoring %s; cannot handle more than %d ref",
398 "ignoring %s; cannot handle more than %d refs",
399 MAX_REVS
), refname
, MAX_REVS
);
402 ref_name
[ref_name_cnt
++] = xstrdup(refname
);
403 ref_name
[ref_name_cnt
] = NULL
;
407 static int append_head_ref(const char *refname
, const struct object_id
*oid
,
408 int flag UNUSED
, void *cb_data UNUSED
)
410 struct object_id tmp
;
412 if (!starts_with(refname
, "refs/heads/"))
414 /* If both heads/foo and tags/foo exists, get_sha1 would
417 if (get_oid(refname
+ ofs
, &tmp
) || !oideq(&tmp
, oid
))
419 return append_ref(refname
+ ofs
, oid
, 0);
422 static int append_remote_ref(const char *refname
, const struct object_id
*oid
,
423 int flag UNUSED
, void *cb_data UNUSED
)
425 struct object_id tmp
;
427 if (!starts_with(refname
, "refs/remotes/"))
429 /* If both heads/foo and tags/foo exists, get_sha1 would
432 if (get_oid(refname
+ ofs
, &tmp
) || !oideq(&tmp
, oid
))
434 return append_ref(refname
+ ofs
, oid
, 0);
437 static int append_tag_ref(const char *refname
, const struct object_id
*oid
,
438 int flag UNUSED
, void *cb_data UNUSED
)
440 if (!starts_with(refname
, "refs/tags/"))
442 return append_ref(refname
+ 5, oid
, 0);
445 static const char *match_ref_pattern
= NULL
;
446 static int match_ref_slash
= 0;
448 static int append_matching_ref(const char *refname
, const struct object_id
*oid
,
449 int flag
, void *cb_data
)
451 /* we want to allow pattern hold/<asterisk> to show all
452 * branches under refs/heads/hold/, and v0.99.9? to show
453 * refs/tags/v0.99.9a and friends.
456 int slash
= count_slashes(refname
);
457 for (tail
= refname
; *tail
&& match_ref_slash
< slash
; )
462 if (wildmatch(match_ref_pattern
, tail
, 0))
464 if (starts_with(refname
, "refs/heads/"))
465 return append_head_ref(refname
, oid
, flag
, cb_data
);
466 if (starts_with(refname
, "refs/tags/"))
467 return append_tag_ref(refname
, oid
, flag
, cb_data
);
468 return append_ref(refname
, oid
, 0);
471 static void snarf_refs(int head
, int remotes
)
474 int orig_cnt
= ref_name_cnt
;
476 for_each_ref(append_head_ref
, NULL
);
477 sort_ref_range(orig_cnt
, ref_name_cnt
);
480 int orig_cnt
= ref_name_cnt
;
482 for_each_ref(append_remote_ref
, NULL
);
483 sort_ref_range(orig_cnt
, ref_name_cnt
);
487 static int rev_is_head(const char *head
, const char *name
)
491 skip_prefix(head
, "refs/heads/", &head
);
492 if (!skip_prefix(name
, "refs/heads/", &name
))
493 skip_prefix(name
, "heads/", &name
);
494 return !strcmp(head
, name
);
497 static int show_merge_base(struct commit_list
*seen
, int num_rev
)
499 int all_mask
= ((1u << (REV_SHIFT
+ num_rev
)) - 1);
500 int all_revs
= all_mask
& ~((1u << REV_SHIFT
) - 1);
504 struct commit
*commit
= pop_commit(&seen
);
505 int flags
= commit
->object
.flags
& all_mask
;
506 if (!(flags
& UNINTERESTING
) &&
507 ((flags
& all_revs
) == all_revs
)) {
508 puts(oid_to_hex(&commit
->object
.oid
));
510 commit
->object
.flags
|= UNINTERESTING
;
516 static int show_independent(struct commit
**rev
,
518 unsigned int *rev_mask
)
522 for (i
= 0; i
< num_rev
; i
++) {
523 struct commit
*commit
= rev
[i
];
524 unsigned int flag
= rev_mask
[i
];
526 if (commit
->object
.flags
== flag
)
527 puts(oid_to_hex(&commit
->object
.oid
));
528 commit
->object
.flags
|= UNINTERESTING
;
533 static void append_one_rev(const char *av
)
535 struct object_id revkey
;
536 if (!get_oid(av
, &revkey
)) {
537 append_ref(av
, &revkey
, 0);
540 if (strpbrk(av
, "*?[")) {
541 /* glob style match */
542 int saved_matches
= ref_name_cnt
;
544 match_ref_pattern
= av
;
545 match_ref_slash
= count_slashes(av
);
546 for_each_ref(append_matching_ref
, NULL
);
547 if (saved_matches
== ref_name_cnt
&&
548 ref_name_cnt
< MAX_REVS
)
549 error(_("no matching refs with %s"), av
);
550 sort_ref_range(saved_matches
, ref_name_cnt
);
553 die("bad sha1 reference %s", av
);
556 static int git_show_branch_config(const char *var
, const char *value
, void *cb
)
558 if (!strcmp(var
, "showbranch.default")) {
560 return config_error_nonbool(var
);
562 * default_arg is now passed to parse_options(), so we need to
563 * mimic the real argv a bit better.
565 if (!default_args
.nr
)
566 strvec_push(&default_args
, "show-branch");
567 strvec_push(&default_args
, value
);
571 if (!strcmp(var
, "color.showbranch")) {
572 showbranch_use_color
= git_config_colorbool(var
, value
);
576 return git_color_default_config(var
, value
, cb
);
579 static int omit_in_dense(struct commit
*commit
, struct commit
**rev
, int n
)
581 /* If the commit is tip of the named branches, do not
583 * Otherwise, if it is a merge that is reachable from only one
584 * tip, it is not that interesting.
587 for (i
= 0; i
< n
; i
++)
588 if (rev
[i
] == commit
)
590 flag
= commit
->object
.flags
;
591 for (i
= count
= 0; i
< n
; i
++) {
592 if (flag
& (1u << (i
+ REV_SHIFT
)))
600 static int reflog
= 0;
602 static int parse_reflog_param(const struct option
*opt
, const char *arg
,
606 const char **base
= (const char **)opt
->value
;
607 BUG_ON_OPT_NEG(unset
);
610 reflog
= strtoul(arg
, &ep
, 10);
614 return error("unrecognized reflog param '%s'", arg
);
618 reflog
= DEFAULT_REFLOG
;
622 int cmd_show_branch(int ac
, const char **av
, const char *prefix
)
624 struct commit
*rev
[MAX_REVS
], *commit
;
625 char *reflog_msg
[MAX_REVS
];
626 struct commit_list
*list
= NULL
, *seen
= NULL
;
627 unsigned int rev_mask
[MAX_REVS
];
628 int num_rev
, i
, extra
= 0;
629 int all_heads
= 0, all_remotes
= 0;
630 int all_mask
, all_revs
;
631 enum rev_sort_order sort_order
= REV_SORT_IN_GRAPH_ORDER
;
633 struct object_id head_oid
;
638 int shown_merge_point
= 0;
639 int with_current_branch
= 0;
643 const char *reflog_base
= NULL
;
644 struct option builtin_show_branch_options
[] = {
645 OPT_BOOL('a', "all", &all_heads
,
646 N_("show remote-tracking and local branches")),
647 OPT_BOOL('r', "remotes", &all_remotes
,
648 N_("show remote-tracking branches")),
649 OPT__COLOR(&showbranch_use_color
,
650 N_("color '*!+-' corresponding to the branch")),
651 { OPTION_INTEGER
, 0, "more", &extra
, N_("n"),
652 N_("show <n> more commits after the common ancestor"),
653 PARSE_OPT_OPTARG
, NULL
, (intptr_t)1 },
654 OPT_SET_INT(0, "list", &extra
, N_("synonym to more=-1"), -1),
655 OPT_BOOL(0, "no-name", &no_name
, N_("suppress naming strings")),
656 OPT_BOOL(0, "current", &with_current_branch
,
657 N_("include the current branch")),
658 OPT_BOOL(0, "sha1-name", &sha1_name
,
659 N_("name commits with their object names")),
660 OPT_BOOL(0, "merge-base", &merge_base
,
661 N_("show possible merge bases")),
662 OPT_BOOL(0, "independent", &independent
,
663 N_("show refs unreachable from any other ref")),
664 OPT_SET_INT(0, "topo-order", &sort_order
,
665 N_("show commits in topological order"),
666 REV_SORT_IN_GRAPH_ORDER
),
667 OPT_BOOL(0, "topics", &topics
,
668 N_("show only commits not on the first branch")),
669 OPT_SET_INT(0, "sparse", &dense
,
670 N_("show merges reachable from only one tip"), 0),
671 OPT_SET_INT(0, "date-order", &sort_order
,
672 N_("topologically sort, maintaining date order "
674 REV_SORT_BY_COMMIT_DATE
),
675 OPT_CALLBACK_F('g', "reflog", &reflog_base
, N_("<n>[,<base>]"),
676 N_("show <n> most recent ref-log entries starting at "
678 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
,
683 init_commit_name_slab(&name_slab
);
685 git_config(git_show_branch_config
, NULL
);
687 /* If nothing is specified, try the default first */
688 if (ac
== 1 && default_args
.nr
) {
689 ac
= default_args
.nr
;
693 ac
= parse_options(ac
, av
, prefix
, builtin_show_branch_options
,
694 show_branch_usage
, PARSE_OPT_STOP_AT_NON_OPTION
);
698 if (extra
|| reflog
) {
699 /* "listing" mode is incompatible with
700 * independent nor merge-base modes.
702 if (independent
|| merge_base
)
703 usage_with_options(show_branch_usage
,
704 builtin_show_branch_options
);
705 if (reflog
&& ((0 < extra
) || all_heads
|| all_remotes
))
707 * Asking for --more in reflog mode does not
708 * make sense. --list is Ok.
710 * Also --all and --remotes do not make sense either.
712 die(_("options '%s' and '%s' cannot be used together"), "--reflog",
713 "--all/--remotes/--independent/--merge-base");
716 if (with_current_branch
&& reflog
)
717 die(_("options '%s' and '%s' cannot be used together"),
718 "--reflog", "--current");
720 /* If nothing is specified, show all branches by default */
721 if (ac
<= topics
&& all_heads
+ all_remotes
== 0)
725 struct object_id oid
;
728 unsigned int flags
= 0;
731 static const char *fake_av
[2];
733 fake_av
[0] = resolve_refdup("HEAD",
734 RESOLVE_REF_READING
, &oid
,
740 die(_("no branches given, and HEAD is not valid"));
743 die(_("--reflog option needs one branch name"));
745 if (MAX_REVS
< reflog
)
746 die(Q_("only %d entry can be shown at one time.",
747 "only %d entries can be shown at one time.",
748 MAX_REVS
), MAX_REVS
);
749 if (!dwim_ref(*av
, strlen(*av
), &oid
, &ref
, 0))
750 die(_("no such ref %s"), *av
);
752 /* Has the base been specified? */
755 base
= strtoul(reflog_base
, &ep
, 10);
757 /* Ah, that is a date spec... */
759 at
= approxidate(reflog_base
);
760 read_ref_at(get_main_ref_store(the_repository
),
761 ref
, flags
, at
, -1, &oid
, NULL
,
766 for (i
= 0; i
< reflog
; i
++) {
771 timestamp_t timestamp
;
774 if (read_ref_at(get_main_ref_store(the_repository
),
775 ref
, flags
, 0, base
+ i
, &oid
, &logmsg
,
776 ×tamp
, &tz
, NULL
)) {
781 end
= strchr(logmsg
, '\n');
785 msg
= (*logmsg
== '\0') ? "(none)" : logmsg
;
786 reflog_msg
[i
] = xstrfmt("(%s) %s",
787 show_date(timestamp
, tz
,
788 DATE_MODE(RELATIVE
)),
792 nth_desc
= xstrfmt("%s@{%d}", *av
, base
+i
);
793 append_ref(nth_desc
, &oid
, 1);
803 if (all_heads
+ all_remotes
)
804 snarf_refs(all_heads
, all_remotes
);
807 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
,
810 if (with_current_branch
&& head
) {
812 for (i
= 0; !has_head
&& i
< ref_name_cnt
; i
++) {
813 /* We are only interested in adding the branch
816 if (rev_is_head(head
, ref_name
[i
]))
820 const char *name
= head
;
821 skip_prefix(name
, "refs/heads/", &name
);
822 append_one_rev(name
);
827 fprintf(stderr
, "No revs to be shown.\n");
831 for (num_rev
= 0; ref_name
[num_rev
]; num_rev
++) {
832 struct object_id revkey
;
833 unsigned int flag
= 1u << (num_rev
+ REV_SHIFT
);
835 if (MAX_REVS
<= num_rev
)
836 die(Q_("cannot handle more than %d rev.",
837 "cannot handle more than %d revs.",
838 MAX_REVS
), MAX_REVS
);
839 if (get_oid(ref_name
[num_rev
], &revkey
))
840 die(_("'%s' is not a valid ref."), ref_name
[num_rev
]);
841 commit
= lookup_commit_reference(the_repository
, &revkey
);
843 die(_("cannot find commit %s (%s)"),
844 ref_name
[num_rev
], oid_to_hex(&revkey
));
845 parse_commit(commit
);
846 mark_seen(commit
, &seen
);
848 /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
849 * and so on. REV_SHIFT bits from bit 0 are used for
850 * internal bookkeeping.
852 commit
->object
.flags
|= flag
;
853 if (commit
->object
.flags
== flag
)
854 commit_list_insert_by_date(commit
, &list
);
855 rev
[num_rev
] = commit
;
857 for (i
= 0; i
< num_rev
; i
++)
858 rev_mask
[i
] = rev
[i
]->object
.flags
;
861 join_revs(&list
, &seen
, num_rev
, extra
);
863 commit_list_sort_by_date(&seen
);
866 return show_merge_base(seen
, num_rev
);
869 return show_independent(rev
, num_rev
, rev_mask
);
871 /* Show list; --more=-1 means list-only */
872 if (1 < num_rev
|| extra
< 0) {
873 for (i
= 0; i
< num_rev
; i
++) {
875 int is_head
= rev_is_head(head
, ref_name
[i
]) &&
876 oideq(&head_oid
, &rev
[i
]->object
.oid
);
879 is_head
? '*' : ' ', ref_name
[i
]);
881 for (j
= 0; j
< i
; j
++)
883 printf("%s%c%s [%s] ",
886 get_color_reset_code(), ref_name
[i
]);
890 /* header lines never need name */
891 show_one_commit(rev
[i
], 1);
900 for (i
= 0; i
< num_rev
; i
++)
908 /* Sort topologically */
909 sort_in_topological_order(&seen
, sort_order
);
911 /* Give names to commits */
912 if (!sha1_name
&& !no_name
)
913 name_commits(seen
, rev
, ref_name
, num_rev
);
915 all_mask
= ((1u << (REV_SHIFT
+ num_rev
)) - 1);
916 all_revs
= all_mask
& ~((1u << REV_SHIFT
) - 1);
919 struct commit
*commit
= pop_commit(&seen
);
920 int this_flag
= commit
->object
.flags
;
921 int is_merge_point
= ((this_flag
& all_revs
) == all_revs
);
923 shown_merge_point
|= is_merge_point
;
926 int is_merge
= !!(commit
->parents
&&
927 commit
->parents
->next
);
930 (this_flag
& (1u << REV_SHIFT
)))
932 if (dense
&& is_merge
&&
933 omit_in_dense(commit
, rev
, num_rev
))
935 for (i
= 0; i
< num_rev
; i
++) {
937 if (!(this_flag
& (1u << (i
+ REV_SHIFT
))))
941 else if (i
== head_at
)
950 mark
, get_color_reset_code());
954 show_one_commit(commit
, no_name
);
956 if (shown_merge_point
&& --extra
< 0)