Code

Don't use memcpy when source and dest. buffers may overlap
[git.git] / builtin-show-branch.c
1 #include <stdlib.h>
2 #include <fnmatch.h>
3 #include "cache.h"
4 #include "commit.h"
5 #include "refs.h"
6 #include "builtin.h"
8 static const char show_branch_usage[] =
9 "git-show-branch [--sparse] [--current] [--all] [--heads] [--tags] [--topo-order] [--more=count | --list | --independent | --merge-base ] [--topics] [<refs>...]";
11 static int default_num;
12 static int default_alloc;
13 static const char **default_arg;
15 #define UNINTERESTING   01
17 #define REV_SHIFT        2
18 #define MAX_REVS        (FLAG_BITS - REV_SHIFT) /* should not exceed bits_per_int - REV_SHIFT */
20 static struct commit *interesting(struct commit_list *list)
21 {
22         while (list) {
23                 struct commit *commit = list->item;
24                 list = list->next;
25                 if (commit->object.flags & UNINTERESTING)
26                         continue;
27                 return commit;
28         }
29         return NULL;
30 }
32 static struct commit *pop_one_commit(struct commit_list **list_p)
33 {
34         struct commit *commit;
35         struct commit_list *list;
36         list = *list_p;
37         commit = list->item;
38         *list_p = list->next;
39         free(list);
40         return commit;
41 }
43 struct commit_name {
44         const char *head_name; /* which head's ancestor? */
45         int generation; /* how many parents away from head_name */
46 };
48 /* Name the commit as nth generation ancestor of head_name;
49  * we count only the first-parent relationship for naming purposes.
50  */
51 static void name_commit(struct commit *commit, const char *head_name, int nth)
52 {
53         struct commit_name *name;
54         if (!commit->util)
55                 commit->util = xmalloc(sizeof(struct commit_name));
56         name = commit->util;
57         name->head_name = head_name;
58         name->generation = nth;
59 }
61 /* Parent is the first parent of the commit.  We may name it
62  * as (n+1)th generation ancestor of the same head_name as
63  * commit is nth generation ancestor of, if that generation
64  * number is better than the name it already has.
65  */
66 static void name_parent(struct commit *commit, struct commit *parent)
67 {
68         struct commit_name *commit_name = commit->util;
69         struct commit_name *parent_name = parent->util;
70         if (!commit_name)
71                 return;
72         if (!parent_name ||
73             commit_name->generation + 1 < parent_name->generation)
74                 name_commit(parent, commit_name->head_name,
75                             commit_name->generation + 1);
76 }
78 static int name_first_parent_chain(struct commit *c)
79 {
80         int i = 0;
81         while (c) {
82                 struct commit *p;
83                 if (!c->util)
84                         break;
85                 if (!c->parents)
86                         break;
87                 p = c->parents->item;
88                 if (!p->util) {
89                         name_parent(c, p);
90                         i++;
91                 }
92                 else
93                         break;
94                 c = p;
95         }
96         return i;
97 }
99 static void name_commits(struct commit_list *list,
100                          struct commit **rev,
101                          char **ref_name,
102                          int num_rev)
104         struct commit_list *cl;
105         struct commit *c;
106         int i;
108         /* First give names to the given heads */
109         for (cl = list; cl; cl = cl->next) {
110                 c = cl->item;
111                 if (c->util)
112                         continue;
113                 for (i = 0; i < num_rev; i++) {
114                         if (rev[i] == c) {
115                                 name_commit(c, ref_name[i], 0);
116                                 break;
117                         }
118                 }
119         }
121         /* Then commits on the first parent ancestry chain */
122         do {
123                 i = 0;
124                 for (cl = list; cl; cl = cl->next) {
125                         i += name_first_parent_chain(cl->item);
126                 }
127         } while (i);
129         /* Finally, any unnamed commits */
130         do {
131                 i = 0;
132                 for (cl = list; cl; cl = cl->next) {
133                         struct commit_list *parents;
134                         struct commit_name *n;
135                         int nth;
136                         c = cl->item;
137                         if (!c->util)
138                                 continue;
139                         n = c->util;
140                         parents = c->parents;
141                         nth = 0;
142                         while (parents) {
143                                 struct commit *p = parents->item;
144                                 char newname[1000], *en;
145                                 parents = parents->next;
146                                 nth++;
147                                 if (p->util)
148                                         continue;
149                                 en = newname;
150                                 switch (n->generation) {
151                                 case 0:
152                                         en += sprintf(en, "%s", n->head_name);
153                                         break;
154                                 case 1:
155                                         en += sprintf(en, "%s^", n->head_name);
156                                         break;
157                                 default:
158                                         en += sprintf(en, "%s~%d",
159                                                 n->head_name, n->generation);
160                                         break;
161                                 }
162                                 if (nth == 1)
163                                         en += sprintf(en, "^");
164                                 else
165                                         en += sprintf(en, "^%d", nth);
166                                 name_commit(p, xstrdup(newname), 0);
167                                 i++;
168                                 name_first_parent_chain(p);
169                         }
170                 }
171         } while (i);
174 static int mark_seen(struct commit *commit, struct commit_list **seen_p)
176         if (!commit->object.flags) {
177                 commit_list_insert(commit, seen_p);
178                 return 1;
179         }
180         return 0;
183 static void join_revs(struct commit_list **list_p,
184                       struct commit_list **seen_p,
185                       int num_rev, int extra)
187         int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
188         int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
190         while (*list_p) {
191                 struct commit_list *parents;
192                 int still_interesting = !!interesting(*list_p);
193                 struct commit *commit = pop_one_commit(list_p);
194                 int flags = commit->object.flags & all_mask;
196                 if (!still_interesting && extra <= 0)
197                         break;
199                 mark_seen(commit, seen_p);
200                 if ((flags & all_revs) == all_revs)
201                         flags |= UNINTERESTING;
202                 parents = commit->parents;
204                 while (parents) {
205                         struct commit *p = parents->item;
206                         int this_flag = p->object.flags;
207                         parents = parents->next;
208                         if ((this_flag & flags) == flags)
209                                 continue;
210                         if (!p->object.parsed)
211                                 parse_commit(p);
212                         if (mark_seen(p, seen_p) && !still_interesting)
213                                 extra--;
214                         p->object.flags |= flags;
215                         insert_by_date(p, list_p);
216                 }
217         }
219         /*
220          * Postprocess to complete well-poisoning.
221          *
222          * At this point we have all the commits we have seen in
223          * seen_p list.  Mark anything that can be reached from
224          * uninteresting commits not interesting.
225          */
226         for (;;) {
227                 int changed = 0;
228                 struct commit_list *s;
229                 for (s = *seen_p; s; s = s->next) {
230                         struct commit *c = s->item;
231                         struct commit_list *parents;
233                         if (((c->object.flags & all_revs) != all_revs) &&
234                             !(c->object.flags & UNINTERESTING))
235                                 continue;
237                         /* The current commit is either a merge base or
238                          * already uninteresting one.  Mark its parents
239                          * as uninteresting commits _only_ if they are
240                          * already parsed.  No reason to find new ones
241                          * here.
242                          */
243                         parents = c->parents;
244                         while (parents) {
245                                 struct commit *p = parents->item;
246                                 parents = parents->next;
247                                 if (!(p->object.flags & UNINTERESTING)) {
248                                         p->object.flags |= UNINTERESTING;
249                                         changed = 1;
250                                 }
251                         }
252                 }
253                 if (!changed)
254                         break;
255         }
258 static void show_one_commit(struct commit *commit, int no_name)
260         char pretty[256], *cp;
261         struct commit_name *name = commit->util;
262         if (commit->object.parsed)
263                 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
264                                     pretty, sizeof(pretty), 0, NULL, NULL, 0);
265         else
266                 strcpy(pretty, "(unavailable)");
267         if (!strncmp(pretty, "[PATCH] ", 8))
268                 cp = pretty + 8;
269         else
270                 cp = pretty;
272         if (!no_name) {
273                 if (name && name->head_name) {
274                         printf("[%s", name->head_name);
275                         if (name->generation) {
276                                 if (name->generation == 1)
277                                         printf("^");
278                                 else
279                                         printf("~%d", name->generation);
280                         }
281                         printf("] ");
282                 }
283                 else
284                         printf("[%s] ",
285                                find_unique_abbrev(commit->object.sha1, 7));
286         }
287         puts(cp);
290 static char *ref_name[MAX_REVS + 1];
291 static int ref_name_cnt;
293 static const char *find_digit_prefix(const char *s, int *v)
295         const char *p;
296         int ver;
297         char ch;
299         for (p = s, ver = 0;
300              '0' <= (ch = *p) && ch <= '9';
301              p++)
302                 ver = ver * 10 + ch - '0';
303         *v = ver;
304         return p;
308 static int version_cmp(const char *a, const char *b)
310         while (1) {
311                 int va, vb;
313                 a = find_digit_prefix(a, &va);
314                 b = find_digit_prefix(b, &vb);
315                 if (va != vb)
316                         return va - vb;
318                 while (1) {
319                         int ca = *a;
320                         int cb = *b;
321                         if ('0' <= ca && ca <= '9')
322                                 ca = 0;
323                         if ('0' <= cb && cb <= '9')
324                                 cb = 0;
325                         if (ca != cb)
326                                 return ca - cb;
327                         if (!ca)
328                                 break;
329                         a++;
330                         b++;
331                 }
332                 if (!*a && !*b)
333                         return 0;
334         }
337 static int compare_ref_name(const void *a_, const void *b_)
339         const char * const*a = a_, * const*b = b_;
340         return version_cmp(*a, *b);
343 static void sort_ref_range(int bottom, int top)
345         qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]),
346               compare_ref_name);
349 static int append_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
351         struct commit *commit = lookup_commit_reference_gently(sha1, 1);
352         int i;
354         if (!commit)
355                 return 0;
356         /* Avoid adding the same thing twice */
357         for (i = 0; i < ref_name_cnt; i++)
358                 if (!strcmp(refname, ref_name[i]))
359                         return 0;
361         if (MAX_REVS <= ref_name_cnt) {
362                 fprintf(stderr, "warning: ignoring %s; "
363                         "cannot handle more than %d refs\n",
364                         refname, MAX_REVS);
365                 return 0;
366         }
367         ref_name[ref_name_cnt++] = xstrdup(refname);
368         ref_name[ref_name_cnt] = NULL;
369         return 0;
372 static int append_head_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
374         unsigned char tmp[20];
375         int ofs = 11;
376         if (strncmp(refname, "refs/heads/", ofs))
377                 return 0;
378         /* If both heads/foo and tags/foo exists, get_sha1 would
379          * get confused.
380          */
381         if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1))
382                 ofs = 5;
383         return append_ref(refname + ofs, sha1, flag, cb_data);
386 static int append_tag_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
388         if (strncmp(refname, "refs/tags/", 10))
389                 return 0;
390         return append_ref(refname + 5, sha1, flag, cb_data);
393 static const char *match_ref_pattern = NULL;
394 static int match_ref_slash = 0;
395 static int count_slash(const char *s)
397         int cnt = 0;
398         while (*s)
399                 if (*s++ == '/')
400                         cnt++;
401         return cnt;
404 static int append_matching_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
406         /* we want to allow pattern hold/<asterisk> to show all
407          * branches under refs/heads/hold/, and v0.99.9? to show
408          * refs/tags/v0.99.9a and friends.
409          */
410         const char *tail;
411         int slash = count_slash(refname);
412         for (tail = refname; *tail && match_ref_slash < slash; )
413                 if (*tail++ == '/')
414                         slash--;
415         if (!*tail)
416                 return 0;
417         if (fnmatch(match_ref_pattern, tail, 0))
418                 return 0;
419         if (!strncmp("refs/heads/", refname, 11))
420                 return append_head_ref(refname, sha1, flag, cb_data);
421         if (!strncmp("refs/tags/", refname, 10))
422                 return append_tag_ref(refname, sha1, flag, cb_data);
423         return append_ref(refname, sha1, flag, cb_data);
426 static void snarf_refs(int head, int tag)
428         if (head) {
429                 int orig_cnt = ref_name_cnt;
430                 for_each_ref(append_head_ref, NULL);
431                 sort_ref_range(orig_cnt, ref_name_cnt);
432         }
433         if (tag) {
434                 int orig_cnt = ref_name_cnt;
435                 for_each_ref(append_tag_ref, NULL);
436                 sort_ref_range(orig_cnt, ref_name_cnt);
437         }
440 static int rev_is_head(char *head, int headlen, char *name,
441                        unsigned char *head_sha1, unsigned char *sha1)
443         if ((!head[0]) ||
444             (head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
445                 return 0;
446         if (!strncmp(head, "refs/heads/", 11))
447                 head += 11;
448         if (!strncmp(name, "refs/heads/", 11))
449                 name += 11;
450         else if (!strncmp(name, "heads/", 6))
451                 name += 6;
452         return !strcmp(head, name);
455 static int show_merge_base(struct commit_list *seen, int num_rev)
457         int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
458         int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
459         int exit_status = 1;
461         while (seen) {
462                 struct commit *commit = pop_one_commit(&seen);
463                 int flags = commit->object.flags & all_mask;
464                 if (!(flags & UNINTERESTING) &&
465                     ((flags & all_revs) == all_revs)) {
466                         puts(sha1_to_hex(commit->object.sha1));
467                         exit_status = 0;
468                         commit->object.flags |= UNINTERESTING;
469                 }
470         }
471         return exit_status;
474 static int show_independent(struct commit **rev,
475                             int num_rev,
476                             char **ref_name,
477                             unsigned int *rev_mask)
479         int i;
481         for (i = 0; i < num_rev; i++) {
482                 struct commit *commit = rev[i];
483                 unsigned int flag = rev_mask[i];
485                 if (commit->object.flags == flag)
486                         puts(sha1_to_hex(commit->object.sha1));
487                 commit->object.flags |= UNINTERESTING;
488         }
489         return 0;
492 static void append_one_rev(const char *av)
494         unsigned char revkey[20];
495         if (!get_sha1(av, revkey)) {
496                 append_ref(av, revkey, 0, NULL);
497                 return;
498         }
499         if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) {
500                 /* glob style match */
501                 int saved_matches = ref_name_cnt;
502                 match_ref_pattern = av;
503                 match_ref_slash = count_slash(av);
504                 for_each_ref(append_matching_ref, NULL);
505                 if (saved_matches == ref_name_cnt &&
506                     ref_name_cnt < MAX_REVS)
507                         error("no matching refs with %s", av);
508                 if (saved_matches + 1 < ref_name_cnt)
509                         sort_ref_range(saved_matches, ref_name_cnt);
510                 return;
511         }
512         die("bad sha1 reference %s", av);
515 static int git_show_branch_config(const char *var, const char *value)
517         if (!strcmp(var, "showbranch.default")) {
518                 if (default_alloc <= default_num + 1) {
519                         default_alloc = default_alloc * 3 / 2 + 20;
520                         default_arg = xrealloc(default_arg, sizeof *default_arg * default_alloc);
521                 }
522                 default_arg[default_num++] = xstrdup(value);
523                 default_arg[default_num] = NULL;
524                 return 0;
525         }
527         return git_default_config(var, value);
530 static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
532         /* If the commit is tip of the named branches, do not
533          * omit it.
534          * Otherwise, if it is a merge that is reachable from only one
535          * tip, it is not that interesting.
536          */
537         int i, flag, count;
538         for (i = 0; i < n; i++)
539                 if (rev[i] == commit)
540                         return 0;
541         flag = commit->object.flags;
542         for (i = count = 0; i < n; i++) {
543                 if (flag & (1u << (i + REV_SHIFT)))
544                         count++;
545         }
546         if (count == 1)
547                 return 1;
548         return 0;
551 int cmd_show_branch(int ac, const char **av, const char *prefix)
553         struct commit *rev[MAX_REVS], *commit;
554         struct commit_list *list = NULL, *seen = NULL;
555         unsigned int rev_mask[MAX_REVS];
556         int num_rev, i, extra = 0;
557         int all_heads = 0, all_tags = 0;
558         int all_mask, all_revs;
559         int lifo = 1;
560         char head[128];
561         const char *head_p;
562         int head_len;
563         unsigned char head_sha1[20];
564         int merge_base = 0;
565         int independent = 0;
566         int no_name = 0;
567         int sha1_name = 0;
568         int shown_merge_point = 0;
569         int with_current_branch = 0;
570         int head_at = -1;
571         int topics = 0;
572         int dense = 1;
574         git_config(git_show_branch_config);
576         /* If nothing is specified, try the default first */
577         if (ac == 1 && default_num) {
578                 ac = default_num + 1;
579                 av = default_arg - 1; /* ick; we would not address av[0] */
580         }
582         while (1 < ac && av[1][0] == '-') {
583                 const char *arg = av[1];
584                 if (!strcmp(arg, "--")) {
585                         ac--; av++;
586                         break;
587                 }
588                 else if (!strcmp(arg, "--all"))
589                         all_heads = all_tags = 1;
590                 else if (!strcmp(arg, "--heads"))
591                         all_heads = 1;
592                 else if (!strcmp(arg, "--tags"))
593                         all_tags = 1;
594                 else if (!strcmp(arg, "--more"))
595                         extra = 1;
596                 else if (!strcmp(arg, "--list"))
597                         extra = -1;
598                 else if (!strcmp(arg, "--no-name"))
599                         no_name = 1;
600                 else if (!strcmp(arg, "--current"))
601                         with_current_branch = 1;
602                 else if (!strcmp(arg, "--sha1-name"))
603                         sha1_name = 1;
604                 else if (!strncmp(arg, "--more=", 7))
605                         extra = atoi(arg + 7);
606                 else if (!strcmp(arg, "--merge-base"))
607                         merge_base = 1;
608                 else if (!strcmp(arg, "--independent"))
609                         independent = 1;
610                 else if (!strcmp(arg, "--topo-order"))
611                         lifo = 1;
612                 else if (!strcmp(arg, "--topics"))
613                         topics = 1;
614                 else if (!strcmp(arg, "--sparse"))
615                         dense = 0;
616                 else if (!strcmp(arg, "--date-order"))
617                         lifo = 0;
618                 else
619                         usage(show_branch_usage);
620                 ac--; av++;
621         }
622         ac--; av++;
624         /* Only one of these is allowed */
625         if (1 < independent + merge_base + (extra != 0))
626                 usage(show_branch_usage);
628         /* If nothing is specified, show all branches by default */
629         if (ac + all_heads + all_tags == 0)
630                 all_heads = 1;
632         if (all_heads + all_tags)
633                 snarf_refs(all_heads, all_tags);
634         while (0 < ac) {
635                 append_one_rev(*av);
636                 ac--; av++;
637         }
639         head_p = resolve_ref("HEAD", head_sha1, 1, NULL);
640         if (head_p) {
641                 head_len = strlen(head_p);
642                 memcpy(head, head_p, head_len + 1);
643         }
644         else {
645                 head_len = 0;
646                 head[0] = 0;
647         }
649         if (with_current_branch && head_p) {
650                 int has_head = 0;
651                 for (i = 0; !has_head && i < ref_name_cnt; i++) {
652                         /* We are only interested in adding the branch
653                          * HEAD points at.
654                          */
655                         if (rev_is_head(head,
656                                         head_len,
657                                         ref_name[i],
658                                         head_sha1, NULL))
659                                 has_head++;
660                 }
661                 if (!has_head) {
662                         int pfxlen = strlen("refs/heads/");
663                         append_one_rev(head + pfxlen);
664                 }
665         }
667         if (!ref_name_cnt) {
668                 fprintf(stderr, "No revs to be shown.\n");
669                 exit(0);
670         }
672         for (num_rev = 0; ref_name[num_rev]; num_rev++) {
673                 unsigned char revkey[20];
674                 unsigned int flag = 1u << (num_rev + REV_SHIFT);
676                 if (MAX_REVS <= num_rev)
677                         die("cannot handle more than %d revs.", MAX_REVS);
678                 if (get_sha1(ref_name[num_rev], revkey))
679                         die("'%s' is not a valid ref.", ref_name[num_rev]);
680                 commit = lookup_commit_reference(revkey);
681                 if (!commit)
682                         die("cannot find commit %s (%s)",
683                             ref_name[num_rev], revkey);
684                 parse_commit(commit);
685                 mark_seen(commit, &seen);
687                 /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
688                  * and so on.  REV_SHIFT bits from bit 0 are used for
689                  * internal bookkeeping.
690                  */
691                 commit->object.flags |= flag;
692                 if (commit->object.flags == flag)
693                         insert_by_date(commit, &list);
694                 rev[num_rev] = commit;
695         }
696         for (i = 0; i < num_rev; i++)
697                 rev_mask[i] = rev[i]->object.flags;
699         if (0 <= extra)
700                 join_revs(&list, &seen, num_rev, extra);
702         sort_by_date(&seen);
704         if (merge_base)
705                 return show_merge_base(seen, num_rev);
707         if (independent)
708                 return show_independent(rev, num_rev, ref_name, rev_mask);
710         /* Show list; --more=-1 means list-only */
711         if (1 < num_rev || extra < 0) {
712                 for (i = 0; i < num_rev; i++) {
713                         int j;
714                         int is_head = rev_is_head(head,
715                                                   head_len,
716                                                   ref_name[i],
717                                                   head_sha1,
718                                                   rev[i]->object.sha1);
719                         if (extra < 0)
720                                 printf("%c [%s] ",
721                                        is_head ? '*' : ' ', ref_name[i]);
722                         else {
723                                 for (j = 0; j < i; j++)
724                                         putchar(' ');
725                                 printf("%c [%s] ",
726                                        is_head ? '*' : '!', ref_name[i]);
727                         }
728                         /* header lines never need name */
729                         show_one_commit(rev[i], 1);
730                         if (is_head)
731                                 head_at = i;
732                 }
733                 if (0 <= extra) {
734                         for (i = 0; i < num_rev; i++)
735                                 putchar('-');
736                         putchar('\n');
737                 }
738         }
739         if (extra < 0)
740                 exit(0);
742         /* Sort topologically */
743         sort_in_topological_order(&seen, lifo);
745         /* Give names to commits */
746         if (!sha1_name && !no_name)
747                 name_commits(seen, rev, ref_name, num_rev);
749         all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
750         all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
752         while (seen) {
753                 struct commit *commit = pop_one_commit(&seen);
754                 int this_flag = commit->object.flags;
755                 int is_merge_point = ((this_flag & all_revs) == all_revs);
757                 shown_merge_point |= is_merge_point;
759                 if (1 < num_rev) {
760                         int is_merge = !!(commit->parents &&
761                                           commit->parents->next);
762                         if (topics &&
763                             !is_merge_point &&
764                             (this_flag & (1u << REV_SHIFT)))
765                                 continue;
766                         if (dense && is_merge &&
767                             omit_in_dense(commit, rev, num_rev))
768                                 continue;
769                         for (i = 0; i < num_rev; i++) {
770                                 int mark;
771                                 if (!(this_flag & (1u << (i + REV_SHIFT))))
772                                         mark = ' ';
773                                 else if (is_merge)
774                                         mark = '-';
775                                 else if (i == head_at)
776                                         mark = '*';
777                                 else
778                                         mark = '+';
779                                 putchar(mark);
780                         }
781                         putchar(' ');
782                 }
783                 show_one_commit(commit, no_name);
785                 if (shown_merge_point && --extra < 0)
786                         break;
787         }
788         return 0;