Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='commit-msg hook' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 7 | test_expect_success 'with no hook' ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 8 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 9 | echo "foo" > file && |
| 10 | git add file && |
| 11 | git commit -m "first" |
| 12 | |
| 13 | ' |
| 14 | |
| 15 | # set up fake editor for interactive editing |
| 16 | cat > fake-editor <<'EOF' |
| 17 | #!/bin/sh |
| 18 | cp FAKE_MSG "$1" |
| 19 | exit 0 |
| 20 | EOF |
| 21 | chmod +x fake-editor |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 22 | |
| 23 | ## Not using test_set_editor here so we can easily ensure the editor variable |
| 24 | ## is only set for the editor tests |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 25 | FAKE_EDITOR="$(pwd)/fake-editor" |
| 26 | export FAKE_EDITOR |
| 27 | |
| 28 | test_expect_success 'with no hook (editor)' ' |
| 29 | |
| 30 | echo "more foo" >> file && |
| 31 | git add file && |
| 32 | echo "more foo" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 33 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 34 | |
| 35 | ' |
| 36 | |
| 37 | test_expect_success '--no-verify with no hook' ' |
| 38 | |
| 39 | echo "bar" > file && |
| 40 | git add file && |
| 41 | git commit --no-verify -m "bar" |
| 42 | |
| 43 | ' |
| 44 | |
| 45 | test_expect_success '--no-verify with no hook (editor)' ' |
| 46 | |
| 47 | echo "more bar" > file && |
| 48 | git add file && |
| 49 | echo "more bar" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 50 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 51 | |
| 52 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 53 | |
| 54 | # now install hook that always succeeds |
| 55 | HOOKDIR="$(git rev-parse --git-dir)/hooks" |
| 56 | HOOK="$HOOKDIR/commit-msg" |
| 57 | mkdir -p "$HOOKDIR" |
| 58 | cat > "$HOOK" <<EOF |
| 59 | #!/bin/sh |
| 60 | exit 0 |
| 61 | EOF |
| 62 | chmod +x "$HOOK" |
| 63 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 64 | test_expect_success 'with succeeding hook' ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 65 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 66 | echo "more" >> file && |
| 67 | git add file && |
| 68 | git commit -m "more" |
| 69 | |
| 70 | ' |
| 71 | |
| 72 | test_expect_success 'with succeeding hook (editor)' ' |
| 73 | |
| 74 | echo "more more" >> file && |
| 75 | git add file && |
| 76 | echo "more more" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 77 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 78 | |
| 79 | ' |
| 80 | |
| 81 | test_expect_success '--no-verify with succeeding hook' ' |
| 82 | |
| 83 | echo "even more" >> file && |
| 84 | git add file && |
| 85 | git commit --no-verify -m "even more" |
| 86 | |
| 87 | ' |
| 88 | |
| 89 | test_expect_success '--no-verify with succeeding hook (editor)' ' |
| 90 | |
| 91 | echo "even more more" >> file && |
| 92 | git add file && |
| 93 | echo "even more more" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 94 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 95 | |
| 96 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 97 | |
| 98 | # now a hook that fails |
| 99 | cat > "$HOOK" <<EOF |
| 100 | #!/bin/sh |
| 101 | exit 1 |
| 102 | EOF |
| 103 | |
Junio C Hamano | 41ac414 | 2008-02-01 01:50:53 -0800 | [diff] [blame] | 104 | test_expect_success 'with failing hook' ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 105 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 106 | echo "another" >> file && |
| 107 | git add file && |
Stephan Beyer | d492b31 | 2008-07-12 17:47:52 +0200 | [diff] [blame] | 108 | test_must_fail git commit -m "another" |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 109 | |
| 110 | ' |
| 111 | |
Junio C Hamano | 41ac414 | 2008-02-01 01:50:53 -0800 | [diff] [blame] | 112 | test_expect_success 'with failing hook (editor)' ' |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 113 | |
| 114 | echo "more another" >> file && |
| 115 | git add file && |
| 116 | echo "more another" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 117 | ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit) |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 118 | |
| 119 | ' |
| 120 | |
| 121 | test_expect_success '--no-verify with failing hook' ' |
| 122 | |
| 123 | echo "stuff" >> file && |
| 124 | git add file && |
| 125 | git commit --no-verify -m "stuff" |
| 126 | |
| 127 | ' |
| 128 | |
| 129 | test_expect_success '--no-verify with failing hook (editor)' ' |
| 130 | |
| 131 | echo "more stuff" >> file && |
| 132 | git add file && |
| 133 | echo "more stuff" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 134 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 135 | |
| 136 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 137 | |
| 138 | chmod -x "$HOOK" |
Johannes Sixt | ee9fb68 | 2009-03-13 22:55:27 +0100 | [diff] [blame] | 139 | test_expect_success POSIXPERM 'with non-executable hook' ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 140 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 141 | echo "content" >> file && |
| 142 | git add file && |
| 143 | git commit -m "content" |
| 144 | |
| 145 | ' |
| 146 | |
Johannes Sixt | ee9fb68 | 2009-03-13 22:55:27 +0100 | [diff] [blame] | 147 | test_expect_success POSIXPERM 'with non-executable hook (editor)' ' |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 148 | |
| 149 | echo "content again" >> file && |
| 150 | git add file && |
| 151 | echo "content again" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 152 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again" |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 153 | |
| 154 | ' |
| 155 | |
Johannes Sixt | ee9fb68 | 2009-03-13 22:55:27 +0100 | [diff] [blame] | 156 | test_expect_success POSIXPERM '--no-verify with non-executable hook' ' |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 157 | |
| 158 | echo "more content" >> file && |
| 159 | git add file && |
| 160 | git commit --no-verify -m "more content" |
| 161 | |
| 162 | ' |
| 163 | |
Johannes Sixt | ee9fb68 | 2009-03-13 22:55:27 +0100 | [diff] [blame] | 164 | test_expect_success POSIXPERM '--no-verify with non-executable hook (editor)' ' |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 165 | |
| 166 | echo "even more content" >> file && |
| 167 | git add file && |
| 168 | echo "even more content" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 169 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 170 | |
| 171 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 172 | |
| 173 | # now a hook that edits the commit message |
| 174 | cat > "$HOOK" <<'EOF' |
| 175 | #!/bin/sh |
| 176 | echo "new message" > "$1" |
| 177 | exit 0 |
| 178 | EOF |
| 179 | chmod +x "$HOOK" |
| 180 | |
| 181 | commit_msg_is () { |
| 182 | test "`git log --pretty=format:%s%b -1`" = "$1" |
| 183 | } |
| 184 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 185 | test_expect_success 'hook edits commit message' ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 186 | |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 187 | echo "additional" >> file && |
| 188 | git add file && |
| 189 | git commit -m "additional" && |
| 190 | commit_msg_is "new message" |
| 191 | |
| 192 | ' |
| 193 | |
| 194 | test_expect_success 'hook edits commit message (editor)' ' |
| 195 | |
| 196 | echo "additional content" >> file && |
| 197 | git add file && |
| 198 | echo "additional content" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 199 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit && |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 200 | commit_msg_is "new message" |
| 201 | |
| 202 | ' |
| 203 | |
| 204 | test_expect_success "hook doesn't edit commit message" ' |
| 205 | |
| 206 | echo "plus" >> file && |
| 207 | git add file && |
| 208 | git commit --no-verify -m "plus" && |
| 209 | commit_msg_is "plus" |
| 210 | |
| 211 | ' |
| 212 | |
| 213 | test_expect_success "hook doesn't edit commit message (editor)" ' |
| 214 | |
| 215 | echo "more plus" >> file && |
| 216 | git add file && |
| 217 | echo "more plus" > FAKE_MSG && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 218 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify && |
Wincent Colaiuta | 80f8660 | 2007-12-10 08:33:26 +0100 | [diff] [blame] | 219 | commit_msg_is "more plus" |
| 220 | |
| 221 | ' |
Wincent Colaiuta | 264474f | 2007-12-08 13:29:47 +0100 | [diff] [blame] | 222 | |
| 223 | test_done |