Coverage Report

Created: 2024-09-08 06:23

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