comparison hgext/commitextras.py @ 39294:1cb7c9777852

commitextras: work nicely with other extensions Before this change, it doesn't add these extra fields when loaded alongside another extension that does a bunch of things, including wrapping commit. I did not investigate exactly why, but - the documentation of extensions.wrapfunction says to use subclassing to play nicely with other extensions - using subclassing does make commitextras work when loaded alongside my other extension Differential Revision: https://phab.mercurial-scm.org/D4404
author Valentin Gatien-Baron <vgatien-baron@janestreet.com>
date Mon, 27 Aug 2018 16:01:55 -0400
parents 75c76cee1b1b
children 3a60416c4fd8
comparison
equal deleted inserted replaced
39293:278eb4541758 39294:1cb7c9777852
15 from mercurial import ( 15 from mercurial import (
16 commands, 16 commands,
17 error, 17 error,
18 extensions, 18 extensions,
19 registrar, 19 registrar,
20 util,
20 ) 21 )
21 22
22 cmdtable = {} 23 cmdtable = {}
23 command = registrar.command(cmdtable) 24 command = registrar.command(cmdtable)
24 testedwith = 'ships-with-hg-core' 25 testedwith = 'ships-with-hg-core'
41 options = entry[1] 42 options = entry[1]
42 options.append(('', 'extra', [], 43 options.append(('', 'extra', [],
43 _('set a changeset\'s extra values'), _("KEY=VALUE"))) 44 _('set a changeset\'s extra values'), _("KEY=VALUE")))
44 45
45 def _commit(orig, ui, repo, *pats, **opts): 46 def _commit(orig, ui, repo, *pats, **opts):
46 origcommit = repo.commit 47 if util.safehasattr(repo, 'unfiltered'):
47 try: 48 repo = repo.unfiltered()
48 def _wrappedcommit(*innerpats, **inneropts): 49 class repoextra(repo.__class__):
50 def commit(self, *innerpats, **inneropts):
49 extras = opts.get(r'extra') 51 extras = opts.get(r'extra')
50 if extras: 52 if extras:
51 for raw in extras: 53 for raw in extras:
52 if '=' not in raw: 54 if '=' not in raw:
53 msg = _("unable to parse '%s', should follow " 55 msg = _("unable to parse '%s', should follow "
64 if k in usedinternally: 66 if k in usedinternally:
65 msg = _("key '%s' is used internally, can't be set " 67 msg = _("key '%s' is used internally, can't be set "
66 "manually") 68 "manually")
67 raise error.Abort(msg % k) 69 raise error.Abort(msg % k)
68 inneropts[r'extra'][k] = v 70 inneropts[r'extra'][k] = v
69 return origcommit(*innerpats, **inneropts) 71 return super(repoextra, self).commit(*innerpats, **inneropts)
70 72 repo.__class__ = repoextra
71 # This __dict__ logic is needed because the normal 73 return orig(ui, repo, *pats, **opts)
72 # extension.wrapfunction doesn't seem to work.
73 repo.__dict__[r'commit'] = _wrappedcommit
74 return orig(ui, repo, *pats, **opts)
75 finally:
76 del repo.__dict__[r'commit']