]>
Commit | Line | Data |
---|---|---|
5227c385 | 1 | #include "cache.h" |
5227c385 | 2 | #include "commit.h" |
920f93ca | 3 | #include "commit-graph.h" |
1d614d41 DS |
4 | #include "decorate.h" |
5 | #include "prio-queue.h" | |
6 | #include "tree.h" | |
6621c838 | 7 | #include "ref-filter.h" |
1d614d41 DS |
8 | #include "revision.h" |
9 | #include "tag.h" | |
5227c385 DS |
10 | #include "commit-reach.h" |
11 | ||
12 | /* Remember to update object flag allocation in object.h */ | |
13 | #define PARENT1 (1u<<16) | |
14 | #define PARENT2 (1u<<17) | |
15 | #define STALE (1u<<18) | |
16 | #define RESULT (1u<<19) | |
17 | ||
18 | static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT); | |
19 | ||
c8d693e1 DS |
20 | static int compare_commits_by_gen(const void *_a, const void *_b) |
21 | { | |
22 | const struct commit *a = *(const struct commit * const *)_a; | |
23 | const struct commit *b = *(const struct commit * const *)_b; | |
24 | ||
25 | timestamp_t generation_a = commit_graph_generation(a); | |
26 | timestamp_t generation_b = commit_graph_generation(b); | |
27 | ||
28 | if (generation_a < generation_b) | |
29 | return -1; | |
30 | if (generation_a > generation_b) | |
31 | return 1; | |
32 | return 0; | |
33 | } | |
34 | ||
5227c385 DS |
35 | static int queue_has_nonstale(struct prio_queue *queue) |
36 | { | |
37 | int i; | |
38 | for (i = 0; i < queue->nr; i++) { | |
39 | struct commit *commit = queue->array[i].data; | |
40 | if (!(commit->object.flags & STALE)) | |
41 | return 1; | |
42 | } | |
43 | return 0; | |
44 | } | |
45 | ||
46 | /* all input commits in one and twos[] must have been parsed! */ | |
c383830a SB |
47 | static struct commit_list *paint_down_to_common(struct repository *r, |
48 | struct commit *one, int n, | |
5227c385 | 49 | struct commit **twos, |
d7f92784 | 50 | timestamp_t min_generation) |
5227c385 DS |
51 | { |
52 | struct prio_queue queue = { compare_commits_by_gen_then_commit_date }; | |
53 | struct commit_list *result = NULL; | |
54 | int i; | |
d7f92784 | 55 | timestamp_t last_gen = GENERATION_NUMBER_INFINITY; |
5227c385 | 56 | |
8d00d7c3 | 57 | if (!min_generation && !corrected_commit_dates_enabled(r)) |
1b7a91da JH |
58 | queue.compare = compare_commits_by_commit_date; |
59 | ||
5227c385 DS |
60 | one->object.flags |= PARENT1; |
61 | if (!n) { | |
62 | commit_list_append(one, &result); | |
63 | return result; | |
64 | } | |
65 | prio_queue_put(&queue, one); | |
66 | ||
67 | for (i = 0; i < n; i++) { | |
68 | twos[i]->object.flags |= PARENT2; | |
69 | prio_queue_put(&queue, twos[i]); | |
70 | } | |
71 | ||
72 | while (queue_has_nonstale(&queue)) { | |
73 | struct commit *commit = prio_queue_get(&queue); | |
74 | struct commit_list *parents; | |
75 | int flags; | |
d7f92784 | 76 | timestamp_t generation = commit_graph_generation(commit); |
5227c385 | 77 | |
c752ad09 | 78 | if (min_generation && generation > last_gen) |
d7f92784 | 79 | BUG("bad generation skip %"PRItime" > %"PRItime" at %s", |
c752ad09 | 80 | generation, last_gen, |
5227c385 | 81 | oid_to_hex(&commit->object.oid)); |
c752ad09 | 82 | last_gen = generation; |
5227c385 | 83 | |
c752ad09 | 84 | if (generation < min_generation) |
5227c385 DS |
85 | break; |
86 | ||
87 | flags = commit->object.flags & (PARENT1 | PARENT2 | STALE); | |
88 | if (flags == (PARENT1 | PARENT2)) { | |
89 | if (!(commit->object.flags & RESULT)) { | |
90 | commit->object.flags |= RESULT; | |
91 | commit_list_insert_by_date(commit, &result); | |
92 | } | |
93 | /* Mark parents of a found merge stale */ | |
94 | flags |= STALE; | |
95 | } | |
96 | parents = commit->parents; | |
97 | while (parents) { | |
98 | struct commit *p = parents->item; | |
99 | parents = parents->next; | |
100 | if ((p->object.flags & flags) == flags) | |
101 | continue; | |
c383830a | 102 | if (repo_parse_commit(r, p)) |
5227c385 DS |
103 | return NULL; |
104 | p->object.flags |= flags; | |
105 | prio_queue_put(&queue, p); | |
106 | } | |
107 | } | |
108 | ||
109 | clear_prio_queue(&queue); | |
110 | return result; | |
111 | } | |
112 | ||
18256a91 SB |
113 | static struct commit_list *merge_bases_many(struct repository *r, |
114 | struct commit *one, int n, | |
115 | struct commit **twos) | |
5227c385 DS |
116 | { |
117 | struct commit_list *list = NULL; | |
118 | struct commit_list *result = NULL; | |
119 | int i; | |
120 | ||
121 | for (i = 0; i < n; i++) { | |
122 | if (one == twos[i]) | |
123 | /* | |
124 | * We do not mark this even with RESULT so we do not | |
125 | * have to clean it up. | |
126 | */ | |
127 | return commit_list_insert(one, &result); | |
128 | } | |
129 | ||
18256a91 | 130 | if (repo_parse_commit(r, one)) |
5227c385 DS |
131 | return NULL; |
132 | for (i = 0; i < n; i++) { | |
18256a91 | 133 | if (repo_parse_commit(r, twos[i])) |
5227c385 DS |
134 | return NULL; |
135 | } | |
136 | ||
18256a91 | 137 | list = paint_down_to_common(r, one, n, twos, 0); |
5227c385 DS |
138 | |
139 | while (list) { | |
140 | struct commit *commit = pop_commit(&list); | |
141 | if (!(commit->object.flags & STALE)) | |
142 | commit_list_insert_by_date(commit, &result); | |
143 | } | |
144 | return result; | |
145 | } | |
146 | ||
147 | struct commit_list *get_octopus_merge_bases(struct commit_list *in) | |
148 | { | |
149 | struct commit_list *i, *j, *k, *ret = NULL; | |
150 | ||
151 | if (!in) | |
152 | return ret; | |
153 | ||
154 | commit_list_insert(in->item, &ret); | |
155 | ||
156 | for (i = in->next; i; i = i->next) { | |
157 | struct commit_list *new_commits = NULL, *end = NULL; | |
158 | ||
159 | for (j = ret; j; j = j->next) { | |
160 | struct commit_list *bases; | |
161 | bases = get_merge_bases(i->item, j->item); | |
162 | if (!new_commits) | |
163 | new_commits = bases; | |
164 | else | |
165 | end->next = bases; | |
166 | for (k = bases; k; k = k->next) | |
167 | end = k; | |
168 | } | |
169 | ret = new_commits; | |
170 | } | |
171 | return ret; | |
172 | } | |
173 | ||
fbc21e3f DS |
174 | static int remove_redundant_no_gen(struct repository *r, |
175 | struct commit **array, int cnt) | |
5227c385 | 176 | { |
5227c385 DS |
177 | struct commit **work; |
178 | unsigned char *redundant; | |
179 | int *filled_index; | |
180 | int i, j, filled; | |
181 | ||
182 | work = xcalloc(cnt, sizeof(*work)); | |
183 | redundant = xcalloc(cnt, 1); | |
184 | ALLOC_ARRAY(filled_index, cnt - 1); | |
185 | ||
186 | for (i = 0; i < cnt; i++) | |
ed8a0e3a | 187 | repo_parse_commit(r, array[i]); |
5227c385 DS |
188 | for (i = 0; i < cnt; i++) { |
189 | struct commit_list *common; | |
d7f92784 | 190 | timestamp_t min_generation = commit_graph_generation(array[i]); |
5227c385 DS |
191 | |
192 | if (redundant[i]) | |
193 | continue; | |
194 | for (j = filled = 0; j < cnt; j++) { | |
d7f92784 | 195 | timestamp_t curr_generation; |
5227c385 DS |
196 | if (i == j || redundant[j]) |
197 | continue; | |
198 | filled_index[filled] = j; | |
199 | work[filled++] = array[j]; | |
200 | ||
c752ad09 AK |
201 | curr_generation = commit_graph_generation(array[j]); |
202 | if (curr_generation < min_generation) | |
203 | min_generation = curr_generation; | |
5227c385 | 204 | } |
ed8a0e3a | 205 | common = paint_down_to_common(r, array[i], filled, |
c383830a | 206 | work, min_generation); |
5227c385 DS |
207 | if (array[i]->object.flags & PARENT2) |
208 | redundant[i] = 1; | |
209 | for (j = 0; j < filled; j++) | |
210 | if (work[j]->object.flags & PARENT1) | |
211 | redundant[filled_index[j]] = 1; | |
212 | clear_commit_marks(array[i], all_flags); | |
213 | clear_commit_marks_many(filled, work, all_flags); | |
214 | free_commit_list(common); | |
215 | } | |
216 | ||
217 | /* Now collect the result */ | |
218 | COPY_ARRAY(work, array, cnt); | |
219 | for (i = filled = 0; i < cnt; i++) | |
220 | if (!redundant[i]) | |
221 | array[filled++] = work[i]; | |
5227c385 DS |
222 | free(work); |
223 | free(redundant); | |
224 | free(filled_index); | |
225 | return filled; | |
226 | } | |
227 | ||
fbc21e3f DS |
228 | static int remove_redundant_with_gen(struct repository *r, |
229 | struct commit **array, int cnt) | |
230 | { | |
231 | int i, count_non_stale = 0; | |
232 | timestamp_t min_generation = GENERATION_NUMBER_INFINITY; | |
233 | struct commit **walk_start; | |
234 | size_t walk_start_nr = 0, walk_start_alloc = cnt; | |
235 | struct prio_queue queue = { compare_commits_by_gen_then_commit_date }; | |
236 | ||
237 | ALLOC_ARRAY(walk_start, walk_start_alloc); | |
238 | ||
239 | /* Mark all parents of the input as STALE */ | |
240 | for (i = 0; i < cnt; i++) { | |
241 | struct commit_list *parents; | |
242 | timestamp_t generation; | |
243 | ||
244 | repo_parse_commit(r, array[i]); | |
245 | parents = array[i]->parents; | |
246 | ||
247 | while (parents) { | |
248 | repo_parse_commit(r, parents->item); | |
249 | if (!(parents->item->object.flags & STALE)) { | |
250 | parents->item->object.flags |= STALE; | |
251 | ALLOC_GROW(walk_start, walk_start_nr + 1, walk_start_alloc); | |
252 | walk_start[walk_start_nr++] = parents->item; | |
253 | prio_queue_put(&queue, parents->item); | |
254 | } | |
255 | parents = parents->next; | |
256 | } | |
257 | ||
258 | generation = commit_graph_generation(array[i]); | |
259 | ||
260 | if (generation < min_generation) | |
261 | min_generation = generation; | |
262 | } | |
263 | ||
264 | /* push the STALE bits up to min generation */ | |
265 | while (queue.nr) { | |
266 | struct commit_list *parents; | |
267 | struct commit *c = prio_queue_get(&queue); | |
268 | ||
269 | repo_parse_commit(r, c); | |
270 | ||
271 | if (commit_graph_generation(c) < min_generation) | |
272 | continue; | |
273 | ||
274 | parents = c->parents; | |
275 | while (parents) { | |
276 | if (!(parents->item->object.flags & STALE)) { | |
277 | parents->item->object.flags |= STALE; | |
278 | prio_queue_put(&queue, parents->item); | |
279 | } | |
280 | parents = parents->next; | |
281 | } | |
282 | } | |
283 | ||
284 | /* rearrange array */ | |
285 | for (i = count_non_stale = 0; i < cnt; i++) { | |
286 | if (!(array[i]->object.flags & STALE)) | |
287 | array[count_non_stale++] = array[i]; | |
288 | } | |
289 | ||
290 | /* clear marks */ | |
291 | clear_commit_marks_many(walk_start_nr, walk_start, STALE); | |
292 | free(walk_start); | |
293 | ||
294 | return count_non_stale; | |
295 | } | |
296 | ||
297 | static int remove_redundant(struct repository *r, struct commit **array, int cnt) | |
298 | { | |
299 | /* | |
300 | * Some commit in the array may be an ancestor of | |
301 | * another commit. Move the independent commits to the | |
302 | * beginning of 'array' and return their number. Callers | |
303 | * should not rely upon the contents of 'array' after | |
304 | * that number. | |
305 | */ | |
306 | if (generation_numbers_enabled(r)) { | |
307 | int i; | |
308 | ||
309 | /* | |
310 | * If we have a single commit with finite generation | |
311 | * number, then the _with_gen algorithm is preferred. | |
312 | */ | |
313 | for (i = 0; i < cnt; i++) { | |
314 | if (commit_graph_generation(array[i]) < GENERATION_NUMBER_INFINITY) | |
315 | return remove_redundant_with_gen(r, array, cnt); | |
316 | } | |
317 | } | |
318 | ||
319 | return remove_redundant_no_gen(r, array, cnt); | |
320 | } | |
321 | ||
f28e87f5 SB |
322 | static struct commit_list *get_merge_bases_many_0(struct repository *r, |
323 | struct commit *one, | |
5227c385 DS |
324 | int n, |
325 | struct commit **twos, | |
326 | int cleanup) | |
327 | { | |
328 | struct commit_list *list; | |
329 | struct commit **rslt; | |
330 | struct commit_list *result; | |
331 | int cnt, i; | |
332 | ||
f28e87f5 | 333 | result = merge_bases_many(r, one, n, twos); |
5227c385 DS |
334 | for (i = 0; i < n; i++) { |
335 | if (one == twos[i]) | |
336 | return result; | |
337 | } | |
338 | if (!result || !result->next) { | |
339 | if (cleanup) { | |
340 | clear_commit_marks(one, all_flags); | |
341 | clear_commit_marks_many(n, twos, all_flags); | |
342 | } | |
343 | return result; | |
344 | } | |
345 | ||
346 | /* There are more than one */ | |
347 | cnt = commit_list_count(result); | |
348 | rslt = xcalloc(cnt, sizeof(*rslt)); | |
349 | for (list = result, i = 0; list; list = list->next) | |
350 | rslt[i++] = list->item; | |
351 | free_commit_list(result); | |
352 | ||
353 | clear_commit_marks(one, all_flags); | |
354 | clear_commit_marks_many(n, twos, all_flags); | |
355 | ||
f28e87f5 | 356 | cnt = remove_redundant(r, rslt, cnt); |
5227c385 DS |
357 | result = NULL; |
358 | for (i = 0; i < cnt; i++) | |
359 | commit_list_insert_by_date(rslt[i], &result); | |
360 | free(rslt); | |
361 | return result; | |
362 | } | |
363 | ||
21a9651b SB |
364 | struct commit_list *repo_get_merge_bases_many(struct repository *r, |
365 | struct commit *one, | |
366 | int n, | |
367 | struct commit **twos) | |
5227c385 | 368 | { |
21a9651b | 369 | return get_merge_bases_many_0(r, one, n, twos, 1); |
5227c385 DS |
370 | } |
371 | ||
21a9651b SB |
372 | struct commit_list *repo_get_merge_bases_many_dirty(struct repository *r, |
373 | struct commit *one, | |
374 | int n, | |
375 | struct commit **twos) | |
5227c385 | 376 | { |
21a9651b | 377 | return get_merge_bases_many_0(r, one, n, twos, 0); |
5227c385 DS |
378 | } |
379 | ||
21a9651b SB |
380 | struct commit_list *repo_get_merge_bases(struct repository *r, |
381 | struct commit *one, | |
382 | struct commit *two) | |
5227c385 | 383 | { |
21a9651b | 384 | return get_merge_bases_many_0(r, one, 1, &two, 1); |
5227c385 DS |
385 | } |
386 | ||
387 | /* | |
388 | * Is "commit" a descendant of one of the elements on the "with_commit" list? | |
389 | */ | |
c1ea625f CMAB |
390 | int repo_is_descendant_of(struct repository *r, |
391 | struct commit *commit, | |
392 | struct commit_list *with_commit) | |
5227c385 DS |
393 | { |
394 | if (!with_commit) | |
395 | return 1; | |
5227c385 | 396 | |
6cc01743 DS |
397 | if (generation_numbers_enabled(the_repository)) { |
398 | struct commit_list *from_list = NULL; | |
399 | int result; | |
400 | commit_list_insert(commit, &from_list); | |
401 | result = can_all_from_reach(from_list, with_commit, 0); | |
402 | free_commit_list(from_list); | |
403 | return result; | |
404 | } else { | |
405 | while (with_commit) { | |
406 | struct commit *other; | |
407 | ||
408 | other = with_commit->item; | |
409 | with_commit = with_commit->next; | |
80b8ada5 | 410 | if (repo_in_merge_bases_many(r, other, 1, &commit)) |
6cc01743 DS |
411 | return 1; |
412 | } | |
413 | return 0; | |
5227c385 | 414 | } |
5227c385 DS |
415 | } |
416 | ||
417 | /* | |
418 | * Is "commit" an ancestor of one of the "references"? | |
419 | */ | |
4d5430f7 SB |
420 | int repo_in_merge_bases_many(struct repository *r, struct commit *commit, |
421 | int nr_reference, struct commit **reference) | |
5227c385 DS |
422 | { |
423 | struct commit_list *bases; | |
424 | int ret = 0, i; | |
d7f92784 | 425 | timestamp_t generation, max_generation = GENERATION_NUMBER_ZERO; |
5227c385 | 426 | |
4d5430f7 | 427 | if (repo_parse_commit(r, commit)) |
5227c385 DS |
428 | return ret; |
429 | for (i = 0; i < nr_reference; i++) { | |
4d5430f7 | 430 | if (repo_parse_commit(r, reference[i])) |
5227c385 | 431 | return ret; |
c752ad09 AK |
432 | |
433 | generation = commit_graph_generation(reference[i]); | |
8791bf18 DS |
434 | if (generation > max_generation) |
435 | max_generation = generation; | |
5227c385 DS |
436 | } |
437 | ||
c752ad09 | 438 | generation = commit_graph_generation(commit); |
8791bf18 | 439 | if (generation > max_generation) |
5227c385 DS |
440 | return ret; |
441 | ||
4d5430f7 | 442 | bases = paint_down_to_common(r, commit, |
c383830a | 443 | nr_reference, reference, |
c752ad09 | 444 | generation); |
5227c385 DS |
445 | if (commit->object.flags & PARENT2) |
446 | ret = 1; | |
447 | clear_commit_marks(commit, all_flags); | |
448 | clear_commit_marks_many(nr_reference, reference, all_flags); | |
449 | free_commit_list(bases); | |
450 | return ret; | |
451 | } | |
452 | ||
453 | /* | |
454 | * Is "commit" an ancestor of (i.e. reachable from) the "reference"? | |
455 | */ | |
4d5430f7 SB |
456 | int repo_in_merge_bases(struct repository *r, |
457 | struct commit *commit, | |
458 | struct commit *reference) | |
5227c385 | 459 | { |
80b8ada5 DS |
460 | int res; |
461 | struct commit_list *list = NULL; | |
462 | struct commit_list **next = &list; | |
463 | ||
464 | next = commit_list_append(commit, next); | |
465 | res = repo_is_descendant_of(r, reference, list); | |
466 | free_commit_list(list); | |
467 | ||
468 | return res; | |
5227c385 DS |
469 | } |
470 | ||
471 | struct commit_list *reduce_heads(struct commit_list *heads) | |
472 | { | |
473 | struct commit_list *p; | |
474 | struct commit_list *result = NULL, **tail = &result; | |
475 | struct commit **array; | |
476 | int num_head, i; | |
477 | ||
478 | if (!heads) | |
479 | return NULL; | |
480 | ||
481 | /* Uniquify */ | |
482 | for (p = heads; p; p = p->next) | |
483 | p->item->object.flags &= ~STALE; | |
484 | for (p = heads, num_head = 0; p; p = p->next) { | |
485 | if (p->item->object.flags & STALE) | |
486 | continue; | |
487 | p->item->object.flags |= STALE; | |
488 | num_head++; | |
489 | } | |
490 | array = xcalloc(num_head, sizeof(*array)); | |
491 | for (p = heads, i = 0; p; p = p->next) { | |
492 | if (p->item->object.flags & STALE) { | |
493 | array[i++] = p->item; | |
494 | p->item->object.flags &= ~STALE; | |
495 | } | |
496 | } | |
ed8a0e3a | 497 | num_head = remove_redundant(the_repository, array, num_head); |
5227c385 DS |
498 | for (i = 0; i < num_head; i++) |
499 | tail = &commit_list_insert(array[i], tail)->next; | |
500 | free(array); | |
501 | return result; | |
502 | } | |
503 | ||
504 | void reduce_heads_replace(struct commit_list **heads) | |
505 | { | |
506 | struct commit_list *result = reduce_heads(*heads); | |
507 | free_commit_list(*heads); | |
508 | *heads = result; | |
509 | } | |
1d614d41 | 510 | |
1d614d41 DS |
511 | int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid) |
512 | { | |
513 | struct object *o; | |
514 | struct commit *old_commit, *new_commit; | |
1e3497a2 | 515 | struct commit_list *old_commit_list = NULL; |
d546fe28 | 516 | int ret; |
1d614d41 DS |
517 | |
518 | /* | |
519 | * Both new_commit and old_commit must be commit-ish and new_commit is descendant of | |
520 | * old_commit. Otherwise we require --force. | |
521 | */ | |
522 | o = deref_tag(the_repository, parse_object(the_repository, old_oid), | |
523 | NULL, 0); | |
524 | if (!o || o->type != OBJ_COMMIT) | |
525 | return 0; | |
526 | old_commit = (struct commit *) o; | |
527 | ||
528 | o = deref_tag(the_repository, parse_object(the_repository, new_oid), | |
529 | NULL, 0); | |
530 | if (!o || o->type != OBJ_COMMIT) | |
531 | return 0; | |
532 | new_commit = (struct commit *) o; | |
533 | ||
534 | if (parse_commit(new_commit) < 0) | |
535 | return 0; | |
536 | ||
1e3497a2 | 537 | commit_list_insert(old_commit, &old_commit_list); |
0258ed1e | 538 | ret = repo_is_descendant_of(the_repository, |
c1ea625f | 539 | new_commit, old_commit_list); |
d546fe28 RS |
540 | free_commit_list(old_commit_list); |
541 | return ret; | |
1d614d41 | 542 | } |
920f93ca DS |
543 | |
544 | /* | |
545 | * Mimicking the real stack, this stack lives on the heap, avoiding stack | |
546 | * overflows. | |
547 | * | |
548 | * At each recursion step, the stack items points to the commits whose | |
549 | * ancestors are to be inspected. | |
550 | */ | |
551 | struct contains_stack { | |
552 | int nr, alloc; | |
553 | struct contains_stack_entry { | |
554 | struct commit *commit; | |
555 | struct commit_list *parents; | |
556 | } *contains_stack; | |
557 | }; | |
558 | ||
559 | static int in_commit_list(const struct commit_list *want, struct commit *c) | |
560 | { | |
561 | for (; want; want = want->next) | |
e43d2dcc | 562 | if (oideq(&want->item->object.oid, &c->object.oid)) |
920f93ca DS |
563 | return 1; |
564 | return 0; | |
565 | } | |
566 | ||
567 | /* | |
568 | * Test whether the candidate is contained in the list. | |
569 | * Do not recurse to find out, though, but return -1 if inconclusive. | |
570 | */ | |
571 | static enum contains_result contains_test(struct commit *candidate, | |
572 | const struct commit_list *want, | |
573 | struct contains_cache *cache, | |
d7f92784 | 574 | timestamp_t cutoff) |
920f93ca DS |
575 | { |
576 | enum contains_result *cached = contains_cache_at(cache, candidate); | |
577 | ||
578 | /* If we already have the answer cached, return that. */ | |
579 | if (*cached) | |
580 | return *cached; | |
581 | ||
582 | /* or are we it? */ | |
583 | if (in_commit_list(want, candidate)) { | |
584 | *cached = CONTAINS_YES; | |
585 | return CONTAINS_YES; | |
586 | } | |
587 | ||
588 | /* Otherwise, we don't know; prepare to recurse */ | |
589 | parse_commit_or_die(candidate); | |
590 | ||
c49c82aa | 591 | if (commit_graph_generation(candidate) < cutoff) |
920f93ca DS |
592 | return CONTAINS_NO; |
593 | ||
594 | return CONTAINS_UNKNOWN; | |
595 | } | |
596 | ||
597 | static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack) | |
598 | { | |
599 | ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc); | |
600 | contains_stack->contains_stack[contains_stack->nr].commit = candidate; | |
601 | contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents; | |
602 | } | |
603 | ||
604 | static enum contains_result contains_tag_algo(struct commit *candidate, | |
605 | const struct commit_list *want, | |
606 | struct contains_cache *cache) | |
607 | { | |
608 | struct contains_stack contains_stack = { 0, 0, NULL }; | |
609 | enum contains_result result; | |
d7f92784 | 610 | timestamp_t cutoff = GENERATION_NUMBER_INFINITY; |
920f93ca DS |
611 | const struct commit_list *p; |
612 | ||
613 | for (p = want; p; p = p->next) { | |
d7f92784 | 614 | timestamp_t generation; |
920f93ca DS |
615 | struct commit *c = p->item; |
616 | load_commit_graph_info(the_repository, c); | |
c752ad09 AK |
617 | generation = commit_graph_generation(c); |
618 | if (generation < cutoff) | |
619 | cutoff = generation; | |
920f93ca DS |
620 | } |
621 | ||
622 | result = contains_test(candidate, want, cache, cutoff); | |
623 | if (result != CONTAINS_UNKNOWN) | |
624 | return result; | |
625 | ||
626 | push_to_contains_stack(candidate, &contains_stack); | |
627 | while (contains_stack.nr) { | |
628 | struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1]; | |
629 | struct commit *commit = entry->commit; | |
630 | struct commit_list *parents = entry->parents; | |
631 | ||
632 | if (!parents) { | |
633 | *contains_cache_at(cache, commit) = CONTAINS_NO; | |
634 | contains_stack.nr--; | |
635 | } | |
636 | /* | |
637 | * If we just popped the stack, parents->item has been marked, | |
638 | * therefore contains_test will return a meaningful yes/no. | |
639 | */ | |
640 | else switch (contains_test(parents->item, want, cache, cutoff)) { | |
641 | case CONTAINS_YES: | |
642 | *contains_cache_at(cache, commit) = CONTAINS_YES; | |
643 | contains_stack.nr--; | |
644 | break; | |
645 | case CONTAINS_NO: | |
646 | entry->parents = parents->next; | |
647 | break; | |
648 | case CONTAINS_UNKNOWN: | |
649 | push_to_contains_stack(parents->item, &contains_stack); | |
650 | break; | |
651 | } | |
652 | } | |
653 | free(contains_stack.contains_stack); | |
654 | return contains_test(candidate, want, cache, cutoff); | |
655 | } | |
656 | ||
657 | int commit_contains(struct ref_filter *filter, struct commit *commit, | |
658 | struct commit_list *list, struct contains_cache *cache) | |
659 | { | |
660 | if (filter->with_commit_tag_algo) | |
661 | return contains_tag_algo(commit, list, cache) == CONTAINS_YES; | |
c1ea625f | 662 | return repo_is_descendant_of(the_repository, commit, list); |
920f93ca | 663 | } |
ba3ca1ed | 664 | |
ba3ca1ed DS |
665 | int can_all_from_reach_with_flag(struct object_array *from, |
666 | unsigned int with_flag, | |
667 | unsigned int assign_flag, | |
4fbcca4e | 668 | time_t min_commit_date, |
d7f92784 | 669 | timestamp_t min_generation) |
ba3ca1ed | 670 | { |
4fbcca4e | 671 | struct commit **list = NULL; |
ba3ca1ed | 672 | int i; |
b67f6b26 | 673 | int nr_commits; |
4fbcca4e | 674 | int result = 1; |
ba3ca1ed | 675 | |
4fbcca4e | 676 | ALLOC_ARRAY(list, from->nr); |
b67f6b26 | 677 | nr_commits = 0; |
ba3ca1ed | 678 | for (i = 0; i < from->nr; i++) { |
b67f6b26 | 679 | struct object *from_one = from->objects[i].item; |
ba3ca1ed | 680 | |
b67f6b26 DS |
681 | if (!from_one || from_one->flags & assign_flag) |
682 | continue; | |
683 | ||
684 | from_one = deref_tag(the_repository, from_one, | |
685 | "a from object", 0); | |
686 | if (!from_one || from_one->type != OBJ_COMMIT) { | |
85806440 DS |
687 | /* |
688 | * no way to tell if this is reachable by | |
b67f6b26 DS |
689 | * looking at the ancestry chain alone, so |
690 | * leave a note to ourselves not to worry about | |
691 | * this object anymore. | |
692 | */ | |
693 | from->objects[i].item->flags |= assign_flag; | |
694 | continue; | |
695 | } | |
696 | ||
697 | list[nr_commits] = (struct commit *)from_one; | |
698 | if (parse_commit(list[nr_commits]) || | |
c49c82aa | 699 | commit_graph_generation(list[nr_commits]) < min_generation) { |
b67f6b26 DS |
700 | result = 0; |
701 | goto cleanup; | |
702 | } | |
703 | ||
704 | nr_commits++; | |
ba3ca1ed | 705 | } |
4fbcca4e | 706 | |
b67f6b26 | 707 | QSORT(list, nr_commits, compare_commits_by_gen); |
4fbcca4e | 708 | |
b67f6b26 | 709 | for (i = 0; i < nr_commits; i++) { |
4fbcca4e DS |
710 | /* DFS from list[i] */ |
711 | struct commit_list *stack = NULL; | |
712 | ||
713 | list[i]->object.flags |= assign_flag; | |
714 | commit_list_insert(list[i], &stack); | |
715 | ||
716 | while (stack) { | |
717 | struct commit_list *parent; | |
718 | ||
b6723e46 | 719 | if (stack->item->object.flags & (with_flag | RESULT)) { |
4fbcca4e | 720 | pop_commit(&stack); |
b6723e46 DS |
721 | if (stack) |
722 | stack->item->object.flags |= RESULT; | |
4fbcca4e DS |
723 | continue; |
724 | } | |
725 | ||
726 | for (parent = stack->item->parents; parent; parent = parent->next) { | |
727 | if (parent->item->object.flags & (with_flag | RESULT)) | |
728 | stack->item->object.flags |= RESULT; | |
729 | ||
730 | if (!(parent->item->object.flags & assign_flag)) { | |
731 | parent->item->object.flags |= assign_flag; | |
732 | ||
733 | if (parse_commit(parent->item) || | |
734 | parent->item->date < min_commit_date || | |
c49c82aa | 735 | commit_graph_generation(parent->item) < min_generation) |
4fbcca4e DS |
736 | continue; |
737 | ||
738 | commit_list_insert(parent->item, &stack); | |
739 | break; | |
740 | } | |
741 | } | |
742 | ||
743 | if (!parent) | |
744 | pop_commit(&stack); | |
745 | } | |
746 | ||
747 | if (!(list[i]->object.flags & (with_flag | RESULT))) { | |
748 | result = 0; | |
749 | goto cleanup; | |
750 | } | |
751 | } | |
752 | ||
753 | cleanup: | |
85806440 | 754 | clear_commit_marks_many(nr_commits, list, RESULT | assign_flag); |
4067a646 DS |
755 | free(list); |
756 | ||
757 | for (i = 0; i < from->nr; i++) | |
758 | from->objects[i].item->flags &= ~assign_flag; | |
759 | ||
4fbcca4e | 760 | return result; |
ba3ca1ed | 761 | } |
1792bc12 DS |
762 | |
763 | int can_all_from_reach(struct commit_list *from, struct commit_list *to, | |
764 | int cutoff_by_min_date) | |
765 | { | |
766 | struct object_array from_objs = OBJECT_ARRAY_INIT; | |
767 | time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0; | |
768 | struct commit_list *from_iter = from, *to_iter = to; | |
769 | int result; | |
d7f92784 | 770 | timestamp_t min_generation = GENERATION_NUMBER_INFINITY; |
1792bc12 DS |
771 | |
772 | while (from_iter) { | |
773 | add_object_array(&from_iter->item->object, NULL, &from_objs); | |
774 | ||
775 | if (!parse_commit(from_iter->item)) { | |
d7f92784 | 776 | timestamp_t generation; |
1792bc12 DS |
777 | if (from_iter->item->date < min_commit_date) |
778 | min_commit_date = from_iter->item->date; | |
4fbcca4e | 779 | |
c752ad09 AK |
780 | generation = commit_graph_generation(from_iter->item); |
781 | if (generation < min_generation) | |
782 | min_generation = generation; | |
1792bc12 DS |
783 | } |
784 | ||
785 | from_iter = from_iter->next; | |
786 | } | |
787 | ||
788 | while (to_iter) { | |
789 | if (!parse_commit(to_iter->item)) { | |
d7f92784 | 790 | timestamp_t generation; |
1792bc12 DS |
791 | if (to_iter->item->date < min_commit_date) |
792 | min_commit_date = to_iter->item->date; | |
4fbcca4e | 793 | |
c752ad09 AK |
794 | generation = commit_graph_generation(to_iter->item); |
795 | if (generation < min_generation) | |
796 | min_generation = generation; | |
1792bc12 DS |
797 | } |
798 | ||
799 | to_iter->item->object.flags |= PARENT2; | |
800 | ||
801 | to_iter = to_iter->next; | |
802 | } | |
803 | ||
804 | result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1, | |
4fbcca4e | 805 | min_commit_date, min_generation); |
1792bc12 DS |
806 | |
807 | while (from) { | |
808 | clear_commit_marks(from->item, PARENT1); | |
809 | from = from->next; | |
810 | } | |
811 | ||
812 | while (to) { | |
813 | clear_commit_marks(to->item, PARENT2); | |
814 | to = to->next; | |
815 | } | |
816 | ||
817 | object_array_clear(&from_objs); | |
818 | return result; | |
819 | } | |
fcb2c076 DS |
820 | |
821 | struct commit_list *get_reachable_subset(struct commit **from, int nr_from, | |
822 | struct commit **to, int nr_to, | |
823 | unsigned int reachable_flag) | |
824 | { | |
825 | struct commit **item; | |
826 | struct commit *current; | |
827 | struct commit_list *found_commits = NULL; | |
828 | struct commit **to_last = to + nr_to; | |
829 | struct commit **from_last = from + nr_from; | |
d7f92784 | 830 | timestamp_t min_generation = GENERATION_NUMBER_INFINITY; |
fcb2c076 DS |
831 | int num_to_find = 0; |
832 | ||
833 | struct prio_queue queue = { compare_commits_by_gen_then_commit_date }; | |
834 | ||
835 | for (item = to; item < to_last; item++) { | |
d7f92784 | 836 | timestamp_t generation; |
fcb2c076 DS |
837 | struct commit *c = *item; |
838 | ||
839 | parse_commit(c); | |
c752ad09 AK |
840 | generation = commit_graph_generation(c); |
841 | if (generation < min_generation) | |
842 | min_generation = generation; | |
fcb2c076 DS |
843 | |
844 | if (!(c->object.flags & PARENT1)) { | |
845 | c->object.flags |= PARENT1; | |
846 | num_to_find++; | |
847 | } | |
848 | } | |
849 | ||
850 | for (item = from; item < from_last; item++) { | |
851 | struct commit *c = *item; | |
852 | if (!(c->object.flags & PARENT2)) { | |
853 | c->object.flags |= PARENT2; | |
854 | parse_commit(c); | |
855 | ||
856 | prio_queue_put(&queue, *item); | |
857 | } | |
858 | } | |
859 | ||
860 | while (num_to_find && (current = prio_queue_get(&queue)) != NULL) { | |
861 | struct commit_list *parents; | |
862 | ||
863 | if (current->object.flags & PARENT1) { | |
864 | current->object.flags &= ~PARENT1; | |
865 | current->object.flags |= reachable_flag; | |
866 | commit_list_insert(current, &found_commits); | |
867 | num_to_find--; | |
868 | } | |
869 | ||
870 | for (parents = current->parents; parents; parents = parents->next) { | |
871 | struct commit *p = parents->item; | |
872 | ||
873 | parse_commit(p); | |
874 | ||
c49c82aa | 875 | if (commit_graph_generation(p) < min_generation) |
fcb2c076 DS |
876 | continue; |
877 | ||
878 | if (p->object.flags & PARENT2) | |
879 | continue; | |
880 | ||
881 | p->object.flags |= PARENT2; | |
882 | prio_queue_put(&queue, p); | |
883 | } | |
884 | } | |
885 | ||
886 | clear_commit_marks_many(nr_to, to, PARENT1); | |
887 | clear_commit_marks_many(nr_from, from, PARENT2); | |
888 | ||
889 | return found_commits; | |
890 | } |