| 417 | char *(*cbm_gitignore_merge_dup_hook_for_test)(const char *) = NULL; |
| 418 | |
| 419 | bool cbm_gitignore_merge(cbm_gitignore_t *dst, const cbm_gitignore_t *src) { |
| 420 | if (!dst) { |
| 421 | return false; |
| 422 | } |
| 423 | if (!src || src->count == 0) { |
| 424 | return true; /* nothing to merge */ |
| 425 | } |
| 426 | int needed = dst->count + src->count; |
| 427 | if (needed > dst->capacity) { |
| 428 | gi_pattern_t *grown = realloc(dst->patterns, (size_t)needed * sizeof(gi_pattern_t)); |
| 429 | if (!grown) { |
| 430 | return false; /* dst left unchanged */ |
| 431 | } |
| 432 | dst->patterns = grown; |
| 433 | dst->capacity = needed; |
| 434 | } |
| 435 | int start_count = dst->count; |
| 436 | for (int i = 0; i < src->count; i++) { |
| 437 | char *pat = cbm_gitignore_merge_dup_hook_for_test |
| 438 | ? cbm_gitignore_merge_dup_hook_for_test(src->patterns[i].pattern) |
| 439 | : strdup(src->patterns[i].pattern); |
| 440 | if (!pat) { |
| 441 | /* Roll back partial copies so dst is unchanged on failure (atomic |
| 442 | * merge). A silent partial merge could drop the very exclude |
| 443 | * pattern the caller relied on while keeping others. */ |
| 444 | for (int j = start_count; j < dst->count; j++) { |
| 445 | free(dst->patterns[j].pattern); |
| 446 | } |
| 447 | dst->count = start_count; |
| 448 | return false; |
| 449 | } |
| 450 | dst->patterns[dst->count].pattern = pat; |
| 451 | dst->patterns[dst->count].negated = src->patterns[i].negated; |
| 452 | dst->patterns[dst->count].dir_only = src->patterns[i].dir_only; |
| 453 | dst->patterns[dst->count].rooted = src->patterns[i].rooted; |
| 454 | dst->count++; |
| 455 | } |
| 456 | return true; |
| 457 | } |
no outgoing calls
no test coverage detected