annotate contrib/python-hook-examples.py @ 27142:060f83d219b9

extensions: refuse to load extensions if minimum hg version not met As the author of several 3rd party extensions, I frequently see bug reports from users attempting to run my extension with an old version of Mercurial that I no longer support in my extension. Oftentimes, the extension will import just fine. But as soon as we run extsetup(), reposetup(), or get into the guts of a wrapped function, we encounter an exception and abort. Today, Mercurial will print a message about extensions that don't have a "testedwith" declaring explicit compatibility with the current version. The existing mechanism is a good start. But it isn't as robust as I would like. Specifically, Mercurial assumes compatibility by default. This means extension authors must perform compatibility checking in their extsetup() or we wait and see if we encounter an abort at runtime. And, compatibility checking can involve a lot of code and lots of error checking. It's a lot of effort for extension authors. Oftentimes, extension authors know which versions of Mercurial there extension works on and more importantly where it is broken. This patch introduces a magic "minimumhgversion" attribute in extensions. When found, the extension loading mechanism will compare the declared version against the current Mercurial version. If the extension explicitly states we require a newer Mercurial version, a warning is printed and the extension isn't loaded beyond importing the Python module. This causes a graceful failure while alerting the user of the compatibility issue. I would be receptive to the idea of making the failure more fatal. However, care would need to be taken to not criple every hg command. e.g. the user may use `hg config` to fix the hgrc and if we aborted trying to run that, the user would effectively be locked out of `hg`! A potential future improvement to this functionality would be to catch ImportError for the extension/module and parse the source code for "minimumhgversion = 'XXX'" and do similar checking. This way we could give more information about why the extension failed to load.
author Gregory Szorc <gregory.szorc@gmail.com>
date Tue, 24 Nov 2015 15:16:25 -0800
parents a8d13ee0ce68
children 2b585677220e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7917
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
1 '''
7918
62f11ef0df5b Change wording in example hook
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7917
diff changeset
2 Examples of useful python hooks for Mercurial.
7917
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
3 '''
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
4 from mercurial import patch, util
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
5
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
6 def diffstat(ui, repo, **kwargs):
7918
62f11ef0df5b Change wording in example hook
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 7917
diff changeset
7 '''Example usage:
7917
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
8
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
9 [hooks]
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
10 commit.diffstat = python:/path/to/this/file.py:diffstat
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
11 changegroup.diffstat = python:/path/to/this/file.py:diffstat
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
12 '''
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
13 if kwargs.get('parent2'):
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
14 return
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
15 node = kwargs['node']
13878
a8d13ee0ce68 misc: replace .parents()[0] with p1()
Matt Mackall <mpm@selenic.com>
parents: 7918
diff changeset
16 first = repo[node].p1().node()
7917
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
17 if 'url' in kwargs:
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
18 last = repo['tip'].node()
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
19 else:
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
20 last = node
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
21 diff = patch.diff(repo, first, last)
5a5396f49420 diffstat hook example
Alexander Solovyov <piranha@piranha.org.ua>
parents:
diff changeset
22 ui.write(patch.diffstat(util.iterlines(diff)))