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 */ |
Derrick Stolee | ba3ca1e | 2018-07-20 16:33:13 +0000 | [diff] [blame] | 13 | #define REACHABLE (1u<<15) |
Derrick Stolee | 5227c38 | 2018-07-20 16:33:02 +0000 | [diff] [blame] | 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 | |
| 21 | static int queue_has_nonstale(struct prio_queue *queue) |
| 22 | { |
| 23 | int i; |
| 24 | for (i = 0; i < queue->nr; i++) { |
| 25 | struct commit *commit = queue->array[i].data; |
| 26 | if (!(commit->object.flags & STALE)) |
| 27 | return 1; |
| 28 | } |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | /* all input commits in one and twos[] must have been parsed! */ |
| 33 | static struct commit_list *paint_down_to_common(struct commit *one, int n, |
| 34 | struct commit **twos, |
| 35 | int min_generation) |
| 36 | { |
| 37 | struct prio_queue queue = { compare_commits_by_gen_then_commit_date }; |
| 38 | struct commit_list *result = NULL; |
| 39 | int i; |
| 40 | uint32_t last_gen = GENERATION_NUMBER_INFINITY; |
| 41 | |
Junio C Hamano | 1b7a91d | 2018-09-17 13:53:52 -0700 | [diff] [blame] | 42 | if (!min_generation) |
| 43 | queue.compare = compare_commits_by_commit_date; |
| 44 | |
Derrick Stolee | 5227c38 | 2018-07-20 16:33:02 +0000 | [diff] [blame] | 45 | one->object.flags |= PARENT1; |
| 46 | if (!n) { |
| 47 | commit_list_append(one, &result); |
| 48 | return result; |
| 49 | } |
| 50 | prio_queue_put(&queue, one); |
| 51 | |
| 52 | for (i = 0; i < n; i++) { |
| 53 | twos[i]->object.flags |= PARENT2; |
| 54 | prio_queue_put(&queue, twos[i]); |
| 55 | } |
| 56 | |
| 57 | while (queue_has_nonstale(&queue)) { |
| 58 | struct commit *commit = prio_queue_get(&queue); |
| 59 | struct commit_list *parents; |
| 60 | int flags; |
| 61 | |
Junio C Hamano | 1b7a91d | 2018-09-17 13:53:52 -0700 | [diff] [blame] | 62 | if (min_generation && commit->generation > last_gen) |
Derrick Stolee | 5227c38 | 2018-07-20 16:33:02 +0000 | [diff] [blame] | 63 | BUG("bad generation skip %8x > %8x at %s", |
| 64 | commit->generation, last_gen, |
| 65 | oid_to_hex(&commit->object.oid)); |
| 66 | last_gen = commit->generation; |
| 67 | |
| 68 | if (commit->generation < min_generation) |
| 69 | break; |
| 70 | |
| 71 | flags = commit->object.flags & (PARENT1 | PARENT2 | STALE); |
| 72 | if (flags == (PARENT1 | PARENT2)) { |
| 73 | if (!(commit->object.flags & RESULT)) { |
| 74 | commit->object.flags |= RESULT; |
| 75 | commit_list_insert_by_date(commit, &result); |
| 76 | } |
| 77 | /* Mark parents of a found merge stale */ |
| 78 | flags |= STALE; |
| 79 | } |
| 80 | parents = commit->parents; |
| 81 | while (parents) { |
| 82 | struct commit *p = parents->item; |
| 83 | parents = parents->next; |
| 84 | if ((p->object.flags & flags) == flags) |
| 85 | continue; |
| 86 | if (parse_commit(p)) |
| 87 | return NULL; |
| 88 | p->object.flags |= flags; |
| 89 | prio_queue_put(&queue, p); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | clear_prio_queue(&queue); |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos) |
| 98 | { |
| 99 | struct commit_list *list = NULL; |
| 100 | struct commit_list *result = NULL; |
| 101 | int i; |
| 102 | |
| 103 | for (i = 0; i < n; i++) { |
| 104 | if (one == twos[i]) |
| 105 | /* |
| 106 | * We do not mark this even with RESULT so we do not |
| 107 | * have to clean it up. |
| 108 | */ |
| 109 | return commit_list_insert(one, &result); |
| 110 | } |
| 111 | |
| 112 | if (parse_commit(one)) |
| 113 | return NULL; |
| 114 | for (i = 0; i < n; i++) { |
| 115 | if (parse_commit(twos[i])) |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | list = paint_down_to_common(one, n, twos, 0); |
| 120 | |
| 121 | while (list) { |
| 122 | struct commit *commit = pop_commit(&list); |
| 123 | if (!(commit->object.flags & STALE)) |
| 124 | commit_list_insert_by_date(commit, &result); |
| 125 | } |
| 126 | return result; |
| 127 | } |
| 128 | |
| 129 | struct commit_list *get_octopus_merge_bases(struct commit_list *in) |
| 130 | { |
| 131 | struct commit_list *i, *j, *k, *ret = NULL; |
| 132 | |
| 133 | if (!in) |
| 134 | return ret; |
| 135 | |
| 136 | commit_list_insert(in->item, &ret); |
| 137 | |
| 138 | for (i = in->next; i; i = i->next) { |
| 139 | struct commit_list *new_commits = NULL, *end = NULL; |
| 140 | |
| 141 | for (j = ret; j; j = j->next) { |
| 142 | struct commit_list *bases; |
| 143 | bases = get_merge_bases(i->item, j->item); |
| 144 | if (!new_commits) |
| 145 | new_commits = bases; |
| 146 | else |
| 147 | end->next = bases; |
| 148 | for (k = bases; k; k = k->next) |
| 149 | end = k; |
| 150 | } |
| 151 | ret = new_commits; |
| 152 | } |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | static int remove_redundant(struct commit **array, int cnt) |
| 157 | { |
| 158 | /* |
| 159 | * Some commit in the array may be an ancestor of |
| 160 | * another commit. Move such commit to the end of |
| 161 | * the array, and return the number of commits that |
| 162 | * are independent from each other. |
| 163 | */ |
| 164 | struct commit **work; |
| 165 | unsigned char *redundant; |
| 166 | int *filled_index; |
| 167 | int i, j, filled; |
| 168 | |
| 169 | work = xcalloc(cnt, sizeof(*work)); |
| 170 | redundant = xcalloc(cnt, 1); |
| 171 | ALLOC_ARRAY(filled_index, cnt - 1); |
| 172 | |
| 173 | for (i = 0; i < cnt; i++) |
| 174 | parse_commit(array[i]); |
| 175 | for (i = 0; i < cnt; i++) { |
| 176 | struct commit_list *common; |
| 177 | uint32_t min_generation = array[i]->generation; |
| 178 | |
| 179 | if (redundant[i]) |
| 180 | continue; |
| 181 | for (j = filled = 0; j < cnt; j++) { |
| 182 | if (i == j || redundant[j]) |
| 183 | continue; |
| 184 | filled_index[filled] = j; |
| 185 | work[filled++] = array[j]; |
| 186 | |
| 187 | if (array[j]->generation < min_generation) |
| 188 | min_generation = array[j]->generation; |
| 189 | } |
| 190 | common = paint_down_to_common(array[i], filled, work, |
| 191 | min_generation); |
| 192 | if (array[i]->object.flags & PARENT2) |
| 193 | redundant[i] = 1; |
| 194 | for (j = 0; j < filled; j++) |
| 195 | if (work[j]->object.flags & PARENT1) |
| 196 | redundant[filled_index[j]] = 1; |
| 197 | clear_commit_marks(array[i], all_flags); |
| 198 | clear_commit_marks_many(filled, work, all_flags); |
| 199 | free_commit_list(common); |
| 200 | } |
| 201 | |
| 202 | /* Now collect the result */ |
| 203 | COPY_ARRAY(work, array, cnt); |
| 204 | for (i = filled = 0; i < cnt; i++) |
| 205 | if (!redundant[i]) |
| 206 | array[filled++] = work[i]; |
| 207 | for (j = filled, i = 0; i < cnt; i++) |
| 208 | if (redundant[i]) |
| 209 | array[j++] = work[i]; |
| 210 | free(work); |
| 211 | free(redundant); |
| 212 | free(filled_index); |
| 213 | return filled; |
| 214 | } |
| 215 | |
| 216 | static struct commit_list *get_merge_bases_many_0(struct commit *one, |
| 217 | int n, |
| 218 | struct commit **twos, |
| 219 | int cleanup) |
| 220 | { |
| 221 | struct commit_list *list; |
| 222 | struct commit **rslt; |
| 223 | struct commit_list *result; |
| 224 | int cnt, i; |
| 225 | |
| 226 | result = merge_bases_many(one, n, twos); |
| 227 | for (i = 0; i < n; i++) { |
| 228 | if (one == twos[i]) |
| 229 | return result; |
| 230 | } |
| 231 | if (!result || !result->next) { |
| 232 | if (cleanup) { |
| 233 | clear_commit_marks(one, all_flags); |
| 234 | clear_commit_marks_many(n, twos, all_flags); |
| 235 | } |
| 236 | return result; |
| 237 | } |
| 238 | |
| 239 | /* There are more than one */ |
| 240 | cnt = commit_list_count(result); |
| 241 | rslt = xcalloc(cnt, sizeof(*rslt)); |
| 242 | for (list = result, i = 0; list; list = list->next) |
| 243 | rslt[i++] = list->item; |
| 244 | free_commit_list(result); |
| 245 | |
| 246 | clear_commit_marks(one, all_flags); |
| 247 | clear_commit_marks_many(n, twos, all_flags); |
| 248 | |
| 249 | cnt = remove_redundant(rslt, cnt); |
| 250 | result = NULL; |
| 251 | for (i = 0; i < cnt; i++) |
| 252 | commit_list_insert_by_date(rslt[i], &result); |
| 253 | free(rslt); |
| 254 | return result; |
| 255 | } |
| 256 | |
| 257 | struct commit_list *get_merge_bases_many(struct commit *one, |
| 258 | int n, |
| 259 | struct commit **twos) |
| 260 | { |
| 261 | return get_merge_bases_many_0(one, n, twos, 1); |
| 262 | } |
| 263 | |
| 264 | struct commit_list *get_merge_bases_many_dirty(struct commit *one, |
| 265 | int n, |
| 266 | struct commit **twos) |
| 267 | { |
| 268 | return get_merge_bases_many_0(one, n, twos, 0); |
| 269 | } |
| 270 | |
| 271 | struct commit_list *get_merge_bases(struct commit *one, struct commit *two) |
| 272 | { |
| 273 | return get_merge_bases_many_0(one, 1, &two, 1); |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Is "commit" a descendant of one of the elements on the "with_commit" list? |
| 278 | */ |
| 279 | int is_descendant_of(struct commit *commit, struct commit_list *with_commit) |
| 280 | { |
| 281 | if (!with_commit) |
| 282 | return 1; |
Derrick Stolee | 5227c38 | 2018-07-20 16:33:02 +0000 | [diff] [blame] | 283 | |
Derrick Stolee | 6cc0174 | 2018-07-20 16:33:30 +0000 | [diff] [blame] | 284 | if (generation_numbers_enabled(the_repository)) { |
| 285 | struct commit_list *from_list = NULL; |
| 286 | int result; |
| 287 | commit_list_insert(commit, &from_list); |
| 288 | result = can_all_from_reach(from_list, with_commit, 0); |
| 289 | free_commit_list(from_list); |
| 290 | return result; |
| 291 | } else { |
| 292 | while (with_commit) { |
| 293 | struct commit *other; |
| 294 | |
| 295 | other = with_commit->item; |
| 296 | with_commit = with_commit->next; |
| 297 | if (in_merge_bases(other, commit)) |
| 298 | return 1; |
| 299 | } |
| 300 | return 0; |
Derrick Stolee | 5227c38 | 2018-07-20 16:33:02 +0000 | [diff] [blame] | 301 | } |
Derrick Stolee | 5227c38 | 2018-07-20 16:33:02 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Is "commit" an ancestor of one of the "references"? |
| 306 | */ |
| 307 | int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference) |
| 308 | { |
| 309 | struct commit_list *bases; |
| 310 | int ret = 0, i; |
| 311 | uint32_t min_generation = GENERATION_NUMBER_INFINITY; |
| 312 | |
| 313 | if (parse_commit(commit)) |
| 314 | return ret; |
| 315 | for (i = 0; i < nr_reference; i++) { |
| 316 | if (parse_commit(reference[i])) |
| 317 | return ret; |
| 318 | if (reference[i]->generation < min_generation) |
| 319 | min_generation = reference[i]->generation; |
| 320 | } |
| 321 | |
| 322 | if (commit->generation > min_generation) |
| 323 | return ret; |
| 324 | |
| 325 | bases = paint_down_to_common(commit, nr_reference, reference, commit->generation); |
| 326 | if (commit->object.flags & PARENT2) |
| 327 | ret = 1; |
| 328 | clear_commit_marks(commit, all_flags); |
| 329 | clear_commit_marks_many(nr_reference, reference, all_flags); |
| 330 | free_commit_list(bases); |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * Is "commit" an ancestor of (i.e. reachable from) the "reference"? |
| 336 | */ |
| 337 | int in_merge_bases(struct commit *commit, struct commit *reference) |
| 338 | { |
| 339 | return in_merge_bases_many(commit, 1, &reference); |
| 340 | } |
| 341 | |
| 342 | struct commit_list *reduce_heads(struct commit_list *heads) |
| 343 | { |
| 344 | struct commit_list *p; |
| 345 | struct commit_list *result = NULL, **tail = &result; |
| 346 | struct commit **array; |
| 347 | int num_head, i; |
| 348 | |
| 349 | if (!heads) |
| 350 | return NULL; |
| 351 | |
| 352 | /* Uniquify */ |
| 353 | for (p = heads; p; p = p->next) |
| 354 | p->item->object.flags &= ~STALE; |
| 355 | for (p = heads, num_head = 0; p; p = p->next) { |
| 356 | if (p->item->object.flags & STALE) |
| 357 | continue; |
| 358 | p->item->object.flags |= STALE; |
| 359 | num_head++; |
| 360 | } |
| 361 | array = xcalloc(num_head, sizeof(*array)); |
| 362 | for (p = heads, i = 0; p; p = p->next) { |
| 363 | if (p->item->object.flags & STALE) { |
| 364 | array[i++] = p->item; |
| 365 | p->item->object.flags &= ~STALE; |
| 366 | } |
| 367 | } |
| 368 | num_head = remove_redundant(array, num_head); |
| 369 | for (i = 0; i < num_head; i++) |
| 370 | tail = &commit_list_insert(array[i], tail)->next; |
| 371 | free(array); |
| 372 | return result; |
| 373 | } |
| 374 | |
| 375 | void reduce_heads_replace(struct commit_list **heads) |
| 376 | { |
| 377 | struct commit_list *result = reduce_heads(*heads); |
| 378 | free_commit_list(*heads); |
| 379 | *heads = result; |
| 380 | } |
Derrick Stolee | 1d614d4 | 2018-07-20 16:33:06 +0000 | [diff] [blame] | 381 | |
Derrick Stolee | 1d614d4 | 2018-07-20 16:33:06 +0000 | [diff] [blame] | 382 | int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid) |
| 383 | { |
| 384 | struct object *o; |
| 385 | struct commit *old_commit, *new_commit; |
Derrick Stolee | 1e3497a | 2018-07-20 16:33:27 +0000 | [diff] [blame] | 386 | struct commit_list *old_commit_list = NULL; |
Derrick Stolee | 1d614d4 | 2018-07-20 16:33:06 +0000 | [diff] [blame] | 387 | |
| 388 | /* |
| 389 | * Both new_commit and old_commit must be commit-ish and new_commit is descendant of |
| 390 | * old_commit. Otherwise we require --force. |
| 391 | */ |
| 392 | o = deref_tag(the_repository, parse_object(the_repository, old_oid), |
| 393 | NULL, 0); |
| 394 | if (!o || o->type != OBJ_COMMIT) |
| 395 | return 0; |
| 396 | old_commit = (struct commit *) o; |
| 397 | |
| 398 | o = deref_tag(the_repository, parse_object(the_repository, new_oid), |
| 399 | NULL, 0); |
| 400 | if (!o || o->type != OBJ_COMMIT) |
| 401 | return 0; |
| 402 | new_commit = (struct commit *) o; |
| 403 | |
| 404 | if (parse_commit(new_commit) < 0) |
| 405 | return 0; |
| 406 | |
Derrick Stolee | 1e3497a | 2018-07-20 16:33:27 +0000 | [diff] [blame] | 407 | commit_list_insert(old_commit, &old_commit_list); |
| 408 | return is_descendant_of(new_commit, old_commit_list); |
Derrick Stolee | 1d614d4 | 2018-07-20 16:33:06 +0000 | [diff] [blame] | 409 | } |
Derrick Stolee | 920f93c | 2018-07-20 16:33:08 +0000 | [diff] [blame] | 410 | |
| 411 | /* |
| 412 | * Mimicking the real stack, this stack lives on the heap, avoiding stack |
| 413 | * overflows. |
| 414 | * |
| 415 | * At each recursion step, the stack items points to the commits whose |
| 416 | * ancestors are to be inspected. |
| 417 | */ |
| 418 | struct contains_stack { |
| 419 | int nr, alloc; |
| 420 | struct contains_stack_entry { |
| 421 | struct commit *commit; |
| 422 | struct commit_list *parents; |
| 423 | } *contains_stack; |
| 424 | }; |
| 425 | |
| 426 | static int in_commit_list(const struct commit_list *want, struct commit *c) |
| 427 | { |
| 428 | for (; want; want = want->next) |
Jeff King | e43d2dc | 2018-10-02 17:19:21 -0400 | [diff] [blame] | 429 | if (oideq(&want->item->object.oid, &c->object.oid)) |
Derrick Stolee | 920f93c | 2018-07-20 16:33:08 +0000 | [diff] [blame] | 430 | return 1; |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Test whether the candidate is contained in the list. |
| 436 | * Do not recurse to find out, though, but return -1 if inconclusive. |
| 437 | */ |
| 438 | static enum contains_result contains_test(struct commit *candidate, |
| 439 | const struct commit_list *want, |
| 440 | struct contains_cache *cache, |
| 441 | uint32_t cutoff) |
| 442 | { |
| 443 | enum contains_result *cached = contains_cache_at(cache, candidate); |
| 444 | |
| 445 | /* If we already have the answer cached, return that. */ |
| 446 | if (*cached) |
| 447 | return *cached; |
| 448 | |
| 449 | /* or are we it? */ |
| 450 | if (in_commit_list(want, candidate)) { |
| 451 | *cached = CONTAINS_YES; |
| 452 | return CONTAINS_YES; |
| 453 | } |
| 454 | |
| 455 | /* Otherwise, we don't know; prepare to recurse */ |
| 456 | parse_commit_or_die(candidate); |
| 457 | |
| 458 | if (candidate->generation < cutoff) |
| 459 | return CONTAINS_NO; |
| 460 | |
| 461 | return CONTAINS_UNKNOWN; |
| 462 | } |
| 463 | |
| 464 | static void push_to_contains_stack(struct commit *candidate, struct contains_stack *contains_stack) |
| 465 | { |
| 466 | ALLOC_GROW(contains_stack->contains_stack, contains_stack->nr + 1, contains_stack->alloc); |
| 467 | contains_stack->contains_stack[contains_stack->nr].commit = candidate; |
| 468 | contains_stack->contains_stack[contains_stack->nr++].parents = candidate->parents; |
| 469 | } |
| 470 | |
| 471 | static enum contains_result contains_tag_algo(struct commit *candidate, |
| 472 | const struct commit_list *want, |
| 473 | struct contains_cache *cache) |
| 474 | { |
| 475 | struct contains_stack contains_stack = { 0, 0, NULL }; |
| 476 | enum contains_result result; |
| 477 | uint32_t cutoff = GENERATION_NUMBER_INFINITY; |
| 478 | const struct commit_list *p; |
| 479 | |
| 480 | for (p = want; p; p = p->next) { |
| 481 | struct commit *c = p->item; |
| 482 | load_commit_graph_info(the_repository, c); |
| 483 | if (c->generation < cutoff) |
| 484 | cutoff = c->generation; |
| 485 | } |
| 486 | |
| 487 | result = contains_test(candidate, want, cache, cutoff); |
| 488 | if (result != CONTAINS_UNKNOWN) |
| 489 | return result; |
| 490 | |
| 491 | push_to_contains_stack(candidate, &contains_stack); |
| 492 | while (contains_stack.nr) { |
| 493 | struct contains_stack_entry *entry = &contains_stack.contains_stack[contains_stack.nr - 1]; |
| 494 | struct commit *commit = entry->commit; |
| 495 | struct commit_list *parents = entry->parents; |
| 496 | |
| 497 | if (!parents) { |
| 498 | *contains_cache_at(cache, commit) = CONTAINS_NO; |
| 499 | contains_stack.nr--; |
| 500 | } |
| 501 | /* |
| 502 | * If we just popped the stack, parents->item has been marked, |
| 503 | * therefore contains_test will return a meaningful yes/no. |
| 504 | */ |
| 505 | else switch (contains_test(parents->item, want, cache, cutoff)) { |
| 506 | case CONTAINS_YES: |
| 507 | *contains_cache_at(cache, commit) = CONTAINS_YES; |
| 508 | contains_stack.nr--; |
| 509 | break; |
| 510 | case CONTAINS_NO: |
| 511 | entry->parents = parents->next; |
| 512 | break; |
| 513 | case CONTAINS_UNKNOWN: |
| 514 | push_to_contains_stack(parents->item, &contains_stack); |
| 515 | break; |
| 516 | } |
| 517 | } |
| 518 | free(contains_stack.contains_stack); |
| 519 | return contains_test(candidate, want, cache, cutoff); |
| 520 | } |
| 521 | |
| 522 | int commit_contains(struct ref_filter *filter, struct commit *commit, |
| 523 | struct commit_list *list, struct contains_cache *cache) |
| 524 | { |
| 525 | if (filter->with_commit_tag_algo) |
| 526 | return contains_tag_algo(commit, list, cache) == CONTAINS_YES; |
| 527 | return is_descendant_of(commit, list); |
| 528 | } |
Derrick Stolee | ba3ca1e | 2018-07-20 16:33:13 +0000 | [diff] [blame] | 529 | |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 530 | static int compare_commits_by_gen(const void *_a, const void *_b) |
Derrick Stolee | ba3ca1e | 2018-07-20 16:33:13 +0000 | [diff] [blame] | 531 | { |
René Scharfe | 8628ace | 2018-10-01 21:16:01 +0200 | [diff] [blame] | 532 | const struct commit *a = *(const struct commit * const *)_a; |
| 533 | const struct commit *b = *(const struct commit * const *)_b; |
Derrick Stolee | ba3ca1e | 2018-07-20 16:33:13 +0000 | [diff] [blame] | 534 | |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 535 | if (a->generation < b->generation) |
| 536 | return -1; |
| 537 | if (a->generation > b->generation) |
| 538 | return 1; |
| 539 | return 0; |
Derrick Stolee | ba3ca1e | 2018-07-20 16:33:13 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | int can_all_from_reach_with_flag(struct object_array *from, |
| 543 | unsigned int with_flag, |
| 544 | unsigned int assign_flag, |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 545 | time_t min_commit_date, |
| 546 | uint32_t min_generation) |
Derrick Stolee | ba3ca1e | 2018-07-20 16:33:13 +0000 | [diff] [blame] | 547 | { |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 548 | struct commit **list = NULL; |
Derrick Stolee | ba3ca1e | 2018-07-20 16:33:13 +0000 | [diff] [blame] | 549 | int i; |
Derrick Stolee | b67f6b2 | 2018-09-21 08:05:26 -0700 | [diff] [blame] | 550 | int nr_commits; |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 551 | int result = 1; |
Derrick Stolee | ba3ca1e | 2018-07-20 16:33:13 +0000 | [diff] [blame] | 552 | |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 553 | ALLOC_ARRAY(list, from->nr); |
Derrick Stolee | b67f6b2 | 2018-09-21 08:05:26 -0700 | [diff] [blame] | 554 | nr_commits = 0; |
Derrick Stolee | ba3ca1e | 2018-07-20 16:33:13 +0000 | [diff] [blame] | 555 | for (i = 0; i < from->nr; i++) { |
Derrick Stolee | b67f6b2 | 2018-09-21 08:05:26 -0700 | [diff] [blame] | 556 | struct object *from_one = from->objects[i].item; |
Derrick Stolee | ba3ca1e | 2018-07-20 16:33:13 +0000 | [diff] [blame] | 557 | |
Derrick Stolee | b67f6b2 | 2018-09-21 08:05:26 -0700 | [diff] [blame] | 558 | if (!from_one || from_one->flags & assign_flag) |
| 559 | continue; |
| 560 | |
| 561 | from_one = deref_tag(the_repository, from_one, |
| 562 | "a from object", 0); |
| 563 | if (!from_one || from_one->type != OBJ_COMMIT) { |
Derrick Stolee | 8580644 | 2018-09-25 13:27:41 +0000 | [diff] [blame] | 564 | /* |
| 565 | * no way to tell if this is reachable by |
Derrick Stolee | b67f6b2 | 2018-09-21 08:05:26 -0700 | [diff] [blame] | 566 | * looking at the ancestry chain alone, so |
| 567 | * leave a note to ourselves not to worry about |
| 568 | * this object anymore. |
| 569 | */ |
| 570 | from->objects[i].item->flags |= assign_flag; |
| 571 | continue; |
| 572 | } |
| 573 | |
| 574 | list[nr_commits] = (struct commit *)from_one; |
| 575 | if (parse_commit(list[nr_commits]) || |
| 576 | list[nr_commits]->generation < min_generation) { |
| 577 | result = 0; |
| 578 | goto cleanup; |
| 579 | } |
| 580 | |
| 581 | nr_commits++; |
Derrick Stolee | ba3ca1e | 2018-07-20 16:33:13 +0000 | [diff] [blame] | 582 | } |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 583 | |
Derrick Stolee | b67f6b2 | 2018-09-21 08:05:26 -0700 | [diff] [blame] | 584 | QSORT(list, nr_commits, compare_commits_by_gen); |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 585 | |
Derrick Stolee | b67f6b2 | 2018-09-21 08:05:26 -0700 | [diff] [blame] | 586 | for (i = 0; i < nr_commits; i++) { |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 587 | /* DFS from list[i] */ |
| 588 | struct commit_list *stack = NULL; |
| 589 | |
| 590 | list[i]->object.flags |= assign_flag; |
| 591 | commit_list_insert(list[i], &stack); |
| 592 | |
| 593 | while (stack) { |
| 594 | struct commit_list *parent; |
| 595 | |
Derrick Stolee | b6723e4 | 2018-10-18 10:24:40 -0700 | [diff] [blame] | 596 | if (stack->item->object.flags & (with_flag | RESULT)) { |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 597 | pop_commit(&stack); |
Derrick Stolee | b6723e4 | 2018-10-18 10:24:40 -0700 | [diff] [blame] | 598 | if (stack) |
| 599 | stack->item->object.flags |= RESULT; |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 600 | continue; |
| 601 | } |
| 602 | |
| 603 | for (parent = stack->item->parents; parent; parent = parent->next) { |
| 604 | if (parent->item->object.flags & (with_flag | RESULT)) |
| 605 | stack->item->object.flags |= RESULT; |
| 606 | |
| 607 | if (!(parent->item->object.flags & assign_flag)) { |
| 608 | parent->item->object.flags |= assign_flag; |
| 609 | |
| 610 | if (parse_commit(parent->item) || |
| 611 | parent->item->date < min_commit_date || |
| 612 | parent->item->generation < min_generation) |
| 613 | continue; |
| 614 | |
| 615 | commit_list_insert(parent->item, &stack); |
| 616 | break; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | if (!parent) |
| 621 | pop_commit(&stack); |
| 622 | } |
| 623 | |
| 624 | if (!(list[i]->object.flags & (with_flag | RESULT))) { |
| 625 | result = 0; |
| 626 | goto cleanup; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | cleanup: |
Derrick Stolee | 8580644 | 2018-09-25 13:27:41 +0000 | [diff] [blame] | 631 | clear_commit_marks_many(nr_commits, list, RESULT | assign_flag); |
Derrick Stolee | 4067a64 | 2018-09-21 08:05:27 -0700 | [diff] [blame] | 632 | free(list); |
| 633 | |
| 634 | for (i = 0; i < from->nr; i++) |
| 635 | from->objects[i].item->flags &= ~assign_flag; |
| 636 | |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 637 | return result; |
Derrick Stolee | ba3ca1e | 2018-07-20 16:33:13 +0000 | [diff] [blame] | 638 | } |
Derrick Stolee | 1792bc1 | 2018-07-20 16:33:23 +0000 | [diff] [blame] | 639 | |
| 640 | int can_all_from_reach(struct commit_list *from, struct commit_list *to, |
| 641 | int cutoff_by_min_date) |
| 642 | { |
| 643 | struct object_array from_objs = OBJECT_ARRAY_INIT; |
| 644 | time_t min_commit_date = cutoff_by_min_date ? from->item->date : 0; |
| 645 | struct commit_list *from_iter = from, *to_iter = to; |
| 646 | int result; |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 647 | uint32_t min_generation = GENERATION_NUMBER_INFINITY; |
Derrick Stolee | 1792bc1 | 2018-07-20 16:33:23 +0000 | [diff] [blame] | 648 | |
| 649 | while (from_iter) { |
| 650 | add_object_array(&from_iter->item->object, NULL, &from_objs); |
| 651 | |
| 652 | if (!parse_commit(from_iter->item)) { |
| 653 | if (from_iter->item->date < min_commit_date) |
| 654 | min_commit_date = from_iter->item->date; |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 655 | |
| 656 | if (from_iter->item->generation < min_generation) |
| 657 | min_generation = from_iter->item->generation; |
Derrick Stolee | 1792bc1 | 2018-07-20 16:33:23 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | from_iter = from_iter->next; |
| 661 | } |
| 662 | |
| 663 | while (to_iter) { |
| 664 | if (!parse_commit(to_iter->item)) { |
| 665 | if (to_iter->item->date < min_commit_date) |
| 666 | min_commit_date = to_iter->item->date; |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 667 | |
| 668 | if (to_iter->item->generation < min_generation) |
| 669 | min_generation = to_iter->item->generation; |
Derrick Stolee | 1792bc1 | 2018-07-20 16:33:23 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | to_iter->item->object.flags |= PARENT2; |
| 673 | |
| 674 | to_iter = to_iter->next; |
| 675 | } |
| 676 | |
| 677 | result = can_all_from_reach_with_flag(&from_objs, PARENT2, PARENT1, |
Derrick Stolee | 4fbcca4 | 2018-07-20 16:33:28 +0000 | [diff] [blame] | 678 | min_commit_date, min_generation); |
Derrick Stolee | 1792bc1 | 2018-07-20 16:33:23 +0000 | [diff] [blame] | 679 | |
| 680 | while (from) { |
| 681 | clear_commit_marks(from->item, PARENT1); |
| 682 | from = from->next; |
| 683 | } |
| 684 | |
| 685 | while (to) { |
| 686 | clear_commit_marks(to->item, PARENT2); |
| 687 | to = to->next; |
| 688 | } |
| 689 | |
| 690 | object_array_clear(&from_objs); |
| 691 | return result; |
| 692 | } |
Derrick Stolee | fcb2c07 | 2018-11-02 06:14:45 -0700 | [diff] [blame] | 693 | |
| 694 | struct commit_list *get_reachable_subset(struct commit **from, int nr_from, |
| 695 | struct commit **to, int nr_to, |
| 696 | unsigned int reachable_flag) |
| 697 | { |
| 698 | struct commit **item; |
| 699 | struct commit *current; |
| 700 | struct commit_list *found_commits = NULL; |
| 701 | struct commit **to_last = to + nr_to; |
| 702 | struct commit **from_last = from + nr_from; |
| 703 | uint32_t min_generation = GENERATION_NUMBER_INFINITY; |
| 704 | int num_to_find = 0; |
| 705 | |
| 706 | struct prio_queue queue = { compare_commits_by_gen_then_commit_date }; |
| 707 | |
| 708 | for (item = to; item < to_last; item++) { |
| 709 | struct commit *c = *item; |
| 710 | |
| 711 | parse_commit(c); |
| 712 | if (c->generation < min_generation) |
| 713 | min_generation = c->generation; |
| 714 | |
| 715 | if (!(c->object.flags & PARENT1)) { |
| 716 | c->object.flags |= PARENT1; |
| 717 | num_to_find++; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | for (item = from; item < from_last; item++) { |
| 722 | struct commit *c = *item; |
| 723 | if (!(c->object.flags & PARENT2)) { |
| 724 | c->object.flags |= PARENT2; |
| 725 | parse_commit(c); |
| 726 | |
| 727 | prio_queue_put(&queue, *item); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | while (num_to_find && (current = prio_queue_get(&queue)) != NULL) { |
| 732 | struct commit_list *parents; |
| 733 | |
| 734 | if (current->object.flags & PARENT1) { |
| 735 | current->object.flags &= ~PARENT1; |
| 736 | current->object.flags |= reachable_flag; |
| 737 | commit_list_insert(current, &found_commits); |
| 738 | num_to_find--; |
| 739 | } |
| 740 | |
| 741 | for (parents = current->parents; parents; parents = parents->next) { |
| 742 | struct commit *p = parents->item; |
| 743 | |
| 744 | parse_commit(p); |
| 745 | |
| 746 | if (p->generation < min_generation) |
| 747 | continue; |
| 748 | |
| 749 | if (p->object.flags & PARENT2) |
| 750 | continue; |
| 751 | |
| 752 | p->object.flags |= PARENT2; |
| 753 | prio_queue_put(&queue, p); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | clear_commit_marks_many(nr_to, to, PARENT1); |
| 758 | clear_commit_marks_many(nr_from, from, PARENT2); |
| 759 | |
| 760 | return found_commits; |
| 761 | } |