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