obsolete: to not overwrite cancopy
This does not bring anything and this confuse verify.
# obsolete.py - introduce the obsolete concept in mercurial.
#
# Copyright 2011 Pierre-Yves David <pierre-yves.david@ens-lyon.org>
# Logilab SA <contact@logilab.fr>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
"""Introduce the Obsolete concept to mercurial
General concept
===============
This extension introduces the *obsolete* concept. It adds a new *obsolete*
relation between two changesets. A relation ``<changeset B> obsolete <changeset
A>`` is set to denote that ``<changeset B>`` is new version of ``<changeset
A>``.
The *obsolete* relation act as a **perpendicular history** to the standard
changeset history. Standard changeset history versions files. The *obsolete*
relation versions changesets.
:obsolete: a changeset that have been replace by another one.
:unstable: a non-obsolet changeset based on another one.
:suspended: an obsolete changeset with unstable descendant.
:extinct: an obsolete changeset without unstable descendant
(subject to garbage collection)
Another name for unstable could be out of sync.
Usage and Feature
=================
Display and Exchange
....................
obsolete changesets are hidden. (except if they have non obsolete changeset)
obsolete changesets are currently not exchange. This will probably change later
but it was the simpler solution for now.
New commands
............
a ``debugobsolete`` command has been added.
It add an obsolete relation between too relation.
Context object
..............
Context gain a ``obsolete`` method that return True if a changeset is obsolete
False otherwise.
revset
......
Add an ``obsolete()`` entry.
repo extension
..............
To Do
-----
* refuse to obsolete published changesets
* handle split
* handle conflict
* handle unstable // out of sync
"""
import os
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from mercurial.i18n import _
import base64
from mercurial import util
from mercurial import context
from mercurial import revset
from mercurial import scmutil
from mercurial import extensions
from mercurial import pushkey
from mercurial import discovery
from mercurial import error
from mercurial import commands
from mercurial import changelog
from mercurial import phases
from mercurial.node import hex, bin, short, nullid
from mercurial.lock import release
try:
from mercurial.localrepo import storecache
storecache('babar') # to trigger import
except (TypeError, ImportError):
def storecache(*args):
return scmutil.filecache(*args, instore=True)
### Patch changectx
#############################
def obsolete(ctx):
"""is the changeset obsolete by other"""
if ctx.node()is None:
return False
return bool(ctx._repo.obsoletedby(ctx.node())) and ctx.phase()
context.changectx.obsolete = obsolete
def unstable(ctx):
"""is the changeset unstable (have obsolete ancestor)"""
if ctx.node() is None:
return False
return ctx.rev() in ctx._repo._unstableset
context.changectx.unstable = unstable
### revset
#############################
def revsetobsolete(repo, subset, x):
"""obsolete changesets"""
args = revset.getargs(x, 0, 0, 'obsolete takes no argument')
return [r for r in subset if r in repo._obsoleteset and repo._phaserev[r] > 0]
def revsetunstable(repo, subset, x):
"""non obsolete changesets descendant of obsolete one"""
args = revset.getargs(x, 0, 0, 'unstable takes no arguments')
return [r for r in subset if r in repo._unstableset]
def revsetsuspended(repo, subset, x):
"""obsolete changesets with non obsolete descendants"""
args = revset.getargs(x, 0, 0, 'unstable takes no arguments')
return [r for r in subset if r in repo._suspendedset]
def revsetextinct(repo, subset, x):
"""obsolete changesets without obsolete descendants"""
args = revset.getargs(x, 0, 0, 'unstable takes no arguments')
return [r for r in subset if r in repo._extinctset]
def _obsparents(repo, s):
"""obsolete parents of a subset"""
cs = set()
nm = repo.changelog.nodemap
for r in s:
for p in repo._obssubrels.get(repo[r].node(), ()):
pr = nm.get(p)
if pr is not None:
cs.add(pr)
return cs
def revsetobsparents(repo, subset, x):
"""obsolete parents"""
s = revset.getset(repo, range(len(repo)), x)
cs = _obsparents(repo, s)
return [r for r in subset if r in cs]
def _obsancestors(repo, s):
"""obsolete ancestors of a subset"""
toproceed = [repo[r].node() for r in s]
seen = set()
while toproceed:
nc = toproceed.pop()
for np in repo._obssubrels.get(nc, ()):
if np not in seen:
seen.add(np)
toproceed.append(np)
nm = repo.changelog.nodemap
cs = set()
for p in seen:
pr = nm.get(p)
if pr is not None:
cs.add(pr)
return cs
def revsetobsancestors(repo, subset, x):
"""obsolete parents"""
s = revset.getset(repo, range(len(repo)), x)
cs = _obsancestors(repo, s)
return [r for r in subset if r in cs]
### Other Extension compat
############################
def buildstate(orig, repo, dest, rebaseset, detach):
"""wrapper for rebase 's buildstate that exclude obsolete changeset"""
rebaseset = repo.revs('%ld - extinct()', rebaseset)
return orig(repo, dest, rebaseset, detach)
def concludenode(orig, repo, rev, *args, **kwargs):
"""wrapper for rebase 's concludenode that set obsolete relation"""
newrev = orig(repo, rev, *args, **kwargs)
oldnode = repo[rev].node()
newnode = repo[newrev].node()
repo.addobsolete(newnode, oldnode)
return newrev
def cmdrebase(orig, repo, ui, *args, **kwargs):
oldkeep = kwargs.pop('keep', False)
if oldkeep:
ui.warn('WARNING --keep option ignored by experimental obsolete extension')
kwargs['keep'] = True
return orig(repo, ui, *args, **kwargs)
def extsetup(ui):
revset.symbols["obsolete"] = revsetobsolete
revset.symbols["unstable"] = revsetunstable
revset.symbols["suspended"] = revsetsuspended
revset.symbols["extinct"] = revsetextinct
revset.symbols["obsparents"] = revsetobsparents
revset.symbols["obsancestors"] = revsetobsancestors
try:
rebase = extensions.find('rebase')
if rebase:
extensions.wrapfunction(rebase, 'buildstate', buildstate)
extensions.wrapfunction(rebase, 'concludenode', concludenode)
extensions.wrapcommand(rebase.cmdtable, "rebase", cmdrebase)
except KeyError:
pass # rebase not found
# Pushkey mechanism for mutable
#########################################
def pushobsolete(repo, key, old, raw):
"""push obsolete relation through pushkey"""
assert key == "relations"
w = repo.wlock()
try:
tmp = StringIO()
tmp.write(raw)
tmp.seek(0)
relations = _obsdeserialise(tmp)
for sub, objs in relations.iteritems():
for obj in objs:
try:
repo.addobsolete(sub, obj)
except error.RepoLookupError:
pass
return 0
finally:
w.release()
def listobsolete(repo):
"""dump all obsolete relation in
XXX this have be improved"""
tmp = StringIO()
_obsserialise(repo._obssubrels, tmp)
return {'relations': base64.b64encode(tmp.getvalue())}
pushkey.register('obsolete', pushobsolete, listobsolete)
### New commands
#############################
def cmddebugobsolete(ui, repo, subject, object):
"""Add an obsolete relation between a too node
The subject is expected to be a newer version of the object"""
sub = repo[subject]
obj = repo[object]
repo.addobsolete(sub.node(), obj.node())
return 0
cmdtable = {'debugobsolete': (cmddebugobsolete, [], '<subject> <object>')}
### Altering existing command
#############################
def wrapmayobsoletewc(origfn, ui, repo, *args, **opts):
res = origfn(ui, repo, *args, **opts)
if repo['.'].obsolete():
ui.warn(_('Working directory parent is obsolete\n'))
return res
def uisetup(ui):
extensions.wrapcommand(commands.table, "update", wrapmayobsoletewc)
extensions.wrapcommand(commands.table, "pull", wrapmayobsoletewc)
### serialisation
#############################
def _obsserialise(obssubrels, flike):
"""serialise an obsolete relation mapping in a plain text one
this is for subject -> [objects] mapping
format is::
<subject-full-hex> <object-full-hex>\n"""
for sub, objs in obssubrels.iteritems():
for obj in objs:
if sub is None:
sub = nullid
flike.write('%s %s\n' % (hex(sub), hex(obj)))
def _obsdeserialise(flike):
"""read a file like object serialised with _obsserialise
this desierialize into a {subject -> objects} mapping"""
rels = {}
for line in flike:
subhex, objhex = line.split()
subnode = bin(subhex)
if subnode == nullid:
subnode = None
rels.setdefault( subnode, set()).add(bin(objhex))
return rels
### diagnostique tools
#############################
def unstables(repo):
"""Return all unstable changeset"""
return scmutil.revrange(repo, ['obsolete():: and (not obsolete())'])
def newerversion(repo, obs):
"""Return the newer version of an obsolete changeset"""
toproceed = set([obs])
# XXX know optimization available
newer = set()
while toproceed:
current = toproceed.pop()
if current in repo._obsobjrels:
toproceed.update(repo._obsobjrels[current])
elif current is not None: # None is kill
newer.add((current,))
else:
newer.add(())
return sorted(newer)
### repo subclassing
#############################
def reposetup(ui, repo):
if not repo.local():
return
opull = repo.pull
opush = repo.push
o_rollback = repo._rollback
o_updatebranchcache = repo.updatebranchcache
# /!\ api change in Hg 2.2 (97efd26eb9576f39590812ea9) /!\
if util.safehasattr(repo, '_journalfiles'): # Hg 2.2
o_journalfiles = repo._journalfiles
o_writejournal = repo._writejournal
class obsoletingrepo(repo.__class__):
### Public method
def obsoletedby(self, node):
"""return the set of node that make <node> obsolete (obj)"""
return self._obsobjrels.get(node, set())
def obsolete(self, node):
"""return the set of node that <node> make obsolete (sub)"""
return self._obssubrels.get(node, set())
@util.propertycache
def _obsoleteset(self):
"""the set of obsolete revision"""
obs = set()
nm = self.changelog.nodemap
for obj in self._obsobjrels:
try: # /!\api change in Hg 2.2 (e8d37b78acfb22ae2c1fb126c2)/!\
rev = nm.get(obj)
except TypeError: #XXX to remove while breaking Hg 2.1 support
rev = nm.get(obj, None)
if rev is not None:
obs.add(rev)
return obs
@util.propertycache
def _unstableset(self):
"""the set of non obsolete revision with obsolete parent"""
return set(self.revs('(obsolete()::) - obsolete()'))
@util.propertycache
def _suspendedset(self):
"""the set of obsolete parent with non obsolete descendant"""
return set(self.revs('obsolete() and obsolete()::unstable()'))
@util.propertycache
def _extinctset(self):
"""the set of obsolete parent without non obsolete descendant"""
return set(self.revs('obsolete() - obsolete()::unstable()'))
def _clearobsoletecache(self):
if '_obsobjrels' in vars(self):
del self._obsobjrels
if '_obssubrels' in vars(self):
del self._obssubrels
if '_obsoleteset' in vars(self):
del self._obsoleteset
self._clearunstablecache()
def updatebranchcache(self):
o_updatebranchcache()
self._clearunstablecache()
def _clearunstablecache(self):
if '_unstableset' in vars(self):
del self._unstableset
if '_suspendedset' in vars(self):
del self._suspendedset
if '_extinct' in vars(self):
del self._extinctset
def addobsolete(self, sub, obj):
"""Add a relation marking that node <sub> is a new version of <obj>"""
if sub == nullid:
sub = None
if obj in self._obssubrels.get(sub, set()):
return 0
if sub == obj:
return 0
self._obssubrels.setdefault(sub, set()).add(obj)
self._obsobjrels.setdefault(obj, set()).add(sub)
try:
if self[obj].phase() == 0:
if sub is None:
self.ui.warn(
_("trying to kill immutable changeset %(obj)s\n")
% {'obj': short(obj)})
if sub is not None:
self.ui.warn(
_("%(sub)s try to obsolete immutable changeset %(obj)s\n")
% {'sub': short(sub), 'obj': short(obj)})
self.changelog.hiddenrevs.add(repo[obj].rev())
except (error.RepoLookupError, error.LookupError):
pass #unknow revision (but keep propagating the data
self._writeobsrels()
self._clearobsoletecache()
return 1
### obsolete storage
@util.propertycache
def _obsobjrels(self):
"""{<old-node> -> set(<new-node>)}
also compute hidden revision"""
#reverse sub -> objs mapping
objrels = {}
for sub, objs in self._obssubrels.iteritems():
for obj in objs:
objrels.setdefault(obj, set()).add(sub)
return objrels
@util.propertycache
def _obssubrels(self):
"""{<new-node> -> set(<old-node>)}"""
return self._readobsrels()
### Disk IO
def _readobsrels(self):
"""Write obsolete relation on disk"""
# XXX handle lock
try:
f = self.opener('obsolete-relations')
try:
return _obsdeserialise(f)
finally:
f.close()
except IOError:
return {}
def _writeobsrels(self):
"""Write obsolete relation on disk"""
# XXX handle lock
lock = self.wlock()
try:
f = self.opener('obsolete-relations', 'w', atomictemp=True)
try:
_obsserialise(self._obssubrels, f)
try:
f.rename()
except AttributeError: # old version
f.close()
finally:
f.close()
finally:
lock.release()
### pull // push support
def pull(self, remote, *args, **kwargs):
"""wrapper around push that push obsolete relation"""
result = opull(remote, *args, **kwargs)
if 'obsolete' in remote.listkeys('namespaces'):
tmp = StringIO()
rels = remote.listkeys('obsolete')['relations']
tmp.write(base64.b64decode(rels))
tmp.seek(0)
obsrels = _obsdeserialise(tmp)
for sub, objs in obsrels.iteritems():
for obj in objs:
self.addobsolete(sub, obj)
return result
def push(self, remote, *args, **opts):
"""wrapper around pull that pull obsolete relation"""
result = opush(remote, *args, **opts)
if 'obsolete' in remote.listkeys('namespaces'):
tmp = StringIO()
_obsserialise(self._obssubrels, tmp)
remote.pushkey('obsolete', 'relations', '', tmp.getvalue())
return result
### rollback support
# /!\ api change in Hg 2.2 (97efd26eb9576f39590812ea9) /!\
if util.safehasattr(repo, '_journalfiles'): # Hg 2.2
def _journalfiles(self):
return o_journalfiles() + (self.join('journal.obsolete-relations'),)
def _writejournal(self, desc):
"""wrapped version of _writejournal that save obsolete data"""
o_writejournal(desc)
filename = 'obsolete-relations'
filepath = self.join(filename)
if os.path.exists(filepath):
journalname = 'journal.' + filename
journalpath = self.join(journalname)
util.copyfile(filepath, journalpath)
else: # XXX remove this bloc while breaking support to Hg 2.1
def _writejournal(self, desc):
"""wrapped version of _writejournal that save obsolete data"""
entries = list(o_writejournal(desc))
filename = 'obsolete-relations'
filepath = self.join(filename)
if os.path.exists(filepath):
journalname = 'journal.' + filename
journalpath = self.join(journalname)
util.copyfile(filepath, journalpath)
entries.append(journalpath)
return tuple(entries)
def _rollback(self, dryrun, force):
"""wrapped version of _rollback that restore obsolete data"""
ret = o_rollback(dryrun, force)
if not (ret or dryrun): #rollback did not failed
src = self.join('undo.obsolete-relations')
dst = self.join('obsolete-relations')
if os.path.exists(src):
util.rename(src, dst)
elif os.path.exists(dst):
# If no state was saved because the file did not existed before.
os.unlink(dst)
# invalidate cache
self.__dict__.pop('_obssubrels', None)
self.__dict__.pop('_obsobjrels', None)
return ret
@storecache('00changelog.i')
def changelog(self):
changelog = getattr(super(obsoletingrepo, self), 'changelog')
old = changelog.__dict__.pop('hiddenrevs', ())
if old:
ui.warn("old wasn't empty ? %r" % old)
def _sethidden(changelog, value):
assert not value
class hchangelog(changelog.__class__):
@util.propertycache
def hiddenrevs(changelog):
shown = ['not obsolete()', '.', 'bookmark()', 'tagged()',
'public()']
basicquery = 'obsolete() - (::(%s))' % (' or '.join(shown))
# !!! self is repo not changelog
result = set(scmutil.revrange(self, [basicquery]))
return result
changelog.__class__ = hchangelog
return changelog
repo.__class__ = obsoletingrepo
if repo.ui.configbool('obsolete', 'secret-unstable', True):
symbol = 'obsolete()'
else:
symbol = 'extinct()'
expobs = [c.node() for c in repo.set('%s - secret()' % symbol)]
if expobs: # do not lock in nothing move. locking for peanut make hgview reload on any command
lock = repo.lock()
try:
phases.retractboundary(repo, 2, expobs)
finally:
lock.release()