@@ -261,4 +261,174 @@ test_expect_success 'blank but not empty at EOF' '
261261 grep "new blank line at EOF" error
262262'
263263
264+ test_expect_success ' applying beyond EOF requires one non-blank context line' '
265+ { echo; echo; echo; echo; } >one &&
266+ git add one &&
267+ { echo b; } >>one &&
268+ git diff -- one >patch &&
269+
270+ git checkout one &&
271+ { echo a; echo; } >one &&
272+ cp one expect &&
273+ test_must_fail git apply --whitespace=fix patch &&
274+ test_cmp one expect &&
275+ test_must_fail git apply --ignore-space-change --whitespace=fix patch &&
276+ test_cmp one expect
277+ '
278+
279+ test_expect_success ' tons of blanks at EOF should not apply' '
280+ for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
281+ echo; echo; echo; echo;
282+ done >one &&
283+ git add one &&
284+ echo a >>one &&
285+ git diff -- one >patch &&
286+
287+ >one &&
288+ test_must_fail git apply --whitespace=fix patch &&
289+ test_must_fail git apply --ignore-space-change --whitespace=fix patch
290+ '
291+
292+ test_expect_success ' missing blank line at end with --whitespace=fix' '
293+ echo a >one &&
294+ echo >>one &&
295+ git add one &&
296+ echo b >>one &&
297+ cp one expect &&
298+ git diff -- one >patch &&
299+ echo a >one &&
300+ cp one saved-one &&
301+ test_must_fail git apply patch &&
302+ git apply --whitespace=fix patch &&
303+ test_cmp one expect &&
304+ mv saved-one one &&
305+ git apply --ignore-space-change --whitespace=fix patch &&
306+ test_cmp one expect
307+ '
308+
309+ test_expect_success ' two missing blank lines at end with --whitespace=fix' '
310+ { echo a; echo; echo b; echo c; } >one &&
311+ cp one no-blank-lines &&
312+ { echo; echo; } >>one &&
313+ git add one &&
314+ echo d >>one &&
315+ cp one expect &&
316+ echo >>one &&
317+ git diff -- one >patch &&
318+ cp no-blank-lines one &&
319+ test_must_fail git apply patch &&
320+ git apply --whitespace=fix patch &&
321+ test_cmp one expect &&
322+ mv no-blank-lines one &&
323+ test_must_fail git apply patch &&
324+ git apply --ignore-space-change --whitespace=fix patch &&
325+ test_cmp one expect
326+ '
327+
328+ test_expect_success ' shrink file with tons of missing blanks at end of file' '
329+ { echo a; echo b; echo c; } >one &&
330+ cp one no-blank-lines &&
331+ for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
332+ echo; echo; echo; echo;
333+ done >>one &&
334+ git add one &&
335+ echo a >one &&
336+ cp one expect &&
337+ git diff -- one >patch &&
338+ cp no-blank-lines one &&
339+ test_must_fail git apply patch &&
340+ git apply --whitespace=fix patch &&
341+ test_cmp one expect &&
342+ mv no-blank-lines one &&
343+ git apply --ignore-space-change --whitespace=fix patch &&
344+ test_cmp one expect
345+ '
346+
347+ test_expect_success ' missing blanks at EOF must only match blank lines' '
348+ { echo a; echo b; } >one &&
349+ git add one &&
350+ { echo c; echo d; } >>one &&
351+ git diff -- one >patch &&
352+
353+ echo a >one &&
354+ test_must_fail git apply patch
355+ test_must_fail git apply --whitespace=fix patch &&
356+ test_must_fail git apply --ignore-space-change --whitespace=fix patch
357+ '
358+
359+ sed -e' s/Z//' > one << EOF
360+ a
361+ b
362+ c
363+ Z
364+ EOF
365+
366+ test_expect_success ' missing blank line should match context line with spaces' '
367+ git add one &&
368+ echo d >>one &&
369+ git diff -- one >patch &&
370+ { echo a; echo b; echo c; } >one &&
371+ cp one expect &&
372+ { echo; echo d; } >>expect &&
373+ git add one &&
374+
375+ git apply --whitespace=fix patch &&
376+ test_cmp one expect
377+ '
378+
379+ sed -e' s/Z//' > one << EOF
380+ a
381+ b
382+ c
383+ Z
384+ EOF
385+
386+ test_expect_success ' same, but with the --ignore-space-option' '
387+ git add one &&
388+ echo d >>one &&
389+ cp one expect &&
390+ git diff -- one >patch &&
391+ { echo a; echo b; echo c; } >one &&
392+ git add one &&
393+
394+ git checkout-index -f one &&
395+ git apply --ignore-space-change --whitespace=fix patch &&
396+ test_cmp one expect
397+ '
398+
399+ test_expect_success ' same, but with CR-LF line endings && cr-at-eol set' '
400+ git config core.whitespace cr-at-eol &&
401+ printf "a\r\n" >one &&
402+ printf "b\r\n" >>one &&
403+ printf "c\r\n" >>one &&
404+ cp one save-one &&
405+ printf " \r\n" >>one
406+ git add one &&
407+ printf "d\r\n" >>one &&
408+ cp one expect &&
409+ git diff -- one >patch &&
410+ mv save-one one &&
411+
412+ git apply --ignore-space-change --whitespace=fix patch &&
413+ test_cmp one expect
414+ '
415+
416+ test_expect_success ' same, but with CR-LF line endings && cr-at-eol unset' '
417+ git config --unset core.whitespace &&
418+ printf "a\r\n" >one &&
419+ printf "b\r\n" >>one &&
420+ printf "c\r\n" >>one &&
421+ cp one save-one &&
422+ printf " \r\n" >>one
423+ git add one &&
424+ cp one expect &&
425+ printf "d\r\n" >>one &&
426+ git diff -- one >patch &&
427+ mv save-one one &&
428+ echo d >>expect &&
429+
430+ git apply --ignore-space-change --whitespace=fix patch &&
431+ test_cmp one expect
432+ '
433+
264434test_done
0 commit comments