Coverage Report

Created: 2025-12-31 07:01

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