Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/commit-reach.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / commit-reach.c` 1531 lines ยท 39520 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 3 #include "git-compat-util.h" 4 #include "commit.h" 5 #include "commit-graph.h" 6 #include "decorate.h" 7 #include "hex.h" 8 #include "prio-queue.h" 9 #include "ref-filter.h" 10 #include "revision.h" 11 #include "tag.h" 12 #include "commit-reach.h" 13 #include "ewah/ewok.h" 14 #include "trace2.h" 15 16 /* Remember to update object flag allocation in object.h */ 17 #define PARENT1 (1u<<16) 18 #define PARENT2 (1u<<17) 19 #define STALE (1u<<18) 20 #define RESULT (1u<<19) 21 #define ENQUEUED (1u<<20) 22 23 static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT | ENQUEUED); 24 25 static int compare_commits_by_gen(const void *_a, const void *_b) 26 { 27 const struct commit *a = *(const struct commit * const *)_a; 28 const struct commit *b = *(const struct commit * const *)_b; 29 30 timestamp_t generation_a = commit_graph_generation(a); 31 timestamp_t generation_b = commit_graph_generation(b); 32 33 if (generation_a < generation_b) 34 return -1; 35 if (generation_a > generation_b) 36 return 1; 37 if (a->date < b->date) 38 return -1; 39 if (a->date > b->date) 40 return 1; 41 return 0; 42 } 43 44 /* 45 * A prio_queue with O(1) termination check. 'max_nonstale' tracks 46 * the lowest-priority non-stale commit enqueued so far; once it is 47 * popped, every remaining entry is known to be STALE. 48 */ 49 struct nonstale_queue { 50 struct prio_queue pq; 51 struct commit *max_nonstale; 52 }; 53 54 static void nonstale_queue_put(struct nonstale_queue *queue, 55 struct commit *c) 56 { 57 struct commit *old = queue->max_nonstale; 58 59 prio_queue_put(&queue->pq, c); 60 if (c->object.flags & STALE) 61 return; 62 if (!old || queue->pq.compare(old, c, queue->pq.cb_data) <= 0) 63 queue->max_nonstale = c; 64 } 65 66 static struct commit *nonstale_queue_get(struct nonstale_queue *queue) 67 { 68 struct commit *commit = prio_queue_get(&queue->pq); 69 70 if (commit == queue->max_nonstale) 71 queue->max_nonstale = NULL; 72 73 return commit; 74 } 75 76 static void clear_nonstale_queue(struct nonstale_queue *queue) 77 { 78 clear_prio_queue(&queue->pq); 79 queue->max_nonstale = NULL; 80 } 81 82 /* 83 * Priority queue with per-side commit counters for paint_down_to_common(). 84 * Each non-stale queued commit occupies exactly one bucket: PARENT1-only, 85 * PARENT2-only, or both (a pending merge-base candidate). 86 */ 87 struct paint_state { 88 struct prio_queue queue; 89 size_t parent1_count; 90 size_t parent2_count; 91 size_t mb_candidate_count; 92 timestamp_t min_generation; 93 timestamp_t last_gen; 94 timestamp_t topo_ceiling; 95 }; 96 97 static void paint_count_update(struct paint_state *state, 98 unsigned flags, int delta) 99 { 100 switch (flags & (PARENT1 | PARENT2 | STALE)) { 101 case PARENT1: 102 state->parent1_count += delta; 103 break; 104 105 case PARENT2: 106 state->parent2_count += delta; 107 break; 108 109 case PARENT1 | PARENT2: 110 state->mb_candidate_count += delta; 111 break; 112 113 case PARENT1 | PARENT2 | STALE: 114 break; 115 116 default: 117 BUG("unexpected paint state"); 118 } 119 } 120 121 static void paint_queue_put(struct paint_state *state, 122 struct commit *c, unsigned add_flags) 123 { 124 unsigned old_flags = c->object.flags; 125 c->object.flags |= add_flags; 126 127 if (old_flags & ENQUEUED) { 128 paint_count_update(state, old_flags, -1); 129 paint_count_update(state, c->object.flags, 1); 130 } else { 131 c->object.flags |= ENQUEUED; 132 prio_queue_put(&state->queue, c); 133 paint_count_update(state, c->object.flags, 1); 134 } 135 } 136 137 /* 138 * Dequeue the next commit for the paint walk, or return NULL when 139 * no more merge bases can be discovered. 140 */ 141 static struct commit *paint_queue_get(struct paint_state *state) 142 { 143 struct commit *commit = prio_queue_get(&state->queue); 144 timestamp_t generation; 145 146 if (!commit) 147 return NULL; 148 149 commit->object.flags &= ~ENQUEUED; 150 generation = commit_graph_generation(commit); 151 152 if (state->min_generation && generation > state->last_gen) 153 BUG("bad generation skip %"PRItime" > %"PRItime" at %s", 154 generation, state->last_gen, 155 oid_to_hex(&commit->object.oid)); 156 state->last_gen = generation; 157 158 /* generation cutoff */ 159 if (generation < state->min_generation) 160 return NULL; 161 162 /* 163 * Check exit condition before decrementing: the counters 164 * still include this commit, so the last non-stale commit 165 * sees a non-zero count and is returned for processing. 166 */ 167 if (!state->mb_candidate_count) { 168 /* only stale entries remain */ 169 if (!state->parent1_count && !state->parent2_count) 170 return NULL; 171 172 /* one side is exhausted */ 173 if ((!state->parent1_count || !state->parent2_count) && 174 generation < state->topo_ceiling) 175 return NULL; 176 } 177 178 paint_count_update(state, commit->object.flags, -1); 179 return commit; 180 } 181 182 /* 183 * See Documentation/technical/paint-down-to-common.adoc 184 * 185 * All input commits in one and twos[] must have been parsed! 186 */ 187 static int paint_down_to_common(struct repository *r, 188 struct commit *one, int n, 189 struct commit **twos, 190 timestamp_t min_generation, 191 enum merge_base_flags mb_flags, 192 struct commit_list **result) 193 { 194 /* 195 * Generation ordering is required for the side-exhaustion and 196 * single-result early exits, which rely on topological traversal 197 * order (children visited before parents) in the ordered region. 198 */ 199 struct paint_state state = { 200 .queue = { compare_commits_by_gen_then_commit_date } 201 }; 202 struct commit *commit; 203 int i; 204 int steps = 0; 205 struct commit_list **tail = result; 206 207 state.min_generation = min_generation; 208 state.last_gen = GENERATION_NUMBER_INFINITY; 209 state.topo_ceiling = corrected_commit_dates_enabled(r) 210 ? GENERATION_NUMBER_INFINITY 211 : GENERATION_NUMBER_V1_MAX; 212 213 214 one->object.flags |= PARENT1; 215 if (!n) { 216 commit_list_append(one, result); 217 return 0; 218 } 219 paint_queue_put(&state, one, 0); 220 221 for (i = 0; i < n; i++) 222 paint_queue_put(&state, twos[i], PARENT2); 223 224 while ((commit = paint_queue_get(&state))) { 225 struct commit_list *parents; 226 int flags; 227 steps++; 228 229 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE); 230 if (flags == (PARENT1 | PARENT2)) { 231 if (!(commit->object.flags & RESULT)) { 232 commit->object.flags |= RESULT; 233 tail = commit_list_append(commit, tail); 234 /* 235 * When the queue is generation-ordered, 236 * no remaining common ancestor can be a 237 * descendant of this one. 238 */ 239 if (!(mb_flags & MERGE_BASE_FIND_ALL) && 240 state.last_gen < state.topo_ceiling) 241 break; 242 } 243 /* Mark parents of a found merge stale */ 244 flags |= STALE; 245 } 246 parents = commit->parents; 247 while (parents) { 248 struct commit *p = parents->item; 249 parents = parents->next; 250 if ((p->object.flags & flags) == flags) 251 continue; 252 if (repo_parse_commit(r, p)) { 253 clear_prio_queue(&state.queue); 254 commit_list_free(*result); 255 *result = NULL; 256 /* 257 * At this stage, we know that the commit is 258 * missing: `repo_parse_commit()` uses 259 * `OBJECT_INFO_DIE_IF_CORRUPT` and therefore 260 * corrupt commits would already have been 261 * dispatched with a `die()`. 262 */ 263 if (mb_flags & MERGE_BASE_IGNORE_MISSING_COMMITS) 264 return 0; 265 return error(_("could not parse commit %s"), 266 oid_to_hex(&p->object.oid)); 267 } 268 paint_queue_put(&state, p, flags); 269 } 270 } 271 272 clear_prio_queue(&state.queue); 273 trace2_data_intmax("paint_down_to_common", r, 274 "steps", steps); 275 commit_list_sort_by_date(result); 276 return 0; 277 } 278 279 static int merge_bases_many(struct repository *r, 280 struct commit *one, int n, 281 struct commit **twos, 282 enum merge_base_flags mb_flags, 283 struct commit_list **result) 284 { 285 struct commit_list *list = NULL, **tail = result; 286 int i; 287 288 for (i = 0; i < n; i++) { 289 if (one == twos[i]) { 290 /* 291 * We do not mark this even with RESULT so we do not 292 * have to clean it up. 293 */ 294 *result = commit_list_insert(one, result); 295 return 0; 296 } 297 } 298 299 if (!one) 300 return 0; 301 if (repo_parse_commit(r, one)) 302 return error(_("could not parse commit %s"), 303 oid_to_hex(&one->object.oid)); 304 for (i = 0; i < n; i++) { 305 if (!twos[i]) 306 return 0; 307 if (repo_parse_commit(r, twos[i])) 308 return error(_("could not parse commit %s"), 309 oid_to_hex(&twos[i]->object.oid)); 310 } 311 312 if (paint_down_to_common(r, one, n, twos, 0, mb_flags, &list)) { 313 commit_list_free(list); 314 return -1; 315 } 316 317 while (list) { 318 struct commit *commit = pop_commit(&list); 319 if (!(commit->object.flags & STALE)) 320 tail = commit_list_append(commit, tail); 321 } 322 commit_list_sort_by_date(result); 323 return 0; 324 } 325 326 int get_octopus_merge_bases(struct commit_list *in, struct commit_list **result) 327 { 328 struct commit_list *i, *j, *k; 329 330 if (!in) 331 return 0; 332 333 commit_list_insert(in->item, result); 334 335 for (i = in->next; i; i = i->next) { 336 struct commit_list *new_commits = NULL, *end = NULL; 337 338 for (j = *result; j; j = j->next) { 339 struct commit_list *bases = NULL; 340 if (repo_get_merge_bases(the_repository, i->item, 341 j->item, &bases) < 0) { 342 commit_list_free(bases); 343 commit_list_free(*result); 344 *result = NULL; 345 return -1; 346 } 347 if (!new_commits) 348 new_commits = bases; 349 else 350 end->next = bases; 351 for (k = bases; k; k = k->next) 352 end = k; 353 } 354 commit_list_free(*result); 355 *result = new_commits; 356 } 357 return 0; 358 } 359 360 static int remove_redundant_no_gen(struct repository *r, 361 struct commit **array, 362 size_t cnt, size_t *dedup_cnt) 363 { 364 struct commit **work; 365 unsigned char *redundant; 366 size_t *filled_index; 367 size_t i, j, filled; 368 369 CALLOC_ARRAY(work, cnt); 370 redundant = xcalloc(cnt, 1); 371 ALLOC_ARRAY(filled_index, cnt - 1); 372 373 for (i = 0; i < cnt; i++) 374 repo_parse_commit(r, array[i]); 375 for (i = 0; i < cnt; i++) { 376 struct commit_list *common = NULL; 377 timestamp_t min_generation = commit_graph_generation(array[i]); 378 379 if (redundant[i]) 380 continue; 381 for (j = filled = 0; j < cnt; j++) { 382 timestamp_t curr_generation; 383 if (i == j || redundant[j]) 384 continue; 385 filled_index[filled] = j; 386 work[filled++] = array[j]; 387 388 curr_generation = commit_graph_generation(array[j]); 389 if (curr_generation < min_generation) 390 min_generation = curr_generation; 391 } 392 if (paint_down_to_common(r, array[i], filled, 393 work, min_generation, 394 MERGE_BASE_FIND_ALL, &common)) { 395 clear_commit_marks(array[i], all_flags); 396 clear_commit_marks_many(filled, work, all_flags); 397 commit_list_free(common); 398 free(work); 399 free(redundant); 400 free(filled_index); 401 return -1; 402 } 403 if (array[i]->object.flags & PARENT2) 404 redundant[i] = 1; 405 for (j = 0; j < filled; j++) 406 if (work[j]->object.flags & PARENT1) 407 redundant[filled_index[j]] = 1; 408 clear_commit_marks(array[i], all_flags); 409 clear_commit_marks_many(filled, work, all_flags); 410 commit_list_free(common); 411 } 412 413 /* Now collect the result */ 414 COPY_ARRAY(work, array, cnt); 415 for (i = filled = 0; i < cnt; i++) 416 if (!redundant[i]) 417 array[filled++] = work[i]; 418 *dedup_cnt = filled; 419 free(work); 420 free(redundant); 421 free(filled_index); 422 return 0; 423 } 424 425 static int remove_redundant_with_gen(struct repository *r, 426 struct commit **array, size_t cnt, 427 size_t *dedup_cnt) 428 { 429 size_t i, count_non_stale = 0, count_still_independent = cnt; 430 timestamp_t min_generation = GENERATION_NUMBER_INFINITY; 431 struct commit **sorted; 432 struct commit_stack walk_start = COMMIT_STACK_INIT; 433 size_t min_gen_pos = 0; 434 435 /* 436 * Sort the input by generation number, ascending. This allows 437 * us to increase the "min_generation" limit when we discover 438 * the commit with lowest generation is STALE. The index 439 * min_gen_pos points to the current position within 'array' 440 * that is not yet known to be STALE. 441 */ 442 DUP_ARRAY(sorted, array, cnt); 443 QSORT(sorted, cnt, compare_commits_by_gen); 444 min_generation = commit_graph_generation(sorted[0]); 445 446 commit_stack_grow(&walk_start, cnt); 447 448 /* Mark all parents of the input as STALE */ 449 for (i = 0; i < cnt; i++) { 450 struct commit_list *parents; 451 452 repo_parse_commit(r, array[i]); 453 array[i]->object.flags |= RESULT; 454 parents = array[i]->parents; 455 456 while (parents) { 457 repo_parse_commit(r, parents->item); 458 if (!(parents->item->object.flags & STALE)) { 459 parents->item->object.flags |= STALE; 460 commit_stack_push(&walk_start, parents->item); 461 } 462 parents = parents->next; 463 } 464 } 465 466 QSORT(walk_start.items, walk_start.nr, compare_commits_by_gen); 467 468 /* remove STALE bit for now to allow walking through parents */ 469 for (i = 0; i < walk_start.nr; i++) 470 walk_start.items[i]->object.flags &= ~STALE; 471 472 /* 473 * Start walking from the highest generation. Hopefully, it will 474 * find all other items during the first-parent walk, and we can 475 * terminate early. Otherwise, we will do the same amount of work 476 * as before. 477 */ 478 for (i = walk_start.nr; i && count_still_independent > 1; i--) { 479 /* push the STALE bits up to min generation */ 480 struct commit_list *stack = NULL; 481 482 commit_list_insert(walk_start.items[i - 1], &stack); 483 walk_start.items[i - 1]->object.flags |= STALE; 484 485 while (stack) { 486 struct commit_list *parents; 487 struct commit *c = stack->item; 488 489 repo_parse_commit(r, c); 490 491 if (c->object.flags & RESULT) { 492 c->object.flags &= ~RESULT; 493 if (--count_still_independent <= 1) 494 break; 495 if (oideq(&c->object.oid, &sorted[min_gen_pos]->object.oid)) { 496 while (min_gen_pos < cnt - 1 && 497 (sorted[min_gen_pos]->object.flags & STALE)) 498 min_gen_pos++; 499 min_generation = commit_graph_generation(sorted[min_gen_pos]); 500 } 501 } 502 503 if (commit_graph_generation(c) < min_generation) { 504 pop_commit(&stack); 505 continue; 506 } 507 508 parents = c->parents; 509 while (parents) { 510 if (!(parents->item->object.flags & STALE)) { 511 parents->item->object.flags |= STALE; 512 commit_list_insert(parents->item, &stack); 513 break; 514 } 515 parents = parents->next; 516 } 517 518 /* pop if all parents have been visited already */ 519 if (!parents) 520 pop_commit(&stack); 521 } 522 commit_list_free(stack); 523 } 524 free(sorted); 525 526 /* clear result */ 527 for (i = 0; i < cnt; i++) 528 array[i]->object.flags &= ~RESULT; 529 530 /* rearrange array */ 531 for (i = count_non_stale = 0; i < cnt; i++) { 532 if (!(array[i]->object.flags & STALE)) 533 array[count_non_stale++] = array[i]; 534 } 535 536 /* clear marks */ 537 clear_commit_marks_many(walk_start.nr, walk_start.items, STALE); 538 commit_stack_clear(&walk_start); 539 540 *dedup_cnt = count_non_stale; 541 return 0; 542 } 543 544 static int remove_redundant(struct repository *r, struct commit **array, 545 size_t cnt, size_t *dedup_cnt) 546 { 547 /* 548 * Some commit in the array may be an ancestor of 549 * another commit. Move the independent commits to the 550 * beginning of 'array' and return their number. Callers 551 * should not rely upon the contents of 'array' after 552 * that number. 553 */ 554 if (generation_numbers_enabled(r)) { 555 /* 556 * If we have a single commit with finite generation 557 * number, then the _with_gen algorithm is preferred. 558 */ 559 for (size_t i = 0; i < cnt; i++) { 560 if (commit_graph_generation(array[i]) < GENERATION_NUMBER_INFINITY) 561 return remove_redundant_with_gen(r, array, cnt, dedup_cnt); 562 } 563 } 564 565 return remove_redundant_no_gen(r, array, cnt, dedup_cnt); 566 } 567 568 static int get_merge_bases_many_0(struct repository *r, 569 struct commit *one, 570 size_t n, 571 struct commit **twos, 572 int cleanup, 573 enum merge_base_flags mb_flags, 574 struct commit_list **result) 575 { 576 struct commit_list *list, **tail = result; 577 struct commit **rslt; 578 size_t cnt, i; 579 int ret; 580 581 if (merge_bases_many(r, one, n, twos, mb_flags, result) < 0) 582 return -1; 583 for (i = 0; i < n; i++) { 584 if (one == twos[i]) 585 return 0; 586 } 587 if (!*result || !(*result)->next) { 588 if (cleanup) { 589 clear_commit_marks(one, all_flags); 590 clear_commit_marks_many(n, twos, all_flags); 591 } 592 return 0; 593 } 594 595 /* There are more than one */ 596 cnt = commit_list_count(*result); 597 CALLOC_ARRAY(rslt, cnt); 598 for (list = *result, i = 0; list; list = list->next) 599 rslt[i++] = list->item; 600 commit_list_free(*result); 601 *result = NULL; 602 603 clear_commit_marks(one, all_flags); 604 clear_commit_marks_many(n, twos, all_flags); 605 606 ret = remove_redundant(r, rslt, cnt, &cnt); 607 if (ret < 0) { 608 free(rslt); 609 return -1; 610 } 611 for (i = 0; i < cnt; i++) 612 tail = commit_list_append(rslt[i], tail); 613 commit_list_sort_by_date(result); 614 free(rslt); 615 return 0; 616 } 617 618 int repo_get_merge_bases_many(struct repository *r, 619 struct commit *one, 620 size_t n, 621 struct commit **twos, 622 struct commit_list **result) 623 { 624 return get_merge_bases_many_0(r, one, n, twos, 1, 625 MERGE_BASE_FIND_ALL, result); 626 } 627 628 int repo_get_merge_bases_many_dirty(struct repository *r, 629 struct commit *one, 630 size_t n, 631 struct commit **twos, 632 enum merge_base_flags mb_flags, 633 struct commit_list **result) 634 { 635 return get_merge_bases_many_0(r, one, n, twos, 0, mb_flags, result); 636 } 637 638 int repo_get_merge_bases(struct repository *r, 639 struct commit *one, 640 struct commit *two, 641 struct commit_list **result) 642 { 643 return get_merge_bases_many_0(r, one, 1, &two, 1, 644 MERGE_BASE_FIND_ALL, result); 645 } 646 647 /* 648 * Is "commit" a descendant of one of the elements on the "with_commit" list? 649 */ 650 int repo_is_descendant_of(struct repository *r, 651 struct commit *commit, 652 struct commit_list *with_commit) 653 { 654 if (!with_commit) 655 return 1; 656 657 if (generation_numbers_enabled(r)) { 658 struct commit_list *from_list = NULL; 659 int result; 660 commit_list_insert(commit, &from_list); 661 result = can_all_from_reach(r, from_list, with_commit, 0); 662 commit_list_free(from_list); 663 return result; 664 } else { 665 while (with_commit) { 666 struct commit *other; 667 int ret; 668 669 other = with_commit->item; 670 with_commit = with_commit->next; 671 ret = repo_in_merge_bases_many(r, other, 1, &commit, 0); 672 if (ret) 673 return ret; 674 } 675 return 0; 676 } 677 } 678 679 /* 680 * Is "commit" an ancestor of one of the "references"? 681 */ 682 int repo_in_merge_bases_many(struct repository *r, struct commit *commit, 683 int nr_reference, struct commit **reference, 684 int ignore_missing_commits) 685 { 686 struct commit_list *bases = NULL; 687 int ret = 0, i; 688 timestamp_t generation, max_generation = GENERATION_NUMBER_ZERO; 689 enum merge_base_flags mb_flags = MERGE_BASE_FIND_ALL; 690 691 if (ignore_missing_commits) 692 mb_flags |= MERGE_BASE_IGNORE_MISSING_COMMITS; 693 694 if (repo_parse_commit(r, commit)) 695 return ignore_missing_commits ? 0 : -1; 696 for (i = 0; i < nr_reference; i++) { 697 if (repo_parse_commit(r, reference[i])) 698 return ignore_missing_commits ? 0 : -1; 699 700 generation = commit_graph_generation(reference[i]); 701 if (generation > max_generation) 702 max_generation = generation; 703 } 704 705 generation = commit_graph_generation(commit); 706 if (generation > max_generation) 707 return ret; 708 709 if (paint_down_to_common(r, commit, 710 nr_reference, reference, 711 generation, mb_flags, &bases)) 712 ret = -1; 713 else if (commit->object.flags & PARENT2) 714 ret = 1; 715 clear_commit_marks(commit, all_flags); 716 clear_commit_marks_many(nr_reference, reference, all_flags); 717 commit_list_free(bases); 718 return ret; 719 } 720 721 /* 722 * Is "commit" an ancestor of (i.e. reachable from) the "reference"? 723 */ 724 int repo_in_merge_bases(struct repository *r, 725 struct commit *commit, 726 struct commit *reference) 727 { 728 int res; 729 struct commit_list *list = NULL; 730 struct commit_list **next = &list; 731 732 next = commit_list_append(commit, next); 733 res = repo_is_descendant_of(r, reference, list); 734 commit_list_free(list); 735 736 return res; 737 } 738 739 struct commit_list *reduce_heads(struct commit_list *heads) 740 { 741 struct commit_list *p; 742 struct commit_list *result = NULL, **tail = &result; 743 struct commit **array; 744 size_t num_head, i; 745 int ret; 746 747 if (!heads) 748 return NULL; 749 750 /* Uniquify */ 751 for (p = heads; p; p = p->next) 752 p->item->object.flags &= ~STALE; 753 for (p = heads, num_head = 0; p; p = p->next) { 754 if (p->item->object.flags & STALE) 755 continue; 756 p->item->object.flags |= STALE; 757 num_head++; 758 } 759 CALLOC_ARRAY(array, num_head); 760 for (p = heads, i = 0; p; p = p->next) { 761 if (p->item->object.flags & STALE) { 762 array[i++] = p->item; 763 p->item->object.flags &= ~STALE; 764 } 765 } 766 767 ret = remove_redundant(the_repository, array, num_head, &num_head); 768 if (ret < 0) { 769 free(array); 770 return NULL; 771 } 772 773 for (i = 0; i < num_head; i++) 774 tail = &commit_list_insert(array[i], tail)->next; 775 free(array); 776 return result; 777 } 778 779 void reduce_heads_replace(struct commit_list **heads) 780 { 781 struct commit_list *result = reduce_heads(*heads); 782 commit_list_free(*heads); 783 *heads = result; 784 } 785 786 int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid) 787 { 788 struct object *o; 789 struct commit *old_commit, *new_commit; 790 struct commit_list *old_commit_list = NULL; 791 int ret; 792 793 /* 794 * Both new_commit and old_commit must be commit-ish and new_commit is descendant of 795 * old_commit. Otherwise we require --force. 796 */ 797 o = deref_tag(the_repository, parse_object(the_repository, old_oid), 798 NULL, 0); 799 if (!o || o->type != OBJ_COMMIT) 800 return 0; 801 old_commit = (struct commit *) o; 802 803 o = deref_tag(the_repository, parse_object(the_repository, new_oid), 804 NULL, 0); 805 if (!o || o->type != OBJ_COMMIT) 806 return 0; 807 new_commit = (struct commit *) o; 808 809 if (repo_parse_commit(the_repository, new_commit) < 0) 810 return 0; 811 812 commit_list_insert(old_commit, &old_commit_list); 813 ret = repo_is_descendant_of(the_repository, 814 new_commit, old_commit_list); 815 if (ret < 0) 816 exit(128); 817 commit_list_free(old_commit_list); 818 return ret; 819 } 820 821 /* 822 * Mimicking the real stack, this stack lives on the heap, avoiding stack 823 * overflows. 824 * 825 * At each recursion step, the stack items points to the commits whose 826 * ancestors are to be inspected. 827 */ 828 struct contains_stack { 829 int nr, alloc; 830 struct contains_stack_entry { 831 struct commit *commit; 832 struct commit_list *parents; 833 } *contains_stack; 834 }; 835 836 static int in_commit_list(const struct commit_list *want, struct commit *c) 837 { 838 for (; want; want = want->next) 839 if (oideq(&want->item->object.oid, &c->object.oid)) 840 return 1; 841 return 0; 842 } 843 844 /* 845 * Test whether the candidate is contained in the list. 846 * Do not recurse to find out, though, but return CONTAINS_UNKNOWN if 847 * inconclusive. 848 */ 849 static enum contains_result contains_test(struct commit *candidate, 850 const struct commit_list *want, 851 struct contains_cache *cache, 852 timestamp_t cutoff) 853 { 854 enum contains_result *cached = contains_cache_at(cache, candidate); 855 856 /* If we already have the answer cached, return that. */ 857 if (*cached) 858 return *cached; 859 860 /* or are we it? */ 861 if (in_commit_list(want, candidate)) { 862 *cached = CONTAINS_YES; 863 return CONTAINS_YES; 864 } 865 866 /* Otherwise, we don't know; prepare to recurse */ 867 parse_commit_or_die(candidate); 868 869 if (commit_graph_generation(candidate) < cutoff) 870 return CONTAINS_NO; 871 872 return CONTAINS_UNKNOWN; 873 } 874 875 static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack) 876 { 877 ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc); 878 contains_stack->contains_stack[contains_stack->nr].commit = candidate; 879 contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents; 880 } 881 882 static enum contains_result contains_tag_algo(struct commit *candidate, 883 const struct commit_list *want, 884 struct contains_cache *cache) 885 { 886 struct contains_stack contains_stack = { 0, 0, NULL }; 887 enum contains_result result; 888 timestamp_t cutoff = GENERATION_NUMBER_INFINITY; 889 const struct commit_list *p; 890 891 for (p = want; p; p = p->next) { 892 timestamp_t generation; 893 struct commit *c = p->item; 894 load_commit_graph_info(the_repository, c); 895 generation = commit_graph_generation(c); 896 if (generation < cutoff) 897 cutoff = generation; 898 } 899 900 result = contains_test(candidate, want, cache, cutoff); 901 if (result != CONTAINS_UNKNOWN) 902 return result; 903 904 *contains_cache_at(cache, candidate) = CONTAINS_IN_PROGRESS; 905 push_to_contains_stack(candidate, &contains_stack); 906 while (contains_stack.nr) { 907 struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1]; 908 struct commit *commit = entry->commit; 909 struct commit_list *parents = entry->parents; 910 911 if (!parents) { 912 *contains_cache_at(cache, commit) = CONTAINS_NO; 913 contains_stack.nr--; 914 } 915 /* 916 * A parent may have just been popped and marked, or may still 917 * be active when replacement refs create a cycle. 918 */ 919 else switch (contains_test(parents->item, want, cache, cutoff)) { 920 case CONTAINS_YES: 921 *contains_cache_at(cache, commit) = CONTAINS_YES; 922 contains_stack.nr--; 923 break; 924 case CONTAINS_NO: 925 entry->parents = parents->next; 926 break; 927 case CONTAINS_IN_PROGRESS: 928 die(_("commit ancestry contains a cycle")); 929 case CONTAINS_UNKNOWN: 930 *contains_cache_at(cache, parents->item) = 931 CONTAINS_IN_PROGRESS; 932 push_to_contains_stack(parents->item, &contains_stack); 933 break; 934 } 935 } 936 free(contains_stack.contains_stack); 937 return contains_test(candidate, want, cache, cutoff); 938 } 939 940 int commit_contains(struct ref_filter *filter, struct commit *commit, 941 struct commit_list *list, struct contains_cache *cache) 942 { 943 int result; 944 945 if (filter->with_commit_tag_algo || 946 generation_numbers_enabled(the_repository)) 947 return contains_tag_algo(commit, list, cache) == CONTAINS_YES; 948 949 result = repo_is_descendant_of(the_repository, commit, list); 950 if (result < 0) 951 die(_("failed to check reachability")); 952 return result; 953 } 954 955 int can_all_from_reach_with_flag(struct repository *r, 956 struct object_array *from, 957 unsigned int with_flag, 958 unsigned int assign_flag, 959 timestamp_t min_commit_date, 960 timestamp_t min_generation) 961 { 962 struct commit **list = NULL; 963 size_t i; 964 size_t nr_commits; 965 int result = 1; 966 967 ALLOC_ARRAY(list, from->nr); 968 nr_commits = 0; 969 for (i = 0; i < from->nr; i++) { 970 struct object *from_one = from->objects[i].item; 971 972 if (!from_one || from_one->flags & assign_flag) 973 continue; 974 975 from_one = deref_tag(r, from_one, "a from object", 0); 976 if (!from_one || from_one->type != OBJ_COMMIT) { 977 /* 978 * no way to tell if this is reachable by 979 * looking at the ancestry chain alone, so 980 * leave a note to ourselves not to worry about 981 * this object anymore. 982 */ 983 from->objects[i].item->flags |= assign_flag; 984 continue; 985 } 986 987 list[nr_commits] = (struct commit *)from_one; 988 if (repo_parse_commit(r, list[nr_commits]) || 989 commit_graph_generation(list[nr_commits]) < min_generation) { 990 result = 0; 991 goto cleanup; 992 } 993 994 nr_commits++; 995 } 996 997 QSORT(list, nr_commits, compare_commits_by_gen); 998 999 for (i = 0; i < nr_commits; i++) { 1000 /* DFS from list[i] */ 1001 struct commit_list *stack = NULL; 1002 1003 list[i]->object.flags |= assign_flag; 1004 commit_list_insert(list[i], &stack); 1005 1006 while (stack) { 1007 struct commit_list *parent; 1008 1009 if (stack->item->object.flags & (with_flag | RESULT)) { 1010 pop_commit(&stack); 1011 if (stack) 1012 stack->item->object.flags |= RESULT; 1013 continue; 1014 } 1015 1016 for (parent = stack->item->parents; parent; parent = parent->next) { 1017 if (parent->item->object.flags & (with_flag | RESULT)) 1018 stack->item->object.flags |= RESULT; 1019 1020 if (!(parent->item->object.flags & assign_flag)) { 1021 parent->item->object.flags |= assign_flag; 1022 1023 if (repo_parse_commit(r, parent->item) || 1024 parent->item->date < min_commit_date || 1025 commit_graph_generation(parent->item) < min_generation) 1026 continue; 1027 1028 commit_list_insert(parent->item, &stack); 1029 break; 1030 } 1031 } 1032 1033 if (!parent) 1034 pop_commit(&stack); 1035 } 1036 1037 if (!(list[i]->object.flags & (with_flag | RESULT))) { 1038 result = 0; 1039 goto cleanup; 1040 } 1041 } 1042 1043 cleanup: 1044 clear_commit_marks_many(nr_commits, list, RESULT | assign_flag); 1045 free(list); 1046 1047 for (i = 0; i < from->nr; i++) { 1048 struct object *from_one = from->objects[i].item; 1049 1050 if (from_one) 1051 from_one->flags &= ~assign_flag; 1052 } 1053 1054 return result; 1055 } 1056 1057 int can_all_from_reach(struct repository *r, struct commit_list *from, 1058 struct commit_list *to, int cutoff_by_min_date) 1059 { 1060 struct object_array from_objs = OBJECT_ARRAY_INIT; 1061 struct commit_list *from_iter = from, *to_iter = to; 1062 int result; 1063 timestamp_t min_commit_date = cutoff_by_min_date ? from->item->date : 0; 1064 timestamp_t min_generation = GENERATION_NUMBER_INFINITY; 1065 1066 while (from_iter) { 1067 add_object_array(&from_iter->item->object, NULL, &from_objs); 1068 1069 if (!repo_parse_commit(r, from_iter->item)) { 1070 timestamp_t generation; 1071 if (from_iter->item->date < min_commit_date) 1072 min_commit_date = from_iter->item->date; 1073 1074 generation = commit_graph_generation(from_iter->item); 1075 if (generation < min_generation) 1076 min_generation = generation; 1077 } 1078 1079 from_iter = from_iter->next; 1080 } 1081 1082 while (to_iter) { 1083 if (!repo_parse_commit(r, to_iter->item)) { 1084 timestamp_t generation; 1085 if (to_iter->item->date < min_commit_date) 1086 min_commit_date = to_iter->item->date; 1087 1088 generation = commit_graph_generation(to_iter->item); 1089 if (generation < min_generation) 1090 min_generation = generation; 1091 } 1092 1093 to_iter->item->object.flags |= PARENT2; 1094 1095 to_iter = to_iter->next; 1096 } 1097 1098 result = can_all_from_reach_with_flag(r, &from_objs, PARENT2, PARENT1, 1099 min_commit_date, min_generation); 1100 1101 while (from) { 1102 clear_commit_marks(from->item, PARENT1); 1103 from = from->next; 1104 } 1105 1106 while (to) { 1107 clear_commit_marks(to->item, PARENT2); 1108 to = to->next; 1109 } 1110 1111 object_array_clear(&from_objs); 1112 return result; 1113 } 1114 1115 struct commit_list *get_reachable_subset(struct commit **from, size_t nr_from, 1116 struct commit **to, size_t nr_to, 1117 unsigned int reachable_flag) 1118 { 1119 struct commit **item; 1120 struct commit *current; 1121 struct commit_list *found_commits = NULL; 1122 struct commit **to_last = to + nr_to; 1123 struct commit **from_last = from + nr_from; 1124 timestamp_t min_generation = GENERATION_NUMBER_INFINITY; 1125 int num_to_find = 0; 1126 1127 struct prio_queue queue = { compare_commits_by_gen_then_commit_date }; 1128 1129 for (item = to; item < to_last; item++) { 1130 timestamp_t generation; 1131 struct commit *c = *item; 1132 1133 repo_parse_commit(the_repository, c); 1134 generation = commit_graph_generation(c); 1135 if (generation < min_generation) 1136 min_generation = generation; 1137 1138 if (!(c->object.flags & PARENT1)) { 1139 c->object.flags |= PARENT1; 1140 num_to_find++; 1141 } 1142 } 1143 1144 for (item = from; item < from_last; item++) { 1145 struct commit *c = *item; 1146 if (!(c->object.flags & PARENT2)) { 1147 c->object.flags |= PARENT2; 1148 repo_parse_commit(the_repository, c); 1149 1150 prio_queue_put(&queue, *item); 1151 } 1152 } 1153 1154 while (num_to_find && (current = prio_queue_get(&queue)) != NULL) { 1155 struct commit_list *parents; 1156 1157 if (current->object.flags & PARENT1) { 1158 current->object.flags &= ~PARENT1; 1159 current->object.flags |= reachable_flag; 1160 commit_list_insert(current, &found_commits); 1161 num_to_find--; 1162 } 1163 1164 for (parents = current->parents; parents; parents = parents->next) { 1165 struct commit *p = parents->item; 1166 1167 repo_parse_commit(the_repository, p); 1168 1169 if (commit_graph_generation(p) < min_generation) 1170 continue; 1171 1172 if (p->object.flags & PARENT2) 1173 continue; 1174 1175 p->object.flags |= PARENT2; 1176 prio_queue_put(&queue, p); 1177 } 1178 } 1179 1180 clear_prio_queue(&queue); 1181 1182 clear_commit_marks_many(nr_to, to, PARENT1); 1183 clear_commit_marks_many(nr_from, from, PARENT2); 1184 1185 return found_commits; 1186 } 1187 1188 define_commit_slab(bit_arrays, struct bitmap *); 1189 static struct bit_arrays bit_arrays; 1190 1191 static void insert_no_dup(struct nonstale_queue *queue, struct commit *c) 1192 { 1193 if (c->object.flags & PARENT2) 1194 return; 1195 nonstale_queue_put(queue, c); 1196 c->object.flags |= PARENT2; 1197 } 1198 1199 static struct bitmap *get_bit_array(struct commit *c, int width) 1200 { 1201 struct bitmap **bitmap = bit_arrays_at(&bit_arrays, c); 1202 if (!*bitmap) 1203 *bitmap = bitmap_word_alloc(width); 1204 return *bitmap; 1205 } 1206 1207 static void free_bit_array(struct commit *c) 1208 { 1209 struct bitmap **bitmap = bit_arrays_at(&bit_arrays, c); 1210 if (!*bitmap) 1211 return; 1212 bitmap_free(*bitmap); 1213 *bitmap = NULL; 1214 } 1215 1216 void ahead_behind(struct repository *r, 1217 struct commit **commits, size_t commits_nr, 1218 struct ahead_behind_count *counts, size_t counts_nr) 1219 { 1220 struct nonstale_queue queue = { 1221 { .compare = compare_commits_by_gen_then_commit_date } 1222 }; 1223 void *entry; 1224 size_t width = DIV_ROUND_UP(commits_nr, BITS_IN_EWORD); 1225 1226 if (!commits_nr || !counts_nr) 1227 return; 1228 1229 for (size_t i = 0; i < counts_nr; i++) { 1230 counts[i].ahead = 0; 1231 counts[i].behind = 0; 1232 } 1233 1234 ensure_generations_valid(r, commits, commits_nr); 1235 1236 init_bit_arrays(&bit_arrays); 1237 1238 for (size_t i = 0; i < commits_nr; i++) { 1239 struct commit *c = commits[i]; 1240 struct bitmap *bitmap = get_bit_array(c, width); 1241 1242 bitmap_set(bitmap, i); 1243 insert_no_dup(&queue, c); 1244 } 1245 1246 while (queue.max_nonstale) { 1247 struct commit *c = nonstale_queue_get(&queue); 1248 struct commit_list *p; 1249 struct bitmap *bitmap_c = get_bit_array(c, width); 1250 1251 for (size_t i = 0; i < counts_nr; i++) { 1252 int reach_from_tip = !!bitmap_get(bitmap_c, counts[i].tip_index); 1253 int reach_from_base = !!bitmap_get(bitmap_c, counts[i].base_index); 1254 1255 if (reach_from_tip ^ reach_from_base) { 1256 if (reach_from_base) 1257 counts[i].behind++; 1258 else 1259 counts[i].ahead++; 1260 } 1261 } 1262 1263 for (p = c->parents; p; p = p->next) { 1264 struct bitmap *bitmap_p; 1265 1266 repo_parse_commit(r, p->item); 1267 1268 bitmap_p = get_bit_array(p->item, width); 1269 bitmap_or(bitmap_p, bitmap_c); 1270 1271 /* 1272 * If this parent is reachable from every starting 1273 * commit, then none of its ancestors can contribute 1274 * to the ahead/behind count. Mark it as STALE, so 1275 * we can stop the walk when every commit in the 1276 * queue is STALE. 1277 */ 1278 if (bitmap_popcount(bitmap_p) == commits_nr) 1279 p->item->object.flags |= STALE; 1280 1281 insert_no_dup(&queue, p->item); 1282 } 1283 1284 free_bit_array(c); 1285 } 1286 1287 /* STALE is used here, PARENT2 is used by insert_no_dup(). */ 1288 repo_clear_commit_marks(r, PARENT2 | STALE); 1289 prio_queue_for_each(&queue.pq, entry) 1290 free_bit_array(entry); 1291 clear_bit_arrays(&bit_arrays); 1292 clear_nonstale_queue(&queue); 1293 } 1294 1295 struct commit_and_index { 1296 struct commit *commit; 1297 timestamp_t generation; 1298 }; 1299 1300 static int compare_commit_and_index_by_generation(const void *va, const void *vb) 1301 { 1302 const struct commit_and_index *a = (const struct commit_and_index *)va; 1303 const struct commit_and_index *b = (const struct commit_and_index *)vb; 1304 1305 if (a->generation > b->generation) 1306 return 1; 1307 if (a->generation < b->generation) 1308 return -1; 1309 return 0; 1310 } 1311 1312 void tips_reachable_from_bases(struct repository *r, 1313 struct commit_list *bases, 1314 struct commit **tips, size_t tips_nr, 1315 int mark) 1316 { 1317 struct commit_and_index *commits; 1318 size_t min_generation_index = 0; 1319 timestamp_t min_generation; 1320 struct commit_list *stack = NULL; 1321 1322 if (!bases || !tips || !tips_nr) 1323 return; 1324 1325 /* 1326 * Do a depth-first search starting at 'bases' to search for the 1327 * tips. Stop at the lowest (un-found) generation number. When 1328 * finding the lowest commit, increase the minimum generation 1329 * number to the next lowest (un-found) generation number. 1330 */ 1331 1332 CALLOC_ARRAY(commits, tips_nr); 1333 1334 for (size_t i = 0; i < tips_nr; i++) { 1335 commits[i].commit = tips[i]; 1336 commits[i].generation = commit_graph_generation(tips[i]); 1337 } 1338 1339 /* Sort with generation number ascending. */ 1340 QSORT(commits, tips_nr, compare_commit_and_index_by_generation); 1341 min_generation = commits[0].generation; 1342 1343 for (size_t i = 0; i < tips_nr; i++) 1344 commits[i].commit->object.flags |= RESULT; 1345 1346 while (bases) { 1347 repo_parse_commit(r, bases->item); 1348 commit_list_insert(bases->item, &stack); 1349 bases = bases->next; 1350 } 1351 1352 while (stack) { 1353 int explored_all_parents = 1; 1354 struct commit_list *p; 1355 struct commit *c = stack->item; 1356 1357 /* Does it match any of our tips? */ 1358 { 1359 if (c->object.flags & RESULT) { 1360 c->object.flags |= mark; 1361 1362 if (commits[min_generation_index].commit->object.flags & mark) { 1363 unsigned int k = min_generation_index + 1; 1364 while (k < tips_nr && 1365 (commits[k].commit->object.flags & mark)) 1366 k++; 1367 1368 /* Terminate early if all found. */ 1369 if (k >= tips_nr) 1370 goto done; 1371 1372 min_generation_index = k; 1373 min_generation = commits[k].generation; 1374 } 1375 } 1376 } 1377 1378 for (p = c->parents; p; p = p->next) { 1379 repo_parse_commit(r, p->item); 1380 1381 /* Have we already explored this parent? */ 1382 if (p->item->object.flags & SEEN) 1383 continue; 1384 1385 /* Is it below the current minimum generation? */ 1386 if (commit_graph_generation(p->item) < min_generation) 1387 continue; 1388 1389 /* Ok, we will explore from here on. */ 1390 p->item->object.flags |= SEEN; 1391 explored_all_parents = 0; 1392 commit_list_insert(p->item, &stack); 1393 break; 1394 } 1395 1396 if (explored_all_parents) 1397 pop_commit(&stack); 1398 } 1399 1400 done: 1401 for (size_t i = 0; i < tips_nr; i++) 1402 commits[i].commit->object.flags &= ~RESULT; 1403 free(commits); 1404 repo_clear_commit_marks(r, SEEN); 1405 commit_list_free(stack); 1406 } 1407 1408 /* 1409 * This slab initializes integers to zero, so use "-1" for "tip is best" and 1410 * "i + 1" for "bases[i] is best". 1411 */ 1412 define_commit_slab(best_branch_base, int); 1413 static struct best_branch_base best_branch_base; 1414 #define get_best(c) (*best_branch_base_at(&best_branch_base, (c))) 1415 #define set_best(c,v) (*best_branch_base_at(&best_branch_base, (c)) = (v)) 1416 1417 int get_branch_base_for_tip(struct repository *r, 1418 struct commit *tip, 1419 struct commit **bases, 1420 size_t bases_nr) 1421 { 1422 int best_index = -1; 1423 struct commit *c, *branch_point = NULL; 1424 struct prio_queue queue = { compare_commits_by_gen_then_commit_date }; 1425 int found_missing_gen = 0; 1426 1427 if (!bases_nr) 1428 return -1; 1429 1430 repo_parse_commit(r, tip); 1431 if (commit_graph_generation(tip) == GENERATION_NUMBER_INFINITY) 1432 found_missing_gen = 1; 1433 1434 /* Check for missing generation numbers. */ 1435 for (size_t i = 0; i < bases_nr; i++) { 1436 struct commit *c = bases[i]; 1437 repo_parse_commit(r, c); 1438 if (commit_graph_generation(c) == GENERATION_NUMBER_INFINITY) 1439 found_missing_gen = 1; 1440 } 1441 1442 if (found_missing_gen) { 1443 struct commit **commits; 1444 size_t commits_nr = bases_nr + 1; 1445 1446 CALLOC_ARRAY(commits, commits_nr); 1447 COPY_ARRAY(commits, bases, bases_nr); 1448 commits[bases_nr] = tip; 1449 ensure_generations_valid(r, commits, commits_nr); 1450 free(commits); 1451 } 1452 1453 /* Initialize queue and slab now that generations are guaranteed. */ 1454 init_best_branch_base(&best_branch_base); 1455 set_best(tip, -1); 1456 prio_queue_put(&queue, tip); 1457 1458 for (size_t i = 0; i < bases_nr; i++) { 1459 struct commit *c = bases[i]; 1460 int best = get_best(c); 1461 1462 /* Has this already been marked as best by another commit? */ 1463 if (best) { 1464 if (best == -1) { 1465 /* We agree at this position. Stop now. */ 1466 best_index = i + 1; 1467 goto cleanup; 1468 } 1469 continue; 1470 } 1471 1472 set_best(c, i + 1); 1473 prio_queue_put(&queue, c); 1474 } 1475 1476 while ((c = prio_queue_get(&queue))) { 1477 int best_for_c = get_best(c); 1478 int best_for_p, positive; 1479 struct commit *parent; 1480 1481 /* Have we reached a known branch point? It's optimal. */ 1482 if (c == branch_point) 1483 break; 1484 1485 repo_parse_commit(r, c); 1486 if (!c->parents) 1487 continue; 1488 1489 parent = c->parents->item; 1490 repo_parse_commit(r, parent); 1491 best_for_p = get_best(parent); 1492 1493 if (!best_for_p) { 1494 /* 'parent' is new, so pass along best_for_c. */ 1495 set_best(parent, best_for_c); 1496 prio_queue_put(&queue, parent); 1497 continue; 1498 } 1499 1500 if (best_for_p > 0 && best_for_c > 0) { 1501 /* Collision among bases. Minimize. */ 1502 if (best_for_c < best_for_p) 1503 set_best(parent, best_for_c); 1504 continue; 1505 } 1506 1507 /* 1508 * At this point, we have reached a commit that is reachable 1509 * from the tip, either from 'c' or from an earlier commit to 1510 * have 'parent' as its first parent. 1511 * 1512 * Update 'best_index' to match the minimum of all base indices 1513 * to reach 'parent'. 1514 */ 1515 1516 /* Exactly one is positive due to initial conditions. */ 1517 positive = (best_for_c < 0) ? best_for_p : best_for_c; 1518 1519 if (best_index < 0 || positive < best_index) 1520 best_index = positive; 1521 1522 /* No matter what, track that the parent is reachable from tip. */ 1523 set_best(parent, -1); 1524 branch_point = parent; 1525 } 1526 1527 cleanup: 1528 clear_best_branch_base(&best_branch_base); 1529 clear_prio_queue(&queue); 1530 return best_index > 0 ? best_index - 1 : -1; 1531 }