##// END OF EJS Templates
Handle functions as the value of a hooks.<name> config variable...
Alexis S. L. Carvalho -
r4070:961ccb61 default
parent child Browse files
Show More
@@ -142,32 +142,34 b' class localrepository(repo.repository):'
142 be run as hooks without wrappers to convert return values.'''
142 be run as hooks without wrappers to convert return values.'''
143
143
144 self.ui.note(_("calling hook %s: %s\n") % (hname, funcname))
144 self.ui.note(_("calling hook %s: %s\n") % (hname, funcname))
145 d = funcname.rfind('.')
145 obj = funcname
146 if d == -1:
146 if not callable(obj):
147 raise util.Abort(_('%s hook is invalid ("%s" not in a module)')
147 d = funcname.rfind('.')
148 % (hname, funcname))
148 if d == -1:
149 modname = funcname[:d]
149 raise util.Abort(_('%s hook is invalid ("%s" not in '
150 try:
150 'a module)') % (hname, funcname))
151 obj = __import__(modname)
151 modname = funcname[:d]
152 except ImportError:
153 try:
152 try:
154 # extensions are loaded with hgext_ prefix
153 obj = __import__(modname)
155 obj = __import__("hgext_%s" % modname)
156 except ImportError:
154 except ImportError:
155 try:
156 # extensions are loaded with hgext_ prefix
157 obj = __import__("hgext_%s" % modname)
158 except ImportError:
159 raise util.Abort(_('%s hook is invalid '
160 '(import of "%s" failed)') %
161 (hname, modname))
162 try:
163 for p in funcname.split('.')[1:]:
164 obj = getattr(obj, p)
165 except AttributeError, err:
157 raise util.Abort(_('%s hook is invalid '
166 raise util.Abort(_('%s hook is invalid '
158 '(import of "%s" failed)') %
167 '("%s" is not defined)') %
159 (hname, modname))
168 (hname, funcname))
160 try:
169 if not callable(obj):
161 for p in funcname.split('.')[1:]:
170 raise util.Abort(_('%s hook is invalid '
162 obj = getattr(obj, p)
171 '("%s" is not callable)') %
163 except AttributeError, err:
172 (hname, funcname))
164 raise util.Abort(_('%s hook is invalid '
165 '("%s" is not defined)') %
166 (hname, funcname))
167 if not callable(obj):
168 raise util.Abort(_('%s hook is invalid '
169 '("%s" is not callable)') %
170 (hname, funcname))
171 try:
173 try:
172 r = obj(ui=self.ui, repo=self, hooktype=name, **args)
174 r = obj(ui=self.ui, repo=self, hooktype=name, **args)
173 except (KeyboardInterrupt, util.SignalInterrupt):
175 except (KeyboardInterrupt, util.SignalInterrupt):
@@ -205,7 +207,9 b' class localrepository(repo.repository):'
205 if hname.split(".", 1)[0] == name and cmd]
207 if hname.split(".", 1)[0] == name and cmd]
206 hooks.sort()
208 hooks.sort()
207 for hname, cmd in hooks:
209 for hname, cmd in hooks:
208 if cmd.startswith('python:'):
210 if callable(cmd):
211 r = callhook(hname, cmd) or r
212 elif cmd.startswith('python:'):
209 r = callhook(hname, cmd[7:].strip()) or r
213 r = callhook(hname, cmd[7:].strip()) or r
210 else:
214 else:
211 r = runhook(hname, cmd) or r
215 r = runhook(hname, cmd) or r
@@ -183,4 +183,24 b" echo 'commit.abort = python:hooktests.ab"
183 echo a >> a
183 echo a >> a
184 hg --traceback commit -A -m a 2>&1 | grep '^Traceback'
184 hg --traceback commit -A -m a 2>&1 | grep '^Traceback'
185
185
186 cd ..
187 hg init c
188 cd c
189
190 cat > hookext.py <<EOF
191 def autohook(**args):
192 print "Automatically installed hook"
193
194 def reposetup(ui, repo):
195 repo.ui.setconfig("hooks", "commit.auto", autohook)
196 EOF
197 echo '[extensions]' >> .hg/hgrc
198 echo 'hookext = hookext.py' >> .hg/hgrc
199
200 touch foo
201 hg add foo
202 hg ci -m 'add foo'
203 echo >> foo
204 hg ci --debug -m 'change foo' | sed -e 's/ at .*>/>/'
205
186 exit 0
206 exit 0
@@ -138,3 +138,7 b' added 1 changesets with 1 changes to 1 f'
138 (run 'hg update' to get a working copy)
138 (run 'hg update' to get a working copy)
139 # make sure --traceback works
139 # make sure --traceback works
140 Traceback (most recent call last):
140 Traceback (most recent call last):
141 Automatically installed hook
142 foo
143 calling hook commit.auto: <function autohook>
144 Automatically installed hook
General Comments 0
You need to be logged in to leave comments. Login now