Merge branch 'mh/fast-import-raise-default-depth'
[git] / builtin / show-branch.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "refs.h"
4 #include "builtin.h"
5 #include "color.h"
6 #include "argv-array.h"
7 #include "parse-options.h"
8 #include "dir.h"
9
10 static const char* show_branch_usage[] = {
11     N_("git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\n"
12        "                [--current] [--color[=<when>] | --no-color] [--sparse]\n"
13        "                [--more=<n> | --list | --independent | --merge-base]\n"
14        "                [--no-name | --sha1-name] [--topics] [(<rev> | <glob>)...]"),
15     N_("git show-branch (-g | --reflog)[=<n>[,<base>]] [--list] [<ref>]"),
16     NULL
17 };
18
19 static int showbranch_use_color = -1;
20
21 static struct argv_array default_args = ARGV_ARRAY_INIT;
22
23 #define UNINTERESTING   01
24
25 #define REV_SHIFT        2
26 #define MAX_REVS        (FLAG_BITS - REV_SHIFT) /* should not exceed bits_per_int - REV_SHIFT */
27
28 #define DEFAULT_REFLOG  4
29
30 static const char *get_color_code(int idx)
31 {
32         if (want_color(showbranch_use_color))
33                 return column_colors_ansi[idx % column_colors_ansi_max];
34         return "";
35 }
36
37 static const char *get_color_reset_code(void)
38 {
39         if (want_color(showbranch_use_color))
40                 return GIT_COLOR_RESET;
41         return "";
42 }
43
44 static struct commit *interesting(struct commit_list *list)
45 {
46         while (list) {
47                 struct commit *commit = list->item;
48                 list = list->next;
49                 if (commit->object.flags & UNINTERESTING)
50                         continue;
51                 return commit;
52         }
53         return NULL;
54 }
55
56 struct commit_name {
57         const char *head_name; /* which head's ancestor? */
58         int generation; /* how many parents away from head_name */
59 };
60
61 /* Name the commit as nth generation ancestor of head_name;
62  * we count only the first-parent relationship for naming purposes.
63  */
64 static void name_commit(struct commit *commit, const char *head_name, int nth)
65 {
66         struct commit_name *name;
67         if (!commit->util)
68                 commit->util = xmalloc(sizeof(struct commit_name));
69         name = commit->util;
70         name->head_name = head_name;
71         name->generation = nth;
72 }
73
74 /* Parent is the first parent of the commit.  We may name it
75  * as (n+1)th generation ancestor of the same head_name as
76  * commit is nth generation ancestor of, if that generation
77  * number is better than the name it already has.
78  */
79 static void name_parent(struct commit *commit, struct commit *parent)
80 {
81         struct commit_name *commit_name = commit->util;
82         struct commit_name *parent_name = parent->util;
83         if (!commit_name)
84                 return;
85         if (!parent_name ||
86             commit_name->generation + 1 < parent_name->generation)
87                 name_commit(parent, commit_name->head_name,
88                             commit_name->generation + 1);
89 }
90
91 static int name_first_parent_chain(struct commit *c)
92 {
93         int i = 0;
94         while (c) {
95                 struct commit *p;
96                 if (!c->util)
97                         break;
98                 if (!c->parents)
99                         break;
100                 p = c->parents->item;
101                 if (!p->util) {
102                         name_parent(c, p);
103                         i++;
104                 }
105                 else
106                         break;
107                 c = p;
108         }
109         return i;
110 }
111
112 static void name_commits(struct commit_list *list,
113                          struct commit **rev,
114                          char **ref_name,
115                          int num_rev)
116 {
117         struct commit_list *cl;
118         struct commit *c;
119         int i;
120
121         /* First give names to the given heads */
122         for (cl = list; cl; cl = cl->next) {
123                 c = cl->item;
124                 if (c->util)
125                         continue;
126                 for (i = 0; i < num_rev; i++) {
127                         if (rev[i] == c) {
128                                 name_commit(c, ref_name[i], 0);
129                                 break;
130                         }
131                 }
132         }
133
134         /* Then commits on the first parent ancestry chain */
135         do {
136                 i = 0;
137                 for (cl = list; cl; cl = cl->next) {
138                         i += name_first_parent_chain(cl->item);
139                 }
140         } while (i);
141
142         /* Finally, any unnamed commits */
143         do {
144                 i = 0;
145                 for (cl = list; cl; cl = cl->next) {
146                         struct commit_list *parents;
147                         struct commit_name *n;
148                         int nth;
149                         c = cl->item;
150                         if (!c->util)
151                                 continue;
152                         n = c->util;
153                         parents = c->parents;
154                         nth = 0;
155                         while (parents) {
156                                 struct commit *p = parents->item;
157                                 struct strbuf newname = STRBUF_INIT;
158                                 parents = parents->next;
159                                 nth++;
160                                 if (p->util)
161                                         continue;
162                                 switch (n->generation) {
163                                 case 0:
164                                         strbuf_addstr(&newname, n->head_name);
165                                         break;
166                                 case 1:
167                                         strbuf_addf(&newname, "%s^", n->head_name);
168                                         break;
169                                 default:
170                                         strbuf_addf(&newname, "%s~%d",
171                                                     n->head_name, n->generation);
172                                         break;
173                                 }
174                                 if (nth == 1)
175                                         strbuf_addch(&newname, '^');
176                                 else
177                                         strbuf_addf(&newname, "^%d", nth);
178                                 name_commit(p, strbuf_detach(&newname, NULL), 0);
179                                 i++;
180                                 name_first_parent_chain(p);
181                         }
182                 }
183         } while (i);
184 }
185
186 static int mark_seen(struct commit *commit, struct commit_list **seen_p)
187 {
188         if (!commit->object.flags) {
189                 commit_list_insert(commit, seen_p);
190                 return 1;
191         }
192         return 0;
193 }
194
195 static void join_revs(struct commit_list **list_p,
196                       struct commit_list **seen_p,
197                       int num_rev, int extra)
198 {
199         int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
200         int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
201
202         while (*list_p) {
203                 struct commit_list *parents;
204                 int still_interesting = !!interesting(*list_p);
205                 struct commit *commit = pop_commit(list_p);
206                 int flags = commit->object.flags & all_mask;
207
208                 if (!still_interesting && extra <= 0)
209                         break;
210
211                 mark_seen(commit, seen_p);
212                 if ((flags & all_revs) == all_revs)
213                         flags |= UNINTERESTING;
214                 parents = commit->parents;
215
216                 while (parents) {
217                         struct commit *p = parents->item;
218                         int this_flag = p->object.flags;
219                         parents = parents->next;
220                         if ((this_flag & flags) == flags)
221                                 continue;
222                         parse_commit(p);
223                         if (mark_seen(p, seen_p) && !still_interesting)
224                                 extra--;
225                         p->object.flags |= flags;
226                         commit_list_insert_by_date(p, list_p);
227                 }
228         }
229
230         /*
231          * Postprocess to complete well-poisoning.
232          *
233          * At this point we have all the commits we have seen in
234          * seen_p list.  Mark anything that can be reached from
235          * uninteresting commits not interesting.
236          */
237         for (;;) {
238                 int changed = 0;
239                 struct commit_list *s;
240                 for (s = *seen_p; s; s = s->next) {
241                         struct commit *c = s->item;
242                         struct commit_list *parents;
243
244                         if (((c->object.flags & all_revs) != all_revs) &&
245                             !(c->object.flags & UNINTERESTING))
246                                 continue;
247
248                         /* The current commit is either a merge base or
249                          * already uninteresting one.  Mark its parents
250                          * as uninteresting commits _only_ if they are
251                          * already parsed.  No reason to find new ones
252                          * here.
253                          */
254                         parents = c->parents;
255                         while (parents) {
256                                 struct commit *p = parents->item;
257                                 parents = parents->next;
258                                 if (!(p->object.flags & UNINTERESTING)) {
259                                         p->object.flags |= UNINTERESTING;
260                                         changed = 1;
261                                 }
262                         }
263                 }
264                 if (!changed)
265                         break;
266         }
267 }
268
269 static void show_one_commit(struct commit *commit, int no_name)
270 {
271         struct strbuf pretty = STRBUF_INIT;
272         const char *pretty_str = "(unavailable)";
273         struct commit_name *name = commit->util;
274
275         if (commit->object.parsed) {
276                 pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty);
277                 pretty_str = pretty.buf;
278         }
279         skip_prefix(pretty_str, "[PATCH] ", &pretty_str);
280
281         if (!no_name) {
282                 if (name && name->head_name) {
283                         printf("[%s", name->head_name);
284                         if (name->generation) {
285                                 if (name->generation == 1)
286                                         printf("^");
287                                 else
288                                         printf("~%d", name->generation);
289                         }
290                         printf("] ");
291                 }
292                 else
293                         printf("[%s] ",
294                                find_unique_abbrev(commit->object.oid.hash,
295                                                   DEFAULT_ABBREV));
296         }
297         puts(pretty_str);
298         strbuf_release(&pretty);
299 }
300
301 static char *ref_name[MAX_REVS + 1];
302 static int ref_name_cnt;
303
304 static const char *find_digit_prefix(const char *s, int *v)
305 {
306         const char *p;
307         int ver;
308         char ch;
309
310         for (p = s, ver = 0;
311              '0' <= (ch = *p) && ch <= '9';
312              p++)
313                 ver = ver * 10 + ch - '0';
314         *v = ver;
315         return p;
316 }
317
318
319 static int version_cmp(const char *a, const char *b)
320 {
321         while (1) {
322                 int va, vb;
323
324                 a = find_digit_prefix(a, &va);
325                 b = find_digit_prefix(b, &vb);
326                 if (va != vb)
327                         return va - vb;
328
329                 while (1) {
330                         int ca = *a;
331                         int cb = *b;
332                         if ('0' <= ca && ca <= '9')
333                                 ca = 0;
334                         if ('0' <= cb && cb <= '9')
335                                 cb = 0;
336                         if (ca != cb)
337                                 return ca - cb;
338                         if (!ca)
339                                 break;
340                         a++;
341                         b++;
342                 }
343                 if (!*a && !*b)
344                         return 0;
345         }
346 }
347
348 static int compare_ref_name(const void *a_, const void *b_)
349 {
350         const char * const*a = a_, * const*b = b_;
351         return version_cmp(*a, *b);
352 }
353
354 static void sort_ref_range(int bottom, int top)
355 {
356         QSORT(ref_name + bottom, top - bottom, compare_ref_name);
357 }
358
359 static int append_ref(const char *refname, const struct object_id *oid,
360                       int allow_dups)
361 {
362         struct commit *commit = lookup_commit_reference_gently(oid, 1);
363         int i;
364
365         if (!commit)
366                 return 0;
367
368         if (!allow_dups) {
369                 /* Avoid adding the same thing twice */
370                 for (i = 0; i < ref_name_cnt; i++)
371                         if (!strcmp(refname, ref_name[i]))
372                                 return 0;
373         }
374         if (MAX_REVS <= ref_name_cnt) {
375                 warning(Q_("ignoring %s; cannot handle more than %d ref",
376                            "ignoring %s; cannot handle more than %d refs",
377                            MAX_REVS), refname, MAX_REVS);
378                 return 0;
379         }
380         ref_name[ref_name_cnt++] = xstrdup(refname);
381         ref_name[ref_name_cnt] = NULL;
382         return 0;
383 }
384
385 static int append_head_ref(const char *refname, const struct object_id *oid,
386                            int flag, void *cb_data)
387 {
388         struct object_id tmp;
389         int ofs = 11;
390         if (!starts_with(refname, "refs/heads/"))
391                 return 0;
392         /* If both heads/foo and tags/foo exists, get_sha1 would
393          * get confused.
394          */
395         if (get_sha1(refname + ofs, tmp.hash) || oidcmp(&tmp, oid))
396                 ofs = 5;
397         return append_ref(refname + ofs, oid, 0);
398 }
399
400 static int append_remote_ref(const char *refname, const struct object_id *oid,
401                              int flag, void *cb_data)
402 {
403         struct object_id tmp;
404         int ofs = 13;
405         if (!starts_with(refname, "refs/remotes/"))
406                 return 0;
407         /* If both heads/foo and tags/foo exists, get_sha1 would
408          * get confused.
409          */
410         if (get_sha1(refname + ofs, tmp.hash) || oidcmp(&tmp, oid))
411                 ofs = 5;
412         return append_ref(refname + ofs, oid, 0);
413 }
414
415 static int append_tag_ref(const char *refname, const struct object_id *oid,
416                           int flag, void *cb_data)
417 {
418         if (!starts_with(refname, "refs/tags/"))
419                 return 0;
420         return append_ref(refname + 5, oid, 0);
421 }
422
423 static const char *match_ref_pattern = NULL;
424 static int match_ref_slash = 0;
425
426 static int append_matching_ref(const char *refname, const struct object_id *oid,
427                                int flag, void *cb_data)
428 {
429         /* we want to allow pattern hold/<asterisk> to show all
430          * branches under refs/heads/hold/, and v0.99.9? to show
431          * refs/tags/v0.99.9a and friends.
432          */
433         const char *tail;
434         int slash = count_slashes(refname);
435         for (tail = refname; *tail && match_ref_slash < slash; )
436                 if (*tail++ == '/')
437                         slash--;
438         if (!*tail)
439                 return 0;
440         if (wildmatch(match_ref_pattern, tail, 0, NULL))
441                 return 0;
442         if (starts_with(refname, "refs/heads/"))
443                 return append_head_ref(refname, oid, flag, cb_data);
444         if (starts_with(refname, "refs/tags/"))
445                 return append_tag_ref(refname, oid, flag, cb_data);
446         return append_ref(refname, oid, 0);
447 }
448
449 static void snarf_refs(int head, int remotes)
450 {
451         if (head) {
452                 int orig_cnt = ref_name_cnt;
453
454                 for_each_ref(append_head_ref, NULL);
455                 sort_ref_range(orig_cnt, ref_name_cnt);
456         }
457         if (remotes) {
458                 int orig_cnt = ref_name_cnt;
459
460                 for_each_ref(append_remote_ref, NULL);
461                 sort_ref_range(orig_cnt, ref_name_cnt);
462         }
463 }
464
465 static int rev_is_head(const char *head, const char *name,
466                        unsigned char *head_sha1, unsigned char *sha1)
467 {
468         if (!head || (head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
469                 return 0;
470         skip_prefix(head, "refs/heads/", &head);
471         if (!skip_prefix(name, "refs/heads/", &name))
472                 skip_prefix(name, "heads/", &name);
473         return !strcmp(head, name);
474 }
475
476 static int show_merge_base(struct commit_list *seen, int num_rev)
477 {
478         int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
479         int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
480         int exit_status = 1;
481
482         while (seen) {
483                 struct commit *commit = pop_commit(&seen);
484                 int flags = commit->object.flags & all_mask;
485                 if (!(flags & UNINTERESTING) &&
486                     ((flags & all_revs) == all_revs)) {
487                         puts(oid_to_hex(&commit->object.oid));
488                         exit_status = 0;
489                         commit->object.flags |= UNINTERESTING;
490                 }
491         }
492         return exit_status;
493 }
494
495 static int show_independent(struct commit **rev,
496                             int num_rev,
497                             char **ref_name,
498                             unsigned int *rev_mask)
499 {
500         int i;
501
502         for (i = 0; i < num_rev; i++) {
503                 struct commit *commit = rev[i];
504                 unsigned int flag = rev_mask[i];
505
506                 if (commit->object.flags == flag)
507                         puts(oid_to_hex(&commit->object.oid));
508                 commit->object.flags |= UNINTERESTING;
509         }
510         return 0;
511 }
512
513 static void append_one_rev(const char *av)
514 {
515         struct object_id revkey;
516         if (!get_sha1(av, revkey.hash)) {
517                 append_ref(av, &revkey, 0);
518                 return;
519         }
520         if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) {
521                 /* glob style match */
522                 int saved_matches = ref_name_cnt;
523
524                 match_ref_pattern = av;
525                 match_ref_slash = count_slashes(av);
526                 for_each_ref(append_matching_ref, NULL);
527                 if (saved_matches == ref_name_cnt &&
528                     ref_name_cnt < MAX_REVS)
529                         error(_("no matching refs with %s"), av);
530                 sort_ref_range(saved_matches, ref_name_cnt);
531                 return;
532         }
533         die("bad sha1 reference %s", av);
534 }
535
536 static int git_show_branch_config(const char *var, const char *value, void *cb)
537 {
538         if (!strcmp(var, "showbranch.default")) {
539                 if (!value)
540                         return config_error_nonbool(var);
541                 /*
542                  * default_arg is now passed to parse_options(), so we need to
543                  * mimic the real argv a bit better.
544                  */
545                 if (!default_args.argc)
546                         argv_array_push(&default_args, "show-branch");
547                 argv_array_push(&default_args, value);
548                 return 0;
549         }
550
551         if (!strcmp(var, "color.showbranch")) {
552                 showbranch_use_color = git_config_colorbool(var, value);
553                 return 0;
554         }
555
556         return git_color_default_config(var, value, cb);
557 }
558
559 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
560 {
561         /* If the commit is tip of the named branches, do not
562          * omit it.
563          * Otherwise, if it is a merge that is reachable from only one
564          * tip, it is not that interesting.
565          */
566         int i, flag, count;
567         for (i = 0; i < n; i++)
568                 if (rev[i] == commit)
569                         return 0;
570         flag = commit->object.flags;
571         for (i = count = 0; i < n; i++) {
572                 if (flag & (1u << (i + REV_SHIFT)))
573                         count++;
574         }
575         if (count == 1)
576                 return 1;
577         return 0;
578 }
579
580 static int reflog = 0;
581
582 static int parse_reflog_param(const struct option *opt, const char *arg,
583                               int unset)
584 {
585         char *ep;
586         const char **base = (const char **)opt->value;
587         if (!arg)
588                 arg = "";
589         reflog = strtoul(arg, &ep, 10);
590         if (*ep == ',')
591                 *base = ep + 1;
592         else if (*ep)
593                 return error("unrecognized reflog param '%s'", arg);
594         else
595                 *base = NULL;
596         if (reflog <= 0)
597                 reflog = DEFAULT_REFLOG;
598         return 0;
599 }
600
601 int cmd_show_branch(int ac, const char **av, const char *prefix)
602 {
603         struct commit *rev[MAX_REVS], *commit;
604         char *reflog_msg[MAX_REVS];
605         struct commit_list *list = NULL, *seen = NULL;
606         unsigned int rev_mask[MAX_REVS];
607         int num_rev, i, extra = 0;
608         int all_heads = 0, all_remotes = 0;
609         int all_mask, all_revs;
610         enum rev_sort_order sort_order = REV_SORT_IN_GRAPH_ORDER;
611         char *head;
612         struct object_id head_oid;
613         int merge_base = 0;
614         int independent = 0;
615         int no_name = 0;
616         int sha1_name = 0;
617         int shown_merge_point = 0;
618         int with_current_branch = 0;
619         int head_at = -1;
620         int topics = 0;
621         int dense = 1;
622         const char *reflog_base = NULL;
623         struct option builtin_show_branch_options[] = {
624                 OPT_BOOL('a', "all", &all_heads,
625                          N_("show remote-tracking and local branches")),
626                 OPT_BOOL('r', "remotes", &all_remotes,
627                          N_("show remote-tracking branches")),
628                 OPT__COLOR(&showbranch_use_color,
629                             N_("color '*!+-' corresponding to the branch")),
630                 { OPTION_INTEGER, 0, "more", &extra, N_("n"),
631                             N_("show <n> more commits after the common ancestor"),
632                             PARSE_OPT_OPTARG, NULL, (intptr_t)1 },
633                 OPT_SET_INT(0, "list", &extra, N_("synonym to more=-1"), -1),
634                 OPT_BOOL(0, "no-name", &no_name, N_("suppress naming strings")),
635                 OPT_BOOL(0, "current", &with_current_branch,
636                          N_("include the current branch")),
637                 OPT_BOOL(0, "sha1-name", &sha1_name,
638                          N_("name commits with their object names")),
639                 OPT_BOOL(0, "merge-base", &merge_base,
640                          N_("show possible merge bases")),
641                 OPT_BOOL(0, "independent", &independent,
642                             N_("show refs unreachable from any other ref")),
643                 OPT_SET_INT(0, "topo-order", &sort_order,
644                             N_("show commits in topological order"),
645                             REV_SORT_IN_GRAPH_ORDER),
646                 OPT_BOOL(0, "topics", &topics,
647                          N_("show only commits not on the first branch")),
648                 OPT_SET_INT(0, "sparse", &dense,
649                             N_("show merges reachable from only one tip"), 0),
650                 OPT_SET_INT(0, "date-order", &sort_order,
651                             N_("topologically sort, maintaining date order "
652                                "where possible"),
653                             REV_SORT_BY_COMMIT_DATE),
654                 { OPTION_CALLBACK, 'g', "reflog", &reflog_base, N_("<n>[,<base>]"),
655                             N_("show <n> most recent ref-log entries starting at "
656                                "base"),
657                             PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP,
658                             parse_reflog_param },
659                 OPT_END()
660         };
661
662         git_config(git_show_branch_config, NULL);
663
664         /* If nothing is specified, try the default first */
665         if (ac == 1 && default_args.argc) {
666                 ac = default_args.argc;
667                 av = default_args.argv;
668         }
669
670         ac = parse_options(ac, av, prefix, builtin_show_branch_options,
671                            show_branch_usage, PARSE_OPT_STOP_AT_NON_OPTION);
672         if (all_heads)
673                 all_remotes = 1;
674
675         if (extra || reflog) {
676                 /* "listing" mode is incompatible with
677                  * independent nor merge-base modes.
678                  */
679                 if (independent || merge_base)
680                         usage_with_options(show_branch_usage,
681                                            builtin_show_branch_options);
682                 if (reflog && ((0 < extra) || all_heads || all_remotes))
683                         /*
684                          * Asking for --more in reflog mode does not
685                          * make sense.  --list is Ok.
686                          *
687                          * Also --all and --remotes do not make sense either.
688                          */
689                         die(_("--reflog is incompatible with --all, --remotes, "
690                               "--independent or --merge-base"));
691         }
692
693         /* If nothing is specified, show all branches by default */
694         if (ac <= topics && all_heads + all_remotes == 0)
695                 all_heads = 1;
696
697         if (reflog) {
698                 struct object_id oid;
699                 char *ref;
700                 int base = 0;
701                 unsigned int flags = 0;
702
703                 if (ac == 0) {
704                         static const char *fake_av[2];
705
706                         fake_av[0] = resolve_refdup("HEAD",
707                                                     RESOLVE_REF_READING,
708                                                     oid.hash, NULL);
709                         fake_av[1] = NULL;
710                         av = fake_av;
711                         ac = 1;
712                         if (!*av)
713                                 die(_("no branches given, and HEAD is not valid"));
714                 }
715                 if (ac != 1)
716                         die(_("--reflog option needs one branch name"));
717
718                 if (MAX_REVS < reflog)
719                         die(Q_("only %d entry can be shown at one time.",
720                                "only %d entries can be shown at one time.",
721                                MAX_REVS), MAX_REVS);
722                 if (!dwim_ref(*av, strlen(*av), oid.hash, &ref))
723                         die(_("no such ref %s"), *av);
724
725                 /* Has the base been specified? */
726                 if (reflog_base) {
727                         char *ep;
728                         base = strtoul(reflog_base, &ep, 10);
729                         if (*ep) {
730                                 /* Ah, that is a date spec... */
731                                 timestamp_t at;
732                                 at = approxidate(reflog_base);
733                                 read_ref_at(ref, flags, at, -1, oid.hash, NULL,
734                                             NULL, NULL, &base);
735                         }
736                 }
737
738                 for (i = 0; i < reflog; i++) {
739                         char *logmsg;
740                         char *nth_desc;
741                         const char *msg;
742                         timestamp_t timestamp;
743                         int tz;
744
745                         if (read_ref_at(ref, flags, 0, base+i, oid.hash, &logmsg,
746                                         &timestamp, &tz, NULL)) {
747                                 reflog = i;
748                                 break;
749                         }
750                         msg = strchr(logmsg, '\t');
751                         if (!msg)
752                                 msg = "(none)";
753                         else
754                                 msg++;
755                         reflog_msg[i] = xstrfmt("(%s) %s",
756                                                 show_date(timestamp, tz,
757                                                           DATE_MODE(RELATIVE)),
758                                                 msg);
759                         free(logmsg);
760
761                         nth_desc = xstrfmt("%s@{%d}", *av, base+i);
762                         append_ref(nth_desc, &oid, 1);
763                         free(nth_desc);
764                 }
765                 free(ref);
766         }
767         else {
768                 while (0 < ac) {
769                         append_one_rev(*av);
770                         ac--; av++;
771                 }
772                 if (all_heads + all_remotes)
773                         snarf_refs(all_heads, all_remotes);
774         }
775
776         head = resolve_refdup("HEAD", RESOLVE_REF_READING,
777                               head_oid.hash, NULL);
778
779         if (with_current_branch && head) {
780                 int has_head = 0;
781                 for (i = 0; !has_head && i < ref_name_cnt; i++) {
782                         /* We are only interested in adding the branch
783                          * HEAD points at.
784                          */
785                         if (rev_is_head(head,
786                                         ref_name[i],
787                                         head_oid.hash, NULL))
788                                 has_head++;
789                 }
790                 if (!has_head) {
791                         const char *name = head;
792                         skip_prefix(name, "refs/heads/", &name);
793                         append_one_rev(name);
794                 }
795         }
796
797         if (!ref_name_cnt) {
798                 fprintf(stderr, "No revs to be shown.\n");
799                 exit(0);
800         }
801
802         for (num_rev = 0; ref_name[num_rev]; num_rev++) {
803                 struct object_id revkey;
804                 unsigned int flag = 1u << (num_rev + REV_SHIFT);
805
806                 if (MAX_REVS <= num_rev)
807                         die(Q_("cannot handle more than %d rev.",
808                                "cannot handle more than %d revs.",
809                                MAX_REVS), MAX_REVS);
810                 if (get_sha1(ref_name[num_rev], revkey.hash))
811                         die(_("'%s' is not a valid ref."), ref_name[num_rev]);
812                 commit = lookup_commit_reference(&revkey);
813                 if (!commit)
814                         die(_("cannot find commit %s (%s)"),
815                             ref_name[num_rev], oid_to_hex(&revkey));
816                 parse_commit(commit);
817                 mark_seen(commit, &seen);
818
819                 /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
820                  * and so on.  REV_SHIFT bits from bit 0 are used for
821                  * internal bookkeeping.
822                  */
823                 commit->object.flags |= flag;
824                 if (commit->object.flags == flag)
825                         commit_list_insert_by_date(commit, &list);
826                 rev[num_rev] = commit;
827         }
828         for (i = 0; i < num_rev; i++)
829                 rev_mask[i] = rev[i]->object.flags;
830
831         if (0 <= extra)
832                 join_revs(&list, &seen, num_rev, extra);
833
834         commit_list_sort_by_date(&seen);
835
836         if (merge_base)
837                 return show_merge_base(seen, num_rev);
838
839         if (independent)
840                 return show_independent(rev, num_rev, ref_name, rev_mask);
841
842         /* Show list; --more=-1 means list-only */
843         if (1 < num_rev || extra < 0) {
844                 for (i = 0; i < num_rev; i++) {
845                         int j;
846                         int is_head = rev_is_head(head,
847                                                   ref_name[i],
848                                                   head_oid.hash,
849                                                   rev[i]->object.oid.hash);
850                         if (extra < 0)
851                                 printf("%c [%s] ",
852                                        is_head ? '*' : ' ', ref_name[i]);
853                         else {
854                                 for (j = 0; j < i; j++)
855                                         putchar(' ');
856                                 printf("%s%c%s [%s] ",
857                                        get_color_code(i),
858                                        is_head ? '*' : '!',
859                                        get_color_reset_code(), ref_name[i]);
860                         }
861
862                         if (!reflog) {
863                                 /* header lines never need name */
864                                 show_one_commit(rev[i], 1);
865                         }
866                         else
867                                 puts(reflog_msg[i]);
868
869                         if (is_head)
870                                 head_at = i;
871                 }
872                 if (0 <= extra) {
873                         for (i = 0; i < num_rev; i++)
874                                 putchar('-');
875                         putchar('\n');
876                 }
877         }
878         if (extra < 0)
879                 exit(0);
880
881         /* Sort topologically */
882         sort_in_topological_order(&seen, sort_order);
883
884         /* Give names to commits */
885         if (!sha1_name && !no_name)
886                 name_commits(seen, rev, ref_name, num_rev);
887
888         all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
889         all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
890
891         while (seen) {
892                 struct commit *commit = pop_commit(&seen);
893                 int this_flag = commit->object.flags;
894                 int is_merge_point = ((this_flag & all_revs) == all_revs);
895
896                 shown_merge_point |= is_merge_point;
897
898                 if (1 < num_rev) {
899                         int is_merge = !!(commit->parents &&
900                                           commit->parents->next);
901                         if (topics &&
902                             !is_merge_point &&
903                             (this_flag & (1u << REV_SHIFT)))
904                                 continue;
905                         if (dense && is_merge &&
906                             omit_in_dense(commit, rev, num_rev))
907                                 continue;
908                         for (i = 0; i < num_rev; i++) {
909                                 int mark;
910                                 if (!(this_flag & (1u << (i + REV_SHIFT))))
911                                         mark = ' ';
912                                 else if (is_merge)
913                                         mark = '-';
914                                 else if (i == head_at)
915                                         mark = '*';
916                                 else
917                                         mark = '+';
918                                 printf("%s%c%s",
919                                        get_color_code(i),
920                                        mark, get_color_reset_code());
921                         }
922                         putchar(' ');
923                 }
924                 show_one_commit(commit, no_name);
925
926                 if (shown_merge_point && --extra < 0)
927                         break;
928         }
929         return 0;
930 }