tg.sh: handle help -h
[topgit/pro.git] / t / t2000-hook-installation.sh
bloba5f1a4eb1fc75d34ac7e9bdda18f3403f99b3a4d
1 #!/bin/sh
3 test_description='test installation of pre-commit hook'
5 TEST_NO_CREATE_REPO=1
7 . ./test-lib.sh
9 test_plan 21
11 has_tg_setup() {
12 test -s "${1:-.}/.git/info/attributes" &&
13 test -s "${1:-.}/.git/hooks/pre-commit" &&
14 test -x "${1:-.}/.git/hooks/pre-commit" &&
15 gcmd_="$(git -C "${1:-.}" config merge.ours.driver)" &&
16 test z"$gcmd_" = z"touch %A"
19 has_no_tg_setup() {
20 test ! -e "${1:-.}/.git/info/attributes" &&
21 test ! -e "${1:-.}/.git/hooks/pre-commit" &&
22 test_must_fail git -C "${1:-.}" config merge.ours.driver
25 has_no_tg_setup_bare() {
26 test ! -e "${1:-.}/info/attributes" &&
27 test ! -e "${1:-.}/hooks/pre-commit" &&
28 test_must_fail git -C "${1:-.}" config merge.ours.driver
31 test_expect_success 'setup_ours' '
32 tab=" " && # single tab character in quotes
33 test_create_repo r1 &&
34 test ! -e r1/.git/info/attributes &&
35 tg_test_include -C r1 &&
36 cd r1 && setup_ours && cd .. &&
37 test -f r1/.git/info/attributes &&
38 test -r r1/.git/info/attributes &&
39 grep -q "^[.]topmsg[ $tab][ $tab]*merge=ours$" r1/.git/info/attributes &&
40 grep -q "^[.]topdeps[ $tab][ $tab]*merge=ours$" r1/.git/info/attributes &&
41 gcmn="$(git -C r1 config merge.ours.name)" &&
42 test -n "$gcmn" &&
43 gcmd="$(git -C r1 config merge.ours.driver)" &&
44 test -n "$gcmd" && test z"$gcmd" = z"touch %A" &&
45 test ! -e r1/.git/hooks/pre-commit &&
46 rm -rf r1
49 tgshell="$PWD/tgsh"
50 >"$tgshell" || die
52 test_expect_success 'setup_hook pre-commit' '
53 test_create_repo r2 &&
54 test ! -e r2/.git/hooks/pre-commit &&
55 tg_test_include -C r2 &&
56 cd r2 && setup_hook "pre-commit" && cd .. &&
57 test -f r2/.git/hooks/pre-commit &&
58 test -x r2/.git/hooks/pre-commit &&
59 test ! -e r2/.git/hooks/pre-commit-chain &&
60 sed -n 1p <r2/.git/hooks/pre-commit | tee "$tgshell" | grep -q "^#!" &&
61 grep -F -q "${0##*/}" r2/.git/hooks/pre-commit &&
62 grep -F -q "/pre-commit" r2/.git/hooks/pre-commit &&
63 test $(wc -l <r2/.git/hooks/pre-commit) -eq 2 &&
64 test ! -e r2/.git/info/attributes &&
65 test_must_fail git -C r2 config merge.ours.name &&
66 test_must_fail git -C r2 config merge.ours.driver &&
67 rm -rf r2
70 read -r tgshellbin <"$tgshell" || die
71 write_script dummy.sh "${tgshellbin#??}" <<-EOT || die
72 echo "dummy.sh here"
73 EOT
75 test_expect_success LASTOK 'setup_hook pre-commit edits matching shell once' '
76 rm -rf r2 && test_create_repo r2 &&
77 test ! -e r2/.git/hooks/pre-commit &&
78 mkdir -p r2/.git/hooks &&
79 cat "$tgshell" >r2/.git/hooks/pre-commit &&
80 echo "echo editme" >>r2/.git/hooks/pre-commit &&
81 chmod a+x r2/.git/hooks/pre-commit &&
82 tg_test_include -C r2 &&
83 # kludge here for some broken ancient sh implementations
84 sane_unset -f cat &&
85 cd r2 && setup_hook "pre-commit" && cd .. &&
86 test -f r2/.git/hooks/pre-commit &&
87 test -x r2/.git/hooks/pre-commit &&
88 test ! -e r2/.git/hooks/pre-commit-chain &&
89 lines=$(wc -l <r2/.git/hooks/pre-commit) &&
90 test ${lines:-0} -gt 2 &&
91 cd r2 && setup_hook "pre-commit" && cd .. &&
92 lines2=$(wc -l <r2/.git/hooks/pre-commit) &&
93 test "$lines" = "$lines2" &&
94 rm -rf r2
97 test_expect_success 'setup_hook pre-commit chains symlink' '
98 rm -rf r2 && test_create_repo r2 &&
99 test ! -e r2/.git/hooks/pre-commit &&
100 mkdir -p r2/.git/hooks &&
101 ln -s ../../../dummy.sh r2/.git/hooks/pre-commit &&
102 tg_test_include -C r2 &&
103 cd r2 && setup_hook "pre-commit" && cd .. &&
104 test -f r2/.git/hooks/pre-commit &&
105 test -x r2/.git/hooks/pre-commit &&
106 test -e r2/.git/hooks/pre-commit-chain &&
107 rm -rf r2
110 test_expect_success 'setup_hook pre-commit chains dead symlink' '
111 rm -rf r2 && test_create_repo r2 &&
112 test ! -e r2/.git/hooks/pre-commit &&
113 mkdir -p r2/.git/hooks &&
114 ln -s ../../../dummy-no-such.sh r2/.git/hooks/pre-commit &&
115 tg_test_include -C r2 &&
116 cd r2 && setup_hook "pre-commit" && cd .. &&
117 test -f r2/.git/hooks/pre-commit &&
118 test -x r2/.git/hooks/pre-commit &&
119 { test -e r2/.git/hooks/pre-commit-chain || test -L r2/.git/hooks/pre-commit-chain; } &&
120 rm -rf r2
123 test_expect_success 'setup_hook pre-commit chains multply-linked' '
124 rm -rf r2 && test_create_repo r2 &&
125 test ! -e r2/.git/hooks/pre-commit &&
126 mkdir -p r2/.git/hooks &&
127 ln dummy.sh r2/.git/hooks/pre-commit &&
128 tg_test_include -C r2 &&
129 cd r2 && setup_hook "pre-commit" && cd .. &&
130 test -f r2/.git/hooks/pre-commit &&
131 test -x r2/.git/hooks/pre-commit &&
132 test -e r2/.git/hooks/pre-commit-chain &&
133 rm -rf r2
136 test_expect_success 'setup_hook pre-commit chains non-executable' '
137 rm -rf r2 && test_create_repo r2 &&
138 test ! -e r2/.git/hooks/pre-commit &&
139 mkdir -p r2/.git/hooks &&
140 cp -p dummy.sh r2/.git/hooks/pre-commit &&
141 chmod a-x r2/.git/hooks/pre-commit &&
142 tg_test_include -C r2 &&
143 cd r2 && setup_hook "pre-commit" && cd .. &&
144 test -f r2/.git/hooks/pre-commit &&
145 test -x r2/.git/hooks/pre-commit &&
146 test -e r2/.git/hooks/pre-commit-chain &&
147 rm -rf r2
150 test_expect_success 'setup_hook pre-commit chains non-writable' '
151 rm -rf r2 && test_create_repo r2 &&
152 test ! -e r2/.git/hooks/pre-commit &&
153 mkdir -p r2/.git/hooks &&
154 cp -p dummy.sh r2/.git/hooks/pre-commit &&
155 chmod a-w r2/.git/hooks/pre-commit &&
156 tg_test_include -C r2 &&
157 cd r2 && setup_hook "pre-commit" && cd .. &&
158 test -f r2/.git/hooks/pre-commit &&
159 test -x r2/.git/hooks/pre-commit &&
160 test -e r2/.git/hooks/pre-commit-chain &&
161 rm -rf r2
164 test_expect_success 'setup_hook pre-commit chains non-readable' '
165 rm -rf r2 && test_create_repo r2 &&
166 test ! -e r2/.git/hooks/pre-commit &&
167 mkdir -p r2/.git/hooks &&
168 cp -p dummy.sh r2/.git/hooks/pre-commit &&
169 chmod a-r r2/.git/hooks/pre-commit &&
170 tg_test_include -C r2 &&
171 cd r2 && setup_hook "pre-commit" && cd .. &&
172 test -f r2/.git/hooks/pre-commit &&
173 test -x r2/.git/hooks/pre-commit &&
174 test -e r2/.git/hooks/pre-commit-chain &&
175 rm -rf r2
178 TG_CMDS="
179 --version
180 --status
181 --hooks-path
182 --exec-path
183 --awk-path
184 --top-bases
185 annihilate
186 base
187 checkout
188 contains
189 create
190 delete
191 depend
192 export
193 files
194 import
195 info
197 mail
198 next
199 patch
200 prev
201 push
202 rebase
203 remote
204 revert
205 shell
206 status
207 summary
209 update
210 version
213 tg_cmd_will_setup() {
214 case "$1" in
215 --version|--status|--hooks-path|--exec-path|--awk-path|--top-bases| \
216 base|contains|export|files|info|log|mail|next|patch|prev|rebase|revert|shell|status|st|summary|tag|version)
217 return 1
218 esac
219 return 0
222 test_expect_success 'no setup happens for help' '
223 test_create_repo r3 && cd r3 &&
224 say "# checking tg help variations" &&
225 test_might_fail tg && has_no_tg_setup &&
226 test_might_fail tg -h && has_no_tg_setup &&
227 test_might_fail tg --help && has_no_tg_setup &&
228 test_might_fail tg --bogus && has_no_tg_setup &&
229 for cmd in $TG_CMDS; do
230 say "# checking tg $cmd help variations" &&
231 test_might_fail </dev/null tg $cmd -h && has_no_tg_setup &&
232 test_might_fail </dev/null tg $cmd --help && has_no_tg_setup || return
233 done &&
234 has_no_tg_setup &&
235 cd .. && rm -rf r3
238 test_expect_success 'no setup happens for exempted commands' '
239 test_create_repo r4 && cd r4 &&
240 for cmd in $TG_CMDS; do
241 if ! tg_cmd_will_setup "$cmd"; then
242 say "# checking tg $cmd does not do setup"
243 test_might_fail </dev/null tg $cmd && has_no_tg_setup &&
244 test_might_fail </dev/null tg $cmd --bogus-option-here && has_no_tg_setup || return
246 done &&
247 has_no_tg_setup &&
248 cd .. && rm -rf r4
251 for cmd in $TG_CMDS; do
252 if tg_cmd_will_setup "$cmd"; then
253 test_expect_success "setup happens for tg $cmd" '
254 rm -rf rt && test_create_repo rt && cd rt &&
255 has_no_tg_setup &&
256 test_might_fail </dev/null tg $cmd &&
257 test_might_fail </dev/null tg $cmd --bogus-option-here &&
258 has_tg_setup &&
259 cd .. && rm -rf rt
262 done
264 # Note that the initial branch name in r5 does
265 # not affect these tests in any way
266 test_expect_success 'no setup happens in bare repository' '
267 git init --bare --quiet r5 && cd r5 &&
268 for cmd in $TG_CMDS; do
269 say "# checking tg $cmd does not do setup in bare repo"
270 test_might_fail </dev/null tg $cmd && has_no_tg_setup_bare &&
271 test_might_fail </dev/null tg $cmd --bogus-option-here && has_no_tg_setup_bare || return
272 done &&
273 has_no_tg_setup_bare &&
274 cd .. && rm -rf r5
277 test_done