cef709555eb9c3e3dec0016909a17ce7cf32650a
   1#!/bin/sh
   2
   3test_description='prepare-commit-msg hook'
   4
   5. ./test-lib.sh
   6
   7test_expect_success 'with no hook' '
   8
   9        echo "foo" > file &&
  10        git add file &&
  11        git commit -m "first"
  12
  13'
  14
  15# set up fake editor for interactive editing
  16cat > fake-editor <<'EOF'
  17#!/bin/sh
  18exit 0
  19EOF
  20chmod +x fake-editor
  21
  22## 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
  27# 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'
  33
  34if test "$2" = commit
  35then
  36        source=$(git rev-parse "$3")
  37else
  38        source=${2-default}
  39fi
  40if test "$GIT_EDITOR" = :
  41then
  42        sed -e "1s/.*/$source (no editor)/" "$1" >msg.tmp
  43else
  44        sed -e "1s/.*/$source/" "$1" >msg.tmp
  45fi
  46mv msg.tmp "$1"
  47exit 0
  48EOF
  49chmod +x "$HOOK"
  50
  51echo dummy template > "$(git rev-parse --git-dir)/template"
  52
  53test_expect_success 'with hook (-m)' '
  54
  55        echo "more" >> file &&
  56        git add file &&
  57        git commit -m "more" &&
  58        test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
  59
  60'
  61
  62test_expect_success 'with hook (-m editor)' '
  63
  64        echo "more" >> file &&
  65        git add file &&
  66        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" &&
  67        test "$(git log -1 --pretty=format:%s)" = message
  68
  69'
  70
  71test_expect_success 'with hook (-t)' '
  72
  73        echo "more" >> file &&
  74        git add file &&
  75        git commit -t "$(git rev-parse --git-dir)/template" &&
  76        test "$(git log -1 --pretty=format:%s)" = template
  77
  78'
  79
  80test_expect_success 'with hook (-F)' '
  81
  82        echo "more" >> file &&
  83        git add file &&
  84        (echo more | git commit -F -) &&
  85        test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
  86
  87'
  88
  89test_expect_success 'with hook (-F editor)' '
  90
  91        echo "more" >> file &&
  92        git add file &&
  93        (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) &&
  94        test "$(git log -1 --pretty=format:%s)" = message
  95
  96'
  97
  98test_expect_success 'with hook (-C)' '
  99
 100        head=$(git rev-parse HEAD) &&
 101        echo "more" >> file &&
 102        git add file &&
 103        git commit -C $head &&
 104        test "$(git log -1 --pretty=format:%s)" = "$head (no editor)"
 105
 106'
 107
 108test_expect_success 'with hook (editor)' '
 109
 110        echo "more more" >> file &&
 111        git add file &&
 112        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
 113        test "$(git log -1 --pretty=format:%s)" = default
 114
 115'
 116
 117test_expect_success 'with hook (--amend)' '
 118
 119        head=$(git rev-parse HEAD) &&
 120        echo "more" >> file &&
 121        git add file &&
 122        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend &&
 123        test "$(git log -1 --pretty=format:%s)" = "$head"
 124
 125'
 126
 127test_expect_success 'with hook (-c)' '
 128
 129        head=$(git rev-parse HEAD) &&
 130        echo "more" >> file &&
 131        git add file &&
 132        GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head &&
 133        test "$(git log -1 --pretty=format:%s)" = "$head"
 134
 135'
 136
 137test_expect_success 'with hook (merge)' '
 138
 139        test_when_finished "git checkout -f master" &&
 140        git checkout -B other HEAD@{1} &&
 141        echo "more" >>file &&
 142        git add file &&
 143        git commit -m other &&
 144        git checkout - &&
 145        git merge --no-ff other &&
 146        test "$(git log -1 --pretty=format:%s)" = "merge (no editor)"
 147'
 148
 149test_expect_success 'with hook and editor (merge)' '
 150
 151        test_when_finished "git checkout -f master" &&
 152        git checkout -B other HEAD@{1} &&
 153        echo "more" >>file &&
 154        git add file &&
 155        git commit -m other &&
 156        git checkout - &&
 157        env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other &&
 158        test "$(git log -1 --pretty=format:%s)" = "merge"
 159'
 160
 161cat > "$HOOK" <<'EOF'
 162#!/bin/sh
 163exit 1
 164EOF
 165
 166test_expect_success 'with failing hook' '
 167
 168        test_when_finished "git checkout -f master" &&
 169        head=$(git rev-parse HEAD) &&
 170        echo "more" >> file &&
 171        git add file &&
 172        test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
 173
 174'
 175
 176test_expect_success 'with failing hook (--no-verify)' '
 177
 178        test_when_finished "git checkout -f master" &&
 179        head=$(git rev-parse HEAD) &&
 180        echo "more" >> file &&
 181        git add file &&
 182        test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
 183
 184'
 185
 186test_expect_success 'with failing hook (merge)' '
 187
 188        test_when_finished "git checkout -f master" &&
 189        git checkout -B other HEAD@{1} &&
 190        echo "more" >> file &&
 191        git add file &&
 192        rm -f "$HOOK" &&
 193        git commit -m other &&
 194        write_script "$HOOK" <<-EOF &&
 195        exit 1
 196        EOF
 197        git checkout - &&
 198        test_must_fail git merge --no-ff other
 199
 200'
 201
 202test_done