Skip to content

Commit 72fac66

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f78d1fe commit 72fac66

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

git-merge-one-file.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ case "${1:-.}${2:-.}${3:-.}" in
3838
# Deleted in both or deleted in one and unchanged in the other
3939
#
4040
"$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+
4149
if test -n "$2"
4250
then
4351
echo "Removing $4"

merge-recursive.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,13 +1535,17 @@ static int read_sha1_strbuf(const unsigned char *sha1, struct strbuf *dst)
15351535
}
15361536

15371537
static int blob_unchanged(const unsigned char *o_sha,
1538+
unsigned o_mode,
15381539
const unsigned char *a_sha,
1540+
unsigned a_mode,
15391541
int renormalize, const char *path)
15401542
{
15411543
struct strbuf o = STRBUF_INIT;
15421544
struct strbuf a = STRBUF_INIT;
15431545
int ret = 0; /* assume changed for safety */
15441546

1547+
if (a_mode != o_mode)
1548+
return 0;
15451549
if (sha_eq(o_sha, a_sha))
15461550
return 1;
15471551
if (!renormalize)
@@ -1727,8 +1731,8 @@ static int process_entry(struct merge_options *o,
17271731
} else if (o_sha && (!a_sha || !b_sha)) {
17281732
/* Case A: Deleted in one */
17291733
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))) {
17321736
/* Deleted in both or deleted in one and
17331737
* unchanged in the other */
17341738
if (a_sha)

t/t6031-merge-filemode.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,27 @@ do_both_modes () {
7474
do_both_modes recursive
7575
do_both_modes resolve
7676

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+
77100
test_done

0 commit comments

Comments
 (0)