| 1 |
# Copyright 2020 Joerg Sonnenberger <[email protected]> |
| 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_new_heads is a hook to check that branches touched by new changesets |
| 7 |
have at most one open head. It can be used to enforce policies for |
| 8 |
merge-before-push or rebase-before-push. It does not handle pre-existing |
| 9 |
hydras. |
| 10 |
|
| 11 |
Usage: |
| 12 |
[hooks] |
| 13 |
pretxnclose.reject_new_heads = \ |
| 14 |
python:hgext.hooklib.reject_new_heads.hook |
| 15 |
""" |
| 16 |
|
| 17 |
from __future__ import annotations |
| 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"pretxnclose": |
| 28 |
raise error.Abort( |
| 29 |
_(b'Unsupported hook type %r') % pycompat.bytestr(hooktype) |
| 30 |
) |
| 31 |
if node is None: |
| 32 |
return |
| 33 |
|
| 34 |
ctx = repo.unfiltered()[node] |
| 35 |
branches = set() |
| 36 |
for rev in repo.changelog.revs(start=ctx.rev()): |
| 37 |
rev = repo[rev] |
| 38 |
branches.add(rev.branch()) |
| 39 |
for branch in branches: |
| 40 |
if len(repo.revs("head() and not closed() and branch(%s)", branch)) > 1: |
| 41 |
raise error.Abort( |
| 42 |
_(b'Changes on branch %r resulted in multiple heads') |
| 43 |
% pycompat.bytestr(branch) |
| 44 |
) |