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