1
0
mirror of https://github.com/git/git synced 2024-07-07 19:39:27 +00:00

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 <chandrapratap3519@gmail.com>
Reviewed-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Chandra Pratap 2023-12-26 15:32:16 -08:00 committed by Junio C Hamano
parent 564d0252ca
commit 0482c32c33
2 changed files with 35 additions and 2 deletions

10
apply.c
View File

@ -3778,8 +3778,14 @@ static int check_preimage(struct apply_state *state,
return error_errno("%s", old_name);
}
if (!state->cached && !previous)
st_mode = ce_mode_from_stat(*ce, st->st_mode);
if (!state->cached && !previous) {
if (!trust_executable_bit)
st_mode = (*ce && (*ce)->ce_mode) ? (*ce)->ce_mode :
(state->apply_in_reverse
? patch->new_mode : patch->old_mode);
else
st_mode = ce_mode_from_stat(*ce, st->st_mode);
}
if (patch->is_new < 0)
patch->is_new = 0;

View File

@ -101,4 +101,31 @@ test_expect_success POSIXPERM 'do not use core.sharedRepository for working tree
)
'
test_expect_success 'git apply respects core.fileMode' '
test_config core.fileMode false &&
echo true >script.sh &&
git add --chmod=+x script.sh &&
git ls-files -s script.sh >ls-files-output &&
test_grep "^100755" ls-files-output &&
test_tick && git commit -m "Add script" &&
git ls-tree -r HEAD script.sh >ls-tree-output &&
test_grep "^100755" ls-tree-output &&
echo true >>script.sh &&
test_tick && git commit -m "Modify script" script.sh &&
git format-patch -1 --stdout >patch &&
test_grep "^index.*100755$" patch &&
git switch -c branch HEAD^ &&
git apply --index patch 2>err &&
test_grep ! "has type 100644, expected 100755" err &&
git reset --hard &&
git apply patch 2>err &&
test_grep ! "has type 100644, expected 100755" err &&
git apply --cached patch 2>err &&
test_grep ! "has type 100644, expected 100755" err
'
test_done