t4014: stop losing return codes of git commands

Currently, there are two ways where the return codes of Git commands are lost. The first way is when a command is in the upstream of a pipe. In a pipe, only the return code of the last command is used. Thus, all other commands will have their return codes masked. Rewrite pipes so that there are no Git commands upstream. The other way is when a command is in a non-assignment subshell. The return code will be lost in favour of the surrounding command's. Rewrite instances of this such that Git commands output to a file and surrounding commands only call subshells with non-Git commands. Signed-off-by: Denton Liu <liu.denton@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Denton Liu committed Aug 27, 2019 at 00:05 UTC 854b5cb46af2281f54038804121a89381169f420
1 file changed +120 -77
t/t4014-format-patch.sh
+120 -77
@@ -33,7 +33,8 @@ test_expect_success setup '
33 git commit -m "Side changes #3 with \\n backslash-n in it." &&
34
35 git checkout master &&
36 - git diff-tree -p C2 | git apply --index &&
36 + git diff-tree -p C2 >patch &&
37 + git apply --index <patch &&
38 test_tick &&
39 git commit -m "Master accepts moral equivalent of #2" &&
40
@@ -110,7 +111,8 @@ test_expect_success 'format-patch --ignore-if-in-upstream result applies' '
111 '
112
113 test_expect_success 'commit did not screw up the log message' '
113 - git cat-file commit side | grep "^Side .* with .* backslash-n"
114 + git cat-file commit side >actual &&
115 + grep "^Side .* with .* backslash-n" actual
116 '
117
118 test_expect_success 'format-patch did not screw up the log message' '
@@ -119,7 +121,8 @@ test_expect_success 'format-patch did not screw up the log message' '
121 '
122
123 test_expect_success 'replay did not screw up the log message' '
122 - git cat-file commit rebuild-1 | grep "^Side .* with .* backslash-n"
124 + git cat-file commit rebuild-1 >actual &&
125 + grep "^Side .* with .* backslash-n" actual
126 '
127
128 test_expect_success 'extra headers' '
@@ -153,63 +156,73 @@ test_expect_success 'extra headers with multiple To:s' '
156
157 test_expect_success 'additional command line cc (ascii)' '
158 git config --replace-all format.headers "Cc: R E Cipient <rcipient@example.com>" &&
156 - git format-patch --cc="S E Cipient <scipient@example.com>" --stdout master..side | sed -e "/^\$/q" >patch5 &&
157 - grep "^Cc: R E Cipient <rcipient@example.com>,\$" patch5 &&
158 - grep "^ *S E Cipient <scipient@example.com>\$" patch5
159 + git format-patch --cc="S E Cipient <scipient@example.com>" --stdout master..side >patch5 &&
160 + sed -e "/^\$/q" patch5 >hdrs5 &&
161 + grep "^Cc: R E Cipient <rcipient@example.com>,\$" hdrs5 &&
162 + grep "^ *S E Cipient <scipient@example.com>\$" hdrs5
163 '
164
165 test_expect_failure 'additional command line cc (rfc822)' '
166 git config --replace-all format.headers "Cc: R E Cipient <rcipient@example.com>" &&
163 - git format-patch --cc="S. E. Cipient <scipient@example.com>" --stdout master..side | sed -e "/^\$/q" >patch5 &&
164 - grep "^Cc: R E Cipient <rcipient@example.com>,\$" patch5 &&
165 - grep "^ *\"S. E. Cipient\" <scipient@example.com>\$" patch5
167 + git format-patch --cc="S. E. Cipient <scipient@example.com>" --stdout master..side >patch5 &&
168 + sed -e "/^\$/q" patch5 >hdrs5 &&
169 + grep "^Cc: R E Cipient <rcipient@example.com>,\$" hdrs5 &&
170 + grep "^ *\"S. E. Cipient\" <scipient@example.com>\$" hdrs5
171 '
172
173 test_expect_success 'command line headers' '
174 git config --unset-all format.headers &&
170 - git format-patch --add-header="Cc: R E Cipient <rcipient@example.com>" --stdout master..side | sed -e "/^\$/q" >patch6 &&
171 - grep "^Cc: R E Cipient <rcipient@example.com>\$" patch6
175 + git format-patch --add-header="Cc: R E Cipient <rcipient@example.com>" --stdout master..side >patch6 &&
176 + sed -e "/^\$/q" patch6 >hdrs6 &&
177 + grep "^Cc: R E Cipient <rcipient@example.com>\$" hdrs6
178 '
179
180 test_expect_success 'configuration headers and command line headers' '
181 git config --replace-all format.headers "Cc: R E Cipient <rcipient@example.com>" &&
176 - git format-patch --add-header="Cc: S E Cipient <scipient@example.com>" --stdout master..side | sed -e "/^\$/q" >patch7 &&
177 - grep "^Cc: R E Cipient <rcipient@example.com>,\$" patch7 &&
178 - grep "^ *S E Cipient <scipient@example.com>\$" patch7
182 + git format-patch --add-header="Cc: S E Cipient <scipient@example.com>" --stdout master..side >patch7 &&
183 + sed -e "/^\$/q" patch7 >hdrs7 &&
184 + grep "^Cc: R E Cipient <rcipient@example.com>,\$" hdrs7 &&
185 + grep "^ *S E Cipient <scipient@example.com>\$" hdrs7
186 '
187
188 test_expect_success 'command line To: header (ascii)' '
189 git config --unset-all format.headers &&
183 - git format-patch --to="R E Cipient <rcipient@example.com>" --stdout master..side | sed -e "/^\$/q" >patch8 &&
184 - grep "^To: R E Cipient <rcipient@example.com>\$" patch8
190 + git format-patch --to="R E Cipient <rcipient@example.com>" --stdout master..side >patch8 &&
191 + sed -e "/^\$/q" patch8 >hdrs8 &&
192 + grep "^To: R E Cipient <rcipient@example.com>\$" hdrs8
193 '
194
195 test_expect_failure 'command line To: header (rfc822)' '
188 - git format-patch --to="R. E. Cipient <rcipient@example.com>" --stdout master..side | sed -e "/^\$/q" >patch8 &&
189 - grep "^To: \"R. E. Cipient\" <rcipient@example.com>\$" patch8
196 + git format-patch --to="R. E. Cipient <rcipient@example.com>" --stdout master..side >patch8 &&
197 + sed -e "/^\$/q" patch8 >hdrs8 &&
198 + grep "^To: \"R. E. Cipient\" <rcipient@example.com>\$" hdrs8
199 '
200
201 test_expect_failure 'command line To: header (rfc2047)' '
193 - git format-patch --to="R Ä Cipient <rcipient@example.com>" --stdout master..side | sed -e "/^\$/q" >patch8 &&
194 - grep "^To: =?UTF-8?q?R=20=C3=84=20Cipient?= <rcipient@example.com>\$" patch8
202 + git format-patch --to="R Ä Cipient <rcipient@example.com>" --stdout master..side >patch8 &&
203 + sed -e "/^\$/q" patch8 >hdrs8 &&
204 + grep "^To: =?UTF-8?q?R=20=C3=84=20Cipient?= <rcipient@example.com>\$" hdrs8
205 '
206
207 test_expect_success 'configuration To: header (ascii)' '
208 git config format.to "R E Cipient <rcipient@example.com>" &&
199 - git format-patch --stdout master..side | sed -e "/^\$/q" >patch9 &&
200 - grep "^To: R E Cipient <rcipient@example.com>\$" patch9
209 + git format-patch --stdout master..side >patch9 &&
210 + sed -e "/^\$/q" patch9 >hdrs9 &&
211 + grep "^To: R E Cipient <rcipient@example.com>\$" hdrs9
212 '
213
214 test_expect_failure 'configuration To: header (rfc822)' '
215 git config format.to "R. E. Cipient <rcipient@example.com>" &&
205 - git format-patch --stdout master..side | sed -e "/^\$/q" >patch9 &&
206 - grep "^To: \"R. E. Cipient\" <rcipient@example.com>\$" patch9
216 + git format-patch --stdout master..side >patch9 &&
217 + sed -e "/^\$/q" patch9 >hdrs9 &&
218 + grep "^To: \"R. E. Cipient\" <rcipient@example.com>\$" hdrs9
219 '
220
221 test_expect_failure 'configuration To: header (rfc2047)' '
222 git config format.to "R Ä Cipient <rcipient@example.com>" &&
211 - git format-patch --stdout master..side | sed -e "/^\$/q" >patch9 &&
212 - grep "^To: =?UTF-8?q?R=20=C3=84=20Cipient?= <rcipient@example.com>\$" patch9
223 + git format-patch --stdout master..side >patch9 &&
224 + sed -e "/^\$/q" patch9 >hdrs9 &&
225 + grep "^To: =?UTF-8?q?R=20=C3=84=20Cipient?= <rcipient@example.com>\$" hdrs9
226 '
227
228 # check_patch <patch>: Verify that <patch> looks like a half-sane
@@ -221,76 +234,76 @@ check_patch () {
234 }
235
236 test_expect_success 'format.from=false' '
224 - git -c format.from=false format-patch --stdout master..side |
225 - sed -e "/^\$/q" >patch &&
237 + git -c format.from=false format-patch --stdout master..side >patch &&
238 + sed -e "/^\$/q" patch >hdrs &&
239 check_patch patch &&
227 - ! grep "^From: C O Mitter <committer@example.com>\$" patch
240 + ! grep "^From: C O Mitter <committer@example.com>\$" hdrs
241 '
242
243 test_expect_success 'format.from=true' '
231 - git -c format.from=true format-patch --stdout master..side |
232 - sed -e "/^\$/q" >patch &&
233 - check_patch patch &&
234 - grep "^From: C O Mitter <committer@example.com>\$" patch
244 + git -c format.from=true format-patch --stdout master..side >patch &&
245 + sed -e "/^\$/q" patch >hdrs &&
246 + check_patch hdrs &&
247 + grep "^From: C O Mitter <committer@example.com>\$" hdrs
248 '
249
250 test_expect_success 'format.from with address' '
238 - git -c format.from="F R Om <from@example.com>" format-patch --stdout master..side |
239 - sed -e "/^\$/q" >patch &&
240 - check_patch patch &&
241 - grep "^From: F R Om <from@example.com>\$" patch
251 + git -c format.from="F R Om <from@example.com>" format-patch --stdout master..side >patch &&
252 + sed -e "/^\$/q" patch >hdrs &&
253 + check_patch hdrs &&
254 + grep "^From: F R Om <from@example.com>\$" hdrs
255 '
256
257 test_expect_success '--no-from overrides format.from' '
245 - git -c format.from="F R Om <from@example.com>" format-patch --no-from --stdout master..side |
246 - sed -e "/^\$/q" >patch &&
247 - check_patch patch &&
248 - ! grep "^From: F R Om <from@example.com>\$" patch
258 + git -c format.from="F R Om <from@example.com>" format-patch --no-from --stdout master..side >patch &&
259 + sed -e "/^\$/q" patch >hdrs &&
260 + check_patch hdrs &&
261 + ! grep "^From: F R Om <from@example.com>\$" hdrs
262 '
263
264 test_expect_success '--from overrides format.from' '
252 - git -c format.from="F R Om <from@example.com>" format-patch --from --stdout master..side |
253 - sed -e "/^\$/q" >patch &&
254 - check_patch patch &&
255 - ! grep "^From: F R Om <from@example.com>\$" patch
265 + git -c format.from="F R Om <from@example.com>" format-patch --from --stdout master..side >patch &&
266 + sed -e "/^\$/q" patch >hdrs &&
267 + check_patch hdrs &&
268 + ! grep "^From: F R Om <from@example.com>\$" hdrs
269 '
270
271 test_expect_success '--no-to overrides config.to' '
272 git config --replace-all format.to \
273 "R E Cipient <rcipient@example.com>" &&
261 - git format-patch --no-to --stdout master..side |
262 - sed -e "/^\$/q" >patch10 &&
263 - check_patch patch10 &&
264 - ! grep "^To: R E Cipient <rcipient@example.com>\$" patch10
274 + git format-patch --no-to --stdout master..side >patch10 &&
275 + sed -e "/^\$/q" patch10 >hdrs10 &&
276 + check_patch hdrs10 &&
277 + ! grep "^To: R E Cipient <rcipient@example.com>\$" hdrs10
278 '
279
280 test_expect_success '--no-to and --to replaces config.to' '
281 git config --replace-all format.to \
282 "Someone <someone@out.there>" &&
283 git format-patch --no-to --to="Someone Else <else@out.there>" \
271 - --stdout master..side |
272 - sed -e "/^\$/q" >patch11 &&
273 - check_patch patch11 &&
274 - ! grep "^To: Someone <someone@out.there>\$" patch11 &&
275 - grep "^To: Someone Else <else@out.there>\$" patch11
284 + --stdout master..side >patch11 &&
285 + sed -e "/^\$/q" patch11 >hdrs11 &&
286 + check_patch hdrs11 &&
287 + ! grep "^To: Someone <someone@out.there>\$" hdrs11 &&
288 + grep "^To: Someone Else <else@out.there>\$" hdrs11
289 '
290
291 test_expect_success '--no-cc overrides config.cc' '
292 git config --replace-all format.cc \
293 "C E Cipient <rcipient@example.com>" &&
281 - git format-patch --no-cc --stdout master..side |
282 - sed -e "/^\$/q" >patch12 &&
283 - check_patch patch12 &&
284 - ! grep "^Cc: C E Cipient <rcipient@example.com>\$" patch12
294 + git format-patch --no-cc --stdout master..side >patch12 &&
295 + sed -e "/^\$/q" patch12 >hdrs12 &&
296 + check_patch hdrs12 &&
297 + ! grep "^Cc: C E Cipient <rcipient@example.com>\$" hdrs12
298 '
299
300 test_expect_success '--no-add-header overrides config.headers' '
301 git config --replace-all format.headers \
302 "Header1: B E Cipient <rcipient@example.com>" &&
290 - git format-patch --no-add-header --stdout master..side |
291 - sed -e "/^\$/q" >patch13 &&
292 - check_patch patch13 &&
293 - ! grep "^Header1: B E Cipient <rcipient@example.com>\$" patch13
303 + git format-patch --no-add-header --stdout master..side >patch13 &&
304 + sed -e "/^\$/q" patch13 >hdrs13 &&
305 + check_patch hdrs13 &&
306 + ! grep "^Header1: B E Cipient <rcipient@example.com>\$" hdrs13
307 '
308
309 test_expect_success 'multiple files' '
@@ -808,20 +821,25 @@ test_expect_success 'format-patch --ignore-if-in-upstream HEAD' '
821 git format-patch --ignore-if-in-upstream HEAD
822 '
823
811 -git_version="$(git --version | sed "s/.* //")"
824 +test_expect_success 'get git version' '
825 + git_version=$(git --version) &&
826 + git_version=${git_version##* }
827 +'
828
829 signature() {
830 printf "%s\n%s\n\n" "-- " "${1:-$git_version}"
831 }
832
833 test_expect_success 'format-patch default signature' '
818 - git format-patch --stdout -1 | tail -n 3 >output &&
834 + git format-patch --stdout -1 >patch &&
835 + tail -n 3 patch >output &&
836 signature >expect &&
837 test_cmp expect output
838 '
839
840 test_expect_success 'format-patch --signature' '
824 - git format-patch --stdout --signature="my sig" -1 | tail -n 3 >output &&
841 + git format-patch --stdout --signature="my sig" -1 >patch &&
842 + tail -n 3 patch >output &&
843 signature "my sig" >expect &&
844 test_cmp expect output
845 '
@@ -1606,19 +1624,40 @@ test_expect_success 'format-patch -o overrides format.outputDirectory' '
1624
1625 test_expect_success 'format-patch --base' '
1626 git checkout patchid &&
1609 - git format-patch --stdout --base=HEAD~3 -1 | tail -n 7 >actual1 &&
1610 - git format-patch --stdout --base=HEAD~3 HEAD~.. | tail -n 7 >actual2 &&
1627 +
1628 + git format-patch --stdout --base=HEAD~3 -1 >patch &&
1629 + tail -n 7 patch >actual1 &&
1630 +
1631 + git format-patch --stdout --base=HEAD~3 HEAD~.. >patch &&
1632 + tail -n 7 patch >actual2 &&
1633 +
1634 echo >expect &&
1612 - echo "base-commit: $(git rev-parse HEAD~3)" >>expect &&
1613 - echo "prerequisite-patch-id: $(git show --patch HEAD~2 | git patch-id --stable | awk "{print \$1}")" >>expect &&
1614 - echo "prerequisite-patch-id: $(git show --patch HEAD~1 | git patch-id --stable | awk "{print \$1}")" >>expect &&
1635 + git rev-parse HEAD~3 >commit-id-base &&
1636 + echo "base-commit: $(cat commit-id-base)" >>expect &&
1637 +
1638 + git show --patch HEAD~2 >patch &&
1639 + git patch-id --stable <patch >patch.id.raw &&
1640 + awk "{print \"prerequisite-patch-id:\", \$1}" <patch.id.raw >>expect &&
1641 +
1642 + git show --patch HEAD~1 >patch &&
1643 + git patch-id --stable <patch >patch.id.raw &&
1644 + awk "{print \"prerequisite-patch-id:\", \$1}" <patch.id.raw >>expect &&
1645 +
1646 signature >>expect &&
1647 test_cmp expect actual1 &&
1648 test_cmp expect actual2 &&
1649 +
1650 echo >fail &&
1619 - echo "base-commit: $(git rev-parse HEAD~3)" >>fail &&
1620 - echo "prerequisite-patch-id: $(git show --patch HEAD~2 | git patch-id --unstable | awk "{print \$1}")" >>fail &&
1621 - echo "prerequisite-patch-id: $(git show --patch HEAD~1 | git patch-id --unstable | awk "{print \$1}")" >>fail &&
1651 + echo "base-commit: $(cat commit-id-base)" >>fail &&
1652 +
1653 + git show --patch HEAD~2 >patch &&
1654 + git patch-id --unstable <patch >patch.id.raw &&
1655 + awk "{print \"prerequisite-patch-id:\", \$1}" <patch.id.raw >>fail &&
1656 +
1657 + git show --patch HEAD~1 >patch &&
1658 + git patch-id --unstable <patch >patch.id.raw &&
1659 + awk "{print \"prerequisite-patch-id:\", \$1}" <patch.id.raw >>fail &&
1660 +
1661 signature >>fail &&
1662 ! test_cmp fail actual1 &&
1663 ! test_cmp fail actual2
@@ -1629,7 +1668,8 @@ test_expect_success 'format-patch --base errors out when base commit is in revis
1668 test_must_fail git format-patch --base=HEAD~1 -2 &&
1669 git format-patch --stdout --base=HEAD~2 -2 >patch &&
1670 grep "^base-commit:" patch >actual &&
1632 - echo "base-commit: $(git rev-parse HEAD~2)" >expect &&
1671 + git rev-parse HEAD~2 >commit-id-base &&
1672 + echo "base-commit: $(cat commit-id-base)" >expect &&
1673 test_cmp expect actual
1674 '
1675
@@ -1668,7 +1708,8 @@ test_expect_success 'format-patch --base=auto' '
1708 test_commit N2 &&
1709 git format-patch --stdout --base=auto -2 >patch &&
1710 grep "^base-commit:" patch >actual &&
1671 - echo "base-commit: $(git rev-parse upstream)" >expect &&
1711 + git rev-parse upstream >commit-id-base &&
1712 + echo "base-commit: $(cat commit-id-base)" >expect &&
1713 test_cmp expect actual
1714 '
1715
@@ -1705,7 +1746,8 @@ test_expect_success 'format-patch format.useAutoBaseoption' '
1746 git config format.useAutoBase true &&
1747 git format-patch --stdout -1 >patch &&
1748 grep "^base-commit:" patch >actual &&
1708 - echo "base-commit: $(git rev-parse upstream)" >expect &&
1749 + git rev-parse upstream >commit-id-base &&
1750 + echo "base-commit: $(cat commit-id-base)" >expect &&
1751 test_cmp expect actual
1752 '
1753
@@ -1714,7 +1756,8 @@ test_expect_success 'format-patch --base overrides format.useAutoBase' '
1756 git config format.useAutoBase true &&
1757 git format-patch --stdout --base=HEAD~1 -1 >patch &&
1758 grep "^base-commit:" patch >actual &&
1717 - echo "base-commit: $(git rev-parse HEAD~1)" >expect &&
1759 + git rev-parse HEAD~1 >commit-id-base &&
1760 + echo "base-commit: $(cat commit-id-base)" >expect &&
1761 test_cmp expect actual
1762 '
1763