view mercurial/repair.py @ 5192:33015dac5df5

convert: fix mercurial_sink.putcommit Changeset 4ebc8693ce72 added some code to putcommit to avoid creating a revision that touches no files, but this can break regular conversions from some repositories: - conceptually, since we're converting a repo, we should try to make the new hg repo as similar as possible to the original repo - we should create a new changeset, even if the original revision didn't touch any files (maybe the commit message had some important bit); - even if a "regular" revision that doesn't touch any file may seem weird (and maybe even broken), it's completely legitimate for a merge revision to not touch any file, and, if we just skip it, the converted repo will end up with wrong history and possibly an extra head. As an example, say the crew and main hg repos are sync'ed. Somebody sends an important patch to the mailing list. Matt quickly applies and pushes it. But at the same time somebody also applies it to crew and pushes it. Suppose the commit message ended up being a bit different (say, there was a typo and somebody didn't fix it) or that the date ended up being different (because of different patch-applying scripts): the changeset hashes will be different, but the manifests will be the same. Since both changesets were pushed to public repos, it's hard to recall them. If both are merged, the manifest from the resulting merge revision will have the exact same contents as its parents - i.e. the merge revision really doesn't touch any file at all. To keep the file filtering stuff "working", the generic code was changed to skip empty revisions if we're filtering the repo, fixing a bug in the process (we want parents[0] instead of tip).
author Alexis S. L. Carvalho <alexis@cecm.usp.br>
date Fri, 17 Aug 2007 20:18:05 -0300
parents 18e91c9def0c
children
line wrap: on
line source

# repair.py - functions for repository repair for mercurial
#
# Copyright 2005, 2006 Chris Mason <mason@suse.com>
# Copyright 2007 Matt Mackall
#
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.

import changegroup, revlog, os, commands

def strip(ui, repo, rev, backup="all"):
    def limitheads(chlog, stop):
        """return the list of all nodes that have no children"""
        p = {}
        h = []
        stoprev = 0
        if stop in chlog.nodemap:
            stoprev = chlog.rev(stop)

        for r in xrange(chlog.count() - 1, -1, -1):
            n = chlog.node(r)
            if n not in p:
                h.append(n)
            if n == stop:
                break
            if r < stoprev:
                break
            for pn in chlog.parents(n):
                p[pn] = 1
        return h

    def bundle(repo, bases, heads, rev, suffix):
        cg = repo.changegroupsubset(bases, heads, 'strip')
        backupdir = repo.join("strip-backup")
        if not os.path.isdir(backupdir):
            os.mkdir(backupdir)
        name = os.path.join(backupdir, "%s-%s" % (revlog.short(rev), suffix))
        ui.warn("saving bundle to %s\n" % name)
        return changegroup.writebundle(cg, name, "HG10BZ")

    def stripall(revnum):
        mm = repo.changectx(rev).manifest()
        seen = {}

        for x in xrange(revnum, repo.changelog.count()):
            for f in repo.changectx(x).files():
                if f in seen:
                    continue
                seen[f] = 1
                if f in mm:
                    filerev = mm[f]
                else:
                    filerev = 0
                seen[f] = filerev
        # we go in two steps here so the strip loop happens in a
        # sensible order.  When stripping many files, this helps keep
        # our disk access patterns under control.
        seen_list = seen.keys()
        seen_list.sort()
        for f in seen_list:
            ff = repo.file(f)
            filerev = seen[f]
            if filerev != 0:
                if filerev in ff.nodemap:
                    filerev = ff.rev(filerev)
                else:
                    filerev = 0
            ff.strip(filerev, revnum)

    chlog = repo.changelog
    # TODO delete the undo files, and handle undo of merge sets
    pp = chlog.parents(rev)
    revnum = chlog.rev(rev)

    # save is a list of all the branches we are truncating away
    # that we actually want to keep.  changegroup will be used
    # to preserve them and add them back after the truncate
    saveheads = []
    savebases = {}

    heads = limitheads(chlog, rev)
    seen = {}

    # search through all the heads, finding those where the revision
    # we want to strip away is an ancestor.  Also look for merges
    # that might be turned into new heads by the strip.
    while heads:
        h = heads.pop()
        n = h
        while True:
            seen[n] = 1
            pp = chlog.parents(n)
            if pp[1] != revlog.nullid:
                for p in pp:
                    if chlog.rev(p) > revnum and p not in seen:
                        heads.append(p)
            if pp[0] == revlog.nullid:
                break
            if chlog.rev(pp[0]) < revnum:
                break
            n = pp[0]
            if n == rev:
                break
        r = chlog.reachable(h, rev)
        if rev not in r:
            saveheads.append(h)
            for x in r:
                if chlog.rev(x) > revnum:
                    savebases[x] = 1

    # create a changegroup for all the branches we need to keep
    if backup == "all":
        bundle(repo, [rev], chlog.heads(), rev, 'backup')
    if saveheads:
        chgrpfile = bundle(repo, savebases.keys(), saveheads, rev, 'temp')

    stripall(revnum)

    change = chlog.read(rev)
    chlog.strip(revnum, revnum)
    repo.manifest.strip(repo.manifest.rev(change[0]), revnum)
    if saveheads:
        ui.status("adding branch\n")
        commands.unbundle(ui, repo, "file:%s" % chgrpfile, update=False)
        if backup != "strip":
            os.unlink(chgrpfile)