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