Restore `stg sink --nopush` capability
[stgit.git] / t / t2703-refresh-pre-commit-hook.sh
blob3d60c555fbb4a1615bbb4b77eac798a2022baece
1 #!/bin/sh
3 test_description='pre-commit hook'
5 . ./test-lib.sh
7 test_expect_success 'Initialise StGit' '
8 stg init &&
9 stg new -m "pre-commit-patch"
10 echo "new file" >> file &&
11 git add file
12 stg refresh
15 # install hook
16 HOOKDIR="$(git rev-parse --git-dir)/hooks"
17 HOOK="$HOOKDIR/pre-commit"
18 mkdir -p "$HOOKDIR"
19 write_script "$HOOK" <<EOF
20 exit 0
21 EOF
23 test_expect_success 'refresh --no-verify with succeeding hook' '
24 echo "no-verify pre-commit-hook-success" >> file &&
25 stg refresh --no-verify
28 test_expect_success 'refresh --no-verify with path limiting, succeeding hook' '
29 echo "no-verify pre-commit-hook-path-limiting-success" >> file &&
30 stg refresh file --no-verify
33 test_expect_success 'refresh with succeeding hook' '
34 echo "pre-commit-hook-success" >> file &&
35 stg refresh
38 test_expect_success 'refresh with path limiting, succeeding hook' '
39 echo "pre-commit-hook-path-limiting-success" >> file &&
40 stg refresh file
43 git config core.hooksPath .my-hooks
44 mv "$HOOKDIR" .my-hooks
45 test_expect_success 'refresh with core.hooksPath' '
46 echo "pre-commit-hook-path-limiting-success" >> file &&
47 stg refresh file
49 mv .my-hooks "$HOOKDIR"
50 git config --unset core.hooksPath
52 # now a hook that fails
53 write_script "$HOOK" <<'EOF'
54 exit 1
55 EOF
57 test_expect_success 'refresh --no-verify with failing hook' '
58 echo "no-verify pre-commit-hook-fail" >> file &&
59 stg refresh --no-verify
62 test_expect_success 'refresh --no-verify with path limiting, failing hook' '
63 echo "no-verify pre-commit-hook-path-limiting-fail" >> file &&
64 stg refresh file --no-verify
67 test_expect_success 'refresh with failing hook' '
68 echo "pre-commit-hook-fail" >> file &&
69 command_error stg refresh 2>&1 |
70 grep -e "pre-commit hook failed" &&
71 git reset HEAD
74 test_expect_success 'refresh with path limiting, failing hook' '
75 echo "pre-commit-hook-path-limiting-fail" >> file &&
76 command_error stg refresh file 2>&1 |
77 grep -e "pre-commit hook failed" &&
78 git reset HEAD
81 chmod -x "$HOOK"
82 test_expect_success 'refresh --no-verify with non-executable hook' '
83 echo "no-verify pre-commit-hook-non-executable" >> file &&
84 stg refresh --no-verify
87 test_expect_success 'refresh with non-executable hook' '
88 echo "pre-commit-hook-non-executable" >> file &&
89 stg refresh
92 # now a hook that edits the files added in index
93 # The hook tests for trailing white spaces
94 # If finds files, then fixes them and returns non-zero exit status
95 write_script "$HOOK" <<'EOF'
96 git diff-index --check HEAD -- && exit
97 sed -e 's/[[:space:]]*$//' file > file.new
98 mv -- file.new file
99 exit 1
102 test_expect_success 'refresh --no-verify with failing hook that modifies file' '
103 echo "no-verify pre-commit-hook-no-remove-whitespace " >> file &&
104 stg refresh --no-verify &&
105 [ "$(tail -1 file)" = "no-verify pre-commit-hook-no-remove-whitespace " ]
108 test_expect_success 'refresh --no-verify with path limiting, failing hook that modifies file' '
109 echo "no-verify pre-commit-hook-path-limiting-no-remove-whitespace " >> file &&
110 stg refresh file --no-verify &&
111 [ "$(tail -1 file)" = "no-verify pre-commit-hook-path-limiting-no-remove-whitespace " ]
114 test_expect_success 'refresh with succeeding hook, does not modify file' '
115 echo "pre-commit-hook-no-whitespace" >> file &&
116 stg refresh &&
117 [ "$(tail -1 file)" = "pre-commit-hook-no-whitespace" ]
120 test_expect_success 'refresh with path limiting, succeeding hook, does not modify file' '
121 echo "pre-commit-hook-path-limiting-no-whitespace" >> file &&
122 stg refresh file &&
123 [ "$(tail -1 file)" = "pre-commit-hook-path-limiting-no-whitespace" ]
126 test_expect_success 'refresh with failing hook that modifies file' '
127 echo "pre-commit-hook-remove-whitespace " >> file &&
128 command_error stg refresh 2>&1 |
129 grep -e "pre-commit hook failed" &&
130 [ "$(git diff --name-only)" = "file" ] &&
131 [ "$(git diff --cached --name-only)" = "file" ] &&
132 [ "$(tail -1 file)" = "pre-commit-hook-remove-whitespace" ]
135 test_expect_success 'refresh again after adding modified files to index' '
136 stg add file
137 stg refresh
140 # now a hook that edits the files added in index and adds them to index
141 # The hook tests for trailing white spaces
142 # If finds files, then fixes them and returns non-zero exit status
143 write_script "$HOOK" <<'EOF'
144 git diff-index --check HEAD -- && exit
145 sed -e 's/[[:space:]]*$//' file > file.new
146 mv -- file.new file
147 git add file
150 test_expect_success 'refresh with failing hook that modifies file, adds to index' '
151 echo "pre-commit-hook-remove-whitespace-add-index " >> file &&
152 stg refresh &&
153 [ "$(tail -1 file)" = "pre-commit-hook-remove-whitespace-add-index" ] &&
154 git diff-index --quiet HEAD
157 test_expect_success 'refresh with path limiting, failing hook that modifies file, adds to index' '
158 echo "pre-commit-hook-remove-whitespace-add-index " >> file &&
159 stg refresh file &&
160 [ "$(tail -1 file)" = "pre-commit-hook-remove-whitespace-add-index" ] &&
161 git diff-index --quiet HEAD
164 test_done