3 test_description
='test installation of pre-commit hook'
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"
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)" &&
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 &&
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 &&
70 read -r tgshellbin
<"$tgshell" || die
71 write_script dummy.sh
"${tgshellbin#??}" <<-EOT || die
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
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" &&
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 &&
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; } &&
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 &&
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 &&
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 &&
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 &&
213 tg_cmd_will_setup
() {
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
)
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
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
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 &&
256 test_might_fail </dev/null tg $cmd &&
257 test_might_fail </dev/null tg $cmd --bogus-option-here &&
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
273 has_no_tg_setup_bare &&