| 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_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 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"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 |
) |