Make "git help" sort git commands in columns
[git/jrn.git] / show-branch.c
blobc7422460be0a68dc34baa1dc8b66aa44559d56de
1 #include <stdlib.h>
2 #include <fnmatch.h>
3 #include "cache.h"
4 #include "commit.h"
5 #include "refs.h"
7 static const char show_branch_usage[] =
8 "git-show-branch [--all] [--heads] [--tags] [--topo-order] [--more=count | --list | --independent | --merge-base ] [<refs>...]";
10 #define UNINTERESTING 01
12 #define REV_SHIFT 2
13 #define MAX_REVS 29 /* should not exceed bits_per_int - REV_SHIFT */
15 static struct commit *interesting(struct commit_list *list)
17 while (list) {
18 struct commit *commit = list->item;
19 list = list->next;
20 if (commit->object.flags & UNINTERESTING)
21 continue;
22 return commit;
24 return NULL;
27 static struct commit *pop_one_commit(struct commit_list **list_p)
29 struct commit *commit;
30 struct commit_list *list;
31 list = *list_p;
32 commit = list->item;
33 *list_p = list->next;
34 free(list);
35 return commit;
38 struct commit_name {
39 const char *head_name; /* which head's ancestor? */
40 int generation; /* how many parents away from head_name */
43 /* Name the commit as nth generation ancestor of head_name;
44 * we count only the first-parent relationship for naming purposes.
46 static void name_commit(struct commit *commit, const char *head_name, int nth)
48 struct commit_name *name;
49 if (!commit->object.util)
50 commit->object.util = xmalloc(sizeof(struct commit_name));
51 name = commit->object.util;
52 name->head_name = head_name;
53 name->generation = nth;
56 /* Parent is the first parent of the commit. We may name it
57 * as (n+1)th generation ancestor of the same head_name as
58 * commit is nth generation ancestor of, if that generation
59 * number is better than the name it already has.
61 static void name_parent(struct commit *commit, struct commit *parent)
63 struct commit_name *commit_name = commit->object.util;
64 struct commit_name *parent_name = parent->object.util;
65 if (!commit_name)
66 return;
67 if (!parent_name ||
68 commit_name->generation + 1 < parent_name->generation)
69 name_commit(parent, commit_name->head_name,
70 commit_name->generation + 1);
73 static int name_first_parent_chain(struct commit *c)
75 int i = 0;
76 while (c) {
77 struct commit *p;
78 if (!c->object.util)
79 break;
80 if (!c->parents)
81 break;
82 p = c->parents->item;
83 if (!p->object.util) {
84 name_parent(c, p);
85 i++;
87 c = p;
89 return i;
92 static void name_commits(struct commit_list *list,
93 struct commit **rev,
94 char **ref_name,
95 int num_rev)
97 struct commit_list *cl;
98 struct commit *c;
99 int i;
101 /* First give names to the given heads */
102 for (cl = list; cl; cl = cl->next) {
103 c = cl->item;
104 if (c->object.util)
105 continue;
106 for (i = 0; i < num_rev; i++) {
107 if (rev[i] == c) {
108 name_commit(c, ref_name[i], 0);
109 break;
114 /* Then commits on the first parent ancestry chain */
115 do {
116 i = 0;
117 for (cl = list; cl; cl = cl->next) {
118 i += name_first_parent_chain(cl->item);
120 } while (i);
122 /* Finally, any unnamed commits */
123 do {
124 i = 0;
125 for (cl = list; cl; cl = cl->next) {
126 struct commit_list *parents;
127 struct commit_name *n;
128 int nth;
129 c = cl->item;
130 if (!c->object.util)
131 continue;
132 n = c->object.util;
133 parents = c->parents;
134 nth = 0;
135 while (parents) {
136 struct commit *p = parents->item;
137 char newname[1000], *en;
138 parents = parents->next;
139 nth++;
140 if (p->object.util)
141 continue;
142 en = newname;
143 switch (n->generation) {
144 case 0:
145 en += sprintf(en, "%s", n->head_name);
146 break;
147 case 1:
148 en += sprintf(en, "%s^", n->head_name);
149 break;
150 default:
151 en += sprintf(en, "%s~%d",
152 n->head_name, n->generation);
153 break;
155 if (nth == 1)
156 en += sprintf(en, "^");
157 else
158 en += sprintf(en, "^%d", nth);
159 name_commit(p, strdup(newname), 0);
160 i++;
161 name_first_parent_chain(p);
164 } while (i);
167 static int mark_seen(struct commit *commit, struct commit_list **seen_p)
169 if (!commit->object.flags) {
170 insert_by_date(commit, seen_p);
171 return 1;
173 return 0;
176 static void join_revs(struct commit_list **list_p,
177 struct commit_list **seen_p,
178 int num_rev, int extra)
180 int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
181 int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
183 while (*list_p) {
184 struct commit_list *parents;
185 int still_interesting = !!interesting(*list_p);
186 struct commit *commit = pop_one_commit(list_p);
187 int flags = commit->object.flags & all_mask;
189 if (!still_interesting && extra <= 0)
190 break;
192 mark_seen(commit, seen_p);
193 if ((flags & all_revs) == all_revs)
194 flags |= UNINTERESTING;
195 parents = commit->parents;
197 while (parents) {
198 struct commit *p = parents->item;
199 int this_flag = p->object.flags;
200 parents = parents->next;
201 if ((this_flag & flags) == flags)
202 continue;
203 if (!p->object.parsed)
204 parse_commit(p);
205 if (mark_seen(p, seen_p) && !still_interesting)
206 extra--;
207 p->object.flags |= flags;
208 insert_by_date(p, list_p);
213 * Postprocess to complete well-poisoning.
215 * At this point we have all the commits we have seen in
216 * seen_p list (which happens to be sorted chronologically but
217 * it does not really matter). Mark anything that can be
218 * reached from uninteresting commits not interesting.
220 for (;;) {
221 int changed = 0;
222 struct commit_list *s;
223 for (s = *seen_p; s; s = s->next) {
224 struct commit *c = s->item;
225 struct commit_list *parents;
227 if (((c->object.flags & all_revs) != all_revs) &&
228 !(c->object.flags & UNINTERESTING))
229 continue;
231 /* The current commit is either a merge base or
232 * already uninteresting one. Mark its parents
233 * as uninteresting commits _only_ if they are
234 * already parsed. No reason to find new ones
235 * here.
237 parents = c->parents;
238 while (parents) {
239 struct commit *p = parents->item;
240 parents = parents->next;
241 if (!(p->object.flags & UNINTERESTING)) {
242 p->object.flags |= UNINTERESTING;
243 changed = 1;
247 if (!changed)
248 break;
252 static void show_one_commit(struct commit *commit, int no_name)
254 char pretty[256], *cp;
255 struct commit_name *name = commit->object.util;
256 if (commit->object.parsed)
257 pretty_print_commit(CMIT_FMT_ONELINE, commit->buffer, ~0,
258 pretty, sizeof(pretty));
259 else
260 strcpy(pretty, "(unavailable)");
261 if (!strncmp(pretty, "[PATCH] ", 8))
262 cp = pretty + 8;
263 else
264 cp = pretty;
266 if (!no_name) {
267 if (name && name->head_name) {
268 printf("[%s", name->head_name);
269 if (name->generation) {
270 if (name->generation == 1)
271 printf("^");
272 else
273 printf("~%d", name->generation);
275 printf("] ");
277 else
278 printf("[%s] ",
279 find_unique_abbrev(commit->object.sha1, 7));
281 puts(cp);
284 static char *ref_name[MAX_REVS + 1];
285 static int ref_name_cnt;
287 static int compare_ref_name(const void *a_, const void *b_)
289 const char * const*a = a_, * const*b = b_;
290 return strcmp(*a, *b);
293 static void sort_ref_range(int bottom, int top)
295 qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]),
296 compare_ref_name);
299 static int append_ref(const char *refname, const unsigned char *sha1)
301 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
302 if (!commit)
303 return 0;
304 if (MAX_REVS <= ref_name_cnt) {
305 fprintf(stderr, "warning: ignoring %s; "
306 "cannot handle more than %d refs",
307 refname, MAX_REVS);
308 return 0;
310 ref_name[ref_name_cnt++] = strdup(refname);
311 ref_name[ref_name_cnt] = NULL;
312 return 0;
315 static int append_head_ref(const char *refname, const unsigned char *sha1)
317 unsigned char tmp[20];
318 int ofs = 11;
319 if (strncmp(refname, "refs/heads/", ofs))
320 return 0;
321 /* If both heads/foo and tags/foo exists, get_sha1 would
322 * get confused.
324 if (get_sha1(refname + ofs, tmp) || memcmp(tmp, sha1, 20))
325 ofs = 5;
326 return append_ref(refname + ofs, sha1);
329 static int append_tag_ref(const char *refname, const unsigned char *sha1)
331 if (strncmp(refname, "refs/tags/", 10))
332 return 0;
333 return append_ref(refname + 5, sha1);
336 static const char *match_ref_pattern = NULL;
337 static int match_ref_slash = 0;
338 static int count_slash(const char *s)
340 int cnt = 0;
341 while (*s)
342 if (*s++ == '/')
343 cnt++;
344 return cnt;
347 static int append_matching_ref(const char *refname, const unsigned char *sha1)
349 /* we want to allow pattern hold/<asterisk> to show all
350 * branches under refs/heads/hold/, and v0.99.9? to show
351 * refs/tags/v0.99.9a and friends.
353 const char *tail;
354 int slash = count_slash(refname);
355 for (tail = refname; *tail && match_ref_slash < slash; )
356 if (*tail++ == '/')
357 slash--;
358 if (!*tail)
359 return 0;
360 if (fnmatch(match_ref_pattern, tail, 0))
361 return 0;
362 if (!strncmp("refs/heads/", refname, 11))
363 return append_head_ref(refname, sha1);
364 if (!strncmp("refs/tags/", refname, 10))
365 return append_tag_ref(refname, sha1);
366 return append_ref(refname, sha1);
369 static void snarf_refs(int head, int tag)
371 if (head) {
372 int orig_cnt = ref_name_cnt;
373 for_each_ref(append_head_ref);
374 sort_ref_range(orig_cnt, ref_name_cnt);
376 if (tag) {
377 int orig_cnt = ref_name_cnt;
378 for_each_ref(append_tag_ref);
379 sort_ref_range(orig_cnt, ref_name_cnt);
383 static int rev_is_head(char *head_path, int headlen,
384 char *name,
385 unsigned char *head_sha1, unsigned char *sha1)
387 int namelen;
388 if ((!head_path[0]) || memcmp(head_sha1, sha1, 20))
389 return 0;
390 namelen = strlen(name);
391 if ((headlen < namelen) ||
392 memcmp(head_path + headlen - namelen, name, namelen))
393 return 0;
394 if (headlen == namelen ||
395 head_path[headlen - namelen - 1] == '/')
396 return 1;
397 return 0;
400 static int show_merge_base(struct commit_list *seen, int num_rev)
402 int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
403 int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
404 int exit_status = 1;
406 while (seen) {
407 struct commit *commit = pop_one_commit(&seen);
408 int flags = commit->object.flags & all_mask;
409 if (!(flags & UNINTERESTING) &&
410 ((flags & all_revs) == all_revs)) {
411 puts(sha1_to_hex(commit->object.sha1));
412 exit_status = 0;
413 commit->object.flags |= UNINTERESTING;
416 return exit_status;
419 static int show_independent(struct commit **rev,
420 int num_rev,
421 char **ref_name,
422 unsigned int *rev_mask)
424 int i;
426 for (i = 0; i < num_rev; i++) {
427 struct commit *commit = rev[i];
428 unsigned int flag = rev_mask[i];
430 if (commit->object.flags == flag)
431 puts(sha1_to_hex(commit->object.sha1));
432 commit->object.flags |= UNINTERESTING;
434 return 0;
437 static void append_one_rev(const char *av)
439 unsigned char revkey[20];
440 if (!get_sha1(av, revkey)) {
441 append_ref(av, revkey);
442 return;
444 if (strchr(av, '*') || strchr(av, '?')) {
445 /* glob style match */
446 int saved_matches = ref_name_cnt;
447 match_ref_pattern = av;
448 match_ref_slash = count_slash(av);
449 for_each_ref(append_matching_ref);
450 if (saved_matches == ref_name_cnt &&
451 ref_name_cnt < MAX_REVS)
452 error("no matching refs with %s", av);
453 if (saved_matches + 1 < ref_name_cnt)
454 sort_ref_range(saved_matches, ref_name_cnt);
455 return;
457 die("bad sha1 reference %s", av);
460 int main(int ac, char **av)
462 struct commit *rev[MAX_REVS], *commit;
463 struct commit_list *list = NULL, *seen = NULL;
464 unsigned int rev_mask[MAX_REVS];
465 int num_rev, i, extra = 0;
466 int all_heads = 0, all_tags = 0;
467 int all_mask, all_revs;
468 char head_path[128];
469 const char *head_path_p;
470 int head_path_len;
471 unsigned char head_sha1[20];
472 int merge_base = 0;
473 int independent = 0;
474 int no_name = 0;
475 int sha1_name = 0;
476 int shown_merge_point = 0;
477 int topo_order = 0;
479 setup_git_directory();
481 while (1 < ac && av[1][0] == '-') {
482 char *arg = av[1];
483 if (!strcmp(arg, "--all"))
484 all_heads = all_tags = 1;
485 else if (!strcmp(arg, "--heads"))
486 all_heads = 1;
487 else if (!strcmp(arg, "--tags"))
488 all_tags = 1;
489 else if (!strcmp(arg, "--more"))
490 extra = 1;
491 else if (!strcmp(arg, "--list"))
492 extra = -1;
493 else if (!strcmp(arg, "--no-name"))
494 no_name = 1;
495 else if (!strcmp(arg, "--sha1-name"))
496 sha1_name = 1;
497 else if (!strncmp(arg, "--more=", 7))
498 extra = atoi(arg + 7);
499 else if (!strcmp(arg, "--merge-base"))
500 merge_base = 1;
501 else if (!strcmp(arg, "--independent"))
502 independent = 1;
503 else if (!strcmp(arg, "--topo-order"))
504 topo_order = 1;
505 else
506 usage(show_branch_usage);
507 ac--; av++;
509 ac--; av++;
511 /* Only one of these is allowed */
512 if (1 < independent + merge_base + (extra != 0))
513 usage(show_branch_usage);
515 if (all_heads + all_tags)
516 snarf_refs(all_heads, all_tags);
518 if (ac) {
519 while (0 < ac) {
520 append_one_rev(*av);
521 ac--; av++;
524 else {
525 /* If no revs given, then add heads */
526 snarf_refs(1, 0);
528 if (!ref_name_cnt) {
529 fprintf(stderr, "No revs to be shown.\n");
530 exit(0);
533 for (num_rev = 0; ref_name[num_rev]; num_rev++) {
534 unsigned char revkey[20];
535 unsigned int flag = 1u << (num_rev + REV_SHIFT);
537 if (MAX_REVS <= num_rev)
538 die("cannot handle more than %d revs.", MAX_REVS);
539 if (get_sha1(ref_name[num_rev], revkey))
540 die("'%s' is not a valid ref.\n", ref_name[num_rev]);
541 commit = lookup_commit_reference(revkey);
542 if (!commit)
543 die("cannot find commit %s (%s)",
544 ref_name[num_rev], revkey);
545 parse_commit(commit);
546 mark_seen(commit, &seen);
548 /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
549 * and so on. REV_SHIFT bits from bit 0 are used for
550 * internal bookkeeping.
552 commit->object.flags |= flag;
553 if (commit->object.flags == flag)
554 insert_by_date(commit, &list);
555 rev[num_rev] = commit;
557 for (i = 0; i < num_rev; i++)
558 rev_mask[i] = rev[i]->object.flags;
560 if (0 <= extra)
561 join_revs(&list, &seen, num_rev, extra);
563 head_path_p = resolve_ref(git_path("HEAD"), head_sha1, 1);
564 if (head_path_p) {
565 head_path_len = strlen(head_path_p);
566 memcpy(head_path, head_path_p, head_path_len + 1);
568 else {
569 head_path_len = 0;
570 head_path[0] = 0;
573 if (merge_base)
574 return show_merge_base(seen, num_rev);
576 if (independent)
577 return show_independent(rev, num_rev, ref_name, rev_mask);
579 /* Show list; --more=-1 means list-only */
580 if (1 < num_rev || extra < 0) {
581 for (i = 0; i < num_rev; i++) {
582 int j;
583 int is_head = rev_is_head(head_path,
584 head_path_len,
585 ref_name[i],
586 head_sha1,
587 rev[i]->object.sha1);
588 if (extra < 0)
589 printf("%c [%s] ",
590 is_head ? '*' : ' ', ref_name[i]);
591 else {
592 for (j = 0; j < i; j++)
593 putchar(' ');
594 printf("%c [%s] ",
595 is_head ? '*' : '!', ref_name[i]);
597 /* header lines never need name */
598 show_one_commit(rev[i], 1);
600 if (0 <= extra) {
601 for (i = 0; i < num_rev; i++)
602 putchar('-');
603 putchar('\n');
606 if (extra < 0)
607 exit(0);
609 /* Sort topologically */
610 if (topo_order)
611 sort_in_topological_order(&seen);
613 /* Give names to commits */
614 if (!sha1_name && !no_name)
615 name_commits(seen, rev, ref_name, num_rev);
617 all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
618 all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
620 while (seen) {
621 struct commit *commit = pop_one_commit(&seen);
622 int this_flag = commit->object.flags;
624 shown_merge_point |= ((this_flag & all_revs) == all_revs);
626 if (1 < num_rev) {
627 for (i = 0; i < num_rev; i++)
628 putchar((this_flag & (1u << (i + REV_SHIFT)))
629 ? '+' : ' ');
630 putchar(' ');
632 show_one_commit(commit, no_name);
634 if (shown_merge_point && --extra < 0)
635 break;
637 return 0;