Merge branch 'mh/diff-stat-color'
[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
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";
11
12 static int showbranch_use_color = -1;
13 static char column_colors[][COLOR_MAXLEN] = {
14         GIT_COLOR_RED,
15         GIT_COLOR_GREEN,
16         GIT_COLOR_YELLOW,
17         GIT_COLOR_BLUE,
18         GIT_COLOR_MAGENTA,
19         GIT_COLOR_CYAN,
20 };
21
22 #define COLUMN_COLORS_MAX (ARRAY_SIZE(column_colors))
23
24 static int default_num;
25 static int default_alloc;
26 static const char **default_arg;
27
28 #define UNINTERESTING   01
29
30 #define REV_SHIFT        2
31 #define MAX_REVS        (FLAG_BITS - REV_SHIFT) /* should not exceed bits_per_int - REV_SHIFT */
32
33 #define DEFAULT_REFLOG  4
34
35 static const char *get_color_code(int idx)
36 {
37         if (showbranch_use_color)
38                 return column_colors[idx];
39         return "";
40 }
41
42 static const char *get_color_reset_code(void)
43 {
44         if (showbranch_use_color)
45                 return GIT_COLOR_RESET;
46         return "";
47 }
48
49 static struct commit *interesting(struct commit_list *list)
50 {
51         while (list) {
52                 struct commit *commit = list->item;
53                 list = list->next;
54                 if (commit->object.flags & UNINTERESTING)
55                         continue;
56                 return commit;
57         }
58         return NULL;
59 }
60
61 static struct commit *pop_one_commit(struct commit_list **list_p)
62 {
63         struct commit *commit;
64         struct commit_list *list;
65         list = *list_p;
66         commit = list->item;
67         *list_p = list->next;
68         free(list);
69         return commit;
70 }
71
72 struct commit_name {
73         const char *head_name; /* which head's ancestor? */
74         int generation; /* how many parents away from head_name */
75 };
76
77 /* Name the commit as nth generation ancestor of head_name;
78  * we count only the first-parent relationship for naming purposes.
79  */
80 static void name_commit(struct commit *commit, const char *head_name, int nth)
81 {
82         struct commit_name *name;
83         if (!commit->util)
84                 commit->util = xmalloc(sizeof(struct commit_name));
85         name = commit->util;
86         name->head_name = head_name;
87         name->generation = nth;
88 }
89
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.
94  */
95 static void name_parent(struct commit *commit, struct commit *parent)
96 {
97         struct commit_name *commit_name = commit->util;
98         struct commit_name *parent_name = parent->util;
99         if (!commit_name)
100                 return;
101         if (!parent_name ||
102             commit_name->generation + 1 < parent_name->generation)
103                 name_commit(parent, commit_name->head_name,
104                             commit_name->generation + 1);
105 }
106
107 static int name_first_parent_chain(struct commit *c)
108 {
109         int i = 0;
110         while (c) {
111                 struct commit *p;
112                 if (!c->util)
113                         break;
114                 if (!c->parents)
115                         break;
116                 p = c->parents->item;
117                 if (!p->util) {
118                         name_parent(c, p);
119                         i++;
120                 }
121                 else
122                         break;
123                 c = p;
124         }
125         return i;
126 }
127
128 static void name_commits(struct commit_list *list,
129                          struct commit **rev,
130                          char **ref_name,
131                          int num_rev)
132 {
133         struct commit_list *cl;
134         struct commit *c;
135         int i;
136
137         /* First give names to the given heads */
138         for (cl = list; cl; cl = cl->next) {
139                 c = cl->item;
140                 if (c->util)
141                         continue;
142                 for (i = 0; i < num_rev; i++) {
143                         if (rev[i] == c) {
144                                 name_commit(c, ref_name[i], 0);
145                                 break;
146                         }
147                 }
148         }
149
150         /* Then commits on the first parent ancestry chain */
151         do {
152                 i = 0;
153                 for (cl = list; cl; cl = cl->next) {
154                         i += name_first_parent_chain(cl->item);
155                 }
156         } while (i);
157
158         /* Finally, any unnamed commits */
159         do {
160                 i = 0;
161                 for (cl = list; cl; cl = cl->next) {
162                         struct commit_list *parents;
163                         struct commit_name *n;
164                         int nth;
165                         c = cl->item;
166                         if (!c->util)
167                                 continue;
168                         n = c->util;
169                         parents = c->parents;
170                         nth = 0;
171                         while (parents) {
172                                 struct commit *p = parents->item;
173                                 char newname[1000], *en;
174                                 parents = parents->next;
175                                 nth++;
176                                 if (p->util)
177                                         continue;
178                                 en = newname;
179                                 switch (n->generation) {
180                                 case 0:
181                                         en += sprintf(en, "%s", n->head_name);
182                                         break;
183                                 case 1:
184                                         en += sprintf(en, "%s^", n->head_name);
185                                         break;
186                                 default:
187                                         en += sprintf(en, "%s~%d",
188                                                 n->head_name, n->generation);
189                                         break;
190                                 }
191                                 if (nth == 1)
192                                         en += sprintf(en, "^");
193                                 else
194                                         en += sprintf(en, "^%d", nth);
195                                 name_commit(p, xstrdup(newname), 0);
196                                 i++;
197                                 name_first_parent_chain(p);
198                         }
199                 }
200         } while (i);
201 }
202
203 static int mark_seen(struct commit *commit, struct commit_list **seen_p)
204 {
205         if (!commit->object.flags) {
206                 commit_list_insert(commit, seen_p);
207                 return 1;
208         }
209         return 0;
210 }
211
212 static void join_revs(struct commit_list **list_p,
213                       struct commit_list **seen_p,
214                       int num_rev, int extra)
215 {
216         int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
217         int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
218
219         while (*list_p) {
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;
224
225                 if (!still_interesting && extra <= 0)
226                         break;
227
228                 mark_seen(commit, seen_p);
229                 if ((flags & all_revs) == all_revs)
230                         flags |= UNINTERESTING;
231                 parents = commit->parents;
232
233                 while (parents) {
234                         struct commit *p = parents->item;
235                         int this_flag = p->object.flags;
236                         parents = parents->next;
237                         if ((this_flag & flags) == flags)
238                                 continue;
239                         if (!p->object.parsed)
240                                 parse_commit(p);
241                         if (mark_seen(p, seen_p) && !still_interesting)
242                                 extra--;
243                         p->object.flags |= flags;
244                         insert_by_date(p, list_p);
245                 }
246         }
247
248         /*
249          * Postprocess to complete well-poisoning.
250          *
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.
254          */
255         for (;;) {
256                 int changed = 0;
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;
261
262                         if (((c->object.flags & all_revs) != all_revs) &&
263                             !(c->object.flags & UNINTERESTING))
264                                 continue;
265
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
270                          * here.
271                          */
272                         parents = c->parents;
273                         while (parents) {
274                                 struct commit *p = parents->item;
275                                 parents = parents->next;
276                                 if (!(p->object.flags & UNINTERESTING)) {
277                                         p->object.flags |= UNINTERESTING;
278                                         changed = 1;
279                                 }
280                         }
281                 }
282                 if (!changed)
283                         break;
284         }
285 }
286
287 static void show_one_commit(struct commit *commit, int no_name)
288 {
289         struct strbuf pretty = STRBUF_INIT;
290         const char *pretty_str = "(unavailable)";
291         struct commit_name *name = commit->util;
292
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;
297         }
298         if (!prefixcmp(pretty_str, "[PATCH] "))
299                 pretty_str += 8;
300
301         if (!no_name) {
302                 if (name && name->head_name) {
303                         printf("[%s", name->head_name);
304                         if (name->generation) {
305                                 if (name->generation == 1)
306                                         printf("^");
307                                 else
308                                         printf("~%d", name->generation);
309                         }
310                         printf("] ");
311                 }
312                 else
313                         printf("[%s] ",
314                                find_unique_abbrev(commit->object.sha1, 7));
315         }
316         puts(pretty_str);
317         strbuf_release(&pretty);
318 }
319
320 static char *ref_name[MAX_REVS + 1];
321 static int ref_name_cnt;
322
323 static const char *find_digit_prefix(const char *s, int *v)
324 {
325         const char *p;
326         int ver;
327         char ch;
328
329         for (p = s, ver = 0;
330              '0' <= (ch = *p) && ch <= '9';
331              p++)
332                 ver = ver * 10 + ch - '0';
333         *v = ver;
334         return p;
335 }
336
337
338 static int version_cmp(const char *a, const char *b)
339 {
340         while (1) {
341                 int va, vb;
342
343                 a = find_digit_prefix(a, &va);
344                 b = find_digit_prefix(b, &vb);
345                 if (va != vb)
346                         return va - vb;
347
348                 while (1) {
349                         int ca = *a;
350                         int cb = *b;
351                         if ('0' <= ca && ca <= '9')
352                                 ca = 0;
353                         if ('0' <= cb && cb <= '9')
354                                 cb = 0;
355                         if (ca != cb)
356                                 return ca - cb;
357                         if (!ca)
358                                 break;
359                         a++;
360                         b++;
361                 }
362                 if (!*a && !*b)
363                         return 0;
364         }
365 }
366
367 static int compare_ref_name(const void *a_, const void *b_)
368 {
369         const char * const*a = a_, * const*b = b_;
370         return version_cmp(*a, *b);
371 }
372
373 static void sort_ref_range(int bottom, int top)
374 {
375         qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]),
376               compare_ref_name);
377 }
378
379 static int append_ref(const char *refname, const unsigned char *sha1,
380                       int allow_dups)
381 {
382         struct commit *commit = lookup_commit_reference_gently(sha1, 1);
383         int i;
384
385         if (!commit)
386                 return 0;
387
388         if (!allow_dups) {
389                 /* Avoid adding the same thing twice */
390                 for (i = 0; i < ref_name_cnt; i++)
391                         if (!strcmp(refname, ref_name[i]))
392                                 return 0;
393         }
394         if (MAX_REVS <= ref_name_cnt) {
395                 warning("ignoring %s; cannot handle more than %d refs",
396                         refname, MAX_REVS);
397                 return 0;
398         }
399         ref_name[ref_name_cnt++] = xstrdup(refname);
400         ref_name[ref_name_cnt] = NULL;
401         return 0;
402 }
403
404 static int append_head_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
405 {
406         unsigned char tmp[20];
407         int ofs = 11;
408         if (prefixcmp(refname, "refs/heads/"))
409                 return 0;
410         /* If both heads/foo and tags/foo exists, get_sha1 would
411          * get confused.
412          */
413         if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1))
414                 ofs = 5;
415         return append_ref(refname + ofs, sha1, 0);
416 }
417
418 static int append_remote_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
419 {
420         unsigned char tmp[20];
421         int ofs = 13;
422         if (prefixcmp(refname, "refs/remotes/"))
423                 return 0;
424         /* If both heads/foo and tags/foo exists, get_sha1 would
425          * get confused.
426          */
427         if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1))
428                 ofs = 5;
429         return append_ref(refname + ofs, sha1, 0);
430 }
431
432 static int append_tag_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
433 {
434         if (prefixcmp(refname, "refs/tags/"))
435                 return 0;
436         return append_ref(refname + 5, sha1, 0);
437 }
438
439 static const char *match_ref_pattern = NULL;
440 static int match_ref_slash = 0;
441 static int count_slash(const char *s)
442 {
443         int cnt = 0;
444         while (*s)
445                 if (*s++ == '/')
446                         cnt++;
447         return cnt;
448 }
449
450 static int append_matching_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
451 {
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.
455          */
456         const char *tail;
457         int slash = count_slash(refname);
458         for (tail = refname; *tail && match_ref_slash < slash; )
459                 if (*tail++ == '/')
460                         slash--;
461         if (!*tail)
462                 return 0;
463         if (fnmatch(match_ref_pattern, tail, 0))
464                 return 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);
470 }
471
472 static void snarf_refs(int head, int remotes)
473 {
474         if (head) {
475                 int orig_cnt = ref_name_cnt;
476                 for_each_ref(append_head_ref, NULL);
477                 sort_ref_range(orig_cnt, ref_name_cnt);
478         }
479         if (remotes) {
480                 int orig_cnt = ref_name_cnt;
481                 for_each_ref(append_remote_ref, NULL);
482                 sort_ref_range(orig_cnt, ref_name_cnt);
483         }
484 }
485
486 static int rev_is_head(char *head, int headlen, char *name,
487                        unsigned char *head_sha1, unsigned char *sha1)
488 {
489         if ((!head[0]) ||
490             (head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
491                 return 0;
492         if (!prefixcmp(head, "refs/heads/"))
493                 head += 11;
494         if (!prefixcmp(name, "refs/heads/"))
495                 name += 11;
496         else if (!prefixcmp(name, "heads/"))
497                 name += 6;
498         return !strcmp(head, name);
499 }
500
501 static int show_merge_base(struct commit_list *seen, int num_rev)
502 {
503         int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
504         int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
505         int exit_status = 1;
506
507         while (seen) {
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));
513                         exit_status = 0;
514                         commit->object.flags |= UNINTERESTING;
515                 }
516         }
517         return exit_status;
518 }
519
520 static int show_independent(struct commit **rev,
521                             int num_rev,
522                             char **ref_name,
523                             unsigned int *rev_mask)
524 {
525         int i;
526
527         for (i = 0; i < num_rev; i++) {
528                 struct commit *commit = rev[i];
529                 unsigned int flag = rev_mask[i];
530
531                 if (commit->object.flags == flag)
532                         puts(sha1_to_hex(commit->object.sha1));
533                 commit->object.flags |= UNINTERESTING;
534         }
535         return 0;
536 }
537
538 static void append_one_rev(const char *av)
539 {
540         unsigned char revkey[20];
541         if (!get_sha1(av, revkey)) {
542                 append_ref(av, revkey, 0);
543                 return;
544         }
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);
556                 return;
557         }
558         die("bad sha1 reference %s", av);
559 }
560
561 static int git_show_branch_config(const char *var, const char *value, void *cb)
562 {
563         if (!strcmp(var, "showbranch.default")) {
564                 if (!value)
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);
569                 }
570                 default_arg[default_num++] = xstrdup(value);
571                 default_arg[default_num] = NULL;
572                 return 0;
573         }
574
575         if (!strcmp(var, "color.showbranch")) {
576                 showbranch_use_color = git_config_colorbool(var, value, -1);
577                 return 0;
578         }
579
580         return git_color_default_config(var, value, cb);
581 }
582
583 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
584 {
585         /* If the commit is tip of the named branches, do not
586          * omit it.
587          * Otherwise, if it is a merge that is reachable from only one
588          * tip, it is not that interesting.
589          */
590         int i, flag, count;
591         for (i = 0; i < n; i++)
592                 if (rev[i] == commit)
593                         return 0;
594         flag = commit->object.flags;
595         for (i = count = 0; i < n; i++) {
596                 if (flag & (1u << (i + REV_SHIFT)))
597                         count++;
598         }
599         if (count == 1)
600                 return 1;
601         return 0;
602 }
603
604 static void parse_reflog_param(const char *arg, int *cnt, const char **base)
605 {
606         char *ep;
607         *cnt = strtoul(arg, &ep, 10);
608         if (*ep == ',')
609                 *base = ep + 1;
610         else if (*ep)
611                 die("unrecognized reflog param '%s'", arg);
612         else
613                 *base = NULL;
614         if (*cnt <= 0)
615                 *cnt = DEFAULT_REFLOG;
616 }
617
618 int cmd_show_branch(int ac, const char **av, const char *prefix)
619 {
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;
627         int lifo = 1;
628         char head[128];
629         const char *head_p;
630         int head_len;
631         unsigned char head_sha1[20];
632         int merge_base = 0;
633         int independent = 0;
634         int no_name = 0;
635         int sha1_name = 0;
636         int shown_merge_point = 0;
637         int with_current_branch = 0;
638         int head_at = -1;
639         int topics = 0;
640         int dense = 1;
641         int reflog = 0;
642         const char *reflog_base = NULL;
643
644         git_config(git_show_branch_config, NULL);
645
646         if (showbranch_use_color == -1)
647                 showbranch_use_color = git_use_color_default;
648
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] */
653         }
654
655         while (1 < ac && av[1][0] == '-') {
656                 const char *arg = av[1];
657                 if (!strcmp(arg, "--")) {
658                         ac--; av++;
659                         break;
660                 }
661                 else if (!strcmp(arg, "--all") || !strcmp(arg, "-a"))
662                         all_heads = all_remotes = 1;
663                 else if (!strcmp(arg, "--remotes") || !strcmp(arg, "-r"))
664                         all_remotes = 1;
665                 else if (!strcmp(arg, "--more"))
666                         extra = 1;
667                 else if (!strcmp(arg, "--list"))
668                         extra = -1;
669                 else if (!strcmp(arg, "--no-name"))
670                         no_name = 1;
671                 else if (!strcmp(arg, "--current"))
672                         with_current_branch = 1;
673                 else if (!strcmp(arg, "--sha1-name"))
674                         sha1_name = 1;
675                 else if (!prefixcmp(arg, "--more="))
676                         extra = atoi(arg + 7);
677                 else if (!strcmp(arg, "--merge-base"))
678                         merge_base = 1;
679                 else if (!strcmp(arg, "--independent"))
680                         independent = 1;
681                 else if (!strcmp(arg, "--topo-order"))
682                         lifo = 1;
683                 else if (!strcmp(arg, "--topics"))
684                         topics = 1;
685                 else if (!strcmp(arg, "--sparse"))
686                         dense = 0;
687                 else if (!strcmp(arg, "--date-order"))
688                         lifo = 0;
689                 else if (!strcmp(arg, "--reflog") || !strcmp(arg, "-g")) {
690                         reflog = DEFAULT_REFLOG;
691                 }
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;
700                 else
701                         usage(show_branch_usage);
702                 ac--; av++;
703         }
704         ac--; av++;
705
706         if (extra || reflog) {
707                 /* "listing" mode is incompatible with
708                  * independent nor merge-base modes.
709                  */
710                 if (independent || merge_base)
711                         usage(show_branch_usage);
712                 if (reflog && ((0 < extra) || all_heads || all_remotes))
713                         /*
714                          * Asking for --more in reflog mode does not
715                          * make sense.  --list is Ok.
716                          *
717                          * Also --all and --remotes do not make sense either.
718                          */
719                         usage(show_branch_usage_reflog);
720         }
721
722         /* If nothing is specified, show all branches by default */
723         if (ac + all_heads + all_remotes == 0)
724                 all_heads = 1;
725
726         if (reflog) {
727                 unsigned char sha1[20];
728                 char nth_desc[256];
729                 char *ref;
730                 int base = 0;
731
732                 if (ac == 0) {
733                         static const char *fake_av[2];
734                         const char *refname;
735
736                         refname = resolve_ref("HEAD", sha1, 1, NULL);
737                         fake_av[0] = xstrdup(refname);
738                         fake_av[1] = NULL;
739                         av = fake_av;
740                         ac = 1;
741                 }
742                 if (ac != 1)
743                         die("--reflog option needs one branch name");
744
745                 if (MAX_REVS < reflog)
746                         die("Only %d entries can be shown at one time.",
747                             MAX_REVS);
748                 if (!dwim_ref(*av, strlen(*av), sha1, &ref))
749                         die("No such ref %s", *av);
750
751                 /* Has the base been specified? */
752                 if (reflog_base) {
753                         char *ep;
754                         base = strtoul(reflog_base, &ep, 10);
755                         if (*ep) {
756                                 /* Ah, that is a date spec... */
757                                 unsigned long at;
758                                 at = approxidate(reflog_base);
759                                 read_ref_at(ref, at, -1, sha1, NULL,
760                                             NULL, NULL, &base);
761                         }
762                 }
763
764                 for (i = 0; i < reflog; i++) {
765                         char *logmsg, *m;
766                         const char *msg;
767                         unsigned long timestamp;
768                         int tz;
769
770                         if (read_ref_at(ref, 0, base+i, sha1, &logmsg,
771                                         &timestamp, &tz, NULL)) {
772                                 reflog = i;
773                                 break;
774                         }
775                         msg = strchr(logmsg, '\t');
776                         if (!msg)
777                                 msg = "(none)";
778                         else
779                                 msg++;
780                         m = xmalloc(strlen(msg) + 200);
781                         sprintf(m, "(%s) %s",
782                                 show_date(timestamp, tz, 1),
783                                 msg);
784                         reflog_msg[i] = m;
785                         free(logmsg);
786                         sprintf(nth_desc, "%s@{%d}", *av, base+i);
787                         append_ref(nth_desc, sha1, 1);
788                 }
789         }
790         else if (all_heads + all_remotes)
791                 snarf_refs(all_heads, all_remotes);
792         else {
793                 while (0 < ac) {
794                         append_one_rev(*av);
795                         ac--; av++;
796                 }
797         }
798
799         head_p = resolve_ref("HEAD", head_sha1, 1, NULL);
800         if (head_p) {
801                 head_len = strlen(head_p);
802                 memcpy(head, head_p, head_len + 1);
803         }
804         else {
805                 head_len = 0;
806                 head[0] = 0;
807         }
808
809         if (with_current_branch && head_p) {
810                 int has_head = 0;
811                 for (i = 0; !has_head && i < ref_name_cnt; i++) {
812                         /* We are only interested in adding the branch
813                          * HEAD points at.
814                          */
815                         if (rev_is_head(head,
816                                         head_len,
817                                         ref_name[i],
818                                         head_sha1, NULL))
819                                 has_head++;
820                 }
821                 if (!has_head) {
822                         int offset = !prefixcmp(head, "refs/heads/") ? 11 : 0;
823                         append_one_rev(head + offset);
824                 }
825         }
826
827         if (!ref_name_cnt) {
828                 fprintf(stderr, "No revs to be shown.\n");
829                 exit(0);
830         }
831
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);
835
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);
841                 if (!commit)
842                         die("cannot find commit %s (%s)",
843                             ref_name[num_rev], revkey);
844                 parse_commit(commit);
845                 mark_seen(commit, &seen);
846
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.
850                  */
851                 commit->object.flags |= flag;
852                 if (commit->object.flags == flag)
853                         insert_by_date(commit, &list);
854                 rev[num_rev] = commit;
855         }
856         for (i = 0; i < num_rev; i++)
857                 rev_mask[i] = rev[i]->object.flags;
858
859         if (0 <= extra)
860                 join_revs(&list, &seen, num_rev, extra);
861
862         sort_by_date(&seen);
863
864         if (merge_base)
865                 return show_merge_base(seen, num_rev);
866
867         if (independent)
868                 return show_independent(rev, num_rev, ref_name, rev_mask);
869
870         /* Show list; --more=-1 means list-only */
871         if (1 < num_rev || extra < 0) {
872                 for (i = 0; i < num_rev; i++) {
873                         int j;
874                         int is_head = rev_is_head(head,
875                                                   head_len,
876                                                   ref_name[i],
877                                                   head_sha1,
878                                                   rev[i]->object.sha1);
879                         if (extra < 0)
880                                 printf("%c [%s] ",
881                                        is_head ? '*' : ' ', ref_name[i]);
882                         else {
883                                 for (j = 0; j < i; j++)
884                                         putchar(' ');
885                                 printf("%s%c%s [%s] ",
886                                        get_color_code(i % COLUMN_COLORS_MAX),
887                                        is_head ? '*' : '!',
888                                        get_color_reset_code(), ref_name[i]);
889                         }
890
891                         if (!reflog) {
892                                 /* header lines never need name */
893                                 show_one_commit(rev[i], 1);
894                         }
895                         else
896                                 puts(reflog_msg[i]);
897
898                         if (is_head)
899                                 head_at = i;
900                 }
901                 if (0 <= extra) {
902                         for (i = 0; i < num_rev; i++)
903                                 putchar('-');
904                         putchar('\n');
905                 }
906         }
907         if (extra < 0)
908                 exit(0);
909
910         /* Sort topologically */
911         sort_in_topological_order(&seen, lifo);
912
913         /* Give names to commits */
914         if (!sha1_name && !no_name)
915                 name_commits(seen, rev, ref_name, num_rev);
916
917         all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
918         all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
919
920         while (seen) {
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);
924
925                 shown_merge_point |= is_merge_point;
926
927                 if (1 < num_rev) {
928                         int is_merge = !!(commit->parents &&
929                                           commit->parents->next);
930                         if (topics &&
931                             !is_merge_point &&
932                             (this_flag & (1u << REV_SHIFT)))
933                                 continue;
934                         if (dense && is_merge &&
935                             omit_in_dense(commit, rev, num_rev))
936                                 continue;
937                         for (i = 0; i < num_rev; i++) {
938                                 int mark;
939                                 if (!(this_flag & (1u << (i + REV_SHIFT))))
940                                         mark = ' ';
941                                 else if (is_merge)
942                                         mark = '-';
943                                 else if (i == head_at)
944                                         mark = '*';
945                                 else
946                                         mark = '+';
947                                 printf("%s%c%s",
948                                        get_color_code(i % COLUMN_COLORS_MAX),
949                                        mark, get_color_reset_code());
950                         }
951                         putchar(' ');
952                 }
953                 show_one_commit(commit, no_name);
954
955                 if (shown_merge_point && --extra < 0)
956                         break;
957         }
958         return 0;
959 }