1#!/bin/sh
   2test_description='commit-msg hook'
   4. ./test-lib.sh
   6test_expect_success 'with no hook' '
   8        echo "foo" > file &&
  10        git add file &&
  11        git commit -m "first"
  12'
  14# set up fake editor for interactive editing
  16cat > fake-editor <<'EOF'
  17#!/bin/sh
  18cp FAKE_MSG "$1"
  19exit 0
  20EOF
  21chmod +x fake-editor
  22## Not using test_set_editor here so we can easily ensure the editor variable
  24## is only set for the editor tests
  25FAKE_EDITOR="$(pwd)/fake-editor"
  26export FAKE_EDITOR
  27test_expect_success 'with no hook (editor)' '
  29        echo "more foo" >> file &&
  31        git add file &&
  32        echo "more foo" > FAKE_MSG &&
  33        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
  34'
  36test_expect_success '--no-verify with no hook' '
  38        echo "bar" > file &&
  40        git add file &&
  41        git commit --no-verify -m "bar"
  42'
  44test_expect_success '--no-verify with no hook (editor)' '
  46        echo "more bar" > file &&
  48        git add file &&
  49        echo "more bar" > FAKE_MSG &&
  50        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
  51'
  53# now install hook that always succeeds
  55HOOKDIR="$(git rev-parse --git-dir)/hooks"
  56HOOK="$HOOKDIR/commit-msg"
  57mkdir -p "$HOOKDIR"
  58cat > "$HOOK" <<EOF
  59#!/bin/sh
  60exit 0
  61EOF
  62chmod +x "$HOOK"
  63test_expect_success 'with succeeding hook' '
  65        echo "more" >> file &&
  67        git add file &&
  68        git commit -m "more"
  69'
  71test_expect_success 'with succeeding hook (editor)' '
  73        echo "more more" >> file &&
  75        git add file &&
  76        echo "more more" > FAKE_MSG &&
  77        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
  78'
  80test_expect_success '--no-verify with succeeding hook' '
  82        echo "even more" >> file &&
  84        git add file &&
  85        git commit --no-verify -m "even more"
  86'
  88test_expect_success '--no-verify with succeeding hook (editor)' '
  90        echo "even more more" >> file &&
  92        git add file &&
  93        echo "even more more" > FAKE_MSG &&
  94        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
  95'
  97# now a hook that fails
  99cat > "$HOOK" <<EOF
 100#!/bin/sh
 101exit 1
 102EOF
 103test_expect_success 'with failing hook' '
 105        echo "another" >> file &&
 107        git add file &&
 108        ! git commit -m "another"
 109'
 111test_expect_success 'with failing hook (editor)' '
 113        echo "more another" >> file &&
 115        git add file &&
 116        echo "more another" > FAKE_MSG &&
 117        ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit)
 118'
 120test_expect_success '--no-verify with failing hook' '
 122        echo "stuff" >> file &&
 124        git add file &&
 125        git commit --no-verify -m "stuff"
 126'
 128test_expect_success '--no-verify with failing hook (editor)' '
 130        echo "more stuff" >> file &&
 132        git add file &&
 133        echo "more stuff" > FAKE_MSG &&
 134        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
 135'
 137chmod -x "$HOOK"
 139test_expect_success 'with non-executable hook' '
 140        echo "content" >> file &&
 142        git add file &&
 143        git commit -m "content"
 144'
 146test_expect_success 'with non-executable hook (editor)' '
 148        echo "content again" >> file &&
 150        git add file &&
 151        echo "content again" > FAKE_MSG &&
 152        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again"
 153'
 155test_expect_success '--no-verify with non-executable hook' '
 157        echo "more content" >> file &&
 159        git add file &&
 160        git commit --no-verify -m "more content"
 161'
 163test_expect_success '--no-verify with non-executable hook (editor)' '
 165        echo "even more content" >> file &&
 167        git add file &&
 168        echo "even more content" > FAKE_MSG &&
 169        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
 170'
 172# now a hook that edits the commit message
 174cat > "$HOOK" <<'EOF'
 175#!/bin/sh
 176echo "new message" > "$1"
 177exit 0
 178EOF
 179chmod +x "$HOOK"
 180commit_msg_is () {
 182        test "`git log --pretty=format:%s%b -1`" = "$1"
 183}
 184test_expect_success 'hook edits commit message' '
 186        echo "additional" >> file &&
 188        git add file &&
 189        git commit -m "additional" &&
 190        commit_msg_is "new message"
 191'
 193test_expect_success 'hook edits commit message (editor)' '
 195        echo "additional content" >> file &&
 197        git add file &&
 198        echo "additional content" > FAKE_MSG &&
 199        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
 200        commit_msg_is "new message"
 201'
 203test_expect_success "hook doesn't edit commit message" '
 205        echo "plus" >> file &&
 207        git add file &&
 208        git commit --no-verify -m "plus" &&
 209        commit_msg_is "plus"
 210'
 212test_expect_success "hook doesn't edit commit message (editor)" '
 214        echo "more plus" >> file &&
 216        git add file &&
 217        echo "more plus" > FAKE_MSG &&
 218        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify &&
 219        commit_msg_is "more plus"
 220'
 222test_done