1#include <stdlib.h>
2#include <fnmatch.h>
3#include "cache.h"
4#include "commit.h"
5#include "refs.h"
6
7static const char show_branch_usage[] =
8"git-show-branch [--all] [--heads] [--tags] [--topo-order] [--more=count | --list | --independent | --merge-base ] [<refs>...]";
9
10#define UNINTERESTING 01
11
12#define REV_SHIFT 2
13#define MAX_REVS 29 /* should not exceed bits_per_int - REV_SHIFT */
14
15static struct commit *interesting(struct commit_list *list)
16{
17 while (list) {
18 struct commit *commit = list->item;
19 list = list->next;
20 if (commit->object.flags & UNINTERESTING)
21 continue;
22 return commit;
23 }
24 return NULL;
25}
26
27static struct commit *pop_one_commit(struct commit_list **list_p)
28{
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;
36}
37
38struct commit_name {
39 const char *head_name; /* which head's ancestor? */
40 int generation; /* how many parents away from head_name */
41};
42
43/* Name the commit as nth generation ancestor of head_name;
44 * we count only the first-parent relationship for naming purposes.
45 */
46static void name_commit(struct commit *commit, const char *head_name, int nth)
47{
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;
54}
55
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.
60 */
61static void name_parent(struct commit *commit, struct commit *parent)
62{
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);
71}
72
73static int name_first_parent_chain(struct commit *c)
74{
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++;
86 }
87 c = p;
88 }
89 return i;
90}
91
92static void name_commits(struct commit_list *list,
93 struct commit **rev,
94 char **ref_name,
95 int num_rev)
96{
97 struct commit_list *cl;
98 struct commit *c;
99 int i;
100
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;
110 }
111 }
112 }
113
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);
119 }
120 } while (i);
121
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;
154 }
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);
162 }
163 }
164 } while (i);
165}
166
167static int mark_seen(struct commit *commit, struct commit_list **seen_p)
168{
169 if (!commit->object.flags) {
170 insert_by_date(commit, seen_p);
171 return 1;
172 }
173 return 0;
174}
175
176static void join_revs(struct commit_list **list_p,
177 struct commit_list **seen_p,
178 int num_rev, int extra)
179{
180 int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
181 int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
182
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;
188
189 if (!still_interesting && extra <= 0)
190 break;
191
192 mark_seen(commit, seen_p);
193 if ((flags & all_revs) == all_revs)
194 flags |= UNINTERESTING;
195 parents = commit->parents;
196
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);
209 }
210 }
211
212 /*
213 * Postprocess to complete well-poisoning.
214 *
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.
219 */
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;
226
227 if (((c->object.flags & all_revs) != all_revs) &&
228 !(c->object.flags & UNINTERESTING))
229 continue;
230
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.
236 */
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;
244 }
245 }
246 }
247 if (!changed)
248 break;
249 }
250}
251
252static void show_one_commit(struct commit *commit, int no_name)
253{
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;
265
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);
274 }
275 printf("] ");
276 }
277 else
278 printf("[%s] ",
279 find_unique_abbrev(commit->object.sha1, 7));
280 }
281 puts(cp);
282}
283
284static char *ref_name[MAX_REVS + 1];
285static int ref_name_cnt;
286
287static const char *find_digit_prefix(const char *s, int *v)
288{
289 const char *p;
290 int ver;
291 char ch;
292
293 for (p = s, ver = 0;
294 '0' <= (ch = *p) && ch <= '9';
295 p++)
296 ver = ver * 10 + ch - '0';
297 *v = ver;
298 return p;
299}
300
301
302static int version_cmp(const char *a, const char *b)
303{
304 while (1) {
305 int va, vb;
306
307 a = find_digit_prefix(a, &va);
308 b = find_digit_prefix(b, &vb);
309 if (va != vb)
310 return va - vb;
311
312 while (1) {
313 int ca = *a;
314 int cb = *b;
315 if ('0' <= ca && ca <= '9')
316 ca = 0;
317 if ('0' <= cb && cb <= '9')
318 cb = 0;
319 if (ca != cb)
320 return ca - cb;
321 if (!ca)
322 break;
323 a++;
324 b++;
325 }
326 if (!*a && !*b)
327 return 0;
328 }
329}
330
331static int compare_ref_name(const void *a_, const void *b_)
332{
333 const char * const*a = a_, * const*b = b_;
334 return version_cmp(*a, *b);
335}
336
337static void sort_ref_range(int bottom, int top)
338{
339 qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]),
340 compare_ref_name);
341}
342
343static int append_ref(const char *refname, const unsigned char *sha1)
344{
345 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
346 int i;
347
348 if (!commit)
349 return 0;
350 /* Avoid adding the same thing twice */
351 for (i = 0; i < ref_name_cnt; i++)
352 if (!strcmp(refname, ref_name[i]))
353 return 0;
354
355 if (MAX_REVS <= ref_name_cnt) {
356 fprintf(stderr, "warning: ignoring %s; "
357 "cannot handle more than %d refs\n",
358 refname, MAX_REVS);
359 return 0;
360 }
361 ref_name[ref_name_cnt++] = strdup(refname);
362 ref_name[ref_name_cnt] = NULL;
363 return 0;
364}
365
366static int append_head_ref(const char *refname, const unsigned char *sha1)
367{
368 unsigned char tmp[20];
369 int ofs = 11;
370 if (strncmp(refname, "refs/heads/", ofs))
371 return 0;
372 /* If both heads/foo and tags/foo exists, get_sha1 would
373 * get confused.
374 */
375 if (get_sha1(refname + ofs, tmp) || memcmp(tmp, sha1, 20))
376 ofs = 5;
377 return append_ref(refname + ofs, sha1);
378}
379
380static int append_tag_ref(const char *refname, const unsigned char *sha1)
381{
382 if (strncmp(refname, "refs/tags/", 10))
383 return 0;
384 return append_ref(refname + 5, sha1);
385}
386
387static const char *match_ref_pattern = NULL;
388static int match_ref_slash = 0;
389static int count_slash(const char *s)
390{
391 int cnt = 0;
392 while (*s)
393 if (*s++ == '/')
394 cnt++;
395 return cnt;
396}
397
398static int append_matching_ref(const char *refname, const unsigned char *sha1)
399{
400 /* we want to allow pattern hold/<asterisk> to show all
401 * branches under refs/heads/hold/, and v0.99.9? to show
402 * refs/tags/v0.99.9a and friends.
403 */
404 const char *tail;
405 int slash = count_slash(refname);
406 for (tail = refname; *tail && match_ref_slash < slash; )
407 if (*tail++ == '/')
408 slash--;
409 if (!*tail)
410 return 0;
411 if (fnmatch(match_ref_pattern, tail, 0))
412 return 0;
413 if (!strncmp("refs/heads/", refname, 11))
414 return append_head_ref(refname, sha1);
415 if (!strncmp("refs/tags/", refname, 10))
416 return append_tag_ref(refname, sha1);
417 return append_ref(refname, sha1);
418}
419
420static void snarf_refs(int head, int tag)
421{
422 if (head) {
423 int orig_cnt = ref_name_cnt;
424 for_each_ref(append_head_ref);
425 sort_ref_range(orig_cnt, ref_name_cnt);
426 }
427 if (tag) {
428 int orig_cnt = ref_name_cnt;
429 for_each_ref(append_tag_ref);
430 sort_ref_range(orig_cnt, ref_name_cnt);
431 }
432}
433
434static int rev_is_head(char *head_path, int headlen,
435 char *name,
436 unsigned char *head_sha1, unsigned char *sha1)
437{
438 int namelen;
439 if ((!head_path[0]) || memcmp(head_sha1, sha1, 20))
440 return 0;
441 namelen = strlen(name);
442 if ((headlen < namelen) ||
443 memcmp(head_path + headlen - namelen, name, namelen))
444 return 0;
445 if (headlen == namelen ||
446 head_path[headlen - namelen - 1] == '/')
447 return 1;
448 return 0;
449}
450
451static int show_merge_base(struct commit_list *seen, int num_rev)
452{
453 int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
454 int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
455 int exit_status = 1;
456
457 while (seen) {
458 struct commit *commit = pop_one_commit(&seen);
459 int flags = commit->object.flags & all_mask;
460 if (!(flags & UNINTERESTING) &&
461 ((flags & all_revs) == all_revs)) {
462 puts(sha1_to_hex(commit->object.sha1));
463 exit_status = 0;
464 commit->object.flags |= UNINTERESTING;
465 }
466 }
467 return exit_status;
468}
469
470static int show_independent(struct commit **rev,
471 int num_rev,
472 char **ref_name,
473 unsigned int *rev_mask)
474{
475 int i;
476
477 for (i = 0; i < num_rev; i++) {
478 struct commit *commit = rev[i];
479 unsigned int flag = rev_mask[i];
480
481 if (commit->object.flags == flag)
482 puts(sha1_to_hex(commit->object.sha1));
483 commit->object.flags |= UNINTERESTING;
484 }
485 return 0;
486}
487
488static void append_one_rev(const char *av)
489{
490 unsigned char revkey[20];
491 if (!get_sha1(av, revkey)) {
492 append_ref(av, revkey);
493 return;
494 }
495 if (strchr(av, '*') || strchr(av, '?')) {
496 /* glob style match */
497 int saved_matches = ref_name_cnt;
498 match_ref_pattern = av;
499 match_ref_slash = count_slash(av);
500 for_each_ref(append_matching_ref);
501 if (saved_matches == ref_name_cnt &&
502 ref_name_cnt < MAX_REVS)
503 error("no matching refs with %s", av);
504 if (saved_matches + 1 < ref_name_cnt)
505 sort_ref_range(saved_matches, ref_name_cnt);
506 return;
507 }
508 die("bad sha1 reference %s", av);
509}
510
511int main(int ac, char **av)
512{
513 struct commit *rev[MAX_REVS], *commit;
514 struct commit_list *list = NULL, *seen = NULL;
515 unsigned int rev_mask[MAX_REVS];
516 int num_rev, i, extra = 0;
517 int all_heads = 0, all_tags = 0;
518 int all_mask, all_revs;
519 char head_path[128];
520 const char *head_path_p;
521 int head_path_len;
522 unsigned char head_sha1[20];
523 int merge_base = 0;
524 int independent = 0;
525 int no_name = 0;
526 int sha1_name = 0;
527 int shown_merge_point = 0;
528 int topo_order = 0;
529
530 setup_git_directory();
531
532 while (1 < ac && av[1][0] == '-') {
533 char *arg = av[1];
534 if (!strcmp(arg, "--all"))
535 all_heads = all_tags = 1;
536 else if (!strcmp(arg, "--heads"))
537 all_heads = 1;
538 else if (!strcmp(arg, "--tags"))
539 all_tags = 1;
540 else if (!strcmp(arg, "--more"))
541 extra = 1;
542 else if (!strcmp(arg, "--list"))
543 extra = -1;
544 else if (!strcmp(arg, "--no-name"))
545 no_name = 1;
546 else if (!strcmp(arg, "--sha1-name"))
547 sha1_name = 1;
548 else if (!strncmp(arg, "--more=", 7))
549 extra = atoi(arg + 7);
550 else if (!strcmp(arg, "--merge-base"))
551 merge_base = 1;
552 else if (!strcmp(arg, "--independent"))
553 independent = 1;
554 else if (!strcmp(arg, "--topo-order"))
555 topo_order = 1;
556 else
557 usage(show_branch_usage);
558 ac--; av++;
559 }
560 ac--; av++;
561
562 /* Only one of these is allowed */
563 if (1 < independent + merge_base + (extra != 0))
564 usage(show_branch_usage);
565
566 /* If nothing is specified, show all branches by default */
567 if (ac + all_heads + all_tags == 0)
568 all_heads = 1;
569
570 if (all_heads + all_tags)
571 snarf_refs(all_heads, all_tags);
572 while (0 < ac) {
573 append_one_rev(*av);
574 ac--; av++;
575 }
576
577 if (!ref_name_cnt) {
578 fprintf(stderr, "No revs to be shown.\n");
579 exit(0);
580 }
581
582 for (num_rev = 0; ref_name[num_rev]; num_rev++) {
583 unsigned char revkey[20];
584 unsigned int flag = 1u << (num_rev + REV_SHIFT);
585
586 if (MAX_REVS <= num_rev)
587 die("cannot handle more than %d revs.", MAX_REVS);
588 if (get_sha1(ref_name[num_rev], revkey))
589 die("'%s' is not a valid ref.", ref_name[num_rev]);
590 commit = lookup_commit_reference(revkey);
591 if (!commit)
592 die("cannot find commit %s (%s)",
593 ref_name[num_rev], revkey);
594 parse_commit(commit);
595 mark_seen(commit, &seen);
596
597 /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
598 * and so on. REV_SHIFT bits from bit 0 are used for
599 * internal bookkeeping.
600 */
601 commit->object.flags |= flag;
602 if (commit->object.flags == flag)
603 insert_by_date(commit, &list);
604 rev[num_rev] = commit;
605 }
606 for (i = 0; i < num_rev; i++)
607 rev_mask[i] = rev[i]->object.flags;
608
609 if (0 <= extra)
610 join_revs(&list, &seen, num_rev, extra);
611
612 head_path_p = resolve_ref(git_path("HEAD"), head_sha1, 1);
613 if (head_path_p) {
614 head_path_len = strlen(head_path_p);
615 memcpy(head_path, head_path_p, head_path_len + 1);
616 }
617 else {
618 head_path_len = 0;
619 head_path[0] = 0;
620 }
621
622 if (merge_base)
623 return show_merge_base(seen, num_rev);
624
625 if (independent)
626 return show_independent(rev, num_rev, ref_name, rev_mask);
627
628 /* Show list; --more=-1 means list-only */
629 if (1 < num_rev || extra < 0) {
630 for (i = 0; i < num_rev; i++) {
631 int j;
632 int is_head = rev_is_head(head_path,
633 head_path_len,
634 ref_name[i],
635 head_sha1,
636 rev[i]->object.sha1);
637 if (extra < 0)
638 printf("%c [%s] ",
639 is_head ? '*' : ' ', ref_name[i]);
640 else {
641 for (j = 0; j < i; j++)
642 putchar(' ');
643 printf("%c [%s] ",
644 is_head ? '*' : '!', ref_name[i]);
645 }
646 /* header lines never need name */
647 show_one_commit(rev[i], 1);
648 }
649 if (0 <= extra) {
650 for (i = 0; i < num_rev; i++)
651 putchar('-');
652 putchar('\n');
653 }
654 }
655 if (extra < 0)
656 exit(0);
657
658 /* Sort topologically */
659 if (topo_order)
660 sort_in_topological_order(&seen);
661
662 /* Give names to commits */
663 if (!sha1_name && !no_name)
664 name_commits(seen, rev, ref_name, num_rev);
665
666 all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
667 all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
668
669 while (seen) {
670 struct commit *commit = pop_one_commit(&seen);
671 int this_flag = commit->object.flags;
672
673 shown_merge_point |= ((this_flag & all_revs) == all_revs);
674
675 if (1 < num_rev) {
676 for (i = 0; i < num_rev; i++)
677 putchar((this_flag & (1u << (i + REV_SHIFT)))
678 ? '+' : ' ');
679 putchar(' ');
680 }
681 show_one_commit(commit, no_name);
682
683 if (shown_merge_point && --extra < 0)
684 break;
685 }
686 return 0;
687}