| 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2007 Shawn O. Pearce |
| 4 | # |
| 5 | |
| 6 | test_description='git apply -p handling.' |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
| 10 | test_expect_success setup ' |
| 11 | mkdir sub && |
| 12 | echo A >sub/file1 && |
| 13 | cp sub/file1 file1.saved && |
| 14 | git add sub/file1 && |
| 15 | echo B >sub/file1 && |
| 16 | git diff >patch.file && |
| 17 | git checkout -- sub/file1 && |
| 18 | git mv sub süb && |
| 19 | echo B >süb/file1 && |
| 20 | git diff >patch.escaped && |
| 21 | grep "[\]" patch.escaped && |
| 22 | rm süb/file1 && |
| 23 | rmdir süb |
| 24 | ' |
| 25 | |
| 26 | test_expect_success 'git apply -p 1 patch' ' |
| 27 | cat >patch <<-\EOF && |
| 28 | From 90ad11d5b2d437e82d4d992f72fb44c2227798b5 Mon Sep 17 00:00:00 2001 |
| 29 | From: Mroik <mroik@delayed.space> |
| 30 | Date: Mon, 9 Mar 2026 23:25:00 +0100 |
| 31 | Subject: [PATCH] Test |
| 32 | |
| 33 | --- |
| 34 | t/test/test | 0 |
| 35 | 1 file changed, 0 insertions(+), 0 deletions(-) |
| 36 | create mode 100644 t/test/test |
| 37 | |
| 38 | diff --git a/t/test/test b/t/test/test |
| 39 | new file mode 100644 |
| 40 | index 0000000000..e69de29bb2 |
| 41 | -- |
| 42 | 2.53.0.851.ga537e3e6e9 |
| 43 | EOF |
| 44 | test_when_finished "rm -rf t" && |
| 45 | git apply -p 1 patch && |
| 46 | test_path_is_dir t |
| 47 | ' |
| 48 | |
| 49 | test_expect_success 'apply fails due to non-num -p' ' |
| 50 | test_when_finished "rm -rf t test err" && |
| 51 | test_must_fail git apply -p malformed patch 2>err && |
| 52 | test_grep "option -p expects a non-negative integer" err |
| 53 | ' |
| 54 | |
| 55 | test_expect_success 'apply fails due to trailing non-digit in -p' ' |
| 56 | test_when_finished "rm -rf t test err" && |
| 57 | test_must_fail git apply -p 2q patch 2>err && |
| 58 | test_grep "option -p expects a non-negative integer" err |
| 59 | ' |
| 60 | |
| 61 | test_expect_success 'apply fails due to negative number in -p' ' |
| 62 | test_when_finished "rm -rf t test err patch" && |
| 63 | test_must_fail git apply -p -1 patch 2> err && |
| 64 | test_grep "option -p expects a non-negative integer" err |
| 65 | ' |
| 66 | |
| 67 | test_expect_success 'apply git diff with -p2' ' |
| 68 | cp file1.saved file1 && |
| 69 | git apply -p2 patch.file |
| 70 | ' |
| 71 | |
| 72 | test_expect_success 'apply with too large -p' ' |
| 73 | cp file1.saved file1 && |
| 74 | test_must_fail git apply --stat -p3 patch.file 2>err && |
| 75 | test_grep "removing 3 leading" err |
| 76 | ' |
| 77 | |
| 78 | test_expect_success 'apply (-p2) traditional diff with funny filenames' ' |
| 79 | cat >patch.quotes <<-\EOF && |
| 80 | diff -u "a/"sub/file1 "b/"sub/file1 |
| 81 | --- "a/"sub/file1 |
| 82 | +++ "b/"sub/file1 |
| 83 | @@ -1 +1 @@ |
| 84 | -A |
| 85 | +B |
| 86 | EOF |
| 87 | echo B >expected && |
| 88 | |
| 89 | cp file1.saved file1 && |
| 90 | git apply -p2 patch.quotes && |
| 91 | test_cmp expected file1 |
| 92 | ' |
| 93 | |
| 94 | test_expect_success 'apply with too large -p and fancy filename' ' |
| 95 | cp file1.saved file1 && |
| 96 | test_must_fail git apply --stat -p3 patch.escaped 2>err && |
| 97 | test_grep "removing 3 leading" err |
| 98 | ' |
| 99 | |
| 100 | test_expect_success 'apply (-p2) diff, mode change only' ' |
| 101 | cat >patch.chmod <<-\EOF && |
| 102 | diff --git a/sub/file1 b/sub/file1 |
| 103 | old mode 100644 |
| 104 | new mode 100755 |
| 105 | EOF |
| 106 | test_chmod -x file1 && |
| 107 | git apply --index -p2 patch.chmod && |
| 108 | case $(git ls-files -s file1) in 100755*) : good;; *) false;; esac |
| 109 | ' |
| 110 | |
| 111 | test_expect_success FILEMODE 'file mode was changed' ' |
| 112 | test -x file1 |
| 113 | ' |
| 114 | |
| 115 | test_expect_success 'apply (-p2) diff, rename' ' |
| 116 | cat >patch.rename <<-\EOF && |
| 117 | diff --git a/sub/file1 b/sub/file2 |
| 118 | similarity index 100% |
| 119 | rename from sub/file1 |
| 120 | rename to sub/file2 |
| 121 | EOF |
| 122 | echo A >expected && |
| 123 | |
| 124 | cp file1.saved file1 && |
| 125 | rm -f file2 && |
| 126 | git apply -p2 patch.rename && |
| 127 | test_cmp expected file2 |
| 128 | ' |
| 129 | |
| 130 | test_done |