Skip to content

Commit 80cee6e

Browse files
derrickstoleegitster
authored andcommitted
merge-recursive: combine error handling
In handle_rename_rename_1to2(), we have duplicated error handling around colliding paths. Specifically, when we want to write out the file and there is a directory or untracked file in the way, we need to create a temporary file to hold the contents. This has some special output to alert the user, and this output is duplicated for each side of the conflict. Simplify the call by generating this new path in a helper function. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b8cd1bb commit 80cee6e

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

merge-recursive.c

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1709,6 +1709,27 @@ static int handle_rename_add(struct merge_options *o,
17091709
ci->dst_entry1->stages[other_stage].mode);
17101710
}
17111711

1712+
static char *find_path_for_conflict(struct merge_options *o,
1713+
const char *path,
1714+
const char *branch1,
1715+
const char *branch2)
1716+
{
1717+
char *new_path = NULL;
1718+
if (dir_in_way(path, !o->call_depth, 0)) {
1719+
new_path = unique_path(o, path, branch1);
1720+
output(o, 1, _("%s is a directory in %s adding "
1721+
"as %s instead"),
1722+
path, branch2, new_path);
1723+
} else if (would_lose_untracked(path)) {
1724+
new_path = unique_path(o, path, branch1);
1725+
output(o, 1, _("Refusing to lose untracked file"
1726+
" at %s; adding as %s instead"),
1727+
path, new_path);
1728+
}
1729+
1730+
return new_path;
1731+
}
1732+
17121733
static int handle_rename_rename_1to2(struct merge_options *o,
17131734
struct rename_conflict_info *ci)
17141735
{
@@ -1783,19 +1804,9 @@ static int handle_rename_rename_1to2(struct merge_options *o,
17831804
&add->oid, add->mode) < 0)
17841805
return -1;
17851806
} else {
1786-
char *new_path = NULL;
1787-
if (dir_in_way(a->path, !o->call_depth, 0)) {
1788-
new_path = unique_path(o, a->path, ci->branch1);
1789-
output(o, 1, _("%s is a directory in %s adding "
1790-
"as %s instead"),
1791-
a->path, ci->branch2, new_path);
1792-
} else if (would_lose_untracked(a->path)) {
1793-
new_path = unique_path(o, a->path, ci->branch1);
1794-
output(o, 1, _("Refusing to lose untracked file"
1795-
" at %s; adding as %s instead"),
1796-
a->path, new_path);
1797-
}
1798-
1807+
char *new_path = find_path_for_conflict(o, a->path,
1808+
ci->branch1,
1809+
ci->branch2);
17991810
if (update_file(o, 0, &mfi.oid, mfi.mode, new_path ? new_path : a->path))
18001811
return -1;
18011812
free(new_path);
@@ -1812,19 +1823,9 @@ static int handle_rename_rename_1to2(struct merge_options *o,
18121823
&mfi.oid, mfi.mode) < 0)
18131824
return -1;
18141825
} else {
1815-
char *new_path = NULL;
1816-
if (dir_in_way(b->path, !o->call_depth, 0)) {
1817-
new_path = unique_path(o, b->path, ci->branch2);
1818-
output(o, 1, _("%s is a directory in %s adding "
1819-
"as %s instead"),
1820-
b->path, ci->branch1, new_path);
1821-
} else if (would_lose_untracked(b->path)) {
1822-
new_path = unique_path(o, b->path, ci->branch2);
1823-
output(o, 1, _("Refusing to lose untracked file"
1824-
" at %s; adding as %s instead"),
1825-
b->path, new_path);
1826-
}
1827-
1826+
char *new_path = find_path_for_conflict(o, b->path,
1827+
ci->branch2,
1828+
ci->branch1);
18281829
if (update_file(o, 0, &mfi.oid, mfi.mode, new_path ? new_path : b->path))
18291830
return -1;
18301831
free(new_path);

0 commit comments

Comments
 (0)