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 <dstolee@microsoft.com> Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Nov 7, 2018 at 20:40 UTC 80cee6e3210120a8a45fc4818e0049f482247b0f
1 file changed +27 -26
merge-recursive.c
+27 -26
@@ -1709,6 +1709,27 @@ static int handle_rename_add(struct merge_options *o,
1709 ci->dst_entry1->stages[other_stage].mode);
1710 }
1711
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 +
1733 static int handle_rename_rename_1to2(struct merge_options *o,
1734 struct rename_conflict_info *ci)
1735 {
@@ -1783,19 +1804,9 @@ static int handle_rename_rename_1to2(struct merge_options *o,
1804 &add->oid, add->mode) < 0)
1805 return -1;
1806 } 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);
1810 if (update_file(o, 0, &mfi.oid, mfi.mode, new_path ? new_path : a->path))
1811 return -1;
1812 free(new_path);
@@ -1812,19 +1823,9 @@ static int handle_rename_rename_1to2(struct merge_options *o,
1823 &mfi.oid, mfi.mode) < 0)
1824 return -1;
1825 } 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);
1829 if (update_file(o, 0, &mfi.oid, mfi.mode, new_path ? new_path : b->path))
1830 return -1;
1831 free(new_path);