]>
Commit | Line | Data |
---|---|---|
f76412ed JH |
1 | #include <stdlib.h> |
2 | #include "cache.h" | |
3 | #include "commit.h" | |
4 | #include "refs.h" | |
5 | ||
6 | static const char show_branch_usage[] = | |
1f8af483 | 7 | "git-show-branch [--all] [--heads] [--tags] [--more=count | --list | --independent | --merge-base ] [<refs>...]"; |
f76412ed JH |
8 | |
9 | #define UNINTERESTING 01 | |
10 | ||
11 | #define REV_SHIFT 2 | |
12 | #define MAX_REVS 29 /* should not exceed bits_per_int - REV_SHIFT */ | |
13 | ||
14 | static struct commit *interesting(struct commit_list *list) | |
15 | { | |
16 | while (list) { | |
17 | struct commit *commit = list->item; | |
18 | list = list->next; | |
19 | if (commit->object.flags & UNINTERESTING) | |
20 | continue; | |
21 | return commit; | |
22 | } | |
23 | return NULL; | |
24 | } | |
25 | ||
26 | static struct commit *pop_one_commit(struct commit_list **list_p) | |
27 | { | |
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; | |
35 | } | |
36 | ||
37 | struct commit_name { | |
8e5dd22b JH |
38 | const char *head_name; /* which head's ancestor? */ |
39 | int generation; /* how many parents away from head_name */ | |
f76412ed JH |
40 | }; |
41 | ||
8e5dd22b | 42 | /* Name the commit as nth generation ancestor of head_name; |
f76412ed JH |
43 | * we count only the first-parent relationship for naming purposes. |
44 | */ | |
8e5dd22b | 45 | static void name_commit(struct commit *commit, const char *head_name, int nth) |
f76412ed JH |
46 | { |
47 | struct commit_name *name; | |
48 | if (!commit->object.util) | |
49 | commit->object.util = xmalloc(sizeof(struct commit_name)); | |
50 | name = commit->object.util; | |
8e5dd22b | 51 | name->head_name = head_name; |
f76412ed JH |
52 | name->generation = nth; |
53 | } | |
54 | ||
55 | /* Parent is the first parent of the commit. We may name it | |
8e5dd22b | 56 | * as (n+1)th generation ancestor of the same head_name as |
f76412ed JH |
57 | * commit is nth generation ancestore of, if that generation |
58 | * number is better than the name it already has. | |
59 | */ | |
60 | static void name_parent(struct commit *commit, struct commit *parent) | |
61 | { | |
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) | |
8e5dd22b | 68 | name_commit(parent, commit_name->head_name, |
f76412ed JH |
69 | commit_name->generation + 1); |
70 | } | |
71 | ||
8e5dd22b JH |
72 | static int name_first_parent_chain(struct commit *c) |
73 | { | |
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++; | |
85 | } | |
86 | c = p; | |
87 | } | |
88 | return i; | |
89 | } | |
90 | ||
91 | static void name_commits(struct commit_list *list, | |
92 | struct commit **rev, | |
93 | char **ref_name, | |
94 | int num_rev) | |
95 | { | |
96 | struct commit_list *cl; | |
97 | struct commit *c; | |
98 | int i; | |
99 | ||
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; | |
109 | } | |
110 | } | |
111 | } | |
112 | ||
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); | |
118 | } | |
119 | } while (i); | |
120 | ||
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 | sprintf(newname, "%s^%d", n->head_name, nth); | |
142 | name_commit(p, strdup(newname), 0); | |
143 | i++; | |
144 | name_first_parent_chain(p); | |
145 | } | |
146 | } | |
147 | } while (i); | |
148 | } | |
149 | ||
f76412ed JH |
150 | static int mark_seen(struct commit *commit, struct commit_list **seen_p) |
151 | { | |
152 | if (!commit->object.flags) { | |
153 | insert_by_date(commit, seen_p); | |
154 | return 1; | |
155 | } | |
156 | return 0; | |
157 | } | |
158 | ||
159 | static void join_revs(struct commit_list **list_p, | |
160 | struct commit_list **seen_p, | |
161 | int num_rev, int extra) | |
162 | { | |
163 | int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1); | |
164 | int all_revs = all_mask & ~((1u << REV_SHIFT) - 1); | |
165 | ||
166 | while (*list_p) { | |
167 | struct commit_list *parents; | |
168 | struct commit *commit = pop_one_commit(list_p); | |
169 | int flags = commit->object.flags & all_mask; | |
f76412ed JH |
170 | int still_interesting = !!interesting(*list_p); |
171 | ||
172 | if (!still_interesting && extra < 0) | |
173 | break; | |
174 | ||
175 | mark_seen(commit, seen_p); | |
176 | if ((flags & all_revs) == all_revs) | |
177 | flags |= UNINTERESTING; | |
178 | parents = commit->parents; | |
179 | ||
180 | while (parents) { | |
181 | struct commit *p = parents->item; | |
182 | int this_flag = p->object.flags; | |
183 | parents = parents->next; | |
f76412ed JH |
184 | if ((this_flag & flags) == flags) |
185 | continue; | |
186 | parse_commit(p); | |
187 | if (mark_seen(p, seen_p) && !still_interesting) | |
188 | extra--; | |
189 | p->object.flags |= flags; | |
190 | insert_by_date(p, list_p); | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
8e5dd22b | 195 | static void show_one_commit(struct commit *commit) |
f76412ed JH |
196 | { |
197 | char pretty[128], *cp; | |
198 | struct commit_name *name = commit->object.util; | |
0a2ba738 JH |
199 | if (commit->object.parsed) |
200 | pretty_print_commit(CMIT_FMT_ONELINE, commit->buffer, ~0, | |
201 | pretty, sizeof(pretty)); | |
202 | else | |
203 | strcpy(pretty, "(unavailable)"); | |
f76412ed JH |
204 | if (!strncmp(pretty, "[PATCH] ", 8)) |
205 | cp = pretty + 8; | |
206 | else | |
207 | cp = pretty; | |
8e5dd22b JH |
208 | if (name && name->head_name) { |
209 | printf("[%s", name->head_name); | |
f76412ed JH |
210 | if (name->generation) |
211 | printf("~%d", name->generation); | |
212 | printf("] "); | |
213 | } | |
214 | puts(cp); | |
215 | } | |
216 | ||
217 | static char *ref_name[MAX_REVS + 1]; | |
218 | static int ref_name_cnt; | |
219 | ||
628894b2 JH |
220 | static int compare_ref_name(const void *a_, const void *b_) |
221 | { | |
222 | const char * const*a = a_, * const*b = b_; | |
223 | return strcmp(*a, *b); | |
224 | } | |
225 | ||
226 | static void sort_ref_range(int bottom, int top) | |
227 | { | |
228 | qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]), | |
229 | compare_ref_name); | |
230 | } | |
231 | ||
f76412ed JH |
232 | static int append_ref(const char *refname, const unsigned char *sha1) |
233 | { | |
234 | struct commit *commit = lookup_commit_reference_gently(sha1, 1); | |
235 | if (!commit) | |
236 | return 0; | |
237 | if (MAX_REVS < ref_name_cnt) { | |
238 | fprintf(stderr, "warning: ignoring %s; " | |
239 | "cannot handle more than %d refs", | |
240 | refname, MAX_REVS); | |
241 | return 0; | |
242 | } | |
243 | ref_name[ref_name_cnt++] = strdup(refname); | |
244 | ref_name[ref_name_cnt] = NULL; | |
245 | return 0; | |
246 | } | |
247 | ||
248 | static int append_head_ref(const char *refname, const unsigned char *sha1) | |
249 | { | |
250 | if (strncmp(refname, "refs/heads/", 11)) | |
251 | return 0; | |
628894b2 | 252 | return append_ref(refname + 11, sha1); |
f76412ed JH |
253 | } |
254 | ||
255 | static int append_tag_ref(const char *refname, const unsigned char *sha1) | |
256 | { | |
257 | if (strncmp(refname, "refs/tags/", 10)) | |
258 | return 0; | |
259 | return append_ref(refname + 5, sha1); | |
260 | } | |
261 | ||
262 | static void snarf_refs(int head, int tag) | |
263 | { | |
628894b2 JH |
264 | if (head) { |
265 | int orig_cnt = ref_name_cnt; | |
f76412ed | 266 | for_each_ref(append_head_ref); |
628894b2 JH |
267 | sort_ref_range(orig_cnt, ref_name_cnt); |
268 | } | |
269 | if (tag) { | |
270 | int orig_cnt = ref_name_cnt; | |
f76412ed | 271 | for_each_ref(append_tag_ref); |
628894b2 JH |
272 | sort_ref_range(orig_cnt, ref_name_cnt); |
273 | } | |
f76412ed JH |
274 | } |
275 | ||
276 | static int rev_is_head(char *head_path, int headlen, | |
277 | char *name, | |
278 | unsigned char *head_sha1, unsigned char *sha1) | |
279 | { | |
280 | int namelen; | |
281 | if ((!head_path[0]) || memcmp(head_sha1, sha1, 20)) | |
282 | return 0; | |
283 | namelen = strlen(name); | |
284 | if ((headlen < namelen) || | |
285 | memcmp(head_path + headlen - namelen, name, namelen)) | |
286 | return 0; | |
287 | if (headlen == namelen || | |
288 | head_path[headlen - namelen - 1] == '/') | |
289 | return 1; | |
290 | return 0; | |
291 | } | |
292 | ||
293 | static int show_merge_base(struct commit_list *seen, int num_rev) | |
294 | { | |
295 | int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1); | |
296 | int all_revs = all_mask & ~((1u << REV_SHIFT) - 1); | |
2f0f8b71 | 297 | int exit_status = 1; |
f76412ed JH |
298 | |
299 | while (seen) { | |
300 | struct commit *commit = pop_one_commit(&seen); | |
301 | int flags = commit->object.flags & all_mask; | |
302 | if (!(flags & UNINTERESTING) && | |
303 | ((flags & all_revs) == all_revs)) { | |
304 | puts(sha1_to_hex(commit->object.sha1)); | |
2f0f8b71 JH |
305 | exit_status = 0; |
306 | commit->object.flags |= UNINTERESTING; | |
f76412ed JH |
307 | } |
308 | } | |
2f0f8b71 | 309 | return exit_status; |
f76412ed JH |
310 | } |
311 | ||
1f8af483 JH |
312 | static int show_independent(struct commit **rev, |
313 | int num_rev, | |
314 | char **ref_name, | |
315 | unsigned int *rev_mask) | |
316 | { | |
317 | int i; | |
318 | ||
319 | for (i = 0; i < num_rev; i++) { | |
320 | struct commit *commit = rev[i]; | |
321 | unsigned int flag = rev_mask[i]; | |
322 | ||
323 | if (commit->object.flags == flag) | |
324 | puts(sha1_to_hex(commit->object.sha1)); | |
325 | commit->object.flags |= UNINTERESTING; | |
326 | } | |
327 | return 0; | |
328 | } | |
329 | ||
f76412ed JH |
330 | int main(int ac, char **av) |
331 | { | |
332 | struct commit *rev[MAX_REVS], *commit; | |
333 | struct commit_list *list = NULL, *seen = NULL; | |
1f8af483 | 334 | unsigned int rev_mask[MAX_REVS]; |
f76412ed JH |
335 | int num_rev, i, extra = 0; |
336 | int all_heads = 0, all_tags = 0; | |
8e5dd22b | 337 | int all_mask, all_revs, shown_merge_point; |
f76412ed JH |
338 | char head_path[128]; |
339 | int head_path_len; | |
340 | unsigned char head_sha1[20]; | |
341 | int merge_base = 0; | |
1f8af483 | 342 | int independent = 0; |
f5e375c9 | 343 | char **label; |
f76412ed | 344 | |
076b2324 JH |
345 | setup_git_directory(); |
346 | ||
f76412ed JH |
347 | while (1 < ac && av[1][0] == '-') { |
348 | char *arg = av[1]; | |
349 | if (!strcmp(arg, "--all")) | |
350 | all_heads = all_tags = 1; | |
351 | else if (!strcmp(arg, "--heads")) | |
352 | all_heads = 1; | |
353 | else if (!strcmp(arg, "--tags")) | |
354 | all_tags = 1; | |
355 | else if (!strcmp(arg, "--more")) | |
356 | extra = 1; | |
1f8af483 JH |
357 | else if (!strcmp(arg, "--list")) |
358 | extra = -1; | |
359 | else if (!strncmp(arg, "--more=", 7)) | |
f76412ed | 360 | extra = atoi(arg + 7); |
f76412ed JH |
361 | else if (!strcmp(arg, "--merge-base")) |
362 | merge_base = 1; | |
1f8af483 JH |
363 | else if (!strcmp(arg, "--independent")) |
364 | independent = 1; | |
f76412ed JH |
365 | else |
366 | usage(show_branch_usage); | |
367 | ac--; av++; | |
368 | } | |
369 | ac--; av++; | |
370 | ||
1f8af483 JH |
371 | /* Only one of these is allowed */ |
372 | if (1 < independent + merge_base + (extra != 0)) | |
373 | usage(show_branch_usage); | |
374 | ||
f76412ed JH |
375 | if (all_heads + all_tags) |
376 | snarf_refs(all_heads, all_tags); | |
377 | ||
378 | while (0 < ac) { | |
379 | unsigned char revkey[20]; | |
380 | if (get_sha1(*av, revkey)) | |
381 | die("bad sha1 reference %s", *av); | |
382 | append_ref(*av, revkey); | |
383 | ac--; av++; | |
384 | } | |
385 | ||
386 | /* If still no revs, then add heads */ | |
387 | if (!ref_name_cnt) | |
388 | snarf_refs(1, 0); | |
389 | ||
390 | for (num_rev = 0; ref_name[num_rev]; num_rev++) { | |
391 | unsigned char revkey[20]; | |
1f8af483 | 392 | unsigned int flag = 1u << (num_rev + REV_SHIFT); |
f76412ed JH |
393 | |
394 | if (MAX_REVS <= num_rev) | |
395 | die("cannot handle more than %d revs.", MAX_REVS); | |
396 | if (get_sha1(ref_name[num_rev], revkey)) | |
397 | usage(show_branch_usage); | |
398 | commit = lookup_commit_reference(revkey); | |
399 | if (!commit) | |
400 | die("cannot find commit %s (%s)", | |
401 | ref_name[num_rev], revkey); | |
402 | parse_commit(commit); | |
f76412ed JH |
403 | mark_seen(commit, &seen); |
404 | ||
405 | /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1, | |
406 | * and so on. REV_SHIFT bits from bit 0 are used for | |
407 | * internal bookkeeping. | |
408 | */ | |
1f8af483 JH |
409 | commit->object.flags |= flag; |
410 | if (commit->object.flags == flag) | |
411 | insert_by_date(commit, &list); | |
f76412ed JH |
412 | rev[num_rev] = commit; |
413 | } | |
1f8af483 JH |
414 | for (i = 0; i < num_rev; i++) |
415 | rev_mask[i] = rev[i]->object.flags; | |
416 | ||
417 | if (0 <= extra) | |
418 | join_revs(&list, &seen, num_rev, extra); | |
f76412ed JH |
419 | |
420 | head_path_len = readlink(".git/HEAD", head_path, sizeof(head_path)-1); | |
421 | if ((head_path_len < 0) || get_sha1("HEAD", head_sha1)) | |
422 | head_path[0] = 0; | |
423 | else | |
424 | head_path[head_path_len] = 0; | |
425 | ||
426 | if (merge_base) | |
427 | return show_merge_base(seen, num_rev); | |
428 | ||
1f8af483 JH |
429 | if (independent) |
430 | return show_independent(rev, num_rev, ref_name, rev_mask); | |
431 | ||
432 | /* Show list; --more=-1 means list-only */ | |
c9d023b2 | 433 | if (1 < num_rev || extra < 0) { |
f76412ed JH |
434 | for (i = 0; i < num_rev; i++) { |
435 | int j; | |
436 | int is_head = rev_is_head(head_path, | |
437 | head_path_len, | |
438 | ref_name[i], | |
439 | head_sha1, | |
440 | rev[i]->object.sha1); | |
1f8af483 JH |
441 | if (extra < 0) |
442 | printf("%c [%s] ", | |
443 | is_head ? '*' : ' ', ref_name[i]); | |
444 | else { | |
445 | for (j = 0; j < i; j++) | |
446 | putchar(' '); | |
447 | printf("%c [%s] ", | |
448 | is_head ? '*' : '!', ref_name[i]); | |
449 | } | |
8e5dd22b | 450 | show_one_commit(rev[i]); |
f76412ed | 451 | } |
1f8af483 JH |
452 | if (0 <= extra) { |
453 | for (i = 0; i < num_rev; i++) | |
454 | putchar('-'); | |
455 | putchar('\n'); | |
456 | } | |
f5e375c9 | 457 | } |
1f8af483 JH |
458 | if (extra < 0) |
459 | exit(0); | |
f5e375c9 | 460 | |
8e5dd22b JH |
461 | /* Sort topologically */ |
462 | sort_in_topological_order(&seen); | |
463 | ||
464 | /* Give names to commits */ | |
465 | name_commits(seen, rev, ref_name, num_rev); | |
466 | ||
467 | all_mask = ((1u << (REV_SHIFT + num_rev)) - 1); | |
468 | all_revs = all_mask & ~((1u << REV_SHIFT) - 1); | |
469 | shown_merge_point = 0; | |
470 | ||
f76412ed JH |
471 | while (seen) { |
472 | struct commit *commit = pop_one_commit(&seen); | |
473 | int this_flag = commit->object.flags; | |
8e5dd22b | 474 | int is_merge_point = (this_flag & all_revs) == all_revs; |
f5e375c9 JH |
475 | static char *obvious[] = { "" }; |
476 | ||
8e5dd22b JH |
477 | if (is_merge_point) |
478 | shown_merge_point = 1; | |
479 | ||
f5e375c9 JH |
480 | if (1 < num_rev) { |
481 | for (i = 0; i < num_rev; i++) | |
482 | putchar((this_flag & (1u << (i + REV_SHIFT))) | |
483 | ? '+' : ' '); | |
484 | putchar(' '); | |
485 | } | |
8e5dd22b | 486 | show_one_commit(commit); |
f5e375c9 JH |
487 | if (num_rev == 1) |
488 | label = obvious; | |
8e5dd22b JH |
489 | if (shown_merge_point && is_merge_point) |
490 | if (--extra < 0) | |
491 | break; | |
f76412ed JH |
492 | } |
493 | return 0; | |
494 | } |