t4129: test that git apply warns for unexpected mode changes

There is no test covering what commit 01aff0a (apply: correctly reverse patch's pre- and post-image mode bits, 2023-12-26) addressed. Prior to that commit, git apply was erroneously unaware of a file's expected mode while reverse-patching a file whose mode was not changing. Add the missing test coverage to assure that git apply is aware of the expected mode of a file being patched when the patch does not indicate that the file's mode is changing. This is achieved by arranging a file mode so that it doesn't agree with patch being applied, and checking git apply's output for the warning it's supposed to raise in this situation. Test in both reverse and normal (forward) directions. Signed-off-by: Mark Mentovai <mark@chromium.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Mark Mentovai committed May 23, 2025 at 23:40 UTC 2cc8c17d67265f1912b79f8e57fc2718597ca686
1 file changed +65 -5
t/t4129-apply-samemode.sh
+65 -5
@@ -102,15 +102,23 @@ test_expect_success POSIXPERM 'do not use core.sharedRepository for working tree
102 )
103 '
104
105 +test_file_mode_staged () {
106 + git ls-files --stage -- "$2" >ls-files-output &&
107 + test_grep "^$1 " ls-files-output
108 +}
109 +
110 +test_file_mode_HEAD () {
111 + git ls-tree HEAD -- "$2" >ls-tree-output &&
112 + test_grep "^$1 " ls-tree-output
113 +}
114 +
115 test_expect_success 'git apply respects core.fileMode' '
116 test_config core.fileMode false &&
117 echo true >script.sh &&
118 git add --chmod=+x script.sh &&
109 - git ls-files -s script.sh >ls-files-output &&
110 - test_grep "^100755" ls-files-output &&
119 + test_file_mode_staged 100755 script.sh &&
120 test_tick && git commit -m "Add script" &&
112 - git ls-tree -r HEAD script.sh >ls-tree-output &&
113 - test_grep "^100755" ls-tree-output &&
121 + test_file_mode_HEAD 100755 script.sh &&
122
123 echo true >>script.sh &&
124 test_tick && git commit -m "Modify script" script.sh &&
@@ -126,7 +134,59 @@ test_expect_success 'git apply respects core.fileMode' '
134 test_grep ! "has type 100644, expected 100755" err &&
135
136 git apply --cached patch 2>err &&
129 - test_grep ! "has type 100644, expected 100755" err
137 + test_grep ! "has type 100644, expected 100755" err &&
138 + git reset --hard
139 +'
140 +
141 +test_expect_success 'setup: git apply [--reverse] warns about incorrect file modes' '
142 + test_config core.fileMode false &&
143 +
144 + >mode_test &&
145 + git add --chmod=-x mode_test &&
146 + test_file_mode_staged 100644 mode_test &&
147 + test_tick && git commit -m "add mode_test" &&
148 + test_file_mode_HEAD 100644 mode_test &&
149 + git tag mode_test_forward_initial &&
150 +
151 + echo content >>mode_test &&
152 + test_tick && git commit -m "append to mode_test" mode_test &&
153 + test_file_mode_HEAD 100644 mode_test &&
154 + git tag mode_test_reverse_initial &&
155 +
156 + git format-patch -1 --stdout >patch &&
157 + test_grep "^index .* 100644$" patch
158 +'
159 +
160 +test_expect_success 'git apply warns about incorrect file modes' '
161 + test_config core.fileMode false &&
162 + git reset --hard mode_test_forward_initial &&
163 +
164 + git add --chmod=+x mode_test &&
165 + test_file_mode_staged 100755 mode_test &&
166 + test_tick && git commit -m "make mode_test executable" &&
167 + test_file_mode_HEAD 100755 mode_test &&
168 +
169 + git apply --index patch 2>err &&
170 + test_grep "has type 100755, expected 100644" err &&
171 + test_file_mode_staged 100755 mode_test &&
172 + test_tick && git commit -m "redo: append to mode_test" &&
173 + test_file_mode_HEAD 100755 mode_test
174 +'
175 +
176 +test_expect_success 'git apply --reverse warns about incorrect file modes' '
177 + test_config core.fileMode false &&
178 + git reset --hard mode_test_reverse_initial &&
179 +
180 + git add --chmod=+x mode_test &&
181 + test_file_mode_staged 100755 mode_test &&
182 + test_tick && git commit -m "make mode_test executable" &&
183 + test_file_mode_HEAD 100755 mode_test &&
184 +
185 + git apply --index --reverse patch 2>err &&
186 + test_grep "has type 100755, expected 100644" err &&
187 + test_file_mode_staged 100755 mode_test &&
188 + test_tick && git commit -m "undo: append to mode_test" &&
189 + test_file_mode_HEAD 100755 mode_test
190 '
191
192 test_expect_success POSIXPERM 'patch mode for new file is canonicalized' '