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