Skip to content

Commit 2cc8c17

Browse files
markmentovaigitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8613c2b commit 2cc8c17

File tree

1 file changed

+65
-5
lines changed

1 file changed

+65
-5
lines changed

t/t4129-apply-samemode.sh

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,23 @@ test_expect_success POSIXPERM 'do not use core.sharedRepository for working tree
102102
)
103103
'
104104

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+
105115
test_expect_success 'git apply respects core.fileMode' '
106116
test_config core.fileMode false &&
107117
echo true >script.sh &&
108118
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 &&
111120
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 &&
114122
115123
echo true >>script.sh &&
116124
test_tick && git commit -m "Modify script" script.sh &&
@@ -126,7 +134,59 @@ test_expect_success 'git apply respects core.fileMode' '
126134
test_grep ! "has type 100644, expected 100755" err &&
127135
128136
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
130190
'
131191

132192
test_expect_success POSIXPERM 'patch mode for new file is canonicalized' '

0 commit comments

Comments
 (0)