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