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