Skip to content

Commit cf47fb7

Browse files
committed
Merge branch 'cp/apply-core-filemode'
"git apply" on a filesystem without filemode support have learned to take a hint from what is in the index for the path, even when not working with the "--index" or "--cached" option, when checking the executable bit match what is required by the preimage in the patch. * cp/apply-core-filemode: apply: code simplification apply: correctly reverse patch's pre- and post-image mode bits apply: ignore working tree filemode when !core.filemode
2 parents b4385bf + 45b6251 commit cf47fb7

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

apply.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,7 +2219,8 @@ static void reverse_patches(struct patch *p)
22192219
struct fragment *frag = p->fragments;
22202220

22212221
SWAP(p->new_name, p->old_name);
2222-
SWAP(p->new_mode, p->old_mode);
2222+
if (p->new_mode)
2223+
SWAP(p->new_mode, p->old_mode);
22232224
SWAP(p->is_new, p->is_delete);
22242225
SWAP(p->lines_added, p->lines_deleted);
22252226
SWAP(p->old_oid_prefix, p->new_oid_prefix);
@@ -3777,8 +3778,17 @@ static int check_preimage(struct apply_state *state,
37773778
return error_errno("%s", old_name);
37783779
}
37793780

3780-
if (!state->cached && !previous)
3781-
st_mode = ce_mode_from_stat(*ce, st->st_mode);
3781+
if (!state->cached && !previous) {
3782+
if (*ce && !(*ce)->ce_mode)
3783+
BUG("ce_mode == 0 for path '%s'", old_name);
3784+
3785+
if (trust_executable_bit)
3786+
st_mode = ce_mode_from_stat(*ce, st->st_mode);
3787+
else if (*ce)
3788+
st_mode = (*ce)->ce_mode;
3789+
else
3790+
st_mode = patch->old_mode;
3791+
}
37823792

37833793
if (patch->is_new < 0)
37843794
patch->is_new = 0;

t/t4129-apply-samemode.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,31 @@ test_expect_success POSIXPERM 'do not use core.sharedRepository for working tree
103103
)
104104
'
105105

106+
test_expect_success 'git apply respects core.fileMode' '
107+
test_config core.fileMode false &&
108+
echo true >script.sh &&
109+
git add --chmod=+x script.sh &&
110+
git ls-files -s script.sh >ls-files-output &&
111+
test_grep "^100755" ls-files-output &&
112+
test_tick && git commit -m "Add script" &&
113+
git ls-tree -r HEAD script.sh >ls-tree-output &&
114+
test_grep "^100755" ls-tree-output &&
115+
116+
echo true >>script.sh &&
117+
test_tick && git commit -m "Modify script" script.sh &&
118+
git format-patch -1 --stdout >patch &&
119+
test_grep "^index.*100755$" patch &&
120+
121+
git switch -c branch HEAD^ &&
122+
git apply --index patch 2>err &&
123+
test_grep ! "has type 100644, expected 100755" err &&
124+
git reset --hard &&
125+
126+
git apply patch 2>err &&
127+
test_grep ! "has type 100644, expected 100755" err &&
128+
129+
git apply --cached patch 2>err &&
130+
test_grep ! "has type 100644, expected 100755" err
131+
'
132+
106133
test_done

0 commit comments

Comments
 (0)