Skip to content

Commit 79d49b7

Browse files
newrengitster
authored andcommitted
merge-recursive: check for file level conflicts then get new name
Before trying to apply directory renames to paths within the given directories, we want to make sure that there aren't conflicts at the file level either. If there aren't any, then get the new name from any directory renames. Reviewed-by: Stefan Beller <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ea625cb commit 79d49b7

File tree

4 files changed

+199
-9
lines changed

4 files changed

+199
-9
lines changed

merge-recursive.c

Lines changed: 166 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,91 @@ static void remove_hashmap_entries(struct hashmap *dir_renames,
15101510
string_list_clear(items_to_remove, 0);
15111511
}
15121512

1513+
/*
1514+
* See if there is a directory rename for path, and if there are any file
1515+
* level conflicts for the renamed location. If there is a rename and
1516+
* there are no conflicts, return the new name. Otherwise, return NULL.
1517+
*/
1518+
static char *handle_path_level_conflicts(struct merge_options *o,
1519+
const char *path,
1520+
struct dir_rename_entry *entry,
1521+
struct hashmap *collisions,
1522+
struct tree *tree)
1523+
{
1524+
char *new_path = NULL;
1525+
struct collision_entry *collision_ent;
1526+
int clean = 1;
1527+
struct strbuf collision_paths = STRBUF_INIT;
1528+
1529+
/*
1530+
* entry has the mapping of old directory name to new directory name
1531+
* that we want to apply to path.
1532+
*/
1533+
new_path = apply_dir_rename(entry, path);
1534+
1535+
if (!new_path) {
1536+
/* This should only happen when entry->non_unique_new_dir set */
1537+
if (!entry->non_unique_new_dir)
1538+
BUG("entry->non_unqiue_dir not set and !new_path");
1539+
output(o, 1, _("CONFLICT (directory rename split): "
1540+
"Unclear where to place %s because directory "
1541+
"%s was renamed to multiple other directories, "
1542+
"with no destination getting a majority of the "
1543+
"files."),
1544+
path, entry->dir);
1545+
clean = 0;
1546+
return NULL;
1547+
}
1548+
1549+
/*
1550+
* The caller needs to have ensured that it has pre-populated
1551+
* collisions with all paths that map to new_path. Do a quick check
1552+
* to ensure that's the case.
1553+
*/
1554+
collision_ent = collision_find_entry(collisions, new_path);
1555+
if (collision_ent == NULL)
1556+
BUG("collision_ent is NULL");
1557+
1558+
/*
1559+
* Check for one-sided add/add/.../add conflicts, i.e.
1560+
* where implicit renames from the other side doing
1561+
* directory rename(s) can affect this side of history
1562+
* to put multiple paths into the same location. Warn
1563+
* and bail on directory renames for such paths.
1564+
*/
1565+
if (collision_ent->reported_already) {
1566+
clean = 0;
1567+
} else if (tree_has_path(tree, new_path)) {
1568+
collision_ent->reported_already = 1;
1569+
strbuf_add_separated_string_list(&collision_paths, ", ",
1570+
&collision_ent->source_files);
1571+
output(o, 1, _("CONFLICT (implicit dir rename): Existing "
1572+
"file/dir at %s in the way of implicit "
1573+
"directory rename(s) putting the following "
1574+
"path(s) there: %s."),
1575+
new_path, collision_paths.buf);
1576+
clean = 0;
1577+
} else if (collision_ent->source_files.nr > 1) {
1578+
collision_ent->reported_already = 1;
1579+
strbuf_add_separated_string_list(&collision_paths, ", ",
1580+
&collision_ent->source_files);
1581+
output(o, 1, _("CONFLICT (implicit dir rename): Cannot map "
1582+
"more than one path to %s; implicit directory "
1583+
"renames tried to put these paths there: %s"),
1584+
new_path, collision_paths.buf);
1585+
clean = 0;
1586+
}
1587+
1588+
/* Free memory we no longer need */
1589+
strbuf_release(&collision_paths);
1590+
if (!clean && new_path) {
1591+
free(new_path);
1592+
return NULL;
1593+
}
1594+
1595+
return new_path;
1596+
}
1597+
15131598
/*
15141599
* There are a couple things we want to do at the directory level:
15151600
* 1. Check for both sides renaming to the same thing, in order to avoid
@@ -1789,6 +1874,59 @@ static void compute_collisions(struct hashmap *collisions,
17891874
}
17901875
}
17911876

1877+
static char *check_for_directory_rename(struct merge_options *o,
1878+
const char *path,
1879+
struct tree *tree,
1880+
struct hashmap *dir_renames,
1881+
struct hashmap *dir_rename_exclusions,
1882+
struct hashmap *collisions,
1883+
int *clean_merge)
1884+
{
1885+
char *new_path = NULL;
1886+
struct dir_rename_entry *entry = check_dir_renamed(path, dir_renames);
1887+
struct dir_rename_entry *oentry = NULL;
1888+
1889+
if (!entry)
1890+
return new_path;
1891+
1892+
/*
1893+
* This next part is a little weird. We do not want to do an
1894+
* implicit rename into a directory we renamed on our side, because
1895+
* that will result in a spurious rename/rename(1to2) conflict. An
1896+
* example:
1897+
* Base commit: dumbdir/afile, otherdir/bfile
1898+
* Side 1: smrtdir/afile, otherdir/bfile
1899+
* Side 2: dumbdir/afile, dumbdir/bfile
1900+
* Here, while working on Side 1, we could notice that otherdir was
1901+
* renamed/merged to dumbdir, and change the diff_filepair for
1902+
* otherdir/bfile into a rename into dumbdir/bfile. However, Side
1903+
* 2 will notice the rename from dumbdir to smrtdir, and do the
1904+
* transitive rename to move it from dumbdir/bfile to
1905+
* smrtdir/bfile. That gives us bfile in dumbdir vs being in
1906+
* smrtdir, a rename/rename(1to2) conflict. We really just want
1907+
* the file to end up in smrtdir. And the way to achieve that is
1908+
* to not let Side1 do the rename to dumbdir, since we know that is
1909+
* the source of one of our directory renames.
1910+
*
1911+
* That's why oentry and dir_rename_exclusions is here.
1912+
*
1913+
* As it turns out, this also prevents N-way transient rename
1914+
* confusion; See testcases 9c and 9d of t6043.
1915+
*/
1916+
oentry = dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);
1917+
if (oentry) {
1918+
output(o, 1, _("WARNING: Avoiding applying %s -> %s rename "
1919+
"to %s, because %s itself was renamed."),
1920+
entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);
1921+
} else {
1922+
new_path = handle_path_level_conflicts(o, path, entry,
1923+
collisions, tree);
1924+
*clean_merge &= (new_path != NULL);
1925+
}
1926+
1927+
return new_path;
1928+
}
1929+
17921930
/*
17931931
* Get information of all renames which occurred in 'pairs', making use of
17941932
* any implicit directory renames inferred from the other side of history.
@@ -1799,11 +1937,13 @@ static void compute_collisions(struct hashmap *collisions,
17991937
static struct string_list *get_renames(struct merge_options *o,
18001938
struct diff_queue_struct *pairs,
18011939
struct hashmap *dir_renames,
1940+
struct hashmap *dir_rename_exclusions,
18021941
struct tree *tree,
18031942
struct tree *o_tree,
18041943
struct tree *a_tree,
18051944
struct tree *b_tree,
1806-
struct string_list *entries)
1945+
struct string_list *entries,
1946+
int *clean_merge)
18071947
{
18081948
int i;
18091949
struct hashmap collisions;
@@ -1818,11 +1958,22 @@ static struct string_list *get_renames(struct merge_options *o,
18181958
struct string_list_item *item;
18191959
struct rename *re;
18201960
struct diff_filepair *pair = pairs->queue[i];
1961+
char *new_path; /* non-NULL only with directory renames */
18211962

1822-
if (pair->status != 'R') {
1963+
if (pair->status == 'D') {
1964+
diff_free_filepair(pair);
1965+
continue;
1966+
}
1967+
new_path = check_for_directory_rename(o, pair->two->path, tree,
1968+
dir_renames,
1969+
dir_rename_exclusions,
1970+
&collisions,
1971+
clean_merge);
1972+
if (pair->status != 'R' && !new_path) {
18231973
diff_free_filepair(pair);
18241974
continue;
18251975
}
1976+
18261977
re = xmalloc(sizeof(*re));
18271978
re->processed = 0;
18281979
re->pair = pair;
@@ -2140,7 +2291,7 @@ static int handle_renames(struct merge_options *o,
21402291
{
21412292
struct diff_queue_struct *head_pairs, *merge_pairs;
21422293
struct hashmap *dir_re_head, *dir_re_merge;
2143-
int clean;
2294+
int clean = 1;
21442295

21452296
ri->head_renames = NULL;
21462297
ri->merge_renames = NULL;
@@ -2159,13 +2310,20 @@ static int handle_renames(struct merge_options *o,
21592310
dir_re_merge, merge);
21602311

21612312
ri->head_renames = get_renames(o, head_pairs,
2162-
dir_re_merge, head,
2163-
common, head, merge, entries);
2313+
dir_re_merge, dir_re_head, head,
2314+
common, head, merge, entries,
2315+
&clean);
2316+
if (clean < 0)
2317+
goto cleanup;
21642318
ri->merge_renames = get_renames(o, merge_pairs,
2165-
dir_re_head, merge,
2166-
common, head, merge, entries);
2167-
clean = process_renames(o, ri->head_renames, ri->merge_renames);
2319+
dir_re_head, dir_re_merge, merge,
2320+
common, head, merge, entries,
2321+
&clean);
2322+
if (clean < 0)
2323+
goto cleanup;
2324+
clean &= process_renames(o, ri->head_renames, ri->merge_renames);
21682325

2326+
cleanup:
21692327
/*
21702328
* Some cleanup is deferred until cleanup_renames() because the
21712329
* data structures are still needed and referenced in

strbuf.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "refs.h"
3+
#include "string-list.h"
34
#include "utf8.h"
45

56
int starts_with(const char *str, const char *prefix)
@@ -163,6 +164,21 @@ struct strbuf **strbuf_split_buf(const char *str, size_t slen,
163164
return ret;
164165
}
165166

167+
void strbuf_add_separated_string_list(struct strbuf *str,
168+
const char *sep,
169+
struct string_list *slist)
170+
{
171+
struct string_list_item *item;
172+
int sep_needed = 0;
173+
174+
for_each_string_list_item(item, slist) {
175+
if (sep_needed)
176+
strbuf_addstr(str, sep);
177+
strbuf_addstr(str, item->string);
178+
sep_needed = 1;
179+
}
180+
}
181+
166182
void strbuf_list_free(struct strbuf **sbs)
167183
{
168184
struct strbuf **s = sbs;

strbuf.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef STRBUF_H
22
#define STRBUF_H
33

4+
struct string_list;
5+
46
/**
57
* strbuf's are meant to be used with all the usual C string and memory
68
* APIs. Given that the length of the buffer is known, it's often better to
@@ -528,6 +530,20 @@ static inline struct strbuf **strbuf_split(const struct strbuf *sb,
528530
return strbuf_split_max(sb, terminator, 0);
529531
}
530532

533+
/*
534+
* Adds all strings of a string list to the strbuf, separated by the given
535+
* separator. For example, if sep is
536+
* ', '
537+
* and slist contains
538+
* ['element1', 'element2', ..., 'elementN'],
539+
* then write:
540+
* 'element1, element2, ..., elementN'
541+
* to str. If only one element, just write "element1" to str.
542+
*/
543+
extern void strbuf_add_separated_string_list(struct strbuf *str,
544+
const char *sep,
545+
struct string_list *slist);
546+
531547
/**
532548
* Free a NULL-terminated list of strbufs (for example, the return
533549
* values of the strbuf_split*() functions).

t/t6043-merge-rename-directories.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ test_expect_success '2a-setup: Directory split into two on one side, with equal
489489
)
490490
'
491491

492-
test_expect_failure '2a-check: Directory split into two on one side, with equal numbers of paths' '
492+
test_expect_success '2a-check: Directory split into two on one side, with equal numbers of paths' '
493493
(
494494
cd 2a &&
495495

0 commit comments

Comments
 (0)