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