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