Merge branch 'fixes'
[git.git] / show-branch.c
blob8429c171cf6e6ddbac2f1a95da4a05f7ce8d34a3
1 #include <stdlib.h>
2 #include "cache.h"
3 #include "commit.h"
4 #include "refs.h"
6 static const char show_branch_usage[] =
7 "git-show-branch [--all] [--heads] [--tags] [--more=count | --list | --independent | --merge-base ] [<refs>...]";
9 #define UNINTERESTING 01
11 #define REV_SHIFT 2
12 #define MAX_REVS 29 /* should not exceed bits_per_int - REV_SHIFT */
14 static struct commit *interesting(struct commit_list *list)
16 while (list) {
17 struct commit *commit = list->item;
18 list = list->next;
19 if (commit->object.flags & UNINTERESTING)
20 continue;
21 return commit;
23 return NULL;
26 static struct commit *pop_one_commit(struct commit_list **list_p)
28 struct commit *commit;
29 struct commit_list *list;
30 list = *list_p;
31 commit = list->item;
32 *list_p = list->next;
33 free(list);
34 return commit;
37 struct commit_name {
38 const char *head_name; /* which head's ancestor? */
39 int generation; /* how many parents away from head_name */
42 /* Name the commit as nth generation ancestor of head_name;
43 * we count only the first-parent relationship for naming purposes.
45 static void name_commit(struct commit *commit, const char *head_name, int nth)
47 struct commit_name *name;
48 if (!commit->object.util)
49 commit->object.util = xmalloc(sizeof(struct commit_name));
50 name = commit->object.util;
51 name->head_name = head_name;
52 name->generation = nth;
55 /* Parent is the first parent of the commit. We may name it
56 * as (n+1)th generation ancestor of the same head_name as
57 * commit is nth generation ancestore of, if that generation
58 * number is better than the name it already has.
60 static void name_parent(struct commit *commit, struct commit *parent)
62 struct commit_name *commit_name = commit->object.util;
63 struct commit_name *parent_name = parent->object.util;
64 if (!commit_name)
65 return;
66 if (!parent_name ||
67 commit_name->generation + 1 < parent_name->generation)
68 name_commit(parent, commit_name->head_name,
69 commit_name->generation + 1);
72 static int name_first_parent_chain(struct commit *c)
74 int i = 0;
75 while (c) {
76 struct commit *p;
77 if (!c->object.util)
78 break;
79 if (!c->parents)
80 break;
81 p = c->parents->item;
82 if (!p->object.util) {
83 name_parent(c, p);
84 i++;
86 c = p;
88 return i;
91 static void name_commits(struct commit_list *list,
92 struct commit **rev,
93 char **ref_name,
94 int num_rev)
96 struct commit_list *cl;
97 struct commit *c;
98 int i;
100 /* First give names to the given heads */
101 for (cl = list; cl; cl = cl->next) {
102 c = cl->item;
103 if (c->object.util)
104 continue;
105 for (i = 0; i < num_rev; i++) {
106 if (rev[i] == c) {
107 name_commit(c, ref_name[i], 0);
108 break;
113 /* Then commits on the first parent ancestry chain */
114 do {
115 i = 0;
116 for (cl = list; cl; cl = cl->next) {
117 i += name_first_parent_chain(cl->item);
119 } while (i);
121 /* Finally, any unnamed commits */
122 do {
123 i = 0;
124 for (cl = list; cl; cl = cl->next) {
125 struct commit_list *parents;
126 struct commit_name *n;
127 int nth;
128 c = cl->item;
129 if (!c->object.util)
130 continue;
131 n = c->object.util;
132 parents = c->parents;
133 nth = 0;
134 while (parents) {
135 struct commit *p = parents->item;
136 char newname[1000];
137 parents = parents->next;
138 nth++;
139 if (p->object.util)
140 continue;
141 switch (n->generation) {
142 case 0:
143 sprintf(newname, "%s^%d",
144 n->head_name, nth);
145 break;
146 case 1:
147 sprintf(newname, "%s^^%d",
148 n->head_name, nth);
149 break;
150 default:
151 sprintf(newname, "%s~%d^%d",
152 n->head_name, n->generation,
153 nth);
155 name_commit(p, strdup(newname), 0);
156 i++;
157 name_first_parent_chain(p);
160 } while (i);
163 static int mark_seen(struct commit *commit, struct commit_list **seen_p)
165 if (!commit->object.flags) {
166 insert_by_date(commit, seen_p);
167 return 1;
169 return 0;
172 static void join_revs(struct commit_list **list_p,
173 struct commit_list **seen_p,
174 int num_rev, int extra)
176 int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
177 int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
179 while (*list_p) {
180 struct commit_list *parents;
181 struct commit *commit = pop_one_commit(list_p);
182 int flags = commit->object.flags & all_mask;
183 int still_interesting = !!interesting(*list_p);
185 if (!still_interesting && extra < 0)
186 break;
188 mark_seen(commit, seen_p);
189 if ((flags & all_revs) == all_revs)
190 flags |= UNINTERESTING;
191 parents = commit->parents;
193 while (parents) {
194 struct commit *p = parents->item;
195 int this_flag = p->object.flags;
196 parents = parents->next;
197 if ((this_flag & flags) == flags)
198 continue;
199 parse_commit(p);
200 if (mark_seen(p, seen_p) && !still_interesting)
201 extra--;
202 p->object.flags |= flags;
203 insert_by_date(p, list_p);
208 static void show_one_commit(struct commit *commit)
210 char pretty[128], *cp;
211 struct commit_name *name = commit->object.util;
212 if (commit->object.parsed)
213 pretty_print_commit(CMIT_FMT_ONELINE, commit->buffer, ~0,
214 pretty, sizeof(pretty));
215 else
216 strcpy(pretty, "(unavailable)");
217 if (!strncmp(pretty, "[PATCH] ", 8))
218 cp = pretty + 8;
219 else
220 cp = pretty;
221 if (name && name->head_name) {
222 printf("[%s", name->head_name);
223 if (name->generation)
224 printf("~%d", name->generation);
225 printf("] ");
227 puts(cp);
230 static char *ref_name[MAX_REVS + 1];
231 static int ref_name_cnt;
233 static int compare_ref_name(const void *a_, const void *b_)
235 const char * const*a = a_, * const*b = b_;
236 return strcmp(*a, *b);
239 static void sort_ref_range(int bottom, int top)
241 qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]),
242 compare_ref_name);
245 static int append_ref(const char *refname, const unsigned char *sha1)
247 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
248 if (!commit)
249 return 0;
250 if (MAX_REVS < ref_name_cnt) {
251 fprintf(stderr, "warning: ignoring %s; "
252 "cannot handle more than %d refs",
253 refname, MAX_REVS);
254 return 0;
256 ref_name[ref_name_cnt++] = strdup(refname);
257 ref_name[ref_name_cnt] = NULL;
258 return 0;
261 static int append_head_ref(const char *refname, const unsigned char *sha1)
263 if (strncmp(refname, "refs/heads/", 11))
264 return 0;
265 return append_ref(refname + 11, sha1);
268 static int append_tag_ref(const char *refname, const unsigned char *sha1)
270 if (strncmp(refname, "refs/tags/", 10))
271 return 0;
272 return append_ref(refname + 5, sha1);
275 static void snarf_refs(int head, int tag)
277 if (head) {
278 int orig_cnt = ref_name_cnt;
279 for_each_ref(append_head_ref);
280 sort_ref_range(orig_cnt, ref_name_cnt);
282 if (tag) {
283 int orig_cnt = ref_name_cnt;
284 for_each_ref(append_tag_ref);
285 sort_ref_range(orig_cnt, ref_name_cnt);
289 static int rev_is_head(char *head_path, int headlen,
290 char *name,
291 unsigned char *head_sha1, unsigned char *sha1)
293 int namelen;
294 if ((!head_path[0]) || memcmp(head_sha1, sha1, 20))
295 return 0;
296 namelen = strlen(name);
297 if ((headlen < namelen) ||
298 memcmp(head_path + headlen - namelen, name, namelen))
299 return 0;
300 if (headlen == namelen ||
301 head_path[headlen - namelen - 1] == '/')
302 return 1;
303 return 0;
306 static int show_merge_base(struct commit_list *seen, int num_rev)
308 int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
309 int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
310 int exit_status = 1;
312 while (seen) {
313 struct commit *commit = pop_one_commit(&seen);
314 int flags = commit->object.flags & all_mask;
315 if (!(flags & UNINTERESTING) &&
316 ((flags & all_revs) == all_revs)) {
317 puts(sha1_to_hex(commit->object.sha1));
318 exit_status = 0;
319 commit->object.flags |= UNINTERESTING;
322 return exit_status;
325 static int show_independent(struct commit **rev,
326 int num_rev,
327 char **ref_name,
328 unsigned int *rev_mask)
330 int i;
332 for (i = 0; i < num_rev; i++) {
333 struct commit *commit = rev[i];
334 unsigned int flag = rev_mask[i];
336 if (commit->object.flags == flag)
337 puts(sha1_to_hex(commit->object.sha1));
338 commit->object.flags |= UNINTERESTING;
340 return 0;
343 int main(int ac, char **av)
345 struct commit *rev[MAX_REVS], *commit;
346 struct commit_list *list = NULL, *seen = NULL;
347 unsigned int rev_mask[MAX_REVS];
348 int num_rev, i, extra = 0;
349 int all_heads = 0, all_tags = 0;
350 int all_mask, all_revs, shown_merge_point;
351 char head_path[128];
352 const char *head_path_p;
353 int head_path_len;
354 unsigned char head_sha1[20];
355 int merge_base = 0;
356 int independent = 0;
357 char **label;
359 setup_git_directory();
361 while (1 < ac && av[1][0] == '-') {
362 char *arg = av[1];
363 if (!strcmp(arg, "--all"))
364 all_heads = all_tags = 1;
365 else if (!strcmp(arg, "--heads"))
366 all_heads = 1;
367 else if (!strcmp(arg, "--tags"))
368 all_tags = 1;
369 else if (!strcmp(arg, "--more"))
370 extra = 1;
371 else if (!strcmp(arg, "--list"))
372 extra = -1;
373 else if (!strncmp(arg, "--more=", 7))
374 extra = atoi(arg + 7);
375 else if (!strcmp(arg, "--merge-base"))
376 merge_base = 1;
377 else if (!strcmp(arg, "--independent"))
378 independent = 1;
379 else
380 usage(show_branch_usage);
381 ac--; av++;
383 ac--; av++;
385 /* Only one of these is allowed */
386 if (1 < independent + merge_base + (extra != 0))
387 usage(show_branch_usage);
389 if (all_heads + all_tags)
390 snarf_refs(all_heads, all_tags);
392 while (0 < ac) {
393 unsigned char revkey[20];
394 if (get_sha1(*av, revkey))
395 die("bad sha1 reference %s", *av);
396 append_ref(*av, revkey);
397 ac--; av++;
400 /* If still no revs, then add heads */
401 if (!ref_name_cnt)
402 snarf_refs(1, 0);
404 for (num_rev = 0; ref_name[num_rev]; num_rev++) {
405 unsigned char revkey[20];
406 unsigned int flag = 1u << (num_rev + REV_SHIFT);
408 if (MAX_REVS <= num_rev)
409 die("cannot handle more than %d revs.", MAX_REVS);
410 if (get_sha1(ref_name[num_rev], revkey))
411 usage(show_branch_usage);
412 commit = lookup_commit_reference(revkey);
413 if (!commit)
414 die("cannot find commit %s (%s)",
415 ref_name[num_rev], revkey);
416 parse_commit(commit);
417 mark_seen(commit, &seen);
419 /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
420 * and so on. REV_SHIFT bits from bit 0 are used for
421 * internal bookkeeping.
423 commit->object.flags |= flag;
424 if (commit->object.flags == flag)
425 insert_by_date(commit, &list);
426 rev[num_rev] = commit;
428 for (i = 0; i < num_rev; i++)
429 rev_mask[i] = rev[i]->object.flags;
431 if (0 <= extra)
432 join_revs(&list, &seen, num_rev, extra);
434 head_path_p = resolve_ref(git_path("HEAD"), head_sha1, 1);
435 if (head_path_p) {
436 head_path_len = strlen(head_path_p);
437 memcpy(head_path, head_path_p, head_path_len + 1);
439 else {
440 head_path_len = 0;
441 head_path[0] = 0;
444 if (merge_base)
445 return show_merge_base(seen, num_rev);
447 if (independent)
448 return show_independent(rev, num_rev, ref_name, rev_mask);
450 /* Show list; --more=-1 means list-only */
451 if (1 < num_rev || extra < 0) {
452 for (i = 0; i < num_rev; i++) {
453 int j;
454 int is_head = rev_is_head(head_path,
455 head_path_len,
456 ref_name[i],
457 head_sha1,
458 rev[i]->object.sha1);
459 if (extra < 0)
460 printf("%c [%s] ",
461 is_head ? '*' : ' ', ref_name[i]);
462 else {
463 for (j = 0; j < i; j++)
464 putchar(' ');
465 printf("%c [%s] ",
466 is_head ? '*' : '!', ref_name[i]);
468 show_one_commit(rev[i]);
470 if (0 <= extra) {
471 for (i = 0; i < num_rev; i++)
472 putchar('-');
473 putchar('\n');
476 if (extra < 0)
477 exit(0);
479 /* Sort topologically */
480 sort_in_topological_order(&seen);
482 /* Give names to commits */
483 name_commits(seen, rev, ref_name, num_rev);
485 all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
486 all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
487 shown_merge_point = 0;
489 while (seen) {
490 struct commit *commit = pop_one_commit(&seen);
491 int this_flag = commit->object.flags;
492 int is_merge_point = (this_flag & all_revs) == all_revs;
493 static char *obvious[] = { "" };
495 if (is_merge_point)
496 shown_merge_point = 1;
498 if (1 < num_rev) {
499 for (i = 0; i < num_rev; i++)
500 putchar((this_flag & (1u << (i + REV_SHIFT)))
501 ? '+' : ' ');
502 putchar(' ');
504 show_one_commit(commit);
505 if (num_rev == 1)
506 label = obvious;
507 if (shown_merge_point && is_merge_point)
508 if (--extra < 0)
509 break;
511 return 0;