comparison hgext/hooklib/reject_merge_commits.py @ 44413:4cabeea6d214

hgext: start building a library for simple hooks Many workflows depend on hooks to enforce certain policies, e.g. to prevent forced pushes. The Mercurial Guide includes some cases and Google can help finding others, but it can save users a lot of time if hg itself has a couple of examples for further customization. Differential Revision: https://phab.mercurial-scm.org/D6825
author Joerg Sonnenberger <joerg@bec.de>
date Sat, 07 Sep 2019 14:50:39 +0200
parents
children 6000f5b25c9b
comparison
equal deleted inserted replaced
44412:edc8504bc26b 44413:4cabeea6d214
1 # Copyright 2020 Joerg Sonnenberger <joerg@bec.de>
2 #
3 # This software may be used and distributed according to the terms of the
4 # GNU General Public License version 2 or any later version.
5
6 """reject_merge_commits is a hook to check new changesets for merge commits.
7 Merge commits are allowed only between different branches, i.e. merging
8 a feature branch into the main development branch. This can be used to
9 enforce policies for linear commit histories.
10
11 Usage:
12 [hooks]
13 pretxnchangegroup.reject_merge_commits = \
14 python:hgext.hooklib.reject_merge_commits.hook
15 """
16
17 from __future__ import absolute_import
18
19 from mercurial.i18n import _
20 from mercurial import (
21 error,
22 pycompat,
23 )
24
25
26 def hook(ui, repo, hooktype, node=None, **kwargs):
27 if hooktype != b"pretxnchangegroup":
28 raise error.Abort(
29 _(b'Unsupported hook type %r') % pycompat.bytestr(hooktype)
30 )
31
32 ctx = repo.unfiltered()[node]
33 for rev in repo.changelog.revs(start=ctx.rev()):
34 rev = repo[rev]
35 parents = rev.parents()
36 if len(parents) < 2:
37 continue
38 if all(repo[p].branch() == rev.branch() for p in parents):
39 raise error.Abort(
40 _(
41 b'%s rejected as merge on the same branch. '
42 b'Please consider rebase.'
43 )
44 % rev
45 )