6aa83204c25350572c34f362dfcb3c164303c63b
   1#!/bin/sh
   2
   3test_description='pre-commit hook'
   4
   5. ./test-lib.sh
   6
   7HOOKDIR="$(git rev-parse --git-dir)/hooks"
   8PRECOMMIT="$HOOKDIR/pre-commit"
   9
  10# Prepare sample scripts that write their $0 to actual_hooks
  11test_expect_success 'sample script setup' '
  12        mkdir -p "$HOOKDIR" &&
  13        write_script "$HOOKDIR/success.sample" <<-\EOF &&
  14        echo $0 >>actual_hooks
  15        exit 0
  16        EOF
  17        write_script "$HOOKDIR/fail.sample" <<-\EOF &&
  18        echo $0 >>actual_hooks
  19        exit 1
  20        EOF
  21        write_script "$HOOKDIR/non-exec.sample" <<-\EOF &&
  22        echo $0 >>actual_hooks
  23        exit 1
  24        EOF
  25        chmod -x "$HOOKDIR/non-exec.sample" &&
  26        write_script "$HOOKDIR/require-prefix.sample" <<-\EOF &&
  27        echo $0 >>actual_hooks
  28        test $GIT_PREFIX = "success/"
  29        EOF
  30        write_script "$HOOKDIR/check-author.sample" <<-\EOF
  31        echo $0 >>actual_hooks
  32        test "$GIT_AUTHOR_NAME" = "New Author" &&
  33        test "$GIT_AUTHOR_EMAIL" = "newauthor@example.com"
  34        EOF
  35'
  36
  37test_expect_success 'with no hook' '
  38        test_when_finished "rm -f actual_hooks" &&
  39        echo "foo" >file &&
  40        git add file &&
  41        git commit -m "first" &&
  42        test_path_is_missing actual_hooks
  43'
  44
  45test_expect_success '--no-verify with no hook' '
  46        test_when_finished "rm -f actual_hooks" &&
  47        echo "bar" >file &&
  48        git add file &&
  49        git commit --no-verify -m "bar" &&
  50        test_path_is_missing actual_hooks
  51'
  52
  53test_expect_success 'with succeeding hook' '
  54        test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
  55        cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
  56        echo "$PRECOMMIT" >expected_hooks &&
  57        echo "more" >>file &&
  58        git add file &&
  59        git commit -m "more" &&
  60        test_cmp expected_hooks actual_hooks
  61'
  62
  63test_expect_success '--no-verify with succeeding hook' '
  64        test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
  65        cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
  66        echo "even more" >>file &&
  67        git add file &&
  68        git commit --no-verify -m "even more" &&
  69        test_path_is_missing actual_hooks
  70'
  71
  72test_expect_success 'with failing hook' '
  73        test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
  74        cp "$HOOKDIR/fail.sample" "$PRECOMMIT" &&
  75        echo "$PRECOMMIT" >expected_hooks &&
  76        echo "another" >>file &&
  77        git add file &&
  78        test_must_fail git commit -m "another" &&
  79        test_cmp expected_hooks actual_hooks
  80'
  81
  82test_expect_success '--no-verify with failing hook' '
  83        test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
  84        cp "$HOOKDIR/fail.sample" "$PRECOMMIT" &&
  85        echo "stuff" >>file &&
  86        git add file &&
  87        git commit --no-verify -m "stuff" &&
  88        test_path_is_missing actual_hooks
  89'
  90
  91test_expect_success POSIXPERM 'with non-executable hook' '
  92        test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
  93        cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
  94        echo "content" >>file &&
  95        git add file &&
  96        git commit -m "content" &&
  97        test_path_is_missing actual_hooks
  98'
  99
 100test_expect_success POSIXPERM '--no-verify with non-executable hook' '
 101        test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
 102        cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
 103        echo "more content" >>file &&
 104        git add file &&
 105        git commit --no-verify -m "more content" &&
 106        test_path_is_missing actual_hooks
 107'
 108
 109test_expect_success 'with hook requiring GIT_PREFIX' '
 110        test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks success" &&
 111        cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" &&
 112        echo "$PRECOMMIT" >expected_hooks &&
 113        echo "more content" >>file &&
 114        git add file &&
 115        mkdir success &&
 116        (
 117                cd success &&
 118                git commit -m "hook requires GIT_PREFIX = success/"
 119        ) &&
 120        test_cmp expected_hooks actual_hooks
 121'
 122
 123test_expect_success 'with failing hook requiring GIT_PREFIX' '
 124        test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks fail" &&
 125        cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" &&
 126        echo "$PRECOMMIT" >expected_hooks &&
 127        echo "more content" >>file &&
 128        git add file &&
 129        mkdir fail &&
 130        (
 131                cd fail &&
 132                test_must_fail git commit -m "hook must fail"
 133        ) &&
 134        git checkout -- file &&
 135        test_cmp expected_hooks actual_hooks
 136'
 137
 138test_expect_success 'check the author in hook' '
 139        test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
 140        cp "$HOOKDIR/check-author.sample" "$PRECOMMIT" &&
 141        cat >expected_hooks <<-EOF &&
 142        $PRECOMMIT
 143        $PRECOMMIT
 144        $PRECOMMIT
 145        EOF
 146        test_must_fail git commit --allow-empty -m "by a.u.thor" &&
 147        (
 148                GIT_AUTHOR_NAME="New Author" &&
 149                GIT_AUTHOR_EMAIL="newauthor@example.com" &&
 150                export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL &&
 151                git commit --allow-empty -m "by new.author via env" &&
 152                git show -s
 153        ) &&
 154        git commit --author="New Author <newauthor@example.com>" \
 155                --allow-empty -m "by new.author via command line" &&
 156        git show -s &&
 157        test_cmp expected_hooks actual_hooks
 158'
 159
 160test_done