7 static const char show_branch_usage
[] =
8 "git show-branch [--sparse] [--current] [--all] [--remotes] [--topo-order] [--more=count | --list | --independent | --merge-base ] [--topics] [<refs>...] | --reflog[=n[,b]] <branch>";
9 static const char show_branch_usage_reflog
[] =
10 "--reflog is incompatible with --all, --remotes, --independent or --merge-base";
12 static int showbranch_use_color
= -1;
13 static char column_colors
[][COLOR_MAXLEN
] = {
22 #define COLUMN_COLORS_MAX (ARRAY_SIZE(column_colors))
24 static int default_num
;
25 static int default_alloc
;
26 static const char **default_arg
;
28 #define UNINTERESTING 01
31 #define MAX_REVS (FLAG_BITS - REV_SHIFT) /* should not exceed bits_per_int - REV_SHIFT */
33 #define DEFAULT_REFLOG 4
35 static const char *get_color_code(int idx
)
37 if (showbranch_use_color
)
38 return column_colors
[idx
];
42 static const char *get_color_reset_code(void)
44 if (showbranch_use_color
)
45 return GIT_COLOR_RESET
;
49 static struct commit
*interesting(struct commit_list
*list
)
52 struct commit
*commit
= list
->item
;
54 if (commit
->object
.flags
& UNINTERESTING
)
61 static struct commit
*pop_one_commit(struct commit_list
**list_p
)
63 struct commit
*commit
;
64 struct commit_list
*list
;
73 const char *head_name
; /* which head's ancestor? */
74 int generation
; /* how many parents away from head_name */
77 /* Name the commit as nth generation ancestor of head_name;
78 * we count only the first-parent relationship for naming purposes.
80 static void name_commit(struct commit
*commit
, const char *head_name
, int nth
)
82 struct commit_name
*name
;
84 commit
->util
= xmalloc(sizeof(struct commit_name
));
86 name
->head_name
= head_name
;
87 name
->generation
= nth
;
90 /* Parent is the first parent of the commit. We may name it
91 * as (n+1)th generation ancestor of the same head_name as
92 * commit is nth generation ancestor of, if that generation
93 * number is better than the name it already has.
95 static void name_parent(struct commit
*commit
, struct commit
*parent
)
97 struct commit_name
*commit_name
= commit
->util
;
98 struct commit_name
*parent_name
= parent
->util
;
102 commit_name
->generation
+ 1 < parent_name
->generation
)
103 name_commit(parent
, commit_name
->head_name
,
104 commit_name
->generation
+ 1);
107 static int name_first_parent_chain(struct commit
*c
)
116 p
= c
->parents
->item
;
128 static void name_commits(struct commit_list
*list
,
133 struct commit_list
*cl
;
137 /* First give names to the given heads */
138 for (cl
= list
; cl
; cl
= cl
->next
) {
142 for (i
= 0; i
< num_rev
; i
++) {
144 name_commit(c
, ref_name
[i
], 0);
150 /* Then commits on the first parent ancestry chain */
153 for (cl
= list
; cl
; cl
= cl
->next
) {
154 i
+= name_first_parent_chain(cl
->item
);
158 /* Finally, any unnamed commits */
161 for (cl
= list
; cl
; cl
= cl
->next
) {
162 struct commit_list
*parents
;
163 struct commit_name
*n
;
169 parents
= c
->parents
;
172 struct commit
*p
= parents
->item
;
173 char newname
[1000], *en
;
174 parents
= parents
->next
;
179 switch (n
->generation
) {
181 en
+= sprintf(en
, "%s", n
->head_name
);
184 en
+= sprintf(en
, "%s^", n
->head_name
);
187 en
+= sprintf(en
, "%s~%d",
188 n
->head_name
, n
->generation
);
192 en
+= sprintf(en
, "^");
194 en
+= sprintf(en
, "^%d", nth
);
195 name_commit(p
, xstrdup(newname
), 0);
197 name_first_parent_chain(p
);
203 static int mark_seen(struct commit
*commit
, struct commit_list
**seen_p
)
205 if (!commit
->object
.flags
) {
206 commit_list_insert(commit
, seen_p
);
212 static void join_revs(struct commit_list
**list_p
,
213 struct commit_list
**seen_p
,
214 int num_rev
, int extra
)
216 int all_mask
= ((1u << (REV_SHIFT
+ num_rev
)) - 1);
217 int all_revs
= all_mask
& ~((1u << REV_SHIFT
) - 1);
220 struct commit_list
*parents
;
221 int still_interesting
= !!interesting(*list_p
);
222 struct commit
*commit
= pop_one_commit(list_p
);
223 int flags
= commit
->object
.flags
& all_mask
;
225 if (!still_interesting
&& extra
<= 0)
228 mark_seen(commit
, seen_p
);
229 if ((flags
& all_revs
) == all_revs
)
230 flags
|= UNINTERESTING
;
231 parents
= commit
->parents
;
234 struct commit
*p
= parents
->item
;
235 int this_flag
= p
->object
.flags
;
236 parents
= parents
->next
;
237 if ((this_flag
& flags
) == flags
)
239 if (!p
->object
.parsed
)
241 if (mark_seen(p
, seen_p
) && !still_interesting
)
243 p
->object
.flags
|= flags
;
244 insert_by_date(p
, list_p
);
249 * Postprocess to complete well-poisoning.
251 * At this point we have all the commits we have seen in
252 * seen_p list. Mark anything that can be reached from
253 * uninteresting commits not interesting.
257 struct commit_list
*s
;
258 for (s
= *seen_p
; s
; s
= s
->next
) {
259 struct commit
*c
= s
->item
;
260 struct commit_list
*parents
;
262 if (((c
->object
.flags
& all_revs
) != all_revs
) &&
263 !(c
->object
.flags
& UNINTERESTING
))
266 /* The current commit is either a merge base or
267 * already uninteresting one. Mark its parents
268 * as uninteresting commits _only_ if they are
269 * already parsed. No reason to find new ones
272 parents
= c
->parents
;
274 struct commit
*p
= parents
->item
;
275 parents
= parents
->next
;
276 if (!(p
->object
.flags
& UNINTERESTING
)) {
277 p
->object
.flags
|= UNINTERESTING
;
287 static void show_one_commit(struct commit
*commit
, int no_name
)
289 struct strbuf pretty
= STRBUF_INIT
;
290 const char *pretty_str
= "(unavailable)";
291 struct commit_name
*name
= commit
->util
;
293 if (commit
->object
.parsed
) {
294 pretty_print_commit(CMIT_FMT_ONELINE
, commit
,
295 &pretty
, 0, NULL
, NULL
, 0, 0);
296 pretty_str
= pretty
.buf
;
298 if (!prefixcmp(pretty_str
, "[PATCH] "))
302 if (name
&& name
->head_name
) {
303 printf("[%s", name
->head_name
);
304 if (name
->generation
) {
305 if (name
->generation
== 1)
308 printf("~%d", name
->generation
);
314 find_unique_abbrev(commit
->object
.sha1
, 7));
317 strbuf_release(&pretty
);
320 static char *ref_name
[MAX_REVS
+ 1];
321 static int ref_name_cnt
;
323 static const char *find_digit_prefix(const char *s
, int *v
)
330 '0' <= (ch
= *p
) && ch
<= '9';
332 ver
= ver
* 10 + ch
- '0';
338 static int version_cmp(const char *a
, const char *b
)
343 a
= find_digit_prefix(a
, &va
);
344 b
= find_digit_prefix(b
, &vb
);
351 if ('0' <= ca
&& ca
<= '9')
353 if ('0' <= cb
&& cb
<= '9')
367 static int compare_ref_name(const void *a_
, const void *b_
)
369 const char * const*a
= a_
, * const*b
= b_
;
370 return version_cmp(*a
, *b
);
373 static void sort_ref_range(int bottom
, int top
)
375 qsort(ref_name
+ bottom
, top
- bottom
, sizeof(ref_name
[0]),
379 static int append_ref(const char *refname
, const unsigned char *sha1
,
382 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
389 /* Avoid adding the same thing twice */
390 for (i
= 0; i
< ref_name_cnt
; i
++)
391 if (!strcmp(refname
, ref_name
[i
]))
394 if (MAX_REVS
<= ref_name_cnt
) {
395 warning("ignoring %s; cannot handle more than %d refs",
399 ref_name
[ref_name_cnt
++] = xstrdup(refname
);
400 ref_name
[ref_name_cnt
] = NULL
;
404 static int append_head_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
406 unsigned char tmp
[20];
408 if (prefixcmp(refname
, "refs/heads/"))
410 /* If both heads/foo and tags/foo exists, get_sha1 would
413 if (get_sha1(refname
+ ofs
, tmp
) || hashcmp(tmp
, sha1
))
415 return append_ref(refname
+ ofs
, sha1
, 0);
418 static int append_remote_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
420 unsigned char tmp
[20];
422 if (prefixcmp(refname
, "refs/remotes/"))
424 /* If both heads/foo and tags/foo exists, get_sha1 would
427 if (get_sha1(refname
+ ofs
, tmp
) || hashcmp(tmp
, sha1
))
429 return append_ref(refname
+ ofs
, sha1
, 0);
432 static int append_tag_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
434 if (prefixcmp(refname
, "refs/tags/"))
436 return append_ref(refname
+ 5, sha1
, 0);
439 static const char *match_ref_pattern
= NULL
;
440 static int match_ref_slash
= 0;
441 static int count_slash(const char *s
)
450 static int append_matching_ref(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
452 /* we want to allow pattern hold/<asterisk> to show all
453 * branches under refs/heads/hold/, and v0.99.9? to show
454 * refs/tags/v0.99.9a and friends.
457 int slash
= count_slash(refname
);
458 for (tail
= refname
; *tail
&& match_ref_slash
< slash
; )
463 if (fnmatch(match_ref_pattern
, tail
, 0))
465 if (!prefixcmp(refname
, "refs/heads/"))
466 return append_head_ref(refname
, sha1
, flag
, cb_data
);
467 if (!prefixcmp(refname
, "refs/tags/"))
468 return append_tag_ref(refname
, sha1
, flag
, cb_data
);
469 return append_ref(refname
, sha1
, 0);
472 static void snarf_refs(int head
, int remotes
)
475 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
;
481 for_each_ref(append_remote_ref
, NULL
);
482 sort_ref_range(orig_cnt
, ref_name_cnt
);
486 static int rev_is_head(char *head
, int headlen
, char *name
,
487 unsigned char *head_sha1
, unsigned char *sha1
)
490 (head_sha1
&& sha1
&& hashcmp(head_sha1
, sha1
)))
492 if (!prefixcmp(head
, "refs/heads/"))
494 if (!prefixcmp(name
, "refs/heads/"))
496 else if (!prefixcmp(name
, "heads/"))
498 return !strcmp(head
, name
);
501 static int show_merge_base(struct commit_list
*seen
, int num_rev
)
503 int all_mask
= ((1u << (REV_SHIFT
+ num_rev
)) - 1);
504 int all_revs
= all_mask
& ~((1u << REV_SHIFT
) - 1);
508 struct commit
*commit
= pop_one_commit(&seen
);
509 int flags
= commit
->object
.flags
& all_mask
;
510 if (!(flags
& UNINTERESTING
) &&
511 ((flags
& all_revs
) == all_revs
)) {
512 puts(sha1_to_hex(commit
->object
.sha1
));
514 commit
->object
.flags
|= UNINTERESTING
;
520 static int show_independent(struct commit
**rev
,
523 unsigned int *rev_mask
)
527 for (i
= 0; i
< num_rev
; i
++) {
528 struct commit
*commit
= rev
[i
];
529 unsigned int flag
= rev_mask
[i
];
531 if (commit
->object
.flags
== flag
)
532 puts(sha1_to_hex(commit
->object
.sha1
));
533 commit
->object
.flags
|= UNINTERESTING
;
538 static void append_one_rev(const char *av
)
540 unsigned char revkey
[20];
541 if (!get_sha1(av
, revkey
)) {
542 append_ref(av
, revkey
, 0);
545 if (strchr(av
, '*') || strchr(av
, '?') || strchr(av
, '[')) {
546 /* glob style match */
547 int saved_matches
= ref_name_cnt
;
548 match_ref_pattern
= av
;
549 match_ref_slash
= count_slash(av
);
550 for_each_ref(append_matching_ref
, NULL
);
551 if (saved_matches
== ref_name_cnt
&&
552 ref_name_cnt
< MAX_REVS
)
553 error("no matching refs with %s", av
);
554 if (saved_matches
+ 1 < ref_name_cnt
)
555 sort_ref_range(saved_matches
, ref_name_cnt
);
558 die("bad sha1 reference %s", av
);
561 static int git_show_branch_config(const char *var
, const char *value
, void *cb
)
563 if (!strcmp(var
, "showbranch.default")) {
565 return config_error_nonbool(var
);
566 if (default_alloc
<= default_num
+ 1) {
567 default_alloc
= default_alloc
* 3 / 2 + 20;
568 default_arg
= xrealloc(default_arg
, sizeof *default_arg
* default_alloc
);
570 default_arg
[default_num
++] = xstrdup(value
);
571 default_arg
[default_num
] = NULL
;
575 if (!strcmp(var
, "color.showbranch")) {
576 showbranch_use_color
= git_config_colorbool(var
, value
, -1);
580 return git_color_default_config(var
, value
, cb
);
583 static int omit_in_dense(struct commit
*commit
, struct commit
**rev
, int n
)
585 /* If the commit is tip of the named branches, do not
587 * Otherwise, if it is a merge that is reachable from only one
588 * tip, it is not that interesting.
591 for (i
= 0; i
< n
; i
++)
592 if (rev
[i
] == commit
)
594 flag
= commit
->object
.flags
;
595 for (i
= count
= 0; i
< n
; i
++) {
596 if (flag
& (1u << (i
+ REV_SHIFT
)))
604 static void parse_reflog_param(const char *arg
, int *cnt
, const char **base
)
607 *cnt
= strtoul(arg
, &ep
, 10);
611 die("unrecognized reflog param '%s'", arg
);
615 *cnt
= DEFAULT_REFLOG
;
618 int cmd_show_branch(int ac
, const char **av
, const char *prefix
)
620 struct commit
*rev
[MAX_REVS
], *commit
;
621 char *reflog_msg
[MAX_REVS
];
622 struct commit_list
*list
= NULL
, *seen
= NULL
;
623 unsigned int rev_mask
[MAX_REVS
];
624 int num_rev
, i
, extra
= 0;
625 int all_heads
= 0, all_remotes
= 0;
626 int all_mask
, all_revs
;
631 unsigned char head_sha1
[20];
636 int shown_merge_point
= 0;
637 int with_current_branch
= 0;
642 const char *reflog_base
= NULL
;
644 git_config(git_show_branch_config
, NULL
);
646 if (showbranch_use_color
== -1)
647 showbranch_use_color
= git_use_color_default
;
649 /* If nothing is specified, try the default first */
650 if (ac
== 1 && default_num
) {
651 ac
= default_num
+ 1;
652 av
= default_arg
- 1; /* ick; we would not address av[0] */
655 while (1 < ac
&& av
[1][0] == '-') {
656 const char *arg
= av
[1];
657 if (!strcmp(arg
, "--")) {
661 else if (!strcmp(arg
, "--all") || !strcmp(arg
, "-a"))
662 all_heads
= all_remotes
= 1;
663 else if (!strcmp(arg
, "--remotes") || !strcmp(arg
, "-r"))
665 else if (!strcmp(arg
, "--more"))
667 else if (!strcmp(arg
, "--list"))
669 else if (!strcmp(arg
, "--no-name"))
671 else if (!strcmp(arg
, "--current"))
672 with_current_branch
= 1;
673 else if (!strcmp(arg
, "--sha1-name"))
675 else if (!prefixcmp(arg
, "--more="))
676 extra
= atoi(arg
+ 7);
677 else if (!strcmp(arg
, "--merge-base"))
679 else if (!strcmp(arg
, "--independent"))
681 else if (!strcmp(arg
, "--topo-order"))
683 else if (!strcmp(arg
, "--topics"))
685 else if (!strcmp(arg
, "--sparse"))
687 else if (!strcmp(arg
, "--date-order"))
689 else if (!strcmp(arg
, "--reflog") || !strcmp(arg
, "-g")) {
690 reflog
= DEFAULT_REFLOG
;
692 else if (!prefixcmp(arg
, "--reflog="))
693 parse_reflog_param(arg
+ 9, &reflog
, &reflog_base
);
694 else if (!prefixcmp(arg
, "-g="))
695 parse_reflog_param(arg
+ 3, &reflog
, &reflog_base
);
696 else if (!strcmp(arg
, "--color"))
697 showbranch_use_color
= 1;
698 else if (!strcmp(arg
, "--no-color"))
699 showbranch_use_color
= 0;
701 usage(show_branch_usage
);
706 if (extra
|| reflog
) {
707 /* "listing" mode is incompatible with
708 * independent nor merge-base modes.
710 if (independent
|| merge_base
)
711 usage(show_branch_usage
);
712 if (reflog
&& ((0 < extra
) || all_heads
|| all_remotes
))
714 * Asking for --more in reflog mode does not
715 * make sense. --list is Ok.
717 * Also --all and --remotes do not make sense either.
719 usage(show_branch_usage_reflog
);
722 /* If nothing is specified, show all branches by default */
723 if (ac
+ all_heads
+ all_remotes
== 0)
727 unsigned char sha1
[20];
733 static const char *fake_av
[2];
736 refname
= resolve_ref("HEAD", sha1
, 1, NULL
);
737 fake_av
[0] = xstrdup(refname
);
743 die("--reflog option needs one branch name");
745 if (MAX_REVS
< reflog
)
746 die("Only %d entries can be shown at one time.",
748 if (!dwim_ref(*av
, strlen(*av
), sha1
, &ref
))
749 die("No such ref %s", *av
);
751 /* Has the base been specified? */
754 base
= strtoul(reflog_base
, &ep
, 10);
756 /* Ah, that is a date spec... */
758 at
= approxidate(reflog_base
);
759 read_ref_at(ref
, at
, -1, sha1
, NULL
,
764 for (i
= 0; i
< reflog
; i
++) {
767 unsigned long timestamp
;
770 if (read_ref_at(ref
, 0, base
+i
, sha1
, &logmsg
,
771 ×tamp
, &tz
, NULL
)) {
775 msg
= strchr(logmsg
, '\t');
780 m
= xmalloc(strlen(msg
) + 200);
781 sprintf(m
, "(%s) %s",
782 show_date(timestamp
, tz
, 1),
786 sprintf(nth_desc
, "%s@{%d}", *av
, base
+i
);
787 append_ref(nth_desc
, sha1
, 1);
790 else if (all_heads
+ all_remotes
)
791 snarf_refs(all_heads
, all_remotes
);
799 head_p
= resolve_ref("HEAD", head_sha1
, 1, NULL
);
801 head_len
= strlen(head_p
);
802 memcpy(head
, head_p
, head_len
+ 1);
809 if (with_current_branch
&& head_p
) {
811 for (i
= 0; !has_head
&& i
< ref_name_cnt
; i
++) {
812 /* We are only interested in adding the branch
815 if (rev_is_head(head
,
822 int offset
= !prefixcmp(head
, "refs/heads/") ? 11 : 0;
823 append_one_rev(head
+ offset
);
828 fprintf(stderr
, "No revs to be shown.\n");
832 for (num_rev
= 0; ref_name
[num_rev
]; num_rev
++) {
833 unsigned char revkey
[20];
834 unsigned int flag
= 1u << (num_rev
+ REV_SHIFT
);
836 if (MAX_REVS
<= num_rev
)
837 die("cannot handle more than %d revs.", MAX_REVS
);
838 if (get_sha1(ref_name
[num_rev
], revkey
))
839 die("'%s' is not a valid ref.", ref_name
[num_rev
]);
840 commit
= lookup_commit_reference(revkey
);
842 die("cannot find commit %s (%s)",
843 ref_name
[num_rev
], revkey
);
844 parse_commit(commit
);
845 mark_seen(commit
, &seen
);
847 /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
848 * and so on. REV_SHIFT bits from bit 0 are used for
849 * internal bookkeeping.
851 commit
->object
.flags
|= flag
;
852 if (commit
->object
.flags
== flag
)
853 insert_by_date(commit
, &list
);
854 rev
[num_rev
] = commit
;
856 for (i
= 0; i
< num_rev
; i
++)
857 rev_mask
[i
] = rev
[i
]->object
.flags
;
860 join_revs(&list
, &seen
, num_rev
, extra
);
865 return show_merge_base(seen
, num_rev
);
868 return show_independent(rev
, num_rev
, ref_name
, rev_mask
);
870 /* Show list; --more=-1 means list-only */
871 if (1 < num_rev
|| extra
< 0) {
872 for (i
= 0; i
< num_rev
; i
++) {
874 int is_head
= rev_is_head(head
,
878 rev
[i
]->object
.sha1
);
881 is_head
? '*' : ' ', ref_name
[i
]);
883 for (j
= 0; j
< i
; j
++)
885 printf("%s%c%s [%s] ",
886 get_color_code(i
% COLUMN_COLORS_MAX
),
888 get_color_reset_code(), ref_name
[i
]);
892 /* header lines never need name */
893 show_one_commit(rev
[i
], 1);
902 for (i
= 0; i
< num_rev
; i
++)
910 /* Sort topologically */
911 sort_in_topological_order(&seen
, lifo
);
913 /* Give names to commits */
914 if (!sha1_name
&& !no_name
)
915 name_commits(seen
, rev
, ref_name
, num_rev
);
917 all_mask
= ((1u << (REV_SHIFT
+ num_rev
)) - 1);
918 all_revs
= all_mask
& ~((1u << REV_SHIFT
) - 1);
921 struct commit
*commit
= pop_one_commit(&seen
);
922 int this_flag
= commit
->object
.flags
;
923 int is_merge_point
= ((this_flag
& all_revs
) == all_revs
);
925 shown_merge_point
|= is_merge_point
;
928 int is_merge
= !!(commit
->parents
&&
929 commit
->parents
->next
);
932 (this_flag
& (1u << REV_SHIFT
)))
934 if (dense
&& is_merge
&&
935 omit_in_dense(commit
, rev
, num_rev
))
937 for (i
= 0; i
< num_rev
; i
++) {
939 if (!(this_flag
& (1u << (i
+ REV_SHIFT
))))
943 else if (i
== head_at
)
948 get_color_code(i
% COLUMN_COLORS_MAX
),
949 mark
, get_color_reset_code());
953 show_one_commit(commit
, no_name
);
955 if (shown_merge_point
&& --extra
< 0)