Skip to content

Commit e5c5e34

Browse files
committed
Merge branch 'en/merge-dual-dir-renames-fix' into maint
Fixes a long-standing corner case bug around directory renames in the merge-ort strategy. source: <[email protected]> * en/merge-dual-dir-renames-fix: merge-ort: fix issue with dual rename and add/add conflict merge-ort: shuffle the computation and cleanup of potential collisions merge-ort: make a separate function for freeing struct collisions merge-ort: small cleanups of check_for_directory_rename t6423: add tests of dual directory rename plus add/add conflict
2 parents 494d31e + 751e165 commit e5c5e34

File tree

2 files changed

+153
-26
lines changed

2 files changed

+153
-26
lines changed

merge-ort.c

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,6 +2260,27 @@ static void compute_collisions(struct strmap *collisions,
22602260
}
22612261
}
22622262

2263+
static void free_collisions(struct strmap *collisions)
2264+
{
2265+
struct hashmap_iter iter;
2266+
struct strmap_entry *entry;
2267+
2268+
/* Free each value in the collisions map */
2269+
strmap_for_each_entry(collisions, &iter, entry) {
2270+
struct collision_info *info = entry->value;
2271+
string_list_clear(&info->source_files, 0);
2272+
}
2273+
/*
2274+
* In compute_collisions(), we set collisions.strdup_strings to 0
2275+
* so that we wouldn't have to make another copy of the new_path
2276+
* allocated by apply_dir_rename(). But now that we've used them
2277+
* and have no other references to these strings, it is time to
2278+
* deallocate them.
2279+
*/
2280+
free_strmap_strings(collisions);
2281+
strmap_clear(collisions, 1);
2282+
}
2283+
22632284
static char *check_for_directory_rename(struct merge_options *opt,
22642285
const char *path,
22652286
unsigned side_index,
@@ -2268,18 +2289,23 @@ static char *check_for_directory_rename(struct merge_options *opt,
22682289
struct strmap *collisions,
22692290
int *clean_merge)
22702291
{
2271-
char *new_path = NULL;
2292+
char *new_path;
22722293
struct strmap_entry *rename_info;
2273-
struct strmap_entry *otherinfo = NULL;
2294+
struct strmap_entry *otherinfo;
22742295
const char *new_dir;
2296+
int other_side = 3 - side_index;
22752297

2298+
/*
2299+
* Cases where we don't have or don't want a directory rename for
2300+
* this path.
2301+
*/
22762302
if (strmap_empty(dir_renames))
2277-
return new_path;
2303+
return NULL;
2304+
if (strmap_get(&collisions[other_side], path))
2305+
return NULL;
22782306
rename_info = check_dir_renamed(path, dir_renames);
22792307
if (!rename_info)
2280-
return new_path;
2281-
/* old_dir = rename_info->key; */
2282-
new_dir = rename_info->value;
2308+
return NULL;
22832309

22842310
/*
22852311
* This next part is a little weird. We do not want to do an
@@ -2305,6 +2331,7 @@ static char *check_for_directory_rename(struct merge_options *opt,
23052331
* As it turns out, this also prevents N-way transient rename
23062332
* confusion; See testcases 9c and 9d of t6043.
23072333
*/
2334+
new_dir = rename_info->value; /* old_dir = rename_info->key; */
23082335
otherinfo = strmap_get_entry(dir_rename_exclusions, new_dir);
23092336
if (otherinfo) {
23102337
path_msg(opt, rename_info->key, 1,
@@ -2315,7 +2342,8 @@ static char *check_for_directory_rename(struct merge_options *opt,
23152342
}
23162343

23172344
new_path = handle_path_level_conflicts(opt, path, side_index,
2318-
rename_info, collisions);
2345+
rename_info,
2346+
&collisions[side_index]);
23192347
*clean_merge &= (new_path != NULL);
23202348

23212349
return new_path;
@@ -3024,18 +3052,15 @@ static int detect_regular_renames(struct merge_options *opt,
30243052
static int collect_renames(struct merge_options *opt,
30253053
struct diff_queue_struct *result,
30263054
unsigned side_index,
3055+
struct strmap *collisions,
30273056
struct strmap *dir_renames_for_side,
30283057
struct strmap *rename_exclusions)
30293058
{
30303059
int i, clean = 1;
3031-
struct strmap collisions;
30323060
struct diff_queue_struct *side_pairs;
3033-
struct hashmap_iter iter;
3034-
struct strmap_entry *entry;
30353061
struct rename_info *renames = &opt->priv->renames;
30363062

30373063
side_pairs = &renames->pairs[side_index];
3038-
compute_collisions(&collisions, dir_renames_for_side, side_pairs);
30393064

30403065
for (i = 0; i < side_pairs->nr; ++i) {
30413066
struct diff_filepair *p = side_pairs->queue[i];
@@ -3051,7 +3076,7 @@ static int collect_renames(struct merge_options *opt,
30513076
side_index,
30523077
dir_renames_for_side,
30533078
rename_exclusions,
3054-
&collisions,
3079+
collisions,
30553080
&clean);
30563081

30573082
possibly_cache_new_pair(renames, p, side_index, new_path);
@@ -3077,20 +3102,6 @@ static int collect_renames(struct merge_options *opt,
30773102
result->queue[result->nr++] = p;
30783103
}
30793104

3080-
/* Free each value in the collisions map */
3081-
strmap_for_each_entry(&collisions, &iter, entry) {
3082-
struct collision_info *info = entry->value;
3083-
string_list_clear(&info->source_files, 0);
3084-
}
3085-
/*
3086-
* In compute_collisions(), we set collisions.strdup_strings to 0
3087-
* so that we wouldn't have to make another copy of the new_path
3088-
* allocated by apply_dir_rename(). But now that we've used them
3089-
* and have no other references to these strings, it is time to
3090-
* deallocate them.
3091-
*/
3092-
free_strmap_strings(&collisions);
3093-
strmap_clear(&collisions, 1);
30943105
return clean;
30953106
}
30963107

@@ -3101,6 +3112,7 @@ static int detect_and_process_renames(struct merge_options *opt,
31013112
{
31023113
struct diff_queue_struct combined = { 0 };
31033114
struct rename_info *renames = &opt->priv->renames;
3115+
struct strmap collisions[3];
31043116
int need_dir_renames, s, i, clean = 1;
31053117
unsigned detection_run = 0;
31063118

@@ -3150,12 +3162,22 @@ static int detect_and_process_renames(struct merge_options *opt,
31503162
ALLOC_GROW(combined.queue,
31513163
renames->pairs[1].nr + renames->pairs[2].nr,
31523164
combined.alloc);
3165+
for (i = MERGE_SIDE1; i <= MERGE_SIDE2; i++) {
3166+
int other_side = 3 - i;
3167+
compute_collisions(&collisions[i],
3168+
&renames->dir_renames[other_side],
3169+
&renames->pairs[i]);
3170+
}
31533171
clean &= collect_renames(opt, &combined, MERGE_SIDE1,
3172+
collisions,
31543173
&renames->dir_renames[2],
31553174
&renames->dir_renames[1]);
31563175
clean &= collect_renames(opt, &combined, MERGE_SIDE2,
3176+
collisions,
31573177
&renames->dir_renames[1],
31583178
&renames->dir_renames[2]);
3179+
for (i = MERGE_SIDE1; i <= MERGE_SIDE2; i++)
3180+
free_collisions(&collisions[i]);
31593181
STABLE_QSORT(combined.queue, combined.nr, compare_pairs);
31603182
trace2_region_leave("merge", "directory renames", opt->repo);
31613183

t/t6423-merge-rename-directories.sh

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5199,6 +5199,111 @@ test_expect_success '12k: Directory rename with sibling causes rename-to-self' '
51995199
)
52005200
'
52015201

5202+
# Testcase 12l, Both sides rename a directory into the other side, both add
5203+
# a file which after directory renames are the same filename
5204+
# Commit O: sub1/file, sub2/other
5205+
# Commit A: sub3/file, sub2/{other, new_add_add_file_1}
5206+
# Commit B: sub1/{file, newfile}, sub1/sub2/{other, new_add_add_file_2}
5207+
#
5208+
# In words:
5209+
# A: sub1/ -> sub3/, add sub2/new_add_add_file_1
5210+
# B: sub2/ -> sub1/sub2, add sub1/newfile, add sub1/sub2/new_add_add_file_2
5211+
#
5212+
# Expected: sub3/{file, newfile, sub2/other}
5213+
# CONFLICT (add/add): sub1/sub2/new_add_add_file
5214+
#
5215+
# Note that sub1/newfile is not extraneous. Directory renames are only
5216+
# detected if they are needed, and they are only needed if the old directory
5217+
# had a new file added on the opposite side of history. So sub1/newfile
5218+
# is needed for there to be a sub1/ -> sub3/ rename.
5219+
5220+
test_setup_12l () {
5221+
test_create_repo 12l_$1 &&
5222+
(
5223+
cd 12l_$1 &&
5224+
5225+
mkdir sub1 sub2
5226+
echo file >sub1/file &&
5227+
echo other >sub2/other &&
5228+
git add sub1 sub2 &&
5229+
git commit -m "O" &&
5230+
5231+
git branch O &&
5232+
git branch A &&
5233+
git branch B &&
5234+
5235+
git checkout A &&
5236+
git mv sub1 sub3 &&
5237+
echo conflicting >sub2/new_add_add_file &&
5238+
git add sub2 &&
5239+
test_tick &&
5240+
git add -u &&
5241+
git commit -m "A" &&
5242+
5243+
git checkout B &&
5244+
echo dissimilar >sub2/new_add_add_file &&
5245+
echo brand >sub1/newfile &&
5246+
git add sub1 sub2 &&
5247+
git mv sub2 sub1 &&
5248+
test_tick &&
5249+
git commit -m "B"
5250+
)
5251+
}
5252+
5253+
test_expect_merge_algorithm failure success '12l (B into A): Rename into each other + add/add conflict' '
5254+
test_setup_12l BintoA &&
5255+
(
5256+
cd 12l_BintoA &&
5257+
5258+
git checkout -q A^0 &&
5259+
5260+
test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 &&
5261+
5262+
test_stdout_line_count = 5 git ls-files -s &&
5263+
5264+
git rev-parse >actual \
5265+
:0:sub3/file :0:sub3/newfile :0:sub3/sub2/other \
5266+
:2:sub1/sub2/new_add_add_file \
5267+
:3:sub1/sub2/new_add_add_file &&
5268+
git rev-parse >expect \
5269+
O:sub1/file B:sub1/newfile O:sub2/other \
5270+
A:sub2/new_add_add_file \
5271+
B:sub1/sub2/new_add_add_file &&
5272+
test_cmp expect actual &&
5273+
5274+
git ls-files -o >actual &&
5275+
test_write_lines actual expect >expect &&
5276+
test_cmp expect actual
5277+
)
5278+
'
5279+
5280+
test_expect_merge_algorithm failure success '12l (A into B): Rename into each other + add/add conflict' '
5281+
test_setup_12l AintoB &&
5282+
(
5283+
cd 12l_AintoB &&
5284+
5285+
git checkout -q B^0 &&
5286+
5287+
test_must_fail git -c merge.directoryRenames=true merge -s recursive A^0 &&
5288+
5289+
test_stdout_line_count = 5 git ls-files -s &&
5290+
5291+
git rev-parse >actual \
5292+
:0:sub3/file :0:sub3/newfile :0:sub3/sub2/other \
5293+
:2:sub1/sub2/new_add_add_file \
5294+
:3:sub1/sub2/new_add_add_file &&
5295+
git rev-parse >expect \
5296+
O:sub1/file B:sub1/newfile O:sub2/other \
5297+
B:sub1/sub2/new_add_add_file \
5298+
A:sub2/new_add_add_file &&
5299+
test_cmp expect actual &&
5300+
5301+
git ls-files -o >actual &&
5302+
test_write_lines actual expect >expect &&
5303+
test_cmp expect actual
5304+
)
5305+
'
5306+
52025307
###########################################################################
52035308
# SECTION 13: Checking informational and conflict messages
52045309
#

0 commit comments

Comments
 (0)