timestamp /

Full commit
# - node recoloring similar to global tags
# Copyright 2012 Friedrich Kastner-Masilko <>
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.
'''node recoloring similar to global tags

This extension basically lets you rename named branches as well as create new named branches after commits already happened.

from mercurial import cmdutil, localrepo, context, encoding, util, error, errno
from mercurial.node import nullid, bin, hex, short
from mercurial.i18n import _
propertycache = util.propertycache

cmdtable = {}
command = cmdutil.command(cmdtable)

#def override_pull(orig, ui, repo, source=None, **opts):
#    result = orig(ui, repo, source, **opts)
#    pull(ui, databasepath(ui), readconfig(ui)[0])
#    return result

def findglobalrebranches(ui, repo, allrebranches):
    '''Find global rebranches in repo by reading .hgrebranch from every
    head that has a distinct version of it, using a cache to avoid excess
    Updates the list allrebranches in place: allrebranches maps a
    (node, node) pair to a branch name (see _readrules() below).'''
    # This is so we can be lazy and assume allbranches contains only global
    # rules when we pass it to _writerulescache().
    assert len(allrebranches) == 0, \
           "findglobalrebranches() should be called first"

    (heads, rulefnode, cacherules, shouldwrite) = _readrulescache(ui, repo)
    if cacherules is not None:
        assert not shouldwrite

    seen = set()                    # set of fnode
    fctx = None
    for head in reversed(heads):        # oldest to newest
        assert head in repo.changelog.nodemap, \
               "rule cache returned bogus head %s" % short(head)

        fnode = rulefnode.get(head)
        if fnode and fnode not in seen:
            if not fctx:
                fctx = repo.filectx('.hgrebranch', fileid=fnode)
                fctx = fctx.filectx(fnode)

            allrebranches.extend(_readrules(ui, repo,, fctx))
    # and update the cache (if necessary)
    if shouldwrite:
        _writerulescache(ui, repo, heads, rulefnode, allrebranches)

def readlocalrebranches(ui, repo, allrebranches):
    '''Read local rebranches in repo.  Update allrebranches.'''
        data ="localrebranch")
    except IOError, inst:
        if inst.errno != errno.ENOENT:

    # localrebranch is in the local encoding; re-encode to UTF-8 on
    # input for consistency with the rest of this module.
        ui, repo, data.splitlines(), "localrebranch",

def _readrules(ui, repo, lines, fn, recode=None):
    '''Read rebranch definitions from a file (or any source of lines).
    Return a mapping from (node, node) to branch name: nodes are the range
    specifiers for a new branch name.  All node ids are binary, not hex.'''

    filerules = []               # map (node, node) to branch name
    count = 0

    def warn(msg):
        ui.warn(_("%s, line %s: %s\n") % (fn, count, msg))

    for line in lines:
        count += 1
        if not line:
            (nodehex1, nodehex2, name) = line.split(" ", 2)
        except ValueError:
            warn(_("cannot parse entry"))
        name = name.strip()
        if recode:
            name = recode(name)
            nodebin1 = bin(nodehex1)            
        except TypeError:
            warn(_("start node '%s' is not well formed") % nodehex1)
            nodebin2 = bin(nodehex2)            
        except TypeError:
            warn(_("end node '%s' is not well formed") % nodehex2)

        # update filerules
        pair = (nodebin1, nodebin2)
        filerules.append(((nodebin1, nodebin2), name))
    return filerules

# The rebranch cache only stores info about heads, not the rebranch
# contents from each head.  I.e. it doesn't try to squeeze out the
# maximum performance, but is simpler has a better chance of actually
# working correctly.  And this gives the biggest performance win: it
# avoids looking up .hgrebranch in the manifest for every head, and it
# can avoid calling heads() at all if there have been no changes to
# the repo.

def _readrulescache(ui, repo):
    '''Read the rebranch cache and return a tuple (heads, fnodes, cacherules,
    shouldwrite).  If the cache is completely up-to-date, cacherules is a
    dict of the form returned by _readrules(); otherwise, it is None and
    heads and fnodes are set.  In that case, heads is the list of all
    heads currently in the repository (ordered from tip to oldest) and
    fnodes is a mapping from head to .hgrebranch filenode.  If those two are
    set, caller is responsible for reading rebranch info from each head.'''

        cachefile = repo.opener('cache/rebranch', 'r')
        # force reading the file for static-http
        cachelines = iter(cachefile)
    except IOError:
        cachefile = None

    # The cache file consists of lines like
    #   <headrev> <headnode> [<rulenode>]
    # where <headrev> and <headnode> redundantly identify a repository
    # head from the time the cache was written, and <rulenode> is the
    # filenode of .hgrebranch on that head.  Heads with no .hgrebranch file
    # will have no <rulenode>.  The cache is ordered from tip to oldest
    # (which is part of why <headrev> is there: a quick visual check is all
    # that's required to ensure correct order).
    # This information is enough to let us avoid the most expensive part
    # of finding global rebranches, which is looking up <rulenode> in the
    # manifest for each head.
    cacherevs = []                      # list of headrev
    cacheheads = []                     # list of headnode
    cachefnode = {}                     # map headnode to filenode
    if cachefile:
            for line in cachelines:
                if line == "\n":
                line = line.rstrip().split()
                headnode = bin(line[1])
                if len(line) == 3:
                    fnode = bin(line[2])
                    cachefnode[headnode] = fnode
        except Exception:
            # corruption of the rebranch cache, just recompute it
            ui.warn(_('.hg/cache/rebranch is corrupt, rebuilding it\n'))
            cacheheads = []
            cacherevs = []
            cachefnode = {}

    tipnode = repo.changelog.tip()
    tiprev = len(repo.changelog) - 1

    # Case 1 (common): tip is the same, so nothing has changed.
    # (Unchanged tip trivially means no changesets have been added.
    # But, thanks to localrepository.destroyed(), it also means none
    # have been destroyed by strip or rollback.)
    if cacheheads and cacheheads[0] == tipnode and cacherevs[0] == tiprev:
        rules = _readrules(ui, repo, cachelines,
        return (None, None, rules, False)
    if cachefile:
        cachefile.close()               # ignore rest of file

    repoheads = repo.heads()
    # Case 2 (uncommon): empty repo; get out quickly and don't bother
    # writing an empty cache.
    if repoheads == [nullid]:
        return ([], {}, {}, False)

    # Case 3 (uncommon): cache file missing or empty.

    # Case 4 (uncommon): tip rev decreased.  This should only happen
    # when we're called from localrepository.destroyed().  Refresh the
    # cache so future invocations will not see disappeared heads in the
    # cache.

    # Case 5 (common): tip has changed, so we've added/replaced heads.

    # As it happens, the code to handle cases 3, 4, 5 is the same.

    # N.B. in case 4 (nodes destroyed), "new head" really means "newly
    # exposed".
    newheads = [head
                for head in repoheads
                if head not in set(cacheheads)]

    # Now we have to lookup the .hgrebranch filenode for every new head.
    # This is the most expensive part of finding rebranches, so performance
    # depends primarily on the size of newheads.  Worst case: no cache
    # file, so newheads == repoheads.
    for head in newheads:
        cctx = repo[head]
            fnode = cctx.filenode('.hgrebranch')
            cachefnode[head] = fnode
        except error.LookupError:
            # no .hgrebranch file on this head

    # Caller has to iterate over all heads, but can use the filenodes in
    # cachefnode to get to each .hgrebranch revision quickly.
    return (repoheads, cachefnode, None, True)

def _writerulescache(ui, repo, heads, rulefnode, cacherules):

        cachefile = repo.opener('cache/rebranch', 'w', atomictemp=True)
    except (OSError, IOError):

    realheads = repo.heads()            # for sanity checks below
    for head in heads:
        # temporary sanity checks; these can probably be removed
        # once this code has been in crew for a few weeks
        assert head in repo.changelog.nodemap, \
               'trying to write non-existent node %s to rebranch cache' % short(head)
        assert head in realheads, \
               'trying to write non-head %s to rebranch cache' % short(head)
        assert head != nullid, \
               'trying to write nullid to rebranch cache'

        # This can't fail because of the first assert above.  When/if we
        # remove that assert, we might want to catch LookupError here
        # and downgrade it to a warning.
        rev = repo.changelog.rev(head)

        fnode = rulefnode.get(head)
        if fnode:
            cachefile.write('%d %s %s\n' % (rev, hex(head), hex(fnode)))
            cachefile.write('%d %s\n' % (rev, hex(head)))

    # Branch names in the cache are in UTF-8 -- which is the whole reason
    # we keep them in UTF-8 throughout this module.  If we converted
    # them local encoding on input, we would lose info writing them to
    # the cache.
    for ((node1, node2), name) in cacherules:
        cachefile.write("%s %s %s\n" % (hex(node1), hex(node2), name))
    except (OSError, IOError):

         [('r', 'rev', None, _('revision set by range specification'), _('REV')),
          ('b', 'branch', None, _('revision set by branch name'), _('BRANCH')),
          ('d', 'delete', None, _('deletes specified branch')),
         _('hg rebranch [-r REV | -b BRANCH | -d] BRANCH'))
def rebranch(ui, repo, newbranch=None, **opts):
    '''rebranches a given revision set to the specified branch name

    If no option is specified, all revisions in the current branch will
    get the new branch name. If -r is given, the revsets determines the
    revisions for the new branch, with -b, the given branch name addresses
    them. With the -d option, all nodes addressed with the branch name will
    get the branch of their ancestors, thus effectively deleting the branch

    This command will affect the current working copy's branch name, if a
    parent on the same branch is affected by the operation. I.e. if your
    current working copy is "mybranch", and the second parent is "mybranch",
    and you change "mybranch" to "newbranch", the working copy will be also
    marked as "newbranch".

    Like with the tag command, this command commits the changes to the
    .hgrebranch file.

def uisetup(ui):
    # Install new functions in localrepo class

    def nodebranch(self, node, default):
        '''return the branch name associated with a node'''
        if not self._rebranchescache.nodebranchcache:
            nodebranchcache = {}
            branchcache = []

            c = self.changelog

            for rule, name in self._rebranchescache.rebranches:
                if name not in branchcache:
                start = c.rev(rule[0])
                visit = [c.rev(rule[1])]
                reachable = {visit[0]:[]}
                while visit:
                    n = visit.pop(0)
                    if n == start:
                    if n < 0:
                    for p in c.parentrevs(n):
                        if p < start:
                        if p not in reachable:
                if start in reachable:                    
                    visit = reachable[start]
                    while visit:
                        n = visit.pop(0)                        
                        if n in reachable and n not in nodecache:
            self._rebranchescache.nodebranchcache = nodebranchcache
            self._rebranchescache.branchcache = branchcache
        rev = self[node].rev()
        if rev in self._rebranchescache.nodebranchcache:
            return encoding.tolocal(self._rebranchescache.branchcache[self._rebranchescache.nodebranchcache[rev]])

        if default not in self._rebranchescache.branchcache:
        return encoding.tolocal(default)

    def _rebranchescache(self):
        '''Returns a rebranchescache object that contains various rebranches related caches.'''

        # This simplifies its cache management by having one decorated
        # function (this one) and the rest simply fetch things from it.
        class rebranchescache(object):
            def __init__(self):
                # These two define the list of rebranches for this repository.
                # rebranches lists rule-to-name tuples;
                self.rebranches = None
                self.nodebranchcache = None
                self.branchcache = None

        cache = rebranchescache()
        cache.rebranches = self._findrebranches()

        return cache

    def _findrebranches(self):
        '''Do the hard work of finding rebranches. Return a list of
           rule/name tuples, with rule being a pair of binary nodes'''

        allrebranches = []

        findglobalrebranches(self.ui, self, allrebranches)        
        readlocalrebranches(self.ui, self, allrebranches)

        return allrebranches

    # Overwrite changectx.branch(self) to call nodebranch function
    def branch(self):
        return self._repo.nodebranch(self._node, self._changeset[5].get("branch"))
    #extensions.wrapcommand(commands.table, 'pull', override_pull)