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