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