t0410: make test description clearer
[git/gitster.git] / builtin / show-branch.c
blobcd6bdf63bc103de7a4bffe61416e720f80cf3482
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "hash.h"
7 #include "hex.h"
8 #include "pretty.h"
9 #include "refs.h"
10 #include "color.h"
11 #include "strvec.h"
12 #include "object-name.h"
13 #include "parse-options.h"
15 #include "dir.h"
16 #include "commit-slab.h"
17 #include "date.h"
18 #include "wildmatch.h"
20 static const char* show_branch_usage[] = {
21 N_("git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\n"
22 " [--current] [--color[=<when>] | --no-color] [--sparse]\n"
23 " [--more=<n> | --list | --independent | --merge-base]\n"
24 " [--no-name | --sha1-name] [--topics]\n"
25 " [(<rev> | <glob>)...]"),
26 N_("git show-branch (-g | --reflog)[=<n>[,<base>]] [--list] [<ref>]"),
27 NULL
30 static int showbranch_use_color = -1;
32 static struct strvec default_args = STRVEC_INIT;
35 * TODO: convert this use of commit->object.flags to commit-slab
36 * instead to store a pointer to ref name directly. Then use the same
37 * UNINTERESTING definition from revision.h here.
39 #define UNINTERESTING 01
41 #define REV_SHIFT 2
42 #define MAX_REVS (FLAG_BITS - REV_SHIFT) /* should not exceed bits_per_int - REV_SHIFT */
44 #define DEFAULT_REFLOG 4
46 static const char *get_color_code(int idx)
48 if (want_color(showbranch_use_color))
49 return column_colors_ansi[idx % column_colors_ansi_max];
50 return "";
53 static const char *get_color_reset_code(void)
55 if (want_color(showbranch_use_color))
56 return GIT_COLOR_RESET;
57 return "";
60 static struct commit *interesting(struct commit_list *list)
62 while (list) {
63 struct commit *commit = list->item;
64 list = list->next;
65 if (commit->object.flags & UNINTERESTING)
66 continue;
67 return commit;
69 return NULL;
72 struct commit_name {
73 const char *head_name; /* which head's ancestor? */
74 int generation; /* how many parents away from head_name */
77 define_commit_slab(commit_name_slab, struct commit_name *);
78 static struct commit_name_slab name_slab;
80 static struct commit_name *commit_to_name(struct commit *commit)
82 return *commit_name_slab_at(&name_slab, commit);
86 /* Name the commit as nth generation ancestor of head_name;
87 * we count only the first-parent relationship for naming purposes.
89 static void name_commit(struct commit *commit, const char *head_name, int nth)
91 struct commit_name *name;
93 name = *commit_name_slab_at(&name_slab, commit);
94 if (!name) {
95 name = xmalloc(sizeof(*name));
96 *commit_name_slab_at(&name_slab, commit) = name;
98 name->head_name = head_name;
99 name->generation = nth;
102 /* Parent is the first parent of the commit. We may name it
103 * as (n+1)th generation ancestor of the same head_name as
104 * commit is nth generation ancestor of, if that generation
105 * number is better than the name it already has.
107 static void name_parent(struct commit *commit, struct commit *parent)
109 struct commit_name *commit_name = commit_to_name(commit);
110 struct commit_name *parent_name = commit_to_name(parent);
111 if (!commit_name)
112 return;
113 if (!parent_name ||
114 commit_name->generation + 1 < parent_name->generation)
115 name_commit(parent, commit_name->head_name,
116 commit_name->generation + 1);
119 static int name_first_parent_chain(struct commit *c)
121 int i = 0;
122 while (c) {
123 struct commit *p;
124 if (!commit_to_name(c))
125 break;
126 if (!c->parents)
127 break;
128 p = c->parents->item;
129 if (!commit_to_name(p)) {
130 name_parent(c, p);
131 i++;
133 else
134 break;
135 c = p;
137 return i;
140 static void name_commits(struct commit_list *list,
141 struct commit **rev,
142 char **ref_name,
143 int num_rev)
145 struct commit_list *cl;
146 struct commit *c;
147 int i;
149 /* First give names to the given heads */
150 for (cl = list; cl; cl = cl->next) {
151 c = cl->item;
152 if (commit_to_name(c))
153 continue;
154 for (i = 0; i < num_rev; i++) {
155 if (rev[i] == c) {
156 name_commit(c, ref_name[i], 0);
157 break;
162 /* Then commits on the first parent ancestry chain */
163 do {
164 i = 0;
165 for (cl = list; cl; cl = cl->next) {
166 i += name_first_parent_chain(cl->item);
168 } while (i);
170 /* Finally, any unnamed commits */
171 do {
172 i = 0;
173 for (cl = list; cl; cl = cl->next) {
174 struct commit_list *parents;
175 struct commit_name *n;
176 int nth;
177 c = cl->item;
178 if (!commit_to_name(c))
179 continue;
180 n = commit_to_name(c);
181 parents = c->parents;
182 nth = 0;
183 while (parents) {
184 struct commit *p = parents->item;
185 struct strbuf newname = STRBUF_INIT;
186 parents = parents->next;
187 nth++;
188 if (commit_to_name(p))
189 continue;
190 switch (n->generation) {
191 case 0:
192 strbuf_addstr(&newname, n->head_name);
193 break;
194 case 1:
195 strbuf_addf(&newname, "%s^", n->head_name);
196 break;
197 default:
198 strbuf_addf(&newname, "%s~%d",
199 n->head_name, n->generation);
200 break;
202 if (nth == 1)
203 strbuf_addch(&newname, '^');
204 else
205 strbuf_addf(&newname, "^%d", nth);
206 name_commit(p, strbuf_detach(&newname, NULL), 0);
207 i++;
208 name_first_parent_chain(p);
211 } while (i);
214 static int mark_seen(struct commit *commit, struct commit_list **seen_p)
216 if (!commit->object.flags) {
217 commit_list_insert(commit, seen_p);
218 return 1;
220 return 0;
223 static void join_revs(struct commit_list **list_p,
224 struct commit_list **seen_p,
225 int num_rev, int extra)
227 int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
228 int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
230 while (*list_p) {
231 struct commit_list *parents;
232 int still_interesting = !!interesting(*list_p);
233 struct commit *commit = pop_commit(list_p);
234 int flags = commit->object.flags & all_mask;
236 if (!still_interesting && extra <= 0)
237 break;
239 mark_seen(commit, seen_p);
240 if ((flags & all_revs) == all_revs)
241 flags |= UNINTERESTING;
242 parents = commit->parents;
244 while (parents) {
245 struct commit *p = parents->item;
246 int this_flag = p->object.flags;
247 parents = parents->next;
248 if ((this_flag & flags) == flags)
249 continue;
250 repo_parse_commit(the_repository, p);
251 if (mark_seen(p, seen_p) && !still_interesting)
252 extra--;
253 p->object.flags |= flags;
254 commit_list_insert_by_date(p, list_p);
259 * Postprocess to complete well-poisoning.
261 * At this point we have all the commits we have seen in
262 * seen_p list. Mark anything that can be reached from
263 * uninteresting commits not interesting.
265 for (;;) {
266 int changed = 0;
267 struct commit_list *s;
268 for (s = *seen_p; s; s = s->next) {
269 struct commit *c = s->item;
270 struct commit_list *parents;
272 if (((c->object.flags & all_revs) != all_revs) &&
273 !(c->object.flags & UNINTERESTING))
274 continue;
276 /* The current commit is either a merge base or
277 * already uninteresting one. Mark its parents
278 * as uninteresting commits _only_ if they are
279 * already parsed. No reason to find new ones
280 * here.
282 parents = c->parents;
283 while (parents) {
284 struct commit *p = parents->item;
285 parents = parents->next;
286 if (!(p->object.flags & UNINTERESTING)) {
287 p->object.flags |= UNINTERESTING;
288 changed = 1;
292 if (!changed)
293 break;
297 static void show_one_commit(struct commit *commit, int no_name)
299 struct strbuf pretty = STRBUF_INIT;
300 const char *pretty_str = "(unavailable)";
301 struct commit_name *name = commit_to_name(commit);
303 if (commit->object.parsed) {
304 pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty);
305 pretty_str = pretty.buf;
307 skip_prefix(pretty_str, "[PATCH] ", &pretty_str);
309 if (!no_name) {
310 if (name && name->head_name) {
311 printf("[%s", name->head_name);
312 if (name->generation) {
313 if (name->generation == 1)
314 printf("^");
315 else
316 printf("~%d", name->generation);
318 printf("] ");
320 else
321 printf("[%s] ",
322 repo_find_unique_abbrev(the_repository, &commit->object.oid,
323 DEFAULT_ABBREV));
325 puts(pretty_str);
326 strbuf_release(&pretty);
329 static char *ref_name[MAX_REVS + 1];
330 static int ref_name_cnt;
332 static const char *find_digit_prefix(const char *s, int *v)
334 const char *p;
335 int ver;
336 char ch;
338 for (p = s, ver = 0;
339 '0' <= (ch = *p) && ch <= '9';
340 p++)
341 ver = ver * 10 + ch - '0';
342 *v = ver;
343 return p;
347 static int version_cmp(const char *a, const char *b)
349 while (1) {
350 int va, vb;
352 a = find_digit_prefix(a, &va);
353 b = find_digit_prefix(b, &vb);
354 if (va != vb)
355 return va - vb;
357 while (1) {
358 int ca = *a;
359 int cb = *b;
360 if ('0' <= ca && ca <= '9')
361 ca = 0;
362 if ('0' <= cb && cb <= '9')
363 cb = 0;
364 if (ca != cb)
365 return ca - cb;
366 if (!ca)
367 break;
368 a++;
369 b++;
371 if (!*a && !*b)
372 return 0;
376 static int compare_ref_name(const void *a_, const void *b_)
378 const char * const*a = a_, * const*b = b_;
379 return version_cmp(*a, *b);
382 static void sort_ref_range(int bottom, int top)
384 QSORT(ref_name + bottom, top - bottom, compare_ref_name);
387 static int append_ref(const char *refname, const struct object_id *oid,
388 int allow_dups)
390 struct commit *commit = lookup_commit_reference_gently(the_repository,
391 oid, 1);
392 int i;
394 if (!commit)
395 return 0;
397 if (!allow_dups) {
398 /* Avoid adding the same thing twice */
399 for (i = 0; i < ref_name_cnt; i++)
400 if (!strcmp(refname, ref_name[i]))
401 return 0;
403 if (MAX_REVS <= ref_name_cnt) {
404 warning(Q_("ignoring %s; cannot handle more than %d ref",
405 "ignoring %s; cannot handle more than %d refs",
406 MAX_REVS), refname, MAX_REVS);
407 return 0;
409 ref_name[ref_name_cnt++] = xstrdup(refname);
410 ref_name[ref_name_cnt] = NULL;
411 return 0;
414 static int append_head_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
415 int flag UNUSED, void *cb_data UNUSED)
417 struct object_id tmp;
418 int ofs = 11;
419 if (!starts_with(refname, "refs/heads/"))
420 return 0;
421 /* If both heads/foo and tags/foo exists, get_sha1 would
422 * get confused.
424 if (repo_get_oid(the_repository, refname + ofs, &tmp) || !oideq(&tmp, oid))
425 ofs = 5;
426 return append_ref(refname + ofs, oid, 0);
429 static int append_remote_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
430 int flag UNUSED, void *cb_data UNUSED)
432 struct object_id tmp;
433 int ofs = 13;
434 if (!starts_with(refname, "refs/remotes/"))
435 return 0;
436 /* If both heads/foo and tags/foo exists, get_sha1 would
437 * get confused.
439 if (repo_get_oid(the_repository, refname + ofs, &tmp) || !oideq(&tmp, oid))
440 ofs = 5;
441 return append_ref(refname + ofs, oid, 0);
444 static int append_tag_ref(const char *refname, const struct object_id *oid,
445 int flag UNUSED, void *cb_data UNUSED)
447 if (!starts_with(refname, "refs/tags/"))
448 return 0;
449 return append_ref(refname + 5, oid, 0);
452 static const char *match_ref_pattern = NULL;
453 static int match_ref_slash = 0;
455 static int append_matching_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,
456 int flag, void *cb_data)
458 /* we want to allow pattern hold/<asterisk> to show all
459 * branches under refs/heads/hold/, and v0.99.9? to show
460 * refs/tags/v0.99.9a and friends.
462 const char *tail;
463 int slash = count_slashes(refname);
464 for (tail = refname; *tail && match_ref_slash < slash; )
465 if (*tail++ == '/')
466 slash--;
467 if (!*tail)
468 return 0;
469 if (wildmatch(match_ref_pattern, tail, 0))
470 return 0;
471 if (starts_with(refname, "refs/heads/"))
472 return append_head_ref(refname, NULL, oid, flag, cb_data);
473 if (starts_with(refname, "refs/tags/"))
474 return append_tag_ref(refname, oid, flag, cb_data);
475 return append_ref(refname, oid, 0);
478 static void snarf_refs(int head, int remotes)
480 if (head) {
481 int orig_cnt = ref_name_cnt;
483 refs_for_each_ref(get_main_ref_store(the_repository),
484 append_head_ref, NULL);
485 sort_ref_range(orig_cnt, ref_name_cnt);
487 if (remotes) {
488 int orig_cnt = ref_name_cnt;
490 refs_for_each_ref(get_main_ref_store(the_repository),
491 append_remote_ref, NULL);
492 sort_ref_range(orig_cnt, ref_name_cnt);
496 static int rev_is_head(const char *head, const char *name)
498 if (!head)
499 return 0;
500 skip_prefix(head, "refs/heads/", &head);
501 if (!skip_prefix(name, "refs/heads/", &name))
502 skip_prefix(name, "heads/", &name);
503 return !strcmp(head, name);
506 static int show_merge_base(const struct commit_list *seen, int num_rev)
508 int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
509 int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
510 int exit_status = 1;
512 for (const struct commit_list *s = seen; s; s = s->next) {
513 struct commit *commit = s->item;
514 int flags = commit->object.flags & all_mask;
515 if (!(flags & UNINTERESTING) &&
516 ((flags & all_revs) == all_revs)) {
517 puts(oid_to_hex(&commit->object.oid));
518 exit_status = 0;
519 commit->object.flags |= UNINTERESTING;
522 return exit_status;
525 static int show_independent(struct commit **rev,
526 int num_rev,
527 unsigned int *rev_mask)
529 int i;
531 for (i = 0; i < num_rev; i++) {
532 struct commit *commit = rev[i];
533 unsigned int flag = rev_mask[i];
535 if (commit->object.flags == flag)
536 puts(oid_to_hex(&commit->object.oid));
537 commit->object.flags |= UNINTERESTING;
539 return 0;
542 static void append_one_rev(const char *av)
544 struct object_id revkey;
545 if (!repo_get_oid(the_repository, av, &revkey)) {
546 append_ref(av, &revkey, 0);
547 return;
549 if (strpbrk(av, "*?[")) {
550 /* glob style match */
551 int saved_matches = ref_name_cnt;
553 match_ref_pattern = av;
554 match_ref_slash = count_slashes(av);
555 refs_for_each_ref(get_main_ref_store(the_repository),
556 append_matching_ref, NULL);
557 if (saved_matches == ref_name_cnt &&
558 ref_name_cnt < MAX_REVS)
559 error(_("no matching refs with %s"), av);
560 sort_ref_range(saved_matches, ref_name_cnt);
561 return;
563 die("bad sha1 reference %s", av);
566 static int git_show_branch_config(const char *var, const char *value,
567 const struct config_context *ctx, void *cb)
569 if (!strcmp(var, "showbranch.default")) {
570 if (!value)
571 return config_error_nonbool(var);
573 * default_arg is now passed to parse_options(), so we need to
574 * mimic the real argv a bit better.
576 if (!default_args.nr)
577 strvec_push(&default_args, "show-branch");
578 strvec_push(&default_args, value);
579 return 0;
582 if (!strcmp(var, "color.showbranch")) {
583 showbranch_use_color = git_config_colorbool(var, value);
584 return 0;
587 if (git_color_config(var, value, cb) < 0)
588 return -1;
590 return git_default_config(var, value, ctx, cb);
593 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
595 /* If the commit is tip of the named branches, do not
596 * omit it.
597 * Otherwise, if it is a merge that is reachable from only one
598 * tip, it is not that interesting.
600 int i, flag, count;
601 for (i = 0; i < n; i++)
602 if (rev[i] == commit)
603 return 0;
604 flag = commit->object.flags;
605 for (i = count = 0; i < n; i++) {
606 if (flag & (1u << (i + REV_SHIFT)))
607 count++;
609 if (count == 1)
610 return 1;
611 return 0;
614 static int reflog = 0;
616 static int parse_reflog_param(const struct option *opt, const char *arg,
617 int unset)
619 char *ep;
620 const char **base = (const char **)opt->value;
621 BUG_ON_OPT_NEG(unset);
622 if (!arg)
623 arg = "";
624 reflog = strtoul(arg, &ep, 10);
625 if (*ep == ',')
626 *base = ep + 1;
627 else if (*ep)
628 return error("unrecognized reflog param '%s'", arg);
629 else
630 *base = NULL;
631 if (reflog <= 0)
632 reflog = DEFAULT_REFLOG;
633 return 0;
636 int cmd_show_branch(int ac,
637 const char **av,
638 const char *prefix,
639 struct repository *repo UNUSED)
641 struct commit *rev[MAX_REVS], *commit;
642 char *reflog_msg[MAX_REVS] = {0};
643 struct commit_list *list = NULL, *seen = NULL;
644 unsigned int rev_mask[MAX_REVS];
645 int num_rev, i, extra = 0;
646 int all_heads = 0, all_remotes = 0;
647 int all_mask, all_revs;
648 enum rev_sort_order sort_order = REV_SORT_IN_GRAPH_ORDER;
649 char *head;
650 struct object_id head_oid;
651 int merge_base = 0;
652 int independent = 0;
653 int no_name = 0;
654 int sha1_name = 0;
655 int shown_merge_point = 0;
656 int with_current_branch = 0;
657 int head_at = -1;
658 int topics = 0;
659 int sparse = 0;
660 const char *reflog_base = NULL;
661 struct option builtin_show_branch_options[] = {
662 OPT_BOOL('a', "all", &all_heads,
663 N_("show remote-tracking and local branches")),
664 OPT_BOOL('r', "remotes", &all_remotes,
665 N_("show remote-tracking branches")),
666 OPT__COLOR(&showbranch_use_color,
667 N_("color '*!+-' corresponding to the branch")),
668 { OPTION_INTEGER, 0, "more", &extra, N_("n"),
669 N_("show <n> more commits after the common ancestor"),
670 PARSE_OPT_OPTARG, NULL, (intptr_t)1 },
671 OPT_SET_INT(0, "list", &extra, N_("synonym to more=-1"), -1),
672 OPT_BOOL(0, "no-name", &no_name, N_("suppress naming strings")),
673 OPT_BOOL(0, "current", &with_current_branch,
674 N_("include the current branch")),
675 OPT_BOOL(0, "sha1-name", &sha1_name,
676 N_("name commits with their object names")),
677 OPT_BOOL(0, "merge-base", &merge_base,
678 N_("show possible merge bases")),
679 OPT_BOOL(0, "independent", &independent,
680 N_("show refs unreachable from any other ref")),
681 OPT_SET_INT_F(0, "topo-order", &sort_order,
682 N_("show commits in topological order"),
683 REV_SORT_IN_GRAPH_ORDER, PARSE_OPT_NONEG),
684 OPT_BOOL(0, "topics", &topics,
685 N_("show only commits not on the first branch")),
686 OPT_SET_INT(0, "sparse", &sparse,
687 N_("show merges reachable from only one tip"), 1),
688 OPT_SET_INT_F(0, "date-order", &sort_order,
689 N_("topologically sort, maintaining date order "
690 "where possible"),
691 REV_SORT_BY_COMMIT_DATE, PARSE_OPT_NONEG),
692 OPT_CALLBACK_F('g', "reflog", &reflog_base, N_("<n>[,<base>]"),
693 N_("show <n> most recent ref-log entries starting at "
694 "base"),
695 PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
696 parse_reflog_param),
697 OPT_END()
699 const char **args_copy = NULL;
700 int ret;
702 init_commit_name_slab(&name_slab);
704 git_config(git_show_branch_config, NULL);
706 /* If nothing is specified, try the default first */
707 if (ac == 1 && default_args.nr) {
708 DUP_ARRAY(args_copy, default_args.v, default_args.nr);
709 ac = default_args.nr;
710 av = args_copy;
713 ac = parse_options(ac, av, prefix, builtin_show_branch_options,
714 show_branch_usage, PARSE_OPT_STOP_AT_NON_OPTION);
715 if (all_heads)
716 all_remotes = 1;
718 if (extra || reflog) {
719 /* "listing" mode is incompatible with
720 * independent nor merge-base modes.
722 if (independent || merge_base)
723 usage_with_options(show_branch_usage,
724 builtin_show_branch_options);
725 if (reflog && ((0 < extra) || all_heads || all_remotes))
727 * Asking for --more in reflog mode does not
728 * make sense. --list is Ok.
730 * Also --all and --remotes do not make sense either.
732 die(_("options '%s' and '%s' cannot be used together"), "--reflog",
733 "--all/--remotes/--independent/--merge-base");
736 if (with_current_branch && reflog)
737 die(_("options '%s' and '%s' cannot be used together"),
738 "--reflog", "--current");
740 /* If nothing is specified, show all branches by default */
741 if (ac <= topics && all_heads + all_remotes == 0)
742 all_heads = 1;
744 if (reflog) {
745 struct object_id oid;
746 char *ref;
747 int base = 0;
748 unsigned int flags = 0;
750 if (ac == 0) {
751 static const char *fake_av[2];
753 fake_av[0] = refs_resolve_refdup(get_main_ref_store(the_repository),
754 "HEAD",
755 RESOLVE_REF_READING,
756 &oid,
757 NULL);
758 fake_av[1] = NULL;
759 av = fake_av;
760 ac = 1;
761 if (!*av)
762 die(_("no branches given, and HEAD is not valid"));
764 if (ac != 1)
765 die(_("--reflog option needs one branch name"));
767 if (MAX_REVS < reflog)
768 die(Q_("only %d entry can be shown at one time.",
769 "only %d entries can be shown at one time.",
770 MAX_REVS), MAX_REVS);
771 if (!repo_dwim_ref(the_repository, *av, strlen(*av), &oid,
772 &ref, 0))
773 die(_("no such ref %s"), *av);
775 /* Has the base been specified? */
776 if (reflog_base) {
777 char *ep;
778 base = strtoul(reflog_base, &ep, 10);
779 if (*ep) {
780 /* Ah, that is a date spec... */
781 timestamp_t at;
782 at = approxidate(reflog_base);
783 read_ref_at(get_main_ref_store(the_repository),
784 ref, flags, at, -1, &oid, NULL,
785 NULL, NULL, &base);
789 for (i = 0; i < reflog; i++) {
790 char *logmsg = NULL;
791 char *nth_desc;
792 const char *msg;
793 char *end;
794 timestamp_t timestamp;
795 int tz;
797 if (read_ref_at(get_main_ref_store(the_repository),
798 ref, flags, 0, base + i, &oid, &logmsg,
799 &timestamp, &tz, NULL)) {
800 free(logmsg);
801 reflog = i;
802 break;
805 end = strchr(logmsg, '\n');
806 if (end)
807 *end = '\0';
809 msg = (*logmsg == '\0') ? "(none)" : logmsg;
810 reflog_msg[i] = xstrfmt("(%s) %s",
811 show_date(timestamp, tz,
812 DATE_MODE(RELATIVE)),
813 msg);
814 free(logmsg);
816 nth_desc = xstrfmt("%s@{%d}", *av, base+i);
817 append_ref(nth_desc, &oid, 1);
818 free(nth_desc);
820 free(ref);
822 else {
823 while (0 < ac) {
824 append_one_rev(*av);
825 ac--; av++;
827 if (all_heads + all_remotes)
828 snarf_refs(all_heads, all_remotes);
831 head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD",
832 RESOLVE_REF_READING,
833 &head_oid, NULL);
835 if (with_current_branch && head) {
836 int has_head = 0;
837 for (i = 0; !has_head && i < ref_name_cnt; i++) {
838 /* We are only interested in adding the branch
839 * HEAD points at.
841 if (rev_is_head(head, ref_name[i]))
842 has_head++;
844 if (!has_head) {
845 const char *name = head;
846 skip_prefix(name, "refs/heads/", &name);
847 append_one_rev(name);
851 if (!ref_name_cnt) {
852 fprintf(stderr, "No revs to be shown.\n");
853 ret = 0;
854 goto out;
857 for (num_rev = 0; ref_name[num_rev]; num_rev++) {
858 struct object_id revkey;
859 unsigned int flag = 1u << (num_rev + REV_SHIFT);
861 if (MAX_REVS <= num_rev)
862 die(Q_("cannot handle more than %d rev.",
863 "cannot handle more than %d revs.",
864 MAX_REVS), MAX_REVS);
865 if (repo_get_oid(the_repository, ref_name[num_rev], &revkey))
866 die(_("'%s' is not a valid ref."), ref_name[num_rev]);
867 commit = lookup_commit_reference(the_repository, &revkey);
868 if (!commit)
869 die(_("cannot find commit %s (%s)"),
870 ref_name[num_rev], oid_to_hex(&revkey));
871 repo_parse_commit(the_repository, commit);
872 mark_seen(commit, &seen);
874 /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
875 * and so on. REV_SHIFT bits from bit 0 are used for
876 * internal bookkeeping.
878 commit->object.flags |= flag;
879 if (commit->object.flags == flag)
880 commit_list_insert_by_date(commit, &list);
881 rev[num_rev] = commit;
883 for (i = 0; i < num_rev; i++)
884 rev_mask[i] = rev[i]->object.flags;
886 if (0 <= extra)
887 join_revs(&list, &seen, num_rev, extra);
889 commit_list_sort_by_date(&seen);
891 if (merge_base) {
892 ret = show_merge_base(seen, num_rev);
893 goto out;
896 if (independent) {
897 ret = show_independent(rev, num_rev, rev_mask);
898 goto out;
901 /* Show list; --more=-1 means list-only */
902 if (1 < num_rev || extra < 0) {
903 for (i = 0; i < num_rev; i++) {
904 int j;
905 int is_head = rev_is_head(head, ref_name[i]) &&
906 oideq(&head_oid, &rev[i]->object.oid);
907 if (extra < 0)
908 printf("%c [%s] ",
909 is_head ? '*' : ' ', ref_name[i]);
910 else {
911 for (j = 0; j < i; j++)
912 putchar(' ');
913 printf("%s%c%s [%s] ",
914 get_color_code(i),
915 is_head ? '*' : '!',
916 get_color_reset_code(), ref_name[i]);
919 if (!reflog) {
920 /* header lines never need name */
921 show_one_commit(rev[i], 1);
923 else
924 puts(reflog_msg[i]);
926 if (is_head)
927 head_at = i;
929 if (0 <= extra) {
930 for (i = 0; i < num_rev; i++)
931 putchar('-');
932 putchar('\n');
935 if (extra < 0) {
936 ret = 0;
937 goto out;
940 /* Sort topologically */
941 sort_in_topological_order(&seen, sort_order);
943 /* Give names to commits */
944 if (!sha1_name && !no_name)
945 name_commits(seen, rev, ref_name, num_rev);
947 all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
948 all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
950 for (struct commit_list *l = seen; l; l = l->next) {
951 struct commit *commit = l->item;
952 int this_flag = commit->object.flags;
953 int is_merge_point = ((this_flag & all_revs) == all_revs);
955 shown_merge_point |= is_merge_point;
957 if (1 < num_rev) {
958 int is_merge = !!(commit->parents &&
959 commit->parents->next);
960 if (topics &&
961 !is_merge_point &&
962 (this_flag & (1u << REV_SHIFT)))
963 continue;
964 if (!sparse && is_merge &&
965 omit_in_dense(commit, rev, num_rev))
966 continue;
967 for (i = 0; i < num_rev; i++) {
968 int mark;
969 if (!(this_flag & (1u << (i + REV_SHIFT))))
970 mark = ' ';
971 else if (is_merge)
972 mark = '-';
973 else if (i == head_at)
974 mark = '*';
975 else
976 mark = '+';
977 if (mark == ' ')
978 putchar(mark);
979 else
980 printf("%s%c%s",
981 get_color_code(i),
982 mark, get_color_reset_code());
984 putchar(' ');
986 show_one_commit(commit, no_name);
988 if (shown_merge_point && --extra < 0)
989 break;
992 ret = 0;
994 out:
995 for (size_t i = 0; i < ARRAY_SIZE(reflog_msg); i++)
996 free(reflog_msg[i]);
997 free_commit_list(seen);
998 free_commit_list(list);
999 free(args_copy);
1000 free(head);
1001 return ret;