comparison mercurial/hook.py @ 26738:9abc2c921bbd

hook.runhooks: return a dict of result values This will be useful to other calling code that would be interested in what the individual hooks return.
author Siddharth Agarwal <sid0@fb.com>
date Wed, 14 Oct 2015 16:19:47 -0700
parents a930d66a04af
children 8429369eeb85
comparison
equal deleted inserted replaced
26737:a930d66a04af 26738:9abc2c921bbd
165 hooks = [] 165 hooks = []
166 for hname, cmd in _allhooks(ui): 166 for hname, cmd in _allhooks(ui):
167 if hname.split('.')[0] == name and cmd: 167 if hname.split('.')[0] == name and cmd:
168 hooks.append((hname, cmd)) 168 hooks.append((hname, cmd))
169 169
170 return runhooks(ui, repo, name, hooks, throw=throw, **args) 170 res = runhooks(ui, repo, name, hooks, throw=throw, **args)
171 r = False
172 for hname, cmd in hooks:
173 r = res[hname] or r
174 return r
171 175
172 def runhooks(ui, repo, name, hooks, throw=False, **args): 176 def runhooks(ui, repo, name, hooks, throw=False, **args):
173 r = False 177 res = {}
174 oldstdout = -1 178 oldstdout = -1
175 179
176 try: 180 try:
177 for hname, cmd in hooks: 181 for hname, cmd in hooks:
178 if oldstdout == -1 and _redirect: 182 if oldstdout == -1 and _redirect:
187 except (OSError, AttributeError): 191 except (OSError, AttributeError):
188 # files seem to be bogus, give up on redirecting (WSGI, etc) 192 # files seem to be bogus, give up on redirecting (WSGI, etc)
189 pass 193 pass
190 194
191 if callable(cmd): 195 if callable(cmd):
192 r = _pythonhook(ui, repo, name, hname, cmd, args, throw) or r 196 r = _pythonhook(ui, repo, name, hname, cmd, args, throw)
193 elif cmd.startswith('python:'): 197 elif cmd.startswith('python:'):
194 if cmd.count(':') >= 2: 198 if cmd.count(':') >= 2:
195 path, cmd = cmd[7:].rsplit(':', 1) 199 path, cmd = cmd[7:].rsplit(':', 1)
196 path = util.expandpath(path) 200 path = util.expandpath(path)
197 if repo: 201 if repo:
202 ui.write(_("loading %s hook failed:\n") % hname) 206 ui.write(_("loading %s hook failed:\n") % hname)
203 raise 207 raise
204 hookfn = getattr(mod, cmd) 208 hookfn = getattr(mod, cmd)
205 else: 209 else:
206 hookfn = cmd[7:].strip() 210 hookfn = cmd[7:].strip()
207 r = _pythonhook(ui, repo, name, hname, hookfn, args, throw) or r 211 r = _pythonhook(ui, repo, name, hname, hookfn, args, throw)
208 else: 212 else:
209 r = _exthook(ui, repo, hname, cmd, args, throw) or r 213 r = _exthook(ui, repo, hname, cmd, args, throw)
214
215 res[hname] = r
210 216
211 # The stderr is fully buffered on Windows when connected to a pipe. 217 # The stderr is fully buffered on Windows when connected to a pipe.
212 # A forcible flush is required to make small stderr data in the 218 # A forcible flush is required to make small stderr data in the
213 # remote side available to the client immediately. 219 # remote side available to the client immediately.
214 sys.stderr.flush() 220 sys.stderr.flush()
215 finally: 221 finally:
216 if _redirect and oldstdout >= 0: 222 if _redirect and oldstdout >= 0:
217 os.dup2(oldstdout, stdoutno) 223 os.dup2(oldstdout, stdoutno)
218 os.close(oldstdout) 224 os.close(oldstdout)
219 225
220 return r 226 return res