phases.py
787 lines
| 26.5 KiB
| text/x-python
|
PythonLexer
/ mercurial / phases.py
Pierre-Yves David
|
r15659 | """ Mercurial phases support code | ||
--- | ||||
Copyright 2011 Pierre-Yves David <pierre-yves.david@ens-lyon.org> | ||||
Logilab SA <contact@logilab.fr> | ||||
Augie Fackler <durin42@gmail.com> | ||||
Martin Geisler
|
r16725 | This software may be used and distributed according to the terms | ||
of the GNU General Public License version 2 or any later version. | ||||
Pierre-Yves David
|
r15659 | |||
--- | ||||
This module implements most phase logic in mercurial. | ||||
Basic Concept | ||||
============= | ||||
Martin Geisler
|
r16724 | A 'changeset phase' is an indicator that tells us how a changeset is | ||
Martin Geisler
|
r16725 | manipulated and communicated. The details of each phase is described | ||
below, here we describe the properties they have in common. | ||||
Pierre-Yves David
|
r15659 | |||
Martin Geisler
|
r16725 | Like bookmarks, phases are not stored in history and thus are not | ||
permanent and leave no audit trail. | ||||
Pierre-Yves David
|
r15659 | |||
Martin Geisler
|
r16725 | First, no changeset can be in two phases at once. Phases are ordered, | ||
so they can be considered from lowest to highest. The default, lowest | ||||
phase is 'public' - this is the normal phase of existing changesets. A | ||||
child changeset can not be in a lower phase than its parents. | ||||
Pierre-Yves David
|
r15659 | |||
These phases share a hierarchy of traits: | ||||
immutable shared | ||||
public: X X | ||||
draft: X | ||||
Pierre-Yves David
|
r15705 | secret: | ||
Pierre-Yves David
|
r15659 | |||
Martin Geisler
|
r16724 | Local commits are draft by default. | ||
Pierre-Yves David
|
r15659 | |||
Martin Geisler
|
r16724 | Phase Movement and Exchange | ||
=========================== | ||||
Pierre-Yves David
|
r15659 | |||
Martin Geisler
|
r16725 | Phase data is exchanged by pushkey on pull and push. Some servers have | ||
a publish option set, we call such a server a "publishing server". | ||||
Pushing a draft changeset to a publishing server changes the phase to | ||||
public. | ||||
Pierre-Yves David
|
r15659 | |||
A small list of fact/rules define the exchange of phase: | ||||
* old client never changes server states | ||||
* pull never changes server states | ||||
Martin Geisler
|
r16724 | * publish and old server changesets are seen as public by client | ||
Martin Geisler
|
r16725 | * any secret changeset seen in another repository is lowered to at | ||
least draft | ||||
Pierre-Yves David
|
r15659 | |||
Martin Geisler
|
r16725 | Here is the final table summing up the 49 possible use cases of phase | ||
exchange: | ||||
Pierre-Yves David
|
r15659 | |||
server | ||||
old publish non-publish | ||||
N X N D P N D P | ||||
old client | ||||
pull | ||||
N - X/X - X/D X/P - X/D X/P | ||||
X - X/X - X/D X/P - X/D X/P | ||||
push | ||||
X X/X X/X X/P X/P X/P X/D X/D X/P | ||||
new client | ||||
pull | ||||
N - P/X - P/D P/P - D/D P/P | ||||
D - P/X - P/D P/P - D/D P/P | ||||
P - P/X - P/D P/P - P/D P/P | ||||
push | ||||
D P/X P/X P/P P/P P/P D/D D/D P/P | ||||
P P/X P/X P/P P/P P/P P/P P/P P/P | ||||
Legend: | ||||
A/B = final state on client / state on server | ||||
* N = new/not present, | ||||
* P = public, | ||||
* D = draft, | ||||
Martin Geisler
|
r16725 | * X = not tracked (i.e., the old client or server has no internal | ||
way of recording the phase.) | ||||
Pierre-Yves David
|
r15659 | |||
passive = only pushes | ||||
A cell here can be read like this: | ||||
Martin Geisler
|
r16725 | "When a new client pushes a draft changeset (D) to a publishing | ||
server where it's not present (N), it's marked public on both | ||||
sides (P/P)." | ||||
Pierre-Yves David
|
r15659 | |||
Martin Geisler
|
r16724 | Note: old client behave as a publishing server with draft only content | ||
Pierre-Yves David
|
r15659 | - other people see it as public | ||
- content is pushed as draft | ||||
""" | ||||
Pierre-Yves David
|
r15417 | |||
Gregory Szorc
|
r25966 | from __future__ import absolute_import | ||
Matt Mackall
|
r15419 | import errno | ||
Boris Feld
|
r34320 | import struct | ||
Gregory Szorc
|
r25966 | |||
from .i18n import _ | ||||
from .node import ( | ||||
bin, | ||||
hex, | ||||
nullid, | ||||
nullrev, | ||||
short, | ||||
) | ||||
from . import ( | ||||
error, | ||||
Joerg Sonnenberger
|
r35310 | pycompat, | ||
Jun Wu
|
r31016 | smartset, | ||
FUJIWARA Katsunori
|
r31053 | txnutil, | ||
Gregory Szorc
|
r32000 | util, | ||
Gregory Szorc
|
r25966 | ) | ||
Pierre-Yves David
|
r15418 | |||
Augie Fackler
|
r43347 | _fphasesentry = struct.Struct(b'>i20s') | ||
Boris Feld
|
r34320 | |||
Augie Fackler
|
r43346 | INTERNAL_FLAG = 64 # Phases for mercurial internal usage only | ||
HIDEABLE_FLAG = 32 # Phases that are hideable | ||||
Boris Feld
|
r39333 | |||
# record phase index | ||||
public, draft, secret = range(3) | ||||
internal = INTERNAL_FLAG | HIDEABLE_FLAG | ||||
Boris Feld
|
r40463 | archived = HIDEABLE_FLAG | ||
Boris Feld
|
r39333 | allphases = range(internal + 1) | ||
Pierre-Yves David
|
r15417 | trackedphases = allphases[1:] | ||
Boris Feld
|
r39333 | # record phase names | ||
Augie Fackler
|
r43347 | cmdphasenames = [b'public', b'draft', b'secret'] # known to `hg phase` command | ||
Boris Feld
|
r39333 | phasenames = [None] * len(allphases) | ||
Augie Fackler
|
r43346 | phasenames[: len(cmdphasenames)] = cmdphasenames | ||
Augie Fackler
|
r43347 | phasenames[archived] = b'archived' | ||
phasenames[internal] = b'internal' | ||||
Boris Feld
|
r39333 | # record phase property | ||
Boris Feld
|
r38174 | mutablephases = tuple(allphases[1:]) | ||
Boris Feld
|
r38175 | remotehiddenphases = tuple(allphases[2:]) | ||
Boris Feld
|
r39333 | localhiddenphases = tuple(p for p in allphases if p & HIDEABLE_FLAG) | ||
Pierre-Yves David
|
r15418 | |||
Augie Fackler
|
r43346 | |||
Boris Feld
|
r39335 | def supportinternal(repo): | ||
"""True if the internal phase can be used on a repository""" | ||||
Augie Fackler
|
r43347 | return b'internal-phase' in repo.requirements | ||
Boris Feld
|
r39335 | |||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r16657 | def _readroots(repo, phasedefaults=None): | ||
Patrick Mezard
|
r16625 | """Read phase roots from disk | ||
phasedefaults is a list of fn(repo, roots) callable, which are | ||||
executed if the phase roots file does not exist. When phases are | ||||
being initialized on an existing repository, this could be used to | ||||
set selected changesets phase to something else than public. | ||||
Return (roots, dirty) where dirty is true if roots differ from | ||||
what is being stored. | ||||
""" | ||||
Pierre-Yves David
|
r18002 | repo = repo.unfiltered() | ||
Patrick Mezard
|
r16625 | dirty = False | ||
Pierre-Yves David
|
r15418 | roots = [set() for i in allphases] | ||
try: | ||||
Augie Fackler
|
r43347 | f, pending = txnutil.trypending(repo.root, repo.svfs, b'phaseroots') | ||
Pierre-Yves David
|
r15418 | try: | ||
for line in f: | ||||
Martin Geisler
|
r16588 | phase, nh = line.split() | ||
Pierre-Yves David
|
r15418 | roots[int(phase)].add(bin(nh)) | ||
finally: | ||||
f.close() | ||||
Gregory Szorc
|
r25660 | except IOError as inst: | ||
Matt Mackall
|
r15419 | if inst.errno != errno.ENOENT: | ||
raise | ||||
Patrick Mezard
|
r16625 | if phasedefaults: | ||
for f in phasedefaults: | ||||
roots = f(repo, roots) | ||||
dirty = True | ||||
return roots, dirty | ||||
Pierre-Yves David
|
r15418 | |||
Augie Fackler
|
r43346 | |||
Boris Feld
|
r34320 | def binaryencode(phasemapping): | ||
"""encode a 'phase -> nodes' mapping into a binary stream | ||||
Since phases are integer the mapping is actually a python list: | ||||
[[PUBLIC_HEADS], [DRAFTS_HEADS], [SECRET_HEADS]] | ||||
""" | ||||
binarydata = [] | ||||
for phase, nodes in enumerate(phasemapping): | ||||
for head in nodes: | ||||
binarydata.append(_fphasesentry.pack(phase, head)) | ||||
Augie Fackler
|
r43347 | return b''.join(binarydata) | ||
Boris Feld
|
r34320 | |||
Augie Fackler
|
r43346 | |||
Boris Feld
|
r34321 | def binarydecode(stream): | ||
"""decode a binary stream into a 'phase -> nodes' mapping | ||||
Since phases are integer the mapping is actually a python list.""" | ||||
headsbyphase = [[] for i in allphases] | ||||
entrysize = _fphasesentry.size | ||||
while True: | ||||
entry = stream.read(entrysize) | ||||
if len(entry) < entrysize: | ||||
if entry: | ||||
Augie Fackler
|
r43347 | raise error.Abort(_(b'bad phase-heads stream')) | ||
Boris Feld
|
r34321 | break | ||
phase, node = _fphasesentry.unpack(entry) | ||||
headsbyphase[phase].append(node) | ||||
return headsbyphase | ||||
Augie Fackler
|
r43346 | |||
Boris Feld
|
r33451 | def _trackphasechange(data, rev, old, new): | ||
"""add a phase move the <data> dictionnary | ||||
If data is None, nothing happens. | ||||
""" | ||||
if data is None: | ||||
return | ||||
existing = data.get(rev) | ||||
if existing is not None: | ||||
old = existing[0] | ||||
data[rev] = (old, new) | ||||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r16657 | class phasecache(object): | ||
Patrick Mezard
|
r16658 | def __init__(self, repo, phasedefaults, _load=True): | ||
if _load: | ||||
# Cheap trick to allow shallow-copy without copy module | ||||
self.phaseroots, self.dirty = _readroots(repo, phasedefaults) | ||||
Yuya Nishihara
|
r35458 | self._loadedrevslen = 0 | ||
Laurent Charignon
|
r25190 | self._phasesets = None | ||
Idan Kamara
|
r18220 | self.filterunknown(repo) | ||
Angel Ezquerra
|
r23878 | self.opener = repo.svfs | ||
Patrick Mezard
|
r16658 | |||
Jun Wu
|
r35331 | def getrevset(self, repo, phases, subset=None): | ||
Jun Wu
|
r31016 | """return a smartset for the given phases""" | ||
Augie Fackler
|
r43346 | self.loadphaserevs(repo) # ensure phase's sets are loaded | ||
Joerg Sonnenberger
|
r35310 | phases = set(phases) | ||
if public not in phases: | ||||
# fast path: _phasesets contains the interesting sets, | ||||
# might only need a union and post-filtering. | ||||
if len(phases) == 1: | ||||
[p] = phases | ||||
revs = self._phasesets[p] | ||||
else: | ||||
revs = set.union(*[self._phasesets[p] for p in phases]) | ||||
Jun Wu
|
r31016 | if repo.changelog.filteredrevs: | ||
revs = revs - repo.changelog.filteredrevs | ||||
Jun Wu
|
r35331 | if subset is None: | ||
return smartset.baseset(revs) | ||||
else: | ||||
return subset & smartset.baseset(revs) | ||||
Jun Wu
|
r31016 | else: | ||
Joerg Sonnenberger
|
r35310 | phases = set(allphases).difference(phases) | ||
if not phases: | ||||
return smartset.fullreposet(repo) | ||||
if len(phases) == 1: | ||||
[p] = phases | ||||
revs = self._phasesets[p] | ||||
else: | ||||
revs = set.union(*[self._phasesets[p] for p in phases]) | ||||
Jun Wu
|
r35331 | if subset is None: | ||
subset = smartset.fullreposet(repo) | ||||
Joerg Sonnenberger
|
r35310 | if not revs: | ||
Jun Wu
|
r35331 | return subset | ||
return subset.filter(lambda r: r not in revs) | ||||
Jun Wu
|
r31016 | |||
Patrick Mezard
|
r16658 | def copy(self): | ||
# Shallow copy meant to ensure isolation in | ||||
# advance/retractboundary(), nothing more. | ||||
Eric Sumner
|
r23631 | ph = self.__class__(None, None, _load=False) | ||
Patrick Mezard
|
r16658 | ph.phaseroots = self.phaseroots[:] | ||
ph.dirty = self.dirty | ||||
ph.opener = self.opener | ||||
Yuya Nishihara
|
r35457 | ph._loadedrevslen = self._loadedrevslen | ||
Pierre-Yves David
|
r25592 | ph._phasesets = self._phasesets | ||
Patrick Mezard
|
r16658 | return ph | ||
def replace(self, phcache): | ||||
Pierre-Yves David
|
r25613 | """replace all values in 'self' with content of phcache""" | ||
Augie Fackler
|
r43346 | for a in ( | ||
Augie Fackler
|
r43347 | b'phaseroots', | ||
b'dirty', | ||||
b'opener', | ||||
b'_loadedrevslen', | ||||
b'_phasesets', | ||||
Augie Fackler
|
r43346 | ): | ||
Patrick Mezard
|
r16658 | setattr(self, a, getattr(phcache, a)) | ||
Patrick Mezard
|
r16657 | |||
Laurent Charignon
|
r24599 | def _getphaserevsnative(self, repo): | ||
Laurent Charignon
|
r24444 | repo = repo.unfiltered() | ||
nativeroots = [] | ||||
for phase in trackedphases: | ||||
Augie Fackler
|
r43346 | nativeroots.append( | ||
pycompat.maplist(repo.changelog.rev, self.phaseroots[phase]) | ||||
) | ||||
Pierre-Yves David
|
r25527 | return repo.changelog.computephases(nativeroots) | ||
Laurent Charignon
|
r24444 | |||
Laurent Charignon
|
r24599 | def _computephaserevspure(self, repo): | ||
Laurent Charignon
|
r24519 | repo = repo.unfiltered() | ||
Joerg Sonnenberger
|
r35310 | cl = repo.changelog | ||
self._phasesets = [set() for phase in allphases] | ||||
Boris Feld
|
r39307 | lowerroots = set() | ||
for phase in reversed(trackedphases): | ||||
roots = pycompat.maplist(cl.rev, self.phaseroots[phase]) | ||||
if roots: | ||||
ps = set(cl.descendants(roots)) | ||||
for root in roots: | ||||
ps.add(root) | ||||
ps.difference_update(lowerroots) | ||||
lowerroots.update(ps) | ||||
self._phasesets[phase] = ps | ||||
Yuya Nishihara
|
r35457 | self._loadedrevslen = len(cl) | ||
Laurent Charignon
|
r24519 | |||
Pierre-Yves David
|
r25611 | def loadphaserevs(self, repo): | ||
"""ensure phase information is loaded in the object""" | ||||
Joerg Sonnenberger
|
r35310 | if self._phasesets is None: | ||
Laurent Charignon
|
r24444 | try: | ||
Jun Wu
|
r31152 | res = self._getphaserevsnative(repo) | ||
Yuya Nishihara
|
r35457 | self._loadedrevslen, self._phasesets = res | ||
Laurent Charignon
|
r24444 | except AttributeError: | ||
Laurent Charignon
|
r24599 | self._computephaserevspure(repo) | ||
Durham Goode
|
r22894 | |||
Durham Goode
|
r22893 | def invalidate(self): | ||
Yuya Nishihara
|
r35458 | self._loadedrevslen = 0 | ||
Pierre-Yves David
|
r25593 | self._phasesets = None | ||
Patrick Mezard
|
r16657 | |||
def phase(self, repo, rev): | ||||
Joerg Sonnenberger
|
r35310 | # We need a repo argument here to be able to build _phasesets | ||
Patrick Mezard
|
r16657 | # if necessary. The repository instance is not stored in | ||
# phasecache to avoid reference cycles. The changelog instance | ||||
# is not stored because it is a filecache() property and can | ||||
# be replaced without us being notified. | ||||
if rev == nullrev: | ||||
return public | ||||
Durham Goode
|
r19984 | if rev < nullrev: | ||
Augie Fackler
|
r43347 | raise ValueError(_(b'cannot lookup negative revision')) | ||
Yuya Nishihara
|
r35457 | if rev >= self._loadedrevslen: | ||
Durham Goode
|
r22893 | self.invalidate() | ||
Pierre-Yves David
|
r25611 | self.loadphaserevs(repo) | ||
Joerg Sonnenberger
|
r35310 | for phase in trackedphases: | ||
if rev in self._phasesets[phase]: | ||||
return phase | ||||
return public | ||||
Patrick Mezard
|
r16657 | |||
def write(self): | ||||
if not self.dirty: | ||||
return | ||||
Augie Fackler
|
r43347 | f = self.opener(b'phaseroots', b'w', atomictemp=True, checkambig=True) | ||
Patrick Mezard
|
r16657 | try: | ||
Pierre-Yves David
|
r22079 | self._write(f) | ||
Patrick Mezard
|
r16657 | finally: | ||
f.close() | ||||
Pierre-Yves David
|
r22079 | |||
def _write(self, fp): | ||||
for phase, roots in enumerate(self.phaseroots): | ||||
Gregory Szorc
|
r36470 | for h in sorted(roots): | ||
Augie Fackler
|
r43347 | fp.write(b'%i %s\n' % (phase, hex(h))) | ||
Patrick Mezard
|
r16657 | self.dirty = False | ||
Pierre-Yves David
|
r15454 | |||
Pierre-Yves David
|
r22080 | def _updateroots(self, phase, newroots, tr): | ||
Patrick Mezard
|
r16658 | self.phaseroots[phase] = newroots | ||
Durham Goode
|
r22893 | self.invalidate() | ||
Patrick Mezard
|
r16658 | self.dirty = True | ||
Augie Fackler
|
r43347 | tr.addfilegenerator(b'phase', (b'phaseroots',), self._write) | ||
tr.hookargs[b'phases_moved'] = b'1' | ||||
Pierre-Yves David
|
r22080 | |||
Boris Feld
|
r33453 | def registernew(self, repo, tr, targetphase, nodes): | ||
repo = repo.unfiltered() | ||||
self._retractboundary(repo, tr, targetphase, nodes) | ||||
Augie Fackler
|
r43347 | if tr is not None and b'phases' in tr.changes: | ||
phasetracking = tr.changes[b'phases'] | ||||
Boris Feld
|
r33453 | torev = repo.changelog.rev | ||
phase = self.phase | ||||
for n in nodes: | ||||
rev = torev(n) | ||||
revphase = phase(repo, rev) | ||||
_trackphasechange(phasetracking, rev, None, revphase) | ||||
repo.invalidatevolatilesets() | ||||
Sushil khanchi
|
r38218 | def advanceboundary(self, repo, tr, targetphase, nodes, dryrun=None): | ||
Boris Feld
|
r33450 | """Set all 'nodes' to phase 'targetphase' | ||
Nodes with a phase lower than 'targetphase' are not affected. | ||||
Sushil khanchi
|
r38218 | |||
If dryrun is True, no actions will be performed | ||||
Returns a set of revs whose phase is changed or should be changed | ||||
Boris Feld
|
r33450 | """ | ||
Patrick Mezard
|
r16658 | # Be careful to preserve shallow-copied values: do not update | ||
# phaseroots values, replace them. | ||||
Boris Feld
|
r33451 | if tr is None: | ||
phasetracking = None | ||||
else: | ||||
Augie Fackler
|
r43347 | phasetracking = tr.changes.get(b'phases') | ||
Patrick Mezard
|
r16658 | |||
Pierre-Yves David
|
r18002 | repo = repo.unfiltered() | ||
Boris Feld
|
r33451 | |||
Augie Fackler
|
r43346 | changes = set() # set of revisions to be changed | ||
delroots = [] # set of root deleted by this path | ||||
Gregory Szorc
|
r38806 | for phase in pycompat.xrange(targetphase + 1, len(allphases)): | ||
Patrick Mezard
|
r16658 | # filter nodes that are not in a compatible phase already | ||
Augie Fackler
|
r43346 | nodes = [ | ||
n for n in nodes if self.phase(repo, repo[n].rev()) >= phase | ||||
] | ||||
Patrick Mezard
|
r16658 | if not nodes: | ||
Augie Fackler
|
r43346 | break # no roots to move anymore | ||
Boris Feld
|
r33450 | |||
Patrick Mezard
|
r16658 | olds = self.phaseroots[phase] | ||
Boris Feld
|
r33451 | |||
Augie Fackler
|
r43347 | affected = repo.revs(b'%ln::%ln', olds, nodes) | ||
Sushil khanchi
|
r38218 | changes.update(affected) | ||
if dryrun: | ||||
continue | ||||
Boris Feld
|
r33451 | for r in affected: | ||
Augie Fackler
|
r43346 | _trackphasechange( | ||
phasetracking, r, self.phase(repo, r), targetphase | ||||
) | ||||
Boris Feld
|
r33450 | |||
Augie Fackler
|
r43346 | roots = set( | ||
ctx.node() | ||||
Augie Fackler
|
r43347 | for ctx in repo.set(b'roots((%ln::) - %ld)', olds, affected) | ||
Augie Fackler
|
r43346 | ) | ||
Patrick Mezard
|
r16658 | if olds != roots: | ||
Pierre-Yves David
|
r22080 | self._updateroots(phase, roots, tr) | ||
Patrick Mezard
|
r16658 | # some roots may need to be declared for lower phases | ||
delroots.extend(olds - roots) | ||||
Sushil khanchi
|
r38218 | if not dryrun: | ||
# declare deleted root in the target phase | ||||
if targetphase != 0: | ||||
self._retractboundary(repo, tr, targetphase, delroots) | ||||
repo.invalidatevolatilesets() | ||||
return changes | ||||
Patrick Mezard
|
r16658 | |||
Pierre-Yves David
|
r22070 | def retractboundary(self, repo, tr, targetphase, nodes): | ||
Augie Fackler
|
r43346 | oldroots = self.phaseroots[: targetphase + 1] | ||
Boris Feld
|
r33458 | if tr is None: | ||
phasetracking = None | ||||
else: | ||||
Augie Fackler
|
r43347 | phasetracking = tr.changes.get(b'phases') | ||
Boris Feld
|
r33458 | repo = repo.unfiltered() | ||
Augie Fackler
|
r43346 | if ( | ||
self._retractboundary(repo, tr, targetphase, nodes) | ||||
and phasetracking is not None | ||||
): | ||||
Boris Feld
|
r33458 | |||
# find the affected revisions | ||||
new = self.phaseroots[targetphase] | ||||
old = oldroots[targetphase] | ||||
Augie Fackler
|
r43347 | affected = set(repo.revs(b'(%ln::) - (%ln::)', new, old)) | ||
Boris Feld
|
r33458 | |||
# find the phase of the affected revision | ||||
Gregory Szorc
|
r38806 | for phase in pycompat.xrange(targetphase, -1, -1): | ||
Boris Feld
|
r33458 | if phase: | ||
roots = oldroots[phase] | ||||
Augie Fackler
|
r43347 | revs = set(repo.revs(b'%ln::%ld', roots, affected)) | ||
Boris Feld
|
r33458 | affected -= revs | ||
Augie Fackler
|
r43346 | else: # public phase | ||
Boris Feld
|
r33458 | revs = affected | ||
for r in revs: | ||||
_trackphasechange(phasetracking, r, phase, targetphase) | ||||
Boris Feld
|
r33452 | repo.invalidatevolatilesets() | ||
def _retractboundary(self, repo, tr, targetphase, nodes): | ||||
Patrick Mezard
|
r16658 | # Be careful to preserve shallow-copied values: do not update | ||
# phaseroots values, replace them. | ||||
Boris Feld
|
r40463 | if targetphase in (archived, internal) and not supportinternal(repo): | ||
name = phasenames[targetphase] | ||||
Augie Fackler
|
r43347 | msg = b'this repository does not support the %s phase' % name | ||
Boris Feld
|
r39335 | raise error.ProgrammingError(msg) | ||
Patrick Mezard
|
r16658 | |||
Pierre-Yves David
|
r18002 | repo = repo.unfiltered() | ||
Patrick Mezard
|
r16658 | currentroots = self.phaseroots[targetphase] | ||
Boris Feld
|
r33457 | finalroots = oldroots = set(currentroots) | ||
Augie Fackler
|
r43346 | newroots = [ | ||
n for n in nodes if self.phase(repo, repo[n].rev()) < targetphase | ||||
] | ||||
Patrick Mezard
|
r16658 | if newroots: | ||
Boris Feld
|
r33452 | |||
Patrick Mezard
|
r16659 | if nullid in newroots: | ||
Augie Fackler
|
r43347 | raise error.Abort(_(b'cannot change null revision phase')) | ||
Patrick Mezard
|
r16658 | currentroots = currentroots.copy() | ||
currentroots.update(newroots) | ||||
Durham Goode
|
r26909 | |||
# Only compute new roots for revs above the roots that are being | ||||
# retracted. | ||||
minnewroot = min(repo[n].rev() for n in newroots) | ||||
Augie Fackler
|
r43346 | aboveroots = [ | ||
n for n in currentroots if repo[n].rev() >= minnewroot | ||||
] | ||||
Augie Fackler
|
r43347 | updatedroots = repo.set(b'roots(%ln::)', aboveroots) | ||
Durham Goode
|
r26909 | |||
Augie Fackler
|
r43346 | finalroots = set( | ||
n for n in currentroots if repo[n].rev() < minnewroot | ||||
) | ||||
Durham Goode
|
r26909 | finalroots.update(ctx.node() for ctx in updatedroots) | ||
Boris Feld
|
r33457 | if finalroots != oldroots: | ||
Durham Goode
|
r26909 | self._updateroots(targetphase, finalroots, tr) | ||
Boris Feld
|
r33457 | return True | ||
return False | ||||
Patrick Mezard
|
r16658 | |||
Idan Kamara
|
r18220 | def filterunknown(self, repo): | ||
"""remove unknown nodes from the phase boundary | ||||
Nothing is lost as unknown nodes only hold data for their descendants. | ||||
""" | ||||
filtered = False | ||||
Augie Fackler
|
r43346 | nodemap = repo.changelog.nodemap # to filter unknown nodes | ||
Idan Kamara
|
r18220 | for phase, nodes in enumerate(self.phaseroots): | ||
Mads Kiilerich
|
r20550 | missing = sorted(node for node in nodes if node not in nodemap) | ||
Idan Kamara
|
r18220 | if missing: | ||
for mnode in missing: | ||||
repo.ui.debug( | ||||
Augie Fackler
|
r43347 | b'removing unknown node %s from %i-phase boundary\n' | ||
Augie Fackler
|
r43346 | % (short(mnode), phase) | ||
) | ||||
Idan Kamara
|
r18220 | nodes.symmetric_difference_update(missing) | ||
filtered = True | ||||
if filtered: | ||||
self.dirty = True | ||||
Pierre-Yves David
|
r18983 | # filterunknown is called by repo.destroyed, we may have no changes in | ||
Joerg Sonnenberger
|
r35310 | # root but _phasesets contents is certainly invalid (or at least we | ||
Mads Kiilerich
|
r19951 | # have not proper way to check that). related to issue 3858. | ||
Pierre-Yves David
|
r18983 | # | ||
Joerg Sonnenberger
|
r35310 | # The other caller is __init__ that have no _phasesets initialized | ||
Pierre-Yves David
|
r18983 | # anyway. If this change we should consider adding a dedicated | ||
Mads Kiilerich
|
r19951 | # "destroyed" function to phasecache or a proper cache key mechanism | ||
Pierre-Yves David
|
r18983 | # (see branchmap one) | ||
Durham Goode
|
r22893 | self.invalidate() | ||
Idan Kamara
|
r18220 | |||
Augie Fackler
|
r43346 | |||
Sushil khanchi
|
r38218 | def advanceboundary(repo, tr, targetphase, nodes, dryrun=None): | ||
Pierre-Yves David
|
r15454 | """Add nodes to a phase changing other nodes phases if necessary. | ||
Martin Geisler
|
r16725 | This function move boundary *forward* this means that all nodes | ||
are set in the target phase or kept in a *lower* phase. | ||||
Pierre-Yves David
|
r15454 | |||
Sushil khanchi
|
r38218 | Simplify boundary to contains phase roots only. | ||
If dryrun is True, no actions will be performed | ||||
Returns a set of revs whose phase is changed or should be changed | ||||
""" | ||||
Patrick Mezard
|
r16658 | phcache = repo._phasecache.copy() | ||
Augie Fackler
|
r43346 | changes = phcache.advanceboundary( | ||
repo, tr, targetphase, nodes, dryrun=dryrun | ||||
) | ||||
Sushil khanchi
|
r38218 | if not dryrun: | ||
repo._phasecache.replace(phcache) | ||||
return changes | ||||
Pierre-Yves David
|
r15482 | |||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r22070 | def retractboundary(repo, tr, targetphase, nodes): | ||
Martin Geisler
|
r16725 | """Set nodes back to a phase changing other nodes phases if | ||
necessary. | ||||
Pierre-Yves David
|
r15482 | |||
Martin Geisler
|
r16725 | This function move boundary *backward* this means that all nodes | ||
are set in the target phase or kept in a *higher* phase. | ||||
Pierre-Yves David
|
r15482 | |||
Simplify boundary to contains phase roots only.""" | ||||
Patrick Mezard
|
r16658 | phcache = repo._phasecache.copy() | ||
Pierre-Yves David
|
r22070 | phcache.retractboundary(repo, tr, targetphase, nodes) | ||
Patrick Mezard
|
r16658 | repo._phasecache.replace(phcache) | ||
Pierre-Yves David
|
r15648 | |||
Augie Fackler
|
r43346 | |||
Boris Feld
|
r33453 | def registernew(repo, tr, targetphase, nodes): | ||
"""register a new revision and its phase | ||||
Code adding revisions to the repository should use this function to | ||||
set new changeset in their target phase (or higher). | ||||
""" | ||||
phcache = repo._phasecache.copy() | ||||
phcache.registernew(repo, tr, targetphase, nodes) | ||||
repo._phasecache.replace(phcache) | ||||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r15648 | def listphases(repo): | ||
Martin Geisler
|
r16724 | """List phases root for serialization over pushkey""" | ||
Gregory Szorc
|
r32000 | # Use ordered dictionary so behavior is deterministic. | ||
keys = util.sortdict() | ||||
Augie Fackler
|
r43347 | value = b'%i' % draft | ||
Boris Feld
|
r34817 | cl = repo.unfiltered().changelog | ||
Patrick Mezard
|
r16657 | for root in repo._phasecache.phaseroots[draft]: | ||
Boris Feld
|
r34817 | if repo._phasecache.phase(repo, cl.rev(root)) <= draft: | ||
keys[hex(root)] = value | ||||
Pierre-Yves David
|
r15892 | |||
Matt Mackall
|
r25624 | if repo.publishing(): | ||
Martin Geisler
|
r16725 | # Add an extra data to let remote know we are a publishing | ||
# repo. Publishing repo can't just pretend they are old repo. | ||||
# When pushing to a publishing repo, the client still need to | ||||
# push phase boundary | ||||
Pierre-Yves David
|
r15648 | # | ||
Martin Geisler
|
r16725 | # Push do not only push changeset. It also push phase data. | ||
# New phase data may apply to common changeset which won't be | ||||
# push (as they are common). Here is a very simple example: | ||||
Pierre-Yves David
|
r15648 | # | ||
# 1) repo A push changeset X as draft to repo B | ||||
# 2) repo B make changeset X public | ||||
Martin Geisler
|
r16725 | # 3) repo B push to repo A. X is not pushed but the data that | ||
# X as now public should | ||||
Pierre-Yves David
|
r15648 | # | ||
Martin Geisler
|
r16725 | # The server can't handle it on it's own as it has no idea of | ||
# client phase data. | ||||
Augie Fackler
|
r43347 | keys[b'publishing'] = b'True' | ||
Pierre-Yves David
|
r15648 | return keys | ||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r15648 | def pushphase(repo, nhex, oldphasestr, newphasestr): | ||
timeless@mozdev.org
|
r17535 | """List phases root for serialization over pushkey""" | ||
Pierre-Yves David
|
r18002 | repo = repo.unfiltered() | ||
Bryan O'Sullivan
|
r27861 | with repo.lock(): | ||
Pierre-Yves David
|
r15648 | currentphase = repo[nhex].phase() | ||
Augie Fackler
|
r43346 | newphase = abs(int(newphasestr)) # let's avoid negative index surprise | ||
oldphase = abs(int(oldphasestr)) # let's avoid negative index surprise | ||||
Pierre-Yves David
|
r15648 | if currentphase == oldphase and newphase < oldphase: | ||
Augie Fackler
|
r43347 | with repo.transaction(b'pushkey-phase') as tr: | ||
Bryan O'Sullivan
|
r27861 | advanceboundary(repo, tr, newphase, [bin(nhex)]) | ||
Martin von Zweigbergk
|
r32822 | return True | ||
Matt Mackall
|
r16051 | elif currentphase == newphase: | ||
# raced, but got correct result | ||||
Martin von Zweigbergk
|
r32822 | return True | ||
Pierre-Yves David
|
r15648 | else: | ||
Martin von Zweigbergk
|
r32822 | return False | ||
Pierre-Yves David
|
r15649 | |||
Augie Fackler
|
r43346 | |||
Martin von Zweigbergk
|
r33031 | def subsetphaseheads(repo, subset): | ||
"""Finds the phase heads for a subset of a history | ||||
Returns a list indexed by phase number where each item is a list of phase | ||||
head nodes. | ||||
""" | ||||
cl = repo.changelog | ||||
headsbyphase = [[] for i in allphases] | ||||
# No need to keep track of secret phase; any heads in the subset that | ||||
# are not mentioned are implicitly secret. | ||||
Boris Feld
|
r39308 | for phase in allphases[:secret]: | ||
Augie Fackler
|
r43347 | revset = b"heads(%%ln & %s())" % phasenames[phase] | ||
Martin von Zweigbergk
|
r33031 | headsbyphase[phase] = [cl.node(r) for r in repo.revs(revset, subset)] | ||
return headsbyphase | ||||
Augie Fackler
|
r43346 | |||
Boris Feld
|
r34322 | def updatephases(repo, trgetter, headsbyphase): | ||
Martin von Zweigbergk
|
r33031 | """Updates the repo with the given phase heads""" | ||
# Now advance phase boundaries of all but secret phase | ||||
Boris Feld
|
r34322 | # | ||
# run the update (and fetch transaction) only if there are actually things | ||||
# to update. This avoid creating empty transaction during no-op operation. | ||||
Martin von Zweigbergk
|
r33031 | for phase in allphases[:-1]: | ||
Augie Fackler
|
r43347 | revset = b'%ln - _phase(%s)' | ||
Boris Feld
|
r39329 | heads = [c.node() for c in repo.set(revset, headsbyphase[phase], phase)] | ||
Boris Feld
|
r34322 | if heads: | ||
advanceboundary(repo, trgetter(), phase, heads) | ||||
Martin von Zweigbergk
|
r33031 | |||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r15649 | def analyzeremotephases(repo, subset, roots): | ||
"""Compute phases heads and root in a subset of node from root dict | ||||
* subset is heads of the subset | ||||
* roots is {<nodeid> => phase} mapping. key and value are string. | ||||
Accept unknown element input | ||||
""" | ||||
Pierre-Yves David
|
r18002 | repo = repo.unfiltered() | ||
Pierre-Yves David
|
r15649 | # build list from dictionary | ||
Pierre-Yves David
|
r15892 | draftroots = [] | ||
Augie Fackler
|
r43346 | nodemap = repo.changelog.nodemap # to filter unknown nodes | ||
Pierre-Yves David
|
r15649 | for nhex, phase in roots.iteritems(): | ||
Augie Fackler
|
r43347 | if nhex == b'publishing': # ignore data related to publish option | ||
Pierre-Yves David
|
r15649 | continue | ||
node = bin(nhex) | ||||
phase = int(phase) | ||||
Gregory Szorc
|
r28174 | if phase == public: | ||
Pierre-Yves David
|
r15892 | if node != nullid: | ||
Augie Fackler
|
r43346 | repo.ui.warn( | ||
Augie Fackler
|
r43347 | _( | ||
b'ignoring inconsistent public root' | ||||
b' from remote: %s\n' | ||||
) | ||||
Augie Fackler
|
r43346 | % nhex | ||
) | ||||
Gregory Szorc
|
r28174 | elif phase == draft: | ||
Sune Foldager
|
r15902 | if node in nodemap: | ||
Pierre-Yves David
|
r15892 | draftroots.append(node) | ||
else: | ||||
Augie Fackler
|
r43346 | repo.ui.warn( | ||
Augie Fackler
|
r43347 | _(b'ignoring unexpected root from remote: %i %s\n') | ||
Augie Fackler
|
r43346 | % (phase, nhex) | ||
) | ||||
Pierre-Yves David
|
r15649 | # compute heads | ||
Pierre-Yves David
|
r15954 | publicheads = newheads(repo, subset, draftroots) | ||
Pierre-Yves David
|
r15892 | return publicheads, draftroots | ||
Pierre-Yves David
|
r15649 | |||
Augie Fackler
|
r43346 | |||
Boris Feld
|
r34820 | class remotephasessummary(object): | ||
"""summarize phase information on the remote side | ||||
:publishing: True is the remote is publishing | ||||
:publicheads: list of remote public phase heads (nodes) | ||||
:draftheads: list of remote draft phase heads (nodes) | ||||
:draftroots: list of remote draft phase root (nodes) | ||||
""" | ||||
def __init__(self, repo, remotesubset, remoteroots): | ||||
unfi = repo.unfiltered() | ||||
self._allremoteroots = remoteroots | ||||
Augie Fackler
|
r43347 | self.publishing = remoteroots.get(b'publishing', False) | ||
Boris Feld
|
r34820 | |||
ana = analyzeremotephases(repo, remotesubset, remoteroots) | ||||
self.publicheads, self.draftroots = ana | ||||
# Get the list of all "heads" revs draft on remote | ||||
Augie Fackler
|
r43347 | dheads = unfi.set(b'heads(%ln::%ln)', self.draftroots, remotesubset) | ||
Boris Feld
|
r34820 | self.draftheads = [c.node() for c in dheads] | ||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r15954 | def newheads(repo, heads, roots): | ||
"""compute new head of a subset minus another | ||||
* `heads`: define the first subset | ||||
Mads Kiilerich
|
r17425 | * `roots`: define the second we subtract from the first""" | ||
Boris Feld
|
r39182 | # prevent an import cycle | ||
# phases > dagop > patch > copies > scmutil > obsolete > obsutil > phases | ||||
from . import dagop | ||||
repo = repo.unfiltered() | ||||
cl = repo.changelog | ||||
rev = cl.nodemap.get | ||||
if not roots: | ||||
return heads | ||||
Boris Feld
|
r39260 | if not heads or heads == [nullid]: | ||
Boris Feld
|
r39182 | return [] | ||
# The logic operated on revisions, convert arguments early for convenience | ||||
new_heads = set(rev(n) for n in heads if n != nullid) | ||||
roots = [rev(n) for n in roots] | ||||
# compute the area we need to remove | ||||
Augie Fackler
|
r43347 | affected_zone = repo.revs(b"(%ld::%ld)", roots, new_heads) | ||
Boris Feld
|
r39182 | # heads in the area are no longer heads | ||
new_heads.difference_update(affected_zone) | ||||
# revisions in the area have children outside of it, | ||||
# They might be new heads | ||||
Augie Fackler
|
r43346 | candidates = repo.revs( | ||
Augie Fackler
|
r43347 | b"parents(%ld + (%ld and merge())) and not null", roots, affected_zone | ||
Augie Fackler
|
r43346 | ) | ||
Boris Feld
|
r39182 | candidates -= affected_zone | ||
if new_heads or candidates: | ||||
# remove candidate that are ancestors of other heads | ||||
new_heads.update(candidates) | ||||
Augie Fackler
|
r43347 | prunestart = repo.revs(b"parents(%ld) and not null", new_heads) | ||
Boris Feld
|
r39182 | pruned = dagop.reachableroots(repo, candidates, prunestart) | ||
new_heads.difference_update(pruned) | ||||
return pycompat.maplist(cl.node, sorted(new_heads)) | ||||
Pierre-Yves David
|
r16030 | |||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r16030 | def newcommitphase(ui): | ||
"""helper to get the target phase of new commit | ||||
Handle all possible values for the phases.new-commit options. | ||||
""" | ||||
Augie Fackler
|
r43347 | v = ui.config(b'phases', b'new-commit') | ||
Pierre-Yves David
|
r16030 | try: | ||
return phasenames.index(v) | ||||
except ValueError: | ||||
try: | ||||
return int(v) | ||||
except ValueError: | ||||
Augie Fackler
|
r43347 | msg = _(b"phases.new-commit: not a valid phase name ('%s')") | ||
Pierre-Yves David
|
r16030 | raise error.ConfigError(msg % v) | ||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r17671 | def hassecret(repo): | ||
"""utility function that check if a repo have any secret changeset.""" | ||||
return bool(repo._phasecache.phaseroots[2]) | ||||
Boris Feld
|
r34711 | |||
Augie Fackler
|
r43346 | |||
Boris Feld
|
r34711 | def preparehookargs(node, old, new): | ||
if old is None: | ||||
Augie Fackler
|
r43347 | old = b'' | ||
Boris Feld
|
r34711 | else: | ||
Kevin Bullock
|
r34877 | old = phasenames[old] | ||
Augie Fackler
|
r43347 | return {b'node': node, b'oldphase': old, b'phase': phasenames[new]} | ||