1#!/bin/sh
   2test_description='prepare-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
  18exit 0
  19EOF
  20chmod +x fake-editor
  21FAKE_EDITOR="$(pwd)/fake-editor"
  22export FAKE_EDITOR
  23# now install hook that always succeeds and adds a message
  25HOOKDIR="$(git rev-parse --git-dir)/hooks"
  26HOOK="$HOOKDIR/prepare-commit-msg"
  27mkdir -p "$HOOKDIR"
  28echo "#!$SHELL_PATH" > "$HOOK"
  29cat >> "$HOOK" <<'EOF'
  30if test "$2" = commit; then
  32  source=$(git-rev-parse "$3")
  33else
  34  source=${2-default}
  35fi
  36if test "$GIT_EDITOR" = :; then
  37  sed -e "1s/.*/$source (no editor)/" "$1" > msg.tmp
  38else
  39  sed -e "1s/.*/$source/" "$1" > msg.tmp
  40fi
  41mv msg.tmp "$1"
  42exit 0
  43EOF
  44chmod +x "$HOOK"
  45echo dummy template > "$(git rev-parse --git-dir)/template"
  47test_expect_success 'with hook (-m)' '
  49        echo "more" >> file &&
  51        git add file &&
  52        git commit -m "more" &&
  53        test "`git log -1 --pretty=format:%s`" = "message (no editor)"
  54'
  56test_expect_success 'with hook (-m editor)' '
  58        echo "more" >> file &&
  60        git add file &&
  61        GIT_EDITOR="$FAKE_EDITOR" git commit -e -m "more more" &&
  62        test "`git log -1 --pretty=format:%s`" = message
  63'
  65test_expect_success 'with hook (-t)' '
  67        echo "more" >> file &&
  69        git add file &&
  70        git commit -t "$(git rev-parse --git-dir)/template" &&
  71        test "`git log -1 --pretty=format:%s`" = template
  72'
  74test_expect_success 'with hook (-F)' '
  76        echo "more" >> file &&
  78        git add file &&
  79        (echo more | git commit -F -) &&
  80        test "`git log -1 --pretty=format:%s`" = "message (no editor)"
  81'
  83test_expect_success 'with hook (-F editor)' '
  85        echo "more" >> file &&
  87        git add file &&
  88        (echo more more | GIT_EDITOR="$FAKE_EDITOR" git commit -e -F -) &&
  89        test "`git log -1 --pretty=format:%s`" = message
  90'
  92test_expect_success 'with hook (-C)' '
  94        head=`git rev-parse HEAD` &&
  96        echo "more" >> file &&
  97        git add file &&
  98        git commit -C $head &&
  99        test "`git log -1 --pretty=format:%s`" = "$head (no editor)"
 100'
 102test_expect_success 'with hook (editor)' '
 104        echo "more more" >> file &&
 106        git add file &&
 107        GIT_EDITOR="$FAKE_EDITOR" git commit &&
 108        test "`git log -1 --pretty=format:%s`" = default
 109'
 111test_expect_success 'with hook (--amend)' '
 113        head=`git rev-parse HEAD` &&
 115        echo "more" >> file &&
 116        git add file &&
 117        GIT_EDITOR="$FAKE_EDITOR" git commit --amend &&
 118        test "`git log -1 --pretty=format:%s`" = "$head"
 119'
 121test_expect_success 'with hook (-c)' '
 123        head=`git rev-parse HEAD` &&
 125        echo "more" >> file &&
 126        git add file &&
 127        GIT_EDITOR="$FAKE_EDITOR" git commit -c $head &&
 128        test "`git log -1 --pretty=format:%s`" = "$head"
 129'
 131cat > "$HOOK" <<'EOF'
 133#!/bin/sh
 134exit 1
 135EOF
 136test_expect_success 'with failing hook' '
 138        head=`git rev-parse HEAD` &&
 140        echo "more" >> file &&
 141        git add file &&
 142        ! GIT_EDITOR="$FAKE_EDITOR" git commit -c $head
 143'
 145test_expect_success 'with failing hook (--no-verify)' '
 147        head=`git rev-parse HEAD` &&
 149        echo "more" >> file &&
 150        git add file &&
 151        ! GIT_EDITOR="$FAKE_EDITOR" git commit --no-verify -c $head
 152'
 154test_done