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