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
  21## Not using test_set_editor here so we can easily ensure the editor variable
  23## is only set for the editor tests
  24FAKE_EDITOR="$(pwd)/fake-editor"
  25export FAKE_EDITOR
  26# now install hook that always succeeds and adds a message
  28HOOKDIR="$(git rev-parse --git-dir)/hooks"
  29HOOK="$HOOKDIR/prepare-commit-msg"
  30mkdir -p "$HOOKDIR"
  31echo "#!$SHELL_PATH" > "$HOOK"
  32cat >> "$HOOK" <<'EOF'
  33if test "$2" = commit; then
  35  source=$(git rev-parse "$3")
  36else
  37  source=${2-default}
  38fi
  39if test "$GIT_EDITOR" = :; then
  40  sed -e "1s/.*/$source (no editor)/" "$1" > msg.tmp
  41else
  42  sed -e "1s/.*/$source/" "$1" > msg.tmp
  43fi
  44mv msg.tmp "$1"
  45exit 0
  46EOF
  47chmod +x "$HOOK"
  48echo dummy template > "$(git rev-parse --git-dir)/template"
  50test_expect_success 'with hook (-m)' '
  52        echo "more" >> file &&
  54        git add file &&
  55        git commit -m "more" &&
  56        test "`git log -1 --pretty=format:%s`" = "message (no editor)"
  57'
  59test_expect_success 'with hook (-m editor)' '
  61        echo "more" >> file &&
  63        git add file &&
  64        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" &&
  65        test "`git log -1 --pretty=format:%s`" = message
  66'
  68test_expect_success 'with hook (-t)' '
  70        echo "more" >> file &&
  72        git add file &&
  73        git commit -t "$(git rev-parse --git-dir)/template" &&
  74        test "`git log -1 --pretty=format:%s`" = template
  75'
  77test_expect_success 'with hook (-F)' '
  79        echo "more" >> file &&
  81        git add file &&
  82        (echo more | git commit -F -) &&
  83        test "`git log -1 --pretty=format:%s`" = "message (no editor)"
  84'
  86test_expect_success 'with hook (-F editor)' '
  88        echo "more" >> file &&
  90        git add file &&
  91        (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) &&
  92        test "`git log -1 --pretty=format:%s`" = message
  93'
  95test_expect_success 'with hook (-C)' '
  97        head=`git rev-parse HEAD` &&
  99        echo "more" >> file &&
 100        git add file &&
 101        git commit -C $head &&
 102        test "`git log -1 --pretty=format:%s`" = "$head (no editor)"
 103'
 105test_expect_success 'with hook (editor)' '
 107        echo "more more" >> file &&
 109        git add file &&
 110        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
 111        test "`git log -1 --pretty=format:%s`" = default
 112'
 114test_expect_success 'with hook (--amend)' '
 116        head=`git rev-parse HEAD` &&
 118        echo "more" >> file &&
 119        git add file &&
 120        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend &&
 121        test "`git log -1 --pretty=format:%s`" = "$head"
 122'
 124test_expect_success 'with hook (-c)' '
 126        head=`git rev-parse HEAD` &&
 128        echo "more" >> file &&
 129        git add file &&
 130        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head &&
 131        test "`git log -1 --pretty=format:%s`" = "$head"
 132'
 134test_expect_success 'with hook (merge)' '
 136        head=`git rev-parse HEAD` &&
 138        git checkout -b other HEAD@{1} &&
 139        echo "more" >> file &&
 140        git add file &&
 141        git commit -m other &&
 142        git checkout - &&
 143        git merge other &&
 144        test "`git log -1 --pretty=format:%s`" = merge
 145'
 146cat > "$HOOK" <<'EOF'
 148#!/bin/sh
 149exit 1
 150EOF
 151test_expect_success 'with failing hook' '
 153        head=`git rev-parse HEAD` &&
 155        echo "more" >> file &&
 156        git add file &&
 157        ! GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
 158'
 160test_expect_success 'with failing hook (--no-verify)' '
 162        head=`git rev-parse HEAD` &&
 164        echo "more" >> file &&
 165        git add file &&
 166        ! GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
 167'
 169test_expect_success 'with failing hook (merge)' '
 171        git checkout -B other HEAD@{1} &&
 173        echo "more" >> file &&
 174        git add file &&
 175        rm -f "$HOOK" &&
 176        git commit -m other &&
 177        write_script "$HOOK" <<-EOF
 178        exit 1
 179        EOF
 180        git checkout - &&
 181        test_must_fail git merge other
 182'
 184test_done