]>
Commit | Line | Data |
---|---|---|
f76412ed JH |
1 | #include "cache.h" |
2 | #include "commit.h" | |
3 | #include "refs.h" | |
51ce34b9 | 4 | #include "builtin.h" |
ab07ba2a | 5 | #include "color.h" |
f76412ed JH |
6 | |
7 | static const char show_branch_usage[] = | |
1b1dd23f | 8 | "git show-branch [--sparse] [--current] [--all] [--remotes] [--topo-order] [--more=count | --list | --independent | --merge-base ] [--topics] [<refs>...] | --reflog[=n[,b]] <branch>"; |
b15af079 JH |
9 | static const char show_branch_usage_reflog[] = |
10 | "--reflog is incompatible with --all, --remotes, --independent or --merge-base"; | |
f76412ed | 11 | |
ab07ba2a MH |
12 | static int showbranch_use_color = -1; |
13 | static char column_colors[][COLOR_MAXLEN] = { | |
14 | GIT_COLOR_RED, | |
15 | GIT_COLOR_GREEN, | |
16 | GIT_COLOR_YELLOW, | |
17 | GIT_COLOR_BLUE, | |
18 | GIT_COLOR_MAGENTA, | |
19 | GIT_COLOR_CYAN, | |
20 | }; | |
21 | ||
22 | #define COLUMN_COLORS_MAX (ARRAY_SIZE(column_colors)) | |
23 | ||
96f1e58f DR |
24 | static int default_num; |
25 | static int default_alloc; | |
26 | static const char **default_arg; | |
c2bdd6af | 27 | |
f76412ed JH |
28 | #define UNINTERESTING 01 |
29 | ||
30 | #define REV_SHIFT 2 | |
885a86ab | 31 | #define MAX_REVS (FLAG_BITS - REV_SHIFT) /* should not exceed bits_per_int - REV_SHIFT */ |
f76412ed | 32 | |
7e3fe904 JH |
33 | #define DEFAULT_REFLOG 4 |
34 | ||
ab07ba2a MH |
35 | static const char *get_color_code(int idx) |
36 | { | |
37 | if (showbranch_use_color) | |
38 | return column_colors[idx]; | |
39 | return ""; | |
40 | } | |
41 | ||
42 | static const char *get_color_reset_code(void) | |
43 | { | |
44 | if (showbranch_use_color) | |
45 | return GIT_COLOR_RESET; | |
46 | return ""; | |
47 | } | |
48 | ||
f76412ed JH |
49 | static struct commit *interesting(struct commit_list *list) |
50 | { | |
51 | while (list) { | |
52 | struct commit *commit = list->item; | |
53 | list = list->next; | |
54 | if (commit->object.flags & UNINTERESTING) | |
55 | continue; | |
56 | return commit; | |
57 | } | |
58 | return NULL; | |
59 | } | |
60 | ||
61 | static struct commit *pop_one_commit(struct commit_list **list_p) | |
62 | { | |
63 | struct commit *commit; | |
64 | struct commit_list *list; | |
65 | list = *list_p; | |
66 | commit = list->item; | |
67 | *list_p = list->next; | |
68 | free(list); | |
69 | return commit; | |
70 | } | |
71 | ||
72 | struct commit_name { | |
8e5dd22b JH |
73 | const char *head_name; /* which head's ancestor? */ |
74 | int generation; /* how many parents away from head_name */ | |
f76412ed JH |
75 | }; |
76 | ||
8e5dd22b | 77 | /* Name the commit as nth generation ancestor of head_name; |
f76412ed JH |
78 | * we count only the first-parent relationship for naming purposes. |
79 | */ | |
8e5dd22b | 80 | static void name_commit(struct commit *commit, const char *head_name, int nth) |
f76412ed JH |
81 | { |
82 | struct commit_name *name; | |
d3ff6f55 LT |
83 | if (!commit->util) |
84 | commit->util = xmalloc(sizeof(struct commit_name)); | |
85 | name = commit->util; | |
8e5dd22b | 86 | name->head_name = head_name; |
f76412ed JH |
87 | name->generation = nth; |
88 | } | |
89 | ||
90 | /* Parent is the first parent of the commit. We may name it | |
8e5dd22b | 91 | * as (n+1)th generation ancestor of the same head_name as |
bcaf60b2 | 92 | * commit is nth generation ancestor of, if that generation |
f76412ed JH |
93 | * number is better than the name it already has. |
94 | */ | |
95 | static void name_parent(struct commit *commit, struct commit *parent) | |
96 | { | |
d3ff6f55 LT |
97 | struct commit_name *commit_name = commit->util; |
98 | struct commit_name *parent_name = parent->util; | |
f76412ed JH |
99 | if (!commit_name) |
100 | return; | |
101 | if (!parent_name || | |
102 | commit_name->generation + 1 < parent_name->generation) | |
8e5dd22b | 103 | name_commit(parent, commit_name->head_name, |
f76412ed JH |
104 | commit_name->generation + 1); |
105 | } | |
106 | ||
8e5dd22b JH |
107 | static int name_first_parent_chain(struct commit *c) |
108 | { | |
109 | int i = 0; | |
110 | while (c) { | |
111 | struct commit *p; | |
d3ff6f55 | 112 | if (!c->util) |
8e5dd22b JH |
113 | break; |
114 | if (!c->parents) | |
115 | break; | |
116 | p = c->parents->item; | |
d3ff6f55 | 117 | if (!p->util) { |
8e5dd22b JH |
118 | name_parent(c, p); |
119 | i++; | |
120 | } | |
f8263c53 AJ |
121 | else |
122 | break; | |
8e5dd22b JH |
123 | c = p; |
124 | } | |
125 | return i; | |
126 | } | |
127 | ||
128 | static void name_commits(struct commit_list *list, | |
129 | struct commit **rev, | |
130 | char **ref_name, | |
131 | int num_rev) | |
132 | { | |
133 | struct commit_list *cl; | |
134 | struct commit *c; | |
135 | int i; | |
136 | ||
137 | /* First give names to the given heads */ | |
138 | for (cl = list; cl; cl = cl->next) { | |
139 | c = cl->item; | |
d3ff6f55 | 140 | if (c->util) |
8e5dd22b JH |
141 | continue; |
142 | for (i = 0; i < num_rev; i++) { | |
143 | if (rev[i] == c) { | |
144 | name_commit(c, ref_name[i], 0); | |
145 | break; | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | /* Then commits on the first parent ancestry chain */ | |
151 | do { | |
152 | i = 0; | |
153 | for (cl = list; cl; cl = cl->next) { | |
154 | i += name_first_parent_chain(cl->item); | |
155 | } | |
156 | } while (i); | |
157 | ||
158 | /* Finally, any unnamed commits */ | |
159 | do { | |
160 | i = 0; | |
161 | for (cl = list; cl; cl = cl->next) { | |
162 | struct commit_list *parents; | |
163 | struct commit_name *n; | |
164 | int nth; | |
165 | c = cl->item; | |
d3ff6f55 | 166 | if (!c->util) |
8e5dd22b | 167 | continue; |
d3ff6f55 | 168 | n = c->util; |
8e5dd22b JH |
169 | parents = c->parents; |
170 | nth = 0; | |
171 | while (parents) { | |
172 | struct commit *p = parents->item; | |
013f276e | 173 | char newname[1000], *en; |
8e5dd22b JH |
174 | parents = parents->next; |
175 | nth++; | |
d3ff6f55 | 176 | if (p->util) |
8e5dd22b | 177 | continue; |
013f276e | 178 | en = newname; |
fbaf834d JH |
179 | switch (n->generation) { |
180 | case 0: | |
013f276e | 181 | en += sprintf(en, "%s", n->head_name); |
fbaf834d JH |
182 | break; |
183 | case 1: | |
013f276e | 184 | en += sprintf(en, "%s^", n->head_name); |
fbaf834d JH |
185 | break; |
186 | default: | |
013f276e JH |
187 | en += sprintf(en, "%s~%d", |
188 | n->head_name, n->generation); | |
189 | break; | |
fbaf834d | 190 | } |
013f276e JH |
191 | if (nth == 1) |
192 | en += sprintf(en, "^"); | |
193 | else | |
194 | en += sprintf(en, "^%d", nth); | |
9befac47 | 195 | name_commit(p, xstrdup(newname), 0); |
8e5dd22b JH |
196 | i++; |
197 | name_first_parent_chain(p); | |
198 | } | |
199 | } | |
200 | } while (i); | |
201 | } | |
202 | ||
f76412ed JH |
203 | static int mark_seen(struct commit *commit, struct commit_list **seen_p) |
204 | { | |
205 | if (!commit->object.flags) { | |
26a8ad25 | 206 | commit_list_insert(commit, seen_p); |
f76412ed JH |
207 | return 1; |
208 | } | |
209 | return 0; | |
210 | } | |
211 | ||
212 | static void join_revs(struct commit_list **list_p, | |
213 | struct commit_list **seen_p, | |
214 | int num_rev, int extra) | |
215 | { | |
216 | int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1); | |
217 | int all_revs = all_mask & ~((1u << REV_SHIFT) - 1); | |
218 | ||
219 | while (*list_p) { | |
220 | struct commit_list *parents; | |
9ce70285 | 221 | int still_interesting = !!interesting(*list_p); |
f76412ed JH |
222 | struct commit *commit = pop_one_commit(list_p); |
223 | int flags = commit->object.flags & all_mask; | |
f76412ed | 224 | |
9ce70285 | 225 | if (!still_interesting && extra <= 0) |
f76412ed JH |
226 | break; |
227 | ||
228 | mark_seen(commit, seen_p); | |
229 | if ((flags & all_revs) == all_revs) | |
230 | flags |= UNINTERESTING; | |
231 | parents = commit->parents; | |
232 | ||
233 | while (parents) { | |
234 | struct commit *p = parents->item; | |
235 | int this_flag = p->object.flags; | |
236 | parents = parents->next; | |
f76412ed JH |
237 | if ((this_flag & flags) == flags) |
238 | continue; | |
6b209d47 JH |
239 | if (!p->object.parsed) |
240 | parse_commit(p); | |
f76412ed JH |
241 | if (mark_seen(p, seen_p) && !still_interesting) |
242 | extra--; | |
243 | p->object.flags |= flags; | |
244 | insert_by_date(p, list_p); | |
245 | } | |
246 | } | |
6b209d47 JH |
247 | |
248 | /* | |
249 | * Postprocess to complete well-poisoning. | |
250 | * | |
251 | * At this point we have all the commits we have seen in | |
26a8ad25 JH |
252 | * seen_p list. Mark anything that can be reached from |
253 | * uninteresting commits not interesting. | |
6b209d47 JH |
254 | */ |
255 | for (;;) { | |
256 | int changed = 0; | |
257 | struct commit_list *s; | |
258 | for (s = *seen_p; s; s = s->next) { | |
259 | struct commit *c = s->item; | |
260 | struct commit_list *parents; | |
261 | ||
262 | if (((c->object.flags & all_revs) != all_revs) && | |
263 | !(c->object.flags & UNINTERESTING)) | |
264 | continue; | |
265 | ||
266 | /* The current commit is either a merge base or | |
267 | * already uninteresting one. Mark its parents | |
268 | * as uninteresting commits _only_ if they are | |
269 | * already parsed. No reason to find new ones | |
270 | * here. | |
271 | */ | |
272 | parents = c->parents; | |
273 | while (parents) { | |
274 | struct commit *p = parents->item; | |
275 | parents = parents->next; | |
276 | if (!(p->object.flags & UNINTERESTING)) { | |
277 | p->object.flags |= UNINTERESTING; | |
278 | changed = 1; | |
279 | } | |
280 | } | |
281 | } | |
282 | if (!changed) | |
283 | break; | |
284 | } | |
f76412ed JH |
285 | } |
286 | ||
013f276e | 287 | static void show_one_commit(struct commit *commit, int no_name) |
f76412ed | 288 | { |
f285a2d7 | 289 | struct strbuf pretty = STRBUF_INIT; |
80583c0e | 290 | const char *pretty_str = "(unavailable)"; |
d3ff6f55 | 291 | struct commit_name *name = commit->util; |
80583c0e JH |
292 | |
293 | if (commit->object.parsed) { | |
674d1727 | 294 | pretty_print_commit(CMIT_FMT_ONELINE, commit, |
4593fb84 | 295 | &pretty, 0, NULL, NULL, 0, 0); |
674d1727 | 296 | pretty_str = pretty.buf; |
80583c0e JH |
297 | } |
298 | if (!prefixcmp(pretty_str, "[PATCH] ")) | |
299 | pretty_str += 8; | |
013f276e JH |
300 | |
301 | if (!no_name) { | |
302 | if (name && name->head_name) { | |
303 | printf("[%s", name->head_name); | |
304 | if (name->generation) { | |
305 | if (name->generation == 1) | |
306 | printf("^"); | |
307 | else | |
308 | printf("~%d", name->generation); | |
309 | } | |
310 | printf("] "); | |
311 | } | |
312 | else | |
313 | printf("[%s] ", | |
314 | find_unique_abbrev(commit->object.sha1, 7)); | |
f76412ed | 315 | } |
80583c0e | 316 | puts(pretty_str); |
674d1727 | 317 | strbuf_release(&pretty); |
f76412ed JH |
318 | } |
319 | ||
320 | static char *ref_name[MAX_REVS + 1]; | |
321 | static int ref_name_cnt; | |
322 | ||
bb5ebed7 JH |
323 | static const char *find_digit_prefix(const char *s, int *v) |
324 | { | |
325 | const char *p; | |
326 | int ver; | |
327 | char ch; | |
328 | ||
329 | for (p = s, ver = 0; | |
330 | '0' <= (ch = *p) && ch <= '9'; | |
331 | p++) | |
332 | ver = ver * 10 + ch - '0'; | |
333 | *v = ver; | |
334 | return p; | |
335 | } | |
336 | ||
337 | ||
338 | static int version_cmp(const char *a, const char *b) | |
339 | { | |
340 | while (1) { | |
341 | int va, vb; | |
342 | ||
343 | a = find_digit_prefix(a, &va); | |
344 | b = find_digit_prefix(b, &vb); | |
345 | if (va != vb) | |
346 | return va - vb; | |
347 | ||
348 | while (1) { | |
349 | int ca = *a; | |
350 | int cb = *b; | |
351 | if ('0' <= ca && ca <= '9') | |
352 | ca = 0; | |
353 | if ('0' <= cb && cb <= '9') | |
354 | cb = 0; | |
355 | if (ca != cb) | |
356 | return ca - cb; | |
357 | if (!ca) | |
358 | break; | |
359 | a++; | |
360 | b++; | |
361 | } | |
362 | if (!*a && !*b) | |
363 | return 0; | |
364 | } | |
365 | } | |
366 | ||
628894b2 JH |
367 | static int compare_ref_name(const void *a_, const void *b_) |
368 | { | |
369 | const char * const*a = a_, * const*b = b_; | |
bb5ebed7 | 370 | return version_cmp(*a, *b); |
628894b2 JH |
371 | } |
372 | ||
373 | static void sort_ref_range(int bottom, int top) | |
374 | { | |
375 | qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]), | |
376 | compare_ref_name); | |
377 | } | |
378 | ||
76a44c5c JH |
379 | static int append_ref(const char *refname, const unsigned char *sha1, |
380 | int allow_dups) | |
f76412ed JH |
381 | { |
382 | struct commit *commit = lookup_commit_reference_gently(sha1, 1); | |
bb5ebed7 JH |
383 | int i; |
384 | ||
f76412ed JH |
385 | if (!commit) |
386 | return 0; | |
bb5ebed7 | 387 | |
76a44c5c JH |
388 | if (!allow_dups) { |
389 | /* Avoid adding the same thing twice */ | |
390 | for (i = 0; i < ref_name_cnt; i++) | |
391 | if (!strcmp(refname, ref_name[i])) | |
392 | return 0; | |
393 | } | |
79778e46 | 394 | if (MAX_REVS <= ref_name_cnt) { |
33fa4d3d | 395 | warning("ignoring %s; cannot handle more than %d refs", |
f76412ed JH |
396 | refname, MAX_REVS); |
397 | return 0; | |
398 | } | |
9befac47 | 399 | ref_name[ref_name_cnt++] = xstrdup(refname); |
f76412ed JH |
400 | ref_name[ref_name_cnt] = NULL; |
401 | return 0; | |
402 | } | |
403 | ||
8da19775 | 404 | static int append_head_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
f76412ed | 405 | { |
92421502 JH |
406 | unsigned char tmp[20]; |
407 | int ofs = 11; | |
1968d77d | 408 | if (prefixcmp(refname, "refs/heads/")) |
f76412ed | 409 | return 0; |
92421502 JH |
410 | /* If both heads/foo and tags/foo exists, get_sha1 would |
411 | * get confused. | |
412 | */ | |
a89fccd2 | 413 | if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1)) |
92421502 | 414 | ofs = 5; |
76a44c5c | 415 | return append_ref(refname + ofs, sha1, 0); |
f76412ed JH |
416 | } |
417 | ||
f49006b0 BG |
418 | static int append_remote_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
419 | { | |
420 | unsigned char tmp[20]; | |
421 | int ofs = 13; | |
1968d77d | 422 | if (prefixcmp(refname, "refs/remotes/")) |
f49006b0 BG |
423 | return 0; |
424 | /* If both heads/foo and tags/foo exists, get_sha1 would | |
425 | * get confused. | |
426 | */ | |
427 | if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1)) | |
428 | ofs = 5; | |
76a44c5c | 429 | return append_ref(refname + ofs, sha1, 0); |
f49006b0 BG |
430 | } |
431 | ||
8da19775 | 432 | static int append_tag_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
f76412ed | 433 | { |
cc44c765 | 434 | if (prefixcmp(refname, "refs/tags/")) |
f76412ed | 435 | return 0; |
76a44c5c | 436 | return append_ref(refname + 5, sha1, 0); |
f76412ed JH |
437 | } |
438 | ||
287f8600 JH |
439 | static const char *match_ref_pattern = NULL; |
440 | static int match_ref_slash = 0; | |
441 | static int count_slash(const char *s) | |
442 | { | |
443 | int cnt = 0; | |
444 | while (*s) | |
445 | if (*s++ == '/') | |
446 | cnt++; | |
447 | return cnt; | |
448 | } | |
449 | ||
8da19775 | 450 | static int append_matching_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
287f8600 JH |
451 | { |
452 | /* we want to allow pattern hold/<asterisk> to show all | |
453 | * branches under refs/heads/hold/, and v0.99.9? to show | |
454 | * refs/tags/v0.99.9a and friends. | |
455 | */ | |
456 | const char *tail; | |
457 | int slash = count_slash(refname); | |
458 | for (tail = refname; *tail && match_ref_slash < slash; ) | |
459 | if (*tail++ == '/') | |
460 | slash--; | |
461 | if (!*tail) | |
462 | return 0; | |
463 | if (fnmatch(match_ref_pattern, tail, 0)) | |
464 | return 0; | |
599065a3 | 465 | if (!prefixcmp(refname, "refs/heads/")) |
8da19775 | 466 | return append_head_ref(refname, sha1, flag, cb_data); |
599065a3 | 467 | if (!prefixcmp(refname, "refs/tags/")) |
8da19775 | 468 | return append_tag_ref(refname, sha1, flag, cb_data); |
76a44c5c | 469 | return append_ref(refname, sha1, 0); |
287f8600 JH |
470 | } |
471 | ||
f49006b0 | 472 | static void snarf_refs(int head, int remotes) |
f76412ed | 473 | { |
628894b2 JH |
474 | if (head) { |
475 | int orig_cnt = ref_name_cnt; | |
cb5d709f | 476 | for_each_ref(append_head_ref, NULL); |
628894b2 JH |
477 | sort_ref_range(orig_cnt, ref_name_cnt); |
478 | } | |
f49006b0 | 479 | if (remotes) { |
628894b2 | 480 | int orig_cnt = ref_name_cnt; |
f49006b0 | 481 | for_each_ref(append_remote_ref, NULL); |
628894b2 JH |
482 | sort_ref_range(orig_cnt, ref_name_cnt); |
483 | } | |
f76412ed JH |
484 | } |
485 | ||
ed378ec7 | 486 | static int rev_is_head(char *head, int headlen, char *name, |
f76412ed JH |
487 | unsigned char *head_sha1, unsigned char *sha1) |
488 | { | |
ed378ec7 | 489 | if ((!head[0]) || |
a89fccd2 | 490 | (head_sha1 && sha1 && hashcmp(head_sha1, sha1))) |
f76412ed | 491 | return 0; |
cc44c765 | 492 | if (!prefixcmp(head, "refs/heads/")) |
afdcec73 | 493 | head += 11; |
cc44c765 | 494 | if (!prefixcmp(name, "refs/heads/")) |
afdcec73 | 495 | name += 11; |
cc44c765 | 496 | else if (!prefixcmp(name, "heads/")) |
afdcec73 | 497 | name += 6; |
ed378ec7 | 498 | return !strcmp(head, name); |
f76412ed JH |
499 | } |
500 | ||
501 | static int show_merge_base(struct commit_list *seen, int num_rev) | |
502 | { | |
503 | int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1); | |
504 | int all_revs = all_mask & ~((1u << REV_SHIFT) - 1); | |
2f0f8b71 | 505 | int exit_status = 1; |
f76412ed JH |
506 | |
507 | while (seen) { | |
508 | struct commit *commit = pop_one_commit(&seen); | |
509 | int flags = commit->object.flags & all_mask; | |
510 | if (!(flags & UNINTERESTING) && | |
511 | ((flags & all_revs) == all_revs)) { | |
512 | puts(sha1_to_hex(commit->object.sha1)); | |
2f0f8b71 JH |
513 | exit_status = 0; |
514 | commit->object.flags |= UNINTERESTING; | |
f76412ed JH |
515 | } |
516 | } | |
2f0f8b71 | 517 | return exit_status; |
f76412ed JH |
518 | } |
519 | ||
1f8af483 JH |
520 | static int show_independent(struct commit **rev, |
521 | int num_rev, | |
522 | char **ref_name, | |
523 | unsigned int *rev_mask) | |
524 | { | |
525 | int i; | |
526 | ||
527 | for (i = 0; i < num_rev; i++) { | |
528 | struct commit *commit = rev[i]; | |
529 | unsigned int flag = rev_mask[i]; | |
530 | ||
531 | if (commit->object.flags == flag) | |
532 | puts(sha1_to_hex(commit->object.sha1)); | |
533 | commit->object.flags |= UNINTERESTING; | |
534 | } | |
535 | return 0; | |
536 | } | |
537 | ||
287f8600 JH |
538 | static void append_one_rev(const char *av) |
539 | { | |
540 | unsigned char revkey[20]; | |
541 | if (!get_sha1(av, revkey)) { | |
76a44c5c | 542 | append_ref(av, revkey, 0); |
287f8600 JH |
543 | return; |
544 | } | |
87758f97 | 545 | if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) { |
287f8600 JH |
546 | /* glob style match */ |
547 | int saved_matches = ref_name_cnt; | |
548 | match_ref_pattern = av; | |
549 | match_ref_slash = count_slash(av); | |
cb5d709f | 550 | for_each_ref(append_matching_ref, NULL); |
287f8600 JH |
551 | if (saved_matches == ref_name_cnt && |
552 | ref_name_cnt < MAX_REVS) | |
553 | error("no matching refs with %s", av); | |
06d900cf JH |
554 | if (saved_matches + 1 < ref_name_cnt) |
555 | sort_ref_range(saved_matches, ref_name_cnt); | |
287f8600 JH |
556 | return; |
557 | } | |
558 | die("bad sha1 reference %s", av); | |
559 | } | |
560 | ||
ef90d6d4 | 561 | static int git_show_branch_config(const char *var, const char *value, void *cb) |
c2bdd6af JH |
562 | { |
563 | if (!strcmp(var, "showbranch.default")) { | |
4f9c412b JH |
564 | if (!value) |
565 | return config_error_nonbool(var); | |
c2bdd6af JH |
566 | if (default_alloc <= default_num + 1) { |
567 | default_alloc = default_alloc * 3 / 2 + 20; | |
568 | default_arg = xrealloc(default_arg, sizeof *default_arg * default_alloc); | |
569 | } | |
9befac47 | 570 | default_arg[default_num++] = xstrdup(value); |
c2bdd6af JH |
571 | default_arg[default_num] = NULL; |
572 | return 0; | |
573 | } | |
574 | ||
ab07ba2a MH |
575 | if (!strcmp(var, "color.showbranch")) { |
576 | showbranch_use_color = git_config_colorbool(var, value, -1); | |
577 | return 0; | |
578 | } | |
579 | ||
580 | return git_color_default_config(var, value, cb); | |
c2bdd6af JH |
581 | } |
582 | ||
c24fe420 JH |
583 | static int omit_in_dense(struct commit *commit, struct commit **rev, int n) |
584 | { | |
585 | /* If the commit is tip of the named branches, do not | |
586 | * omit it. | |
587 | * Otherwise, if it is a merge that is reachable from only one | |
588 | * tip, it is not that interesting. | |
589 | */ | |
590 | int i, flag, count; | |
591 | for (i = 0; i < n; i++) | |
592 | if (rev[i] == commit) | |
593 | return 0; | |
594 | flag = commit->object.flags; | |
595 | for (i = count = 0; i < n; i++) { | |
596 | if (flag & (1u << (i + REV_SHIFT))) | |
597 | count++; | |
598 | } | |
599 | if (count == 1) | |
600 | return 1; | |
601 | return 0; | |
602 | } | |
603 | ||
76a44c5c JH |
604 | static void parse_reflog_param(const char *arg, int *cnt, const char **base) |
605 | { | |
606 | char *ep; | |
607 | *cnt = strtoul(arg, &ep, 10); | |
608 | if (*ep == ',') | |
609 | *base = ep + 1; | |
610 | else if (*ep) | |
5acb3e50 | 611 | die("unrecognized reflog param '%s'", arg); |
76a44c5c JH |
612 | else |
613 | *base = NULL; | |
614 | if (*cnt <= 0) | |
615 | *cnt = DEFAULT_REFLOG; | |
616 | } | |
617 | ||
a633fca0 | 618 | int cmd_show_branch(int ac, const char **av, const char *prefix) |
f76412ed JH |
619 | { |
620 | struct commit *rev[MAX_REVS], *commit; | |
76a44c5c | 621 | char *reflog_msg[MAX_REVS]; |
f76412ed | 622 | struct commit_list *list = NULL, *seen = NULL; |
1f8af483 | 623 | unsigned int rev_mask[MAX_REVS]; |
f76412ed | 624 | int num_rev, i, extra = 0; |
f49006b0 | 625 | int all_heads = 0, all_remotes = 0; |
6b209d47 | 626 | int all_mask, all_revs; |
4c8725f1 | 627 | int lifo = 1; |
ed378ec7 LT |
628 | char head[128]; |
629 | const char *head_p; | |
630 | int head_len; | |
f76412ed JH |
631 | unsigned char head_sha1[20]; |
632 | int merge_base = 0; | |
1f8af483 | 633 | int independent = 0; |
013f276e JH |
634 | int no_name = 0; |
635 | int sha1_name = 0; | |
6b209d47 | 636 | int shown_merge_point = 0; |
1aa68d67 | 637 | int with_current_branch = 0; |
ebedc319 | 638 | int head_at = -1; |
d320a543 | 639 | int topics = 0; |
c24fe420 | 640 | int dense = 1; |
7e3fe904 | 641 | int reflog = 0; |
76a44c5c | 642 | const char *reflog_base = NULL; |
f76412ed | 643 | |
ef90d6d4 | 644 | git_config(git_show_branch_config, NULL); |
076b2324 | 645 | |
ab07ba2a MH |
646 | if (showbranch_use_color == -1) |
647 | showbranch_use_color = git_use_color_default; | |
648 | ||
c2bdd6af JH |
649 | /* If nothing is specified, try the default first */ |
650 | if (ac == 1 && default_num) { | |
651 | ac = default_num + 1; | |
652 | av = default_arg - 1; /* ick; we would not address av[0] */ | |
653 | } | |
654 | ||
f76412ed | 655 | while (1 < ac && av[1][0] == '-') { |
51ce34b9 | 656 | const char *arg = av[1]; |
c2bdd6af JH |
657 | if (!strcmp(arg, "--")) { |
658 | ac--; av++; | |
659 | break; | |
660 | } | |
f49006b0 BG |
661 | else if (!strcmp(arg, "--all") || !strcmp(arg, "-a")) |
662 | all_heads = all_remotes = 1; | |
663 | else if (!strcmp(arg, "--remotes") || !strcmp(arg, "-r")) | |
664 | all_remotes = 1; | |
f76412ed JH |
665 | else if (!strcmp(arg, "--more")) |
666 | extra = 1; | |
1f8af483 JH |
667 | else if (!strcmp(arg, "--list")) |
668 | extra = -1; | |
013f276e JH |
669 | else if (!strcmp(arg, "--no-name")) |
670 | no_name = 1; | |
1aa68d67 JH |
671 | else if (!strcmp(arg, "--current")) |
672 | with_current_branch = 1; | |
013f276e JH |
673 | else if (!strcmp(arg, "--sha1-name")) |
674 | sha1_name = 1; | |
cc44c765 | 675 | else if (!prefixcmp(arg, "--more=")) |
f76412ed | 676 | extra = atoi(arg + 7); |
f76412ed JH |
677 | else if (!strcmp(arg, "--merge-base")) |
678 | merge_base = 1; | |
1f8af483 JH |
679 | else if (!strcmp(arg, "--independent")) |
680 | independent = 1; | |
6b209d47 | 681 | else if (!strcmp(arg, "--topo-order")) |
4c8725f1 | 682 | lifo = 1; |
d320a543 JH |
683 | else if (!strcmp(arg, "--topics")) |
684 | topics = 1; | |
c24fe420 JH |
685 | else if (!strcmp(arg, "--sparse")) |
686 | dense = 0; | |
4c8725f1 JH |
687 | else if (!strcmp(arg, "--date-order")) |
688 | lifo = 0; | |
084ae0a7 | 689 | else if (!strcmp(arg, "--reflog") || !strcmp(arg, "-g")) { |
7e3fe904 JH |
690 | reflog = DEFAULT_REFLOG; |
691 | } | |
cc44c765 | 692 | else if (!prefixcmp(arg, "--reflog=")) |
76a44c5c | 693 | parse_reflog_param(arg + 9, &reflog, &reflog_base); |
cc44c765 | 694 | else if (!prefixcmp(arg, "-g=")) |
084ae0a7 | 695 | parse_reflog_param(arg + 3, &reflog, &reflog_base); |
ab07ba2a MH |
696 | else if (!strcmp(arg, "--color")) |
697 | showbranch_use_color = 1; | |
698 | else if (!strcmp(arg, "--no-color")) | |
699 | showbranch_use_color = 0; | |
f76412ed JH |
700 | else |
701 | usage(show_branch_usage); | |
702 | ac--; av++; | |
703 | } | |
704 | ac--; av++; | |
705 | ||
df373ea9 | 706 | if (extra || reflog) { |
76a44c5c JH |
707 | /* "listing" mode is incompatible with |
708 | * independent nor merge-base modes. | |
709 | */ | |
710 | if (independent || merge_base) | |
711 | usage(show_branch_usage); | |
df373ea9 | 712 | if (reflog && ((0 < extra) || all_heads || all_remotes)) |
76a44c5c JH |
713 | /* |
714 | * Asking for --more in reflog mode does not | |
b15af079 JH |
715 | * make sense. --list is Ok. |
716 | * | |
717 | * Also --all and --remotes do not make sense either. | |
76a44c5c | 718 | */ |
b15af079 | 719 | usage(show_branch_usage_reflog); |
76a44c5c | 720 | } |
1f8af483 | 721 | |
bb5ebed7 | 722 | /* If nothing is specified, show all branches by default */ |
f49006b0 | 723 | if (ac + all_heads + all_remotes == 0) |
bb5ebed7 JH |
724 | all_heads = 1; |
725 | ||
7e3fe904 | 726 | if (reflog) { |
76a44c5c JH |
727 | unsigned char sha1[20]; |
728 | char nth_desc[256]; | |
729 | char *ref; | |
730 | int base = 0; | |
df373ea9 JH |
731 | |
732 | if (ac == 0) { | |
733 | static const char *fake_av[2]; | |
632ac9fd JH |
734 | const char *refname; |
735 | ||
736 | refname = resolve_ref("HEAD", sha1, 1, NULL); | |
737 | fake_av[0] = xstrdup(refname); | |
df373ea9 JH |
738 | fake_av[1] = NULL; |
739 | av = fake_av; | |
740 | ac = 1; | |
741 | } | |
76a44c5c | 742 | if (ac != 1) |
7e3fe904 | 743 | die("--reflog option needs one branch name"); |
df373ea9 | 744 | |
b15af079 JH |
745 | if (MAX_REVS < reflog) |
746 | die("Only %d entries can be shown at one time.", | |
747 | MAX_REVS); | |
76a44c5c JH |
748 | if (!dwim_ref(*av, strlen(*av), sha1, &ref)) |
749 | die("No such ref %s", *av); | |
750 | ||
751 | /* Has the base been specified? */ | |
752 | if (reflog_base) { | |
753 | char *ep; | |
754 | base = strtoul(reflog_base, &ep, 10); | |
755 | if (*ep) { | |
756 | /* Ah, that is a date spec... */ | |
757 | unsigned long at; | |
758 | at = approxidate(reflog_base); | |
759 | read_ref_at(ref, at, -1, sha1, NULL, | |
760 | NULL, NULL, &base); | |
761 | } | |
762 | } | |
763 | ||
7e3fe904 | 764 | for (i = 0; i < reflog; i++) { |
3a55602e SP |
765 | char *logmsg, *m; |
766 | const char *msg; | |
76a44c5c JH |
767 | unsigned long timestamp; |
768 | int tz; | |
769 | ||
770 | if (read_ref_at(ref, 0, base+i, sha1, &logmsg, | |
771 | ×tamp, &tz, NULL)) { | |
772 | reflog = i; | |
773 | break; | |
774 | } | |
775 | msg = strchr(logmsg, '\t'); | |
776 | if (!msg) | |
777 | msg = "(none)"; | |
778 | else | |
779 | msg++; | |
780 | m = xmalloc(strlen(msg) + 200); | |
781 | sprintf(m, "(%s) %s", | |
16bfefee | 782 | show_date(timestamp, tz, 1), |
76a44c5c JH |
783 | msg); |
784 | reflog_msg[i] = m; | |
785 | free(logmsg); | |
786 | sprintf(nth_desc, "%s@{%d}", *av, base+i); | |
787 | append_ref(nth_desc, sha1, 1); | |
7e3fe904 JH |
788 | } |
789 | } | |
df373ea9 JH |
790 | else if (all_heads + all_remotes) |
791 | snarf_refs(all_heads, all_remotes); | |
7e3fe904 JH |
792 | else { |
793 | while (0 < ac) { | |
794 | append_one_rev(*av); | |
795 | ac--; av++; | |
796 | } | |
287f8600 | 797 | } |
bb5ebed7 | 798 | |
8da19775 | 799 | head_p = resolve_ref("HEAD", head_sha1, 1, NULL); |
ed378ec7 LT |
800 | if (head_p) { |
801 | head_len = strlen(head_p); | |
802 | memcpy(head, head_p, head_len + 1); | |
1aa68d67 JH |
803 | } |
804 | else { | |
ed378ec7 LT |
805 | head_len = 0; |
806 | head[0] = 0; | |
1aa68d67 JH |
807 | } |
808 | ||
ed378ec7 | 809 | if (with_current_branch && head_p) { |
1aa68d67 JH |
810 | int has_head = 0; |
811 | for (i = 0; !has_head && i < ref_name_cnt; i++) { | |
812 | /* We are only interested in adding the branch | |
813 | * HEAD points at. | |
814 | */ | |
ed378ec7 LT |
815 | if (rev_is_head(head, |
816 | head_len, | |
1aa68d67 JH |
817 | ref_name[i], |
818 | head_sha1, NULL)) | |
819 | has_head++; | |
820 | } | |
821 | if (!has_head) { | |
f8fcb57b JH |
822 | int offset = !prefixcmp(head, "refs/heads/") ? 11 : 0; |
823 | append_one_rev(head + offset); | |
1aa68d67 JH |
824 | } |
825 | } | |
826 | ||
287f8600 JH |
827 | if (!ref_name_cnt) { |
828 | fprintf(stderr, "No revs to be shown.\n"); | |
829 | exit(0); | |
830 | } | |
f76412ed JH |
831 | |
832 | for (num_rev = 0; ref_name[num_rev]; num_rev++) { | |
833 | unsigned char revkey[20]; | |
1f8af483 | 834 | unsigned int flag = 1u << (num_rev + REV_SHIFT); |
f76412ed JH |
835 | |
836 | if (MAX_REVS <= num_rev) | |
837 | die("cannot handle more than %d revs.", MAX_REVS); | |
838 | if (get_sha1(ref_name[num_rev], revkey)) | |
7246ed43 | 839 | die("'%s' is not a valid ref.", ref_name[num_rev]); |
f76412ed JH |
840 | commit = lookup_commit_reference(revkey); |
841 | if (!commit) | |
842 | die("cannot find commit %s (%s)", | |
843 | ref_name[num_rev], revkey); | |
844 | parse_commit(commit); | |
f76412ed JH |
845 | mark_seen(commit, &seen); |
846 | ||
847 | /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1, | |
848 | * and so on. REV_SHIFT bits from bit 0 are used for | |
849 | * internal bookkeeping. | |
850 | */ | |
1f8af483 JH |
851 | commit->object.flags |= flag; |
852 | if (commit->object.flags == flag) | |
853 | insert_by_date(commit, &list); | |
f76412ed JH |
854 | rev[num_rev] = commit; |
855 | } | |
1f8af483 JH |
856 | for (i = 0; i < num_rev; i++) |
857 | rev_mask[i] = rev[i]->object.flags; | |
858 | ||
859 | if (0 <= extra) | |
860 | join_revs(&list, &seen, num_rev, extra); | |
f76412ed | 861 | |
26a8ad25 JH |
862 | sort_by_date(&seen); |
863 | ||
f76412ed JH |
864 | if (merge_base) |
865 | return show_merge_base(seen, num_rev); | |
866 | ||
1f8af483 JH |
867 | if (independent) |
868 | return show_independent(rev, num_rev, ref_name, rev_mask); | |
869 | ||
870 | /* Show list; --more=-1 means list-only */ | |
c9d023b2 | 871 | if (1 < num_rev || extra < 0) { |
f76412ed JH |
872 | for (i = 0; i < num_rev; i++) { |
873 | int j; | |
ed378ec7 LT |
874 | int is_head = rev_is_head(head, |
875 | head_len, | |
f76412ed JH |
876 | ref_name[i], |
877 | head_sha1, | |
878 | rev[i]->object.sha1); | |
1f8af483 JH |
879 | if (extra < 0) |
880 | printf("%c [%s] ", | |
881 | is_head ? '*' : ' ', ref_name[i]); | |
882 | else { | |
883 | for (j = 0; j < i; j++) | |
884 | putchar(' '); | |
ab07ba2a MH |
885 | printf("%s%c%s [%s] ", |
886 | get_color_code(i % COLUMN_COLORS_MAX), | |
887 | is_head ? '*' : '!', | |
888 | get_color_reset_code(), ref_name[i]); | |
1f8af483 | 889 | } |
76a44c5c JH |
890 | |
891 | if (!reflog) { | |
892 | /* header lines never need name */ | |
893 | show_one_commit(rev[i], 1); | |
894 | } | |
895 | else | |
896 | puts(reflog_msg[i]); | |
897 | ||
ebedc319 JH |
898 | if (is_head) |
899 | head_at = i; | |
f76412ed | 900 | } |
1f8af483 JH |
901 | if (0 <= extra) { |
902 | for (i = 0; i < num_rev; i++) | |
903 | putchar('-'); | |
904 | putchar('\n'); | |
905 | } | |
f5e375c9 | 906 | } |
1f8af483 JH |
907 | if (extra < 0) |
908 | exit(0); | |
f5e375c9 | 909 | |
8e5dd22b | 910 | /* Sort topologically */ |
4c8725f1 | 911 | sort_in_topological_order(&seen, lifo); |
8e5dd22b JH |
912 | |
913 | /* Give names to commits */ | |
013f276e JH |
914 | if (!sha1_name && !no_name) |
915 | name_commits(seen, rev, ref_name, num_rev); | |
8e5dd22b JH |
916 | |
917 | all_mask = ((1u << (REV_SHIFT + num_rev)) - 1); | |
918 | all_revs = all_mask & ~((1u << REV_SHIFT) - 1); | |
8e5dd22b | 919 | |
f76412ed JH |
920 | while (seen) { |
921 | struct commit *commit = pop_one_commit(&seen); | |
922 | int this_flag = commit->object.flags; | |
f794c234 | 923 | int is_merge_point = ((this_flag & all_revs) == all_revs); |
f5e375c9 | 924 | |
f794c234 | 925 | shown_merge_point |= is_merge_point; |
8e5dd22b | 926 | |
f5e375c9 | 927 | if (1 < num_rev) { |
c24fe420 JH |
928 | int is_merge = !!(commit->parents && |
929 | commit->parents->next); | |
f794c234 JH |
930 | if (topics && |
931 | !is_merge_point && | |
932 | (this_flag & (1u << REV_SHIFT))) | |
933 | continue; | |
c24fe420 JH |
934 | if (dense && is_merge && |
935 | omit_in_dense(commit, rev, num_rev)) | |
936 | continue; | |
ebedc319 JH |
937 | for (i = 0; i < num_rev; i++) { |
938 | int mark; | |
939 | if (!(this_flag & (1u << (i + REV_SHIFT)))) | |
940 | mark = ' '; | |
941 | else if (is_merge) | |
942 | mark = '-'; | |
943 | else if (i == head_at) | |
944 | mark = '*'; | |
945 | else | |
946 | mark = '+'; | |
ab07ba2a MH |
947 | printf("%s%c%s", |
948 | get_color_code(i % COLUMN_COLORS_MAX), | |
949 | mark, get_color_reset_code()); | |
ebedc319 | 950 | } |
f5e375c9 JH |
951 | putchar(' '); |
952 | } | |
013f276e | 953 | show_one_commit(commit, no_name); |
6b209d47 JH |
954 | |
955 | if (shown_merge_point && --extra < 0) | |
956 | break; | |
f76412ed JH |
957 | } |
958 | return 0; | |
959 | } |