Skip to content

Commit 0482c32

Browse files
Chandra Pratapgitster
authored andcommitted
apply: ignore working tree filemode when !core.filemode
When applying a patch that adds an executable file, git apply ignores the core.fileMode setting (core.fileMode in git config specifies whether the executable bit on files in the working tree should be honored or not) resulting in warnings like: warning: script.sh has type 100644, expected 100755 even when core.fileMode is set to false, which is undesired. This is extra true for systems like Windows. Fix this by inferring the correct file mode from either the existing index entry, and when it is unavailable, assuming that the file mode was OK by pretending it had the mode that the preimage wants to see, when core.filemode is set to false. Add a test case that verifies the change and prevents future regression. Signed-off-by: Chandra Pratap <[email protected]> Reviewed-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 564d025 commit 0482c32

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

apply.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3778,8 +3778,14 @@ static int check_preimage(struct apply_state *state,
37783778
return error_errno("%s", old_name);
37793779
}
37803780

3781-
if (!state->cached && !previous)
3782-
st_mode = ce_mode_from_stat(*ce, st->st_mode);
3781+
if (!state->cached && !previous) {
3782+
if (!trust_executable_bit)
3783+
st_mode = (*ce && (*ce)->ce_mode) ? (*ce)->ce_mode :
3784+
(state->apply_in_reverse
3785+
? patch->new_mode : patch->old_mode);
3786+
else
3787+
st_mode = ce_mode_from_stat(*ce, st->st_mode);
3788+
}
37833789

37843790
if (patch->is_new < 0)
37853791
patch->is_new = 0;

t/t4129-apply-samemode.sh

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

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

0 commit comments

Comments
 (0)