comparison tests/test-hook @ 2155:ff255b41b4aa

support hooks written in python. to write hook in python, create module with hook function inside. make sure mercurial can import module (put it in $PYTHONPATH or load it as extension). hook function should look like this: def myhook(ui, repo, hooktype, **kwargs): if hook_passes: return True elif hook_explicitly_fails: return False elif some_other_failure: import util raise util.Abort('helpful failure message') else: return # implicit return of None makes hook fail! then in .hgrc, add hook with "python:" prefix: [hooks] commit = python:mymodule.myhook
author Vadim Gelfer <vadim.gelfer@gmail.com>
date Fri, 28 Apr 2006 15:50:22 -0700
parents 7544700fd931
children cbd458228a96
comparison
equal deleted inserted replaced
2153:635653cd73ab 2155:ff255b41b4aa
85 85
86 # preoutgoing hook can prevent outgoing changes 86 # preoutgoing hook can prevent outgoing changes
87 echo 'preoutgoing.forbid = echo preoutgoing.forbid hook; exit 1' >> ../a/.hg/hgrc 87 echo 'preoutgoing.forbid = echo preoutgoing.forbid hook; exit 1' >> ../a/.hg/hgrc
88 hg pull ../a 88 hg pull ../a
89 89
90 cat > hooktests.py <<EOF
91 from mercurial import util
92
93 uncallable = 0
94
95 def printargs(args):
96 args.pop('ui', None)
97 args.pop('repo', None)
98 a = list(args.items())
99 a.sort()
100 print 'hook args:'
101 for k, v in a:
102 print ' ', k, v
103 return True
104
105 def passhook(**args):
106 printargs(args)
107 return True
108
109 def failhook(**args):
110 printargs(args)
111
112 class LocalException(Exception):
113 pass
114
115 def raisehook(**args):
116 raise LocalException('exception from hook')
117
118 def aborthook(**args):
119 raise util.Abort('raise abort from hook')
120
121 def brokenhook(**args):
122 return 1 + {}
123
124 class container:
125 unreachable = 1
126 EOF
127
128 echo '# test python hooks'
129 PYTHONPATH="$PWD:$PYTHONPATH"
130 export PYTHONPATH
131
132 echo '[hooks]' > ../a/.hg/hgrc
133 echo 'preoutgoing.broken = python:hooktests.brokenhook' >> ../a/.hg/hgrc
134 hg pull ../a 2>&1 | grep 'raised an exception'
135
136 echo '[hooks]' > ../a/.hg/hgrc
137 echo 'preoutgoing.raise = python:hooktests.raisehook' >> ../a/.hg/hgrc
138 hg pull ../a 2>&1 | grep 'raised an exception'
139
140 echo '[hooks]' > ../a/.hg/hgrc
141 echo 'preoutgoing.abort = python:hooktests.aborthook' >> ../a/.hg/hgrc
142 hg pull ../a
143
144 echo '[hooks]' > ../a/.hg/hgrc
145 echo 'preoutgoing.fail = python:hooktests.failhook' >> ../a/.hg/hgrc
146 hg pull ../a
147
148 echo '[hooks]' > ../a/.hg/hgrc
149 echo 'preoutgoing.uncallable = python:hooktests.uncallable' >> ../a/.hg/hgrc
150 hg pull ../a
151
152 echo '[hooks]' > ../a/.hg/hgrc
153 echo 'preoutgoing.nohook = python:hooktests.nohook' >> ../a/.hg/hgrc
154 hg pull ../a
155
156 echo '[hooks]' > ../a/.hg/hgrc
157 echo 'preoutgoing.nomodule = python:nomodule' >> ../a/.hg/hgrc
158 hg pull ../a
159
160 echo '[hooks]' > ../a/.hg/hgrc
161 echo 'preoutgoing.badmodule = python:nomodule.nowhere' >> ../a/.hg/hgrc
162 hg pull ../a
163
164 echo '[hooks]' > ../a/.hg/hgrc
165 echo 'preoutgoing.unreachable = python:hooktests.container.unreachable' >> ../a/.hg/hgrc
166 hg pull ../a
167
168 echo '[hooks]' > ../a/.hg/hgrc
169 echo 'preoutgoing.pass = python:hooktests.passhook' >> ../a/.hg/hgrc
170 hg pull ../a
171
172 echo '# make sure --traceback works'
173 echo '[hooks]' > .hg/hgrc
174 echo 'commit.abort = python:hooktests.aborthook' >> .hg/hgrc
175
176 echo a >> a
177 hg --traceback commit -A -m a 2>&1 | grep '^Traceback'
178
90 exit 0 179 exit 0