contrib/convert-repo
author Thomas Arendsen Hein <thomas@intevation.de>
Sat, 27 Aug 2005 23:25:55 +0200
changeset 1106 95a044b73bd5
parent 705 574869103985
child 1237 227cfbe34109
permissions -rwxr-xr-x
Whitespace cleanup.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
     1
#!/usr/bin/env python
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
     2
#
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
     3
# This is a generalized framework for converting between SCM
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
     4
# repository formats.
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
     5
#
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
     6
# In its current form, it's hardcoded to convert incrementally between
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
     7
# git and Mercurial.
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
     8
#
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
     9
# To use, you must first import the first git version into Mercurial,
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    10
# and establish a mapping between the git commit hash and the hash in
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    11
# Mercurial for that version. This mapping is kept in a simple text
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    12
# file with lines like so:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    13
#
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    14
# <git hash> <mercurial hash>
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    15
#
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    16
# To convert the rest of the repo, run:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    17
#
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    18
# convert-repo <git-dir> <hg-dir> <mapfile>
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    19
#
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    20
# This updates the mapfile on each commit copied, so it can be
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    21
# interrupted and can be run repeatedly to copy new commits.
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    22
694
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    23
import sys, os, zlib, sha, time
450
9d785fd7deec Get set_exec from util in convert_repo
mpm@selenic.com
parents: 431
diff changeset
    24
from mercurial import hg, ui, util
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    25
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    26
class convert_git:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    27
    def __init__(self, path):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    28
        self.path = path
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    29
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    30
    def getheads(self):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    31
        h = file(self.path + "/.git/HEAD").read()[:-1]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    32
        return [h]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    33
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    34
    def catfile(self, rev, type):
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    35
        if rev == "0" * 40: raise IOError()
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    36
        path = os.getcwd()
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    37
        os.chdir(self.path)
694
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    38
        fh = os.popen("git-cat-file %s %s 2>/dev/null" % (type, rev))
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    39
        os.chdir(path)
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    40
        return fh.read()
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    41
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    42
    def getfile(self, name, rev):
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    43
        return self.catfile(rev, "blob")
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    44
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    45
    def getchanges(self, version):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    46
        path = os.getcwd()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    47
        os.chdir(self.path)
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    48
        fh = os.popen("git-diff-tree --root -m -r %s" % (version))
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    49
        os.chdir(path)
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    50
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    51
        changes = []
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    52
        for l in fh:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    53
            if "\t" not in l: continue
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    54
            m, f = l[:-1].split("\t")
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    55
            m = m.split()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    56
            h = m[3]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    57
            p = (m[1] == "100755")
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    58
            changes.append((f, h, p))
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    59
        return changes
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    60
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    61
    def getcommit(self, version):
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    62
        c = self.catfile(version, "commit") # read the commit hash
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    63
        end = c.find("\n\n")
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    64
        message = c[end+2:]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    65
        l = c[:end].splitlines()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    66
        manifest = l[0].split()[1]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    67
        parents = []
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    68
        for e in l[1:]:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    69
            n,v = e.split(" ", 1)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    70
            if n == "author":
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    71
                p = v.split()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    72
                date = " ".join(p[-2:])
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    73
                author = " ".join(p[:-2])
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    74
                if author[0] == "<": author = author[1:-1]
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    75
            if n == "committer":
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
    76
                p = v.split()
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
    77
                date = " ".join(p[-2:])
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
    78
                committer = " ".join(p[:-2])
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
    79
                if committer[0] == "<": committer = committer[1:-1]
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
    80
                message += "\ncommitter: %s %s\n" % (committer, date)
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    81
            if n == "parent": parents.append(v)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    82
        return (parents, author, date, message)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    83
694
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    84
    def gettags(self):
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    85
        tags = {}
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    86
        for f in os.listdir(self.path + "/.git/refs/tags"):
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    87
            try:
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    88
                h = file(self.path + "/.git/refs/tags/" + f).read().strip()
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    89
                p, a, d, m = self.getcommit(h)
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    90
                if not p: p = [h] # git is ugly, don't blame me
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    91
                tags[f] = p[0]
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    92
            except:
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    93
                pass
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    94
        return tags
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    95
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    96
class convert_mercurial:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    97
    def __init__(self, path):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    98
        self.path = path
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    99
        u = ui.ui()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   100
        self.repo = hg.repository(u, path)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   101
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   102
    def getheads(self):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   103
        h = self.repo.changelog.heads()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   104
        h = [ hg.hex(x) for x in h ]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   105
        return h
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   106
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   107
    def putfile(self, f, e, data):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   108
        self.repo.wfile(f, "w").write(data)
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   109
        if self.repo.dirstate.state(f) == '?':
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   110
            self.repo.dirstate.update([f], "a")
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   111
450
9d785fd7deec Get set_exec from util in convert_repo
mpm@selenic.com
parents: 431
diff changeset
   112
        util.set_exec(self.repo.wjoin(f), e)
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   113
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   114
    def delfile(self, f):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   115
        try:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   116
            os.unlink(self.repo.wjoin(f))
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   117
            #self.repo.remove([f])
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   118
        except:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   119
            pass
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   120
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   121
    def putcommit(self, files, parents, author, dest, text):
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   122
        seen = {}
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   123
        pl = []
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   124
        for p in parents:
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   125
            if p not in seen:
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   126
                pl.append(p)
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   127
                seen[p] = 1
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   128
        parents = pl
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   129
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   130
        if len(parents) < 2: parents.append("0" * 40)
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   131
        if len(parents) < 2: parents.append("0" * 40)
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   132
        p2 = parents.pop(0)
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   133
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   134
        while parents:
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   135
            p1 = p2
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   136
            p2 = parents.pop(0)
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   137
            self.repo.rawcommit(files, text, author, dest,
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   138
                                hg.bin(p1), hg.bin(p2))
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   139
            text = "(octopus merge fixup)\n"
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   140
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   141
        return hg.hex(self.repo.changelog.tip())
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   142
694
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   143
    def puttags(self, tags):
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   144
        try:
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   145
            old = self.repo.wfile(".hgtags").read()
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   146
            oldlines = old.splitlines(1)
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   147
            oldlines.sort()
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   148
        except:
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   149
            oldlines = []
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   150
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   151
        k = tags.keys()
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   152
        k.sort()
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   153
        newlines = []
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   154
        for tag in k:
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   155
            newlines.append("%s %s\n" % (tags[tag], tag))
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   156
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   157
        newlines.sort()
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   158
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   159
        if newlines != oldlines:
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   160
            print "updating tags"
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   161
            f = self.repo.wfile(".hgtags", "w")
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   162
            f.write("".join(newlines))
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   163
            f.close()
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   164
            if not oldlines: self.repo.add([".hgtags"])
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   165
            date = "%s 0" % time.mktime(time.gmtime())
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   166
            self.repo.rawcommit([".hgtags"], "update tags", "convert-repo",
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   167
                                date, self.repo.changelog.tip(), hg.nullid)
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   168
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   169
class convert:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   170
    def __init__(self, source, dest, mapfile):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   171
        self.source = source
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   172
        self.dest = dest
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   173
        self.mapfile = mapfile
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   174
        self.commitcache = {}
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   175
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   176
        self.map = {}
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   177
        for l in file(self.mapfile):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   178
            sv, dv = l[:-1].split()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   179
            self.map[sv] = dv
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   180
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   181
    def walktree(self, heads):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   182
        visit = heads
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   183
        known = {}
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   184
        parents = {}
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   185
        while visit:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   186
            n = visit.pop(0)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   187
            if n in known or n in self.map: continue
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   188
            known[n] = 1
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   189
            self.commitcache[n] = self.source.getcommit(n)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   190
            cp = self.commitcache[n][0]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   191
            for p in cp:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   192
                parents.setdefault(n, []).append(p)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   193
                visit.append(p)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   194
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   195
        return parents
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   196
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   197
    def toposort(self, parents):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   198
        visit = parents.keys()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   199
        seen = {}
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   200
        children = {}
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   201
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   202
        while visit:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   203
            n = visit.pop(0)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   204
            if n in seen: continue
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   205
            seen[n] = 1
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   206
            pc = 0
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   207
            if n in parents:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   208
                for p in parents[n]:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   209
                    if p not in self.map: pc += 1
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   210
                    visit.append(p)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   211
                    children.setdefault(p, []).append(n)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   212
            if not pc: root = n
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   213
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   214
        s = []
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   215
        removed = {}
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   216
        visit = children.keys()
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   217
        while visit:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   218
            n = visit.pop(0)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   219
            if n in removed: continue
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   220
            dep = 0
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   221
            if n in parents:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   222
                for p in parents[n]:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   223
                    if p in self.map: continue
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   224
                    if p not in removed:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   225
                        # we're still dependent
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   226
                        visit.append(n)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   227
                        dep = 1
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   228
                        break
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   229
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   230
            if not dep:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   231
                # all n's parents are in the list
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   232
                removed[n] = 1
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   233
                s.append(n)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   234
                if n in children:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   235
                    for c in children[n]:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   236
                        visit.insert(0, c)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   237
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   238
        return s
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   239
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   240
    def copy(self, rev):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   241
        p, a, d, t = self.commitcache[rev]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   242
        files = self.source.getchanges(rev)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   243
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   244
        for f,v,e in files:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   245
            try:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   246
                data = self.source.getfile(f, v)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   247
            except IOError, inst:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   248
                self.dest.delfile(f)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   249
            else:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   250
                self.dest.putfile(f, e, data)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   251
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   252
        r = [self.map[v] for v in p]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   253
        f = [f for f,v,e in files]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   254
        self.map[rev] = self.dest.putcommit(f, r, a, d, t)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   255
        file(self.mapfile, "a").write("%s %s\n" % (rev, self.map[rev]))
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   256
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   257
    def convert(self):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   258
        heads = self.source.getheads()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   259
        parents = self.walktree(heads)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   260
        t = self.toposort(parents)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   261
        num = len(t)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   262
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   263
        for c in t:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   264
            num -= 1
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   265
            if c in self.map: continue
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   266
            desc = self.commitcache[c][3].splitlines()[0]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   267
            print num, desc
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   268
            self.copy(c)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   269
694
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   270
        tags = self.source.gettags()
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   271
        ctags = {}
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   272
        for k in tags:
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   273
            v = tags[k]
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   274
            if v in self.map:
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   275
                ctags[k] = self.map[v]
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   276
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   277
        self.dest.puttags(ctags)
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   278
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   279
gitpath, hgpath, mapfile = sys.argv[1:]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   280
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   281
c = convert(convert_git(gitpath), convert_mercurial(hgpath), mapfile)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   282
c.convert()