Skip to content

Commit 21583d7

Browse files
committed
merge-ort: populate caches of rename detection results
Fill in cache_pairs, cached_target_names, and cached_irrelevant based on rename detection results. Future commits will make use of these values. Signed-off-by: Elijah Newren <[email protected]>
1 parent 272174d commit 21583d7

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

merge-ort.c

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,6 +2271,47 @@ static void resolve_diffpair_statuses(struct diff_queue_struct *q)
22712271
}
22722272
}
22732273

2274+
static void possibly_cache_new_pair(struct rename_info *renames,
2275+
struct diff_filepair *p,
2276+
unsigned side,
2277+
char *new_path)
2278+
{
2279+
char *old_value;
2280+
2281+
if (!new_path) {
2282+
int val = strintmap_get(&renames->relevant_sources[side],
2283+
p->one->path);
2284+
if (val == 0) {
2285+
assert(p->status == 'D');
2286+
strset_add(&renames->cached_irrelevant[side],
2287+
p->one->path);
2288+
}
2289+
if (val <= 0)
2290+
return;
2291+
}
2292+
if (p->status == 'D') {
2293+
/*
2294+
* If we already had this delete, we'll just set it's value
2295+
* to NULL again, so no harm.
2296+
*/
2297+
strmap_put(&renames->cached_pairs[side], p->one->path, NULL);
2298+
} else if (p->status == 'R') {
2299+
if (!new_path)
2300+
new_path = p->two->path;
2301+
new_path = xstrdup(new_path);
2302+
old_value = strmap_put(&renames->cached_pairs[side],
2303+
p->one->path, new_path);
2304+
strset_add(&renames->cached_target_names[side], new_path);
2305+
free(old_value);
2306+
} else if (p->status == 'A' && new_path) {
2307+
new_path = xstrdup(new_path);
2308+
old_value = strmap_put(&renames->cached_pairs[side],
2309+
p->two->path, new_path);
2310+
strset_add(&renames->cached_target_names[side], new_path);
2311+
assert(!old_value);
2312+
}
2313+
}
2314+
22742315
static int compare_pairs(const void *a_, const void *b_)
22752316
{
22762317
const struct diff_filepair *a = *((const struct diff_filepair **)a_);
@@ -2352,6 +2393,7 @@ static int collect_renames(struct merge_options *opt,
23522393
struct diff_filepair *p = side_pairs->queue[i];
23532394
char *new_path; /* non-NULL only with directory renames */
23542395

2396+
possibly_cache_new_pair(renames, p, side_index, NULL);
23552397
if (p->status != 'A' && p->status != 'R') {
23562398
diff_free_filepair(p);
23572399
continue;
@@ -2368,7 +2410,7 @@ static int collect_renames(struct merge_options *opt,
23682410
diff_free_filepair(p);
23692411
continue;
23702412
}
2371-
2413+
possibly_cache_new_pair(renames, p, side_index, new_path);
23722414
if (new_path)
23732415
apply_directory_rename_modifications(opt, p, new_path);
23742416

@@ -3532,8 +3574,16 @@ static void merge_start(struct merge_options *opt, struct merge_result *result)
35323574
NULL, 1);
35333575
strmap_init_with_options(&renames->dir_renames[i],
35343576
NULL, 0);
3577+
/*
3578+
* relevant_sources uses -1 for the default, because we need
3579+
* to be able to distinguish not-in-strintmap from valid
3580+
* relevant_source values from enum file_rename_relevance.
3581+
* In particular, possibly_cache_new_pair() expects a negative
3582+
* value for not-found entries.
3583+
*/
35353584
strintmap_init_with_options(&renames->relevant_sources[i],
3536-
0, NULL, 0);
3585+
-1 /* explicitly invalid */,
3586+
NULL, 0);
35373587
strmap_init_with_options(&renames->cached_pairs[i],
35383588
NULL, 1);
35393589
strset_init_with_options(&renames->cached_irrelevant[i],

0 commit comments

Comments
 (0)