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