Skip to content
Snippets Groups Projects
Commit cac20dfe000d authored by Georges Racinet's avatar Georges Racinet
Browse files

hook check_publish: porting to future hg 5.4

The structure of the `phases` transaction attribute has changed.
It's now a list of (key, value) pairs instead of a dict,
and keys are lazy ranges (`xrange` in Python2) of revision numbers.

The new unit test doesn't prove it really works for a given
Mercurial version, only a separate run will do that, but it'll
help developers work on both versions at once if they need to
change the logic.
parent d325833962a6
No related branches found
No related tags found
No related merge requests found
Pipeline #5212 passed
......@@ -7,6 +7,7 @@
from mercurial import (
configitems,
error,
pycompat,
scmutil,
phases,
)
......@@ -115,6 +116,29 @@
return 1
def extract_published_revs(tr):
"""Extract revision numbers of revision published by given transaction.
:returns: iterable of revision numbers, without duplicates.
"""
phaseschanges = tr.changes.get("phases", ())
try:
# phases changes is a dict in hg < 5.4
phaseschanges = pycompat.iteritems(phaseschanges)
revs_range = False
except AttributeError:
# phaseschanges is a list of pairs in hg >= 5.4
revs_range = True
publishing = set(rev for rev, (old, new) in phaseschanges
if new == phases.public and old != phases.public)
# TODO drop this once we are on hg >= 5.4 only
if revs_range:
publishing = set(rev for rev_range in publishing for rev in rev_range)
return publishing
def check_publish(repo, *args, **kwargs):
validate_hook_type('pretxnclose', **kwargs)
remoteuser = get_remote_user(repo)
......@@ -129,9 +153,7 @@
tr = repo.currenttransaction()
assert tr is not None and tr.running()
phaseschanges = tr.changes.get("phases", {})
publishing = set(rev for rev, (old, new) in phaseschanges.iteritems()
if new == phases.public and old != phases.public)
publishing = extract_published_revs(tr)
if publishing:
node = repo.changelog.node
nodes = [node(r) for r in sorted(publishing)]
......
......@@ -9,9 +9,10 @@
from mercurial import (
error,
phases,
pycompat,
)
from heptapod.testhelpers import (
LocalRepoWrapper,
)
from .utils import switch_user
......@@ -12,9 +13,10 @@
)
from heptapod.testhelpers import (
LocalRepoWrapper,
)
from .utils import switch_user
from ..perm import extract_published_revs
def init_repo(basedir):
......@@ -60,3 +62,25 @@
wrapper.write_commit('foo')
assert 'precommit' in exc_info.value.args[0]
class FakeTransaction(object):
def __init__(self, phases):
self.changes = dict(phases=phases)
def test_extract_published_revs_hg_5_3():
txn = FakeTransaction({3: (phases.draft, phases.public),
19: (phases.secret, phases.public),
7: (phases.secret, phases.draft),
})
assert set(extract_published_revs(txn)) == {3, 19}
def test_extract_published_revs_hg_5_4():
txn = FakeTransaction([
(pycompat.xrange(3, 5), (phases.draft, phases.public)),
(pycompat.xrange(2), (phases.secret, phases.public)),
(pycompat.xrange(7, 8), (phases.secret, phases.draft)),
])
assert set(extract_published_revs(txn)) == {0, 1, 3, 4}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment