merge: detect delete/modechange conflict

If one side deletes a file and the other changes its content, we notice and report a conflict. However, if instead of changing the content, we change only the mode, the merge does not notice (and the mode change is silently dropped). The trivial index merge notices the problem and correctly leaves the conflict in the index, but both merge-recursive and merge-one-file will silently resolve this in favor of the deletion. In many cases that is a sane resolution, but we should be punting to the user whenever there is any question. So let's detect and treat this as a conflict (in both strategies). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 26, 2015 at 17:39 UTC 72fac66bca302dbecbe42dfa0ddc7e42db2fe567
3 files changed +37 -2
git-merge-one-file.sh
+8
@@ -38,6 +38,14 @@ case "${1:-.}${2:-.}${3:-.}" in
38 # Deleted in both or deleted in one and unchanged in the other
39 #
40 "$1.." | "$1.$1" | "$1$1.")
41 + if { test -z "$6" && test "$5" != "$7"; } ||
42 + { test -z "$7" && test "$5" != "$6"; }
43 + then
44 + echo "ERROR: File $4 deleted on one branch but had its" >&2
45 + echo "ERROR: permissions changed on the other." >&2
46 + exit 1
47 + fi
48 +
49 if test -n "$2"
50 then
51 echo "Removing $4"
merge-recursive.c
+6 -2
@@ -1535,13 +1535,17 @@ static int read_sha1_strbuf(const unsigned char *sha1, struct strbuf *dst)
1535 }
1536
1537 static int blob_unchanged(const unsigned char *o_sha,
1538 + unsigned o_mode,
1539 const unsigned char *a_sha,
1540 + unsigned a_mode,
1541 int renormalize, const char *path)
1542 {
1543 struct strbuf o = STRBUF_INIT;
1544 struct strbuf a = STRBUF_INIT;
1545 int ret = 0; /* assume changed for safety */
1546
1547 + if (a_mode != o_mode)
1548 + return 0;
1549 if (sha_eq(o_sha, a_sha))
1550 return 1;
1551 if (!renormalize)
@@ -1727,8 +1731,8 @@ static int process_entry(struct merge_options *o,
1731 } else if (o_sha && (!a_sha || !b_sha)) {
1732 /* Case A: Deleted in one */
1733 if ((!a_sha && !b_sha) ||
1730 - (!b_sha && blob_unchanged(o_sha, a_sha, normalize, path)) ||
1731 - (!a_sha && blob_unchanged(o_sha, b_sha, normalize, path))) {
1734 + (!b_sha && blob_unchanged(o_sha, o_mode, a_sha, a_mode, normalize, path)) ||
1735 + (!a_sha && blob_unchanged(o_sha, o_mode, b_sha, b_mode, normalize, path))) {
1736 /* Deleted in both or deleted in one and
1737 * unchanged in the other */
1738 if (a_sha)
t/t6031-merge-filemode.sh
+23
@@ -74,4 +74,27 @@ do_both_modes () {
74 do_both_modes recursive
75 do_both_modes resolve
76
77 +test_expect_success 'set up delete/modechange scenario' '
78 + git reset --hard &&
79 + git checkout -b deletion master &&
80 + git rm file1 &&
81 + git commit -m deletion
82 +'
83 +
84 +do_delete_modechange () {
85 + strategy=$1
86 + us=$2
87 + them=$3
88 + test_expect_success "detect delete/modechange conflict ($strategy, $us)" '
89 + git reset --hard &&
90 + git checkout $us &&
91 + test_must_fail git merge -s $strategy $them
92 + '
93 +}
94 +
95 +do_delete_modechange recursive b1 deletion
96 +do_delete_modechange recursive deletion b1
97 +do_delete_modechange resolve b1 deletion
98 +do_delete_modechange resolve deletion b1
99 +
100 test_done