Skip to content

Commit e95ab70

Browse files
newrengitster
authored andcommitted
merge-recursive: add computation of collisions due to dir rename & merging
directory renaming and merging can cause one or more files to be moved to where an existing file is, or to cause several files to all be moved to the same (otherwise vacant) location. Add checking and reporting for such cases, falling back to no-directory-rename handling for such paths. Reviewed-by: Stefan Beller <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 96e7ffb commit e95ab70

File tree

2 files changed

+150
-3
lines changed

2 files changed

+150
-3
lines changed

merge-recursive.c

Lines changed: 143 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,29 @@ static void dir_rename_entry_init(struct dir_rename_entry *entry,
8787
string_list_init(&entry->possible_new_dirs, 0);
8888
}
8989

90+
static struct collision_entry *collision_find_entry(struct hashmap *hashmap,
91+
char *target_file)
92+
{
93+
struct collision_entry key;
94+
95+
hashmap_entry_init(&key, strhash(target_file));
96+
key.target_file = target_file;
97+
return hashmap_get(hashmap, &key, NULL);
98+
}
99+
100+
static int collision_cmp(void *unused_cmp_data,
101+
const struct collision_entry *e1,
102+
const struct collision_entry *e2,
103+
const void *unused_keydata)
104+
{
105+
return strcmp(e1->target_file, e2->target_file);
106+
}
107+
108+
static void collision_init(struct hashmap *map)
109+
{
110+
hashmap_init(map, (hashmap_cmp_fn) collision_cmp, NULL, 0);
111+
}
112+
90113
static void flush_output(struct merge_options *o)
91114
{
92115
if (o->buffer_output < 2 && o->obuf.len) {
@@ -1404,6 +1427,31 @@ static int tree_has_path(struct tree *tree, const char *path)
14041427
&hashy, &mode_o);
14051428
}
14061429

1430+
/*
1431+
* Return a new string that replaces the beginning portion (which matches
1432+
* entry->dir), with entry->new_dir. In perl-speak:
1433+
* new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);
1434+
* NOTE:
1435+
* Caller must ensure that old_path starts with entry->dir + '/'.
1436+
*/
1437+
static char *apply_dir_rename(struct dir_rename_entry *entry,
1438+
const char *old_path)
1439+
{
1440+
struct strbuf new_path = STRBUF_INIT;
1441+
int oldlen, newlen;
1442+
1443+
if (entry->non_unique_new_dir)
1444+
return NULL;
1445+
1446+
oldlen = strlen(entry->dir);
1447+
newlen = entry->new_dir.len + (strlen(old_path) - oldlen) + 1;
1448+
strbuf_grow(&new_path, newlen);
1449+
strbuf_addbuf(&new_path, &entry->new_dir);
1450+
strbuf_addstr(&new_path, &old_path[oldlen]);
1451+
1452+
return strbuf_detach(&new_path, NULL);
1453+
}
1454+
14071455
static void get_renamed_dir_portion(const char *old_path, const char *new_path,
14081456
char **old_dir, char **new_dir)
14091457
{
@@ -1673,6 +1721,84 @@ static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs,
16731721
return dir_renames;
16741722
}
16751723

1724+
static struct dir_rename_entry *check_dir_renamed(const char *path,
1725+
struct hashmap *dir_renames)
1726+
{
1727+
char temp[PATH_MAX];
1728+
char *end;
1729+
struct dir_rename_entry *entry;
1730+
1731+
strcpy(temp, path);
1732+
while ((end = strrchr(temp, '/'))) {
1733+
*end = '\0';
1734+
entry = dir_rename_find_entry(dir_renames, temp);
1735+
if (entry)
1736+
return entry;
1737+
}
1738+
return NULL;
1739+
}
1740+
1741+
static void compute_collisions(struct hashmap *collisions,
1742+
struct hashmap *dir_renames,
1743+
struct diff_queue_struct *pairs)
1744+
{
1745+
int i;
1746+
1747+
/*
1748+
* Multiple files can be mapped to the same path due to directory
1749+
* renames done by the other side of history. Since that other
1750+
* side of history could have merged multiple directories into one,
1751+
* if our side of history added the same file basename to each of
1752+
* those directories, then all N of them would get implicitly
1753+
* renamed by the directory rename detection into the same path,
1754+
* and we'd get an add/add/.../add conflict, and all those adds
1755+
* from *this* side of history. This is not representable in the
1756+
* index, and users aren't going to easily be able to make sense of
1757+
* it. So we need to provide a good warning about what's
1758+
* happening, and fall back to no-directory-rename detection
1759+
* behavior for those paths.
1760+
*
1761+
* See testcases 9e and all of section 5 from t6043 for examples.
1762+
*/
1763+
collision_init(collisions);
1764+
1765+
for (i = 0; i < pairs->nr; ++i) {
1766+
struct dir_rename_entry *dir_rename_ent;
1767+
struct collision_entry *collision_ent;
1768+
char *new_path;
1769+
struct diff_filepair *pair = pairs->queue[i];
1770+
1771+
if (pair->status == 'D')
1772+
continue;
1773+
dir_rename_ent = check_dir_renamed(pair->two->path,
1774+
dir_renames);
1775+
if (!dir_rename_ent)
1776+
continue;
1777+
1778+
new_path = apply_dir_rename(dir_rename_ent, pair->two->path);
1779+
if (!new_path)
1780+
/*
1781+
* dir_rename_ent->non_unique_new_path is true, which
1782+
* means there is no directory rename for us to use,
1783+
* which means it won't cause us any additional
1784+
* collisions.
1785+
*/
1786+
continue;
1787+
collision_ent = collision_find_entry(collisions, new_path);
1788+
if (!collision_ent) {
1789+
collision_ent = xcalloc(1,
1790+
sizeof(struct collision_entry));
1791+
hashmap_entry_init(collision_ent, strhash(new_path));
1792+
hashmap_put(collisions, collision_ent);
1793+
collision_ent->target_file = new_path;
1794+
} else {
1795+
free(new_path);
1796+
}
1797+
string_list_insert(&collision_ent->source_files,
1798+
pair->two->path);
1799+
}
1800+
}
1801+
16761802
/*
16771803
* Get information of all renames which occurred in 'pairs', making use of
16781804
* any implicit directory renames inferred from the other side of history.
@@ -1682,15 +1808,20 @@ static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs,
16821808
*/
16831809
static struct string_list *get_renames(struct merge_options *o,
16841810
struct diff_queue_struct *pairs,
1811+
struct hashmap *dir_renames,
16851812
struct tree *tree,
16861813
struct tree *o_tree,
16871814
struct tree *a_tree,
16881815
struct tree *b_tree,
16891816
struct string_list *entries)
16901817
{
16911818
int i;
1819+
struct hashmap collisions;
1820+
struct hashmap_iter iter;
1821+
struct collision_entry *e;
16921822
struct string_list *renames;
16931823

1824+
compute_collisions(&collisions, dir_renames, pairs);
16941825
renames = xcalloc(1, sizeof(struct string_list));
16951826

16961827
for (i = 0; i < pairs->nr; ++i) {
@@ -1721,6 +1852,13 @@ static struct string_list *get_renames(struct merge_options *o,
17211852
item = string_list_insert(renames, pair->one->path);
17221853
item->util = re;
17231854
}
1855+
1856+
hashmap_iter_init(&collisions, &iter);
1857+
while ((e = hashmap_iter_next(&iter))) {
1858+
free(e->target_file);
1859+
string_list_clear(&e->source_files, 0);
1860+
}
1861+
hashmap_free(&collisions, 1);
17241862
return renames;
17251863
}
17261864

@@ -2030,9 +2168,11 @@ static int handle_renames(struct merge_options *o,
20302168
dir_re_head, head,
20312169
dir_re_merge, merge);
20322170

2033-
ri->head_renames = get_renames(o, head_pairs, head,
2034-
common, head, merge, entries);
2035-
ri->merge_renames = get_renames(o, merge_pairs, merge,
2171+
ri->head_renames = get_renames(o, head_pairs,
2172+
dir_re_merge, head,
2173+
common, head, merge, entries);
2174+
ri->merge_renames = get_renames(o, merge_pairs,
2175+
dir_re_head, merge,
20362176
common, head, merge, entries);
20372177
clean = process_renames(o, ri->head_renames, ri->merge_renames);
20382178

merge-recursive.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ struct dir_rename_entry {
4747
struct string_list possible_new_dirs;
4848
};
4949

50+
struct collision_entry {
51+
struct hashmap_entry ent; /* must be the first member! */
52+
char *target_file;
53+
struct string_list source_files;
54+
unsigned reported_already:1;
55+
};
56+
5057
/* merge_trees() but with recursive ancestor consolidation */
5158
int merge_recursive(struct merge_options *o,
5259
struct commit *h1,

0 commit comments

Comments
 (0)