contrib/convert-repo
author mpm@selenic.com
Fri, 24 Jun 2005 22:58:14 -0800
changeset 461 9ae0034f2772
parent 450 9d785fd7deec
child 692 695dd9a491da
permissions -rwxr-xr-x
[PATCH] /dev/null for other OS -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 [PATCH] /dev/null for other OS From: K Thananchayan <thananck@yahoo.com> Null device name is platform dependent. Introduce util.nulldev - - platform dependent constant and use it instead of '/dev/null' manifest hash: 95a1bfdb712c53ef97aaff73f7f4c87e9299cf59 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQFCvQCGywK+sNU5EO8RAiRBAKCcXasCCFT2+6wgklkgqIt4l3UwugCeKWii h/8iRYEw8Rp8B67abiFiwyY= =L3Mk -----END PGP SIGNATURE-----
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
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    23
import sys, os, zlib, sha
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
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    34
    def getfile(self, name, rev):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    35
        a = file(self.path + ("/.git/objects/%s/%s"
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    36
                              % (rev[:2], rev[2:]))).read()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    37
        b = zlib.decompress(a)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    38
        if sha.sha(b).hexdigest() != rev: raise "bad hash"
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    39
        head, text = b.split('\0', 1)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    40
        return text
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    41
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    42
    def getchanges(self, version):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    43
        path = os.getcwd()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    44
        os.chdir(self.path)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    45
        fh = os.popen("git-diff-tree -m -r %s" % (version))
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    46
        os.chdir(path)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    47
        
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    48
        changes = []
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    49
        for l in fh:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    50
            if "\t" not in l: continue
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    51
            m, f = l[:-1].split("\t")
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    52
            m = m.split()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    53
            h = m[3]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    54
            p = (m[1] == "100755")
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    55
            changes.append((f, h, p))
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    56
        return changes
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    57
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    58
    def getcommit(self, version):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    59
        c = self.getfile("", version) # read the commit hash
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    60
        end = c.find("\n\n")
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    61
        message = c[end+2:]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    62
        l = c[:end].splitlines()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    63
        manifest = l[0].split()[1]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    64
        parents = []
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    65
        for e in l[1:]:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    66
            n,v = e.split(" ", 1)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    67
            if n == "author":
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    68
                p = v.split()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    69
                date = " ".join(p[-2:])
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    70
                author = " ".join(p[:-2])
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    71
                if author[0] == "<": author = author[1:-1]
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
    72
            if n == "committer": 
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
    73
                p = v.split()
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
    74
                date = " ".join(p[-2:])
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
    75
                committer = " ".join(p[:-2])
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
    76
                if committer[0] == "<": committer = committer[1:-1]
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
    77
                message += "\ncommitter: %s %s\n" % (committer, date)
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    78
            if n == "parent": parents.append(v)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    79
        return (parents, author, date, message)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    80
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    81
class convert_mercurial:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    82
    def __init__(self, path):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    83
        self.path = path
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    84
        u = ui.ui()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    85
        self.repo = hg.repository(u, path)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    86
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    87
    def getheads(self):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    88
        h = self.repo.changelog.heads()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    89
        h = [ hg.hex(x) for x in h ]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    90
        return h
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    91
        
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    92
    def putfile(self, f, e, data):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    93
        self.repo.wfile(f, "w").write(data)
450
9d785fd7deec Get set_exec from util in convert_repo
mpm@selenic.com
parents: 431
diff changeset
    94
        util.set_exec(self.repo.wjoin(f), e)
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    95
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    96
    def delfile(self, f):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    97
        try:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    98
            os.unlink(self.repo.wjoin(f))
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    99
            self.repo.remove([f])
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   100
        except:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   101
            pass
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   102
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   103
    def putcommit(self, files, parents, author, dest, text):
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   104
        if not parents: parents = ["0" * 40]
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   105
        if len(parents) < 2: parents.append("0" * 40)
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   106
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   107
        seen = {}
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   108
        pl = []
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   109
        for p in parents:
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   110
            if p not in seen:
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   111
                pl.append(p)
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   112
                seen[p] = 1
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   113
        parents = pl
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   114
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   115
        p2 = parents.pop(0)
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   116
        c = self.repo.changelog.count()
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   117
        while parents:
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   118
            p1 = p2
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   119
            p2 = parents.pop(0)
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   120
            self.repo.rawcommit(files, text, author, dest, 
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   121
                                hg.bin(p1), hg.bin(p2))
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   122
            text = "(octopus merge fixup)\n"
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   123
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   124
        return hg.hex(self.repo.changelog.node(c))
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   125
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   126
class convert:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   127
    def __init__(self, source, dest, mapfile):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   128
        self.source = source
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   129
        self.dest = dest
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   130
        self.mapfile = mapfile
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   131
        self.commitcache = {}
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   132
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   133
        self.map = {}
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   134
        for l in file(self.mapfile):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   135
            sv, dv = l[:-1].split()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   136
            self.map[sv] = dv
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   137
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   138
    def walktree(self, heads):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   139
        visit = heads
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   140
        known = {}
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   141
        parents = {}
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   142
        while visit:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   143
            n = visit.pop(0)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   144
            if n in known or n in self.map: continue
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   145
            known[n] = 1
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   146
            self.commitcache[n] = self.source.getcommit(n)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   147
            cp = self.commitcache[n][0]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   148
            for p in cp:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   149
                parents.setdefault(n, []).append(p)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   150
                visit.append(p)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   151
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   152
        return parents
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   153
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   154
    def toposort(self, parents):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   155
        visit = parents.keys()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   156
        seen = {}
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   157
        children = {}
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   158
        while visit:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   159
            n = visit.pop(0)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   160
            if n in seen: continue
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   161
            seen[n] = 1
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   162
            pc = 0
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   163
            if n in parents:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   164
                for p in parents[n]:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   165
                    if p not in self.map: pc += 1
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   166
                    visit.append(p)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   167
                    children.setdefault(p, []).append(n)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   168
            if not pc: root = n
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   169
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   170
        s = []
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   171
        removed = {}
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   172
        visit = parents.keys()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   173
        while visit:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   174
            n = visit.pop(0)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   175
            if n in removed: continue
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   176
            dep = 0
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   177
            if n in parents:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   178
                for p in parents[n]:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   179
                    if p in self.map: continue
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   180
                    if p not in removed:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   181
                        # we're still dependent
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   182
                        visit.append(n)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   183
                        dep = 1
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   184
                        break
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   185
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   186
            if not dep:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   187
                # all n's parents are in the list
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   188
                removed[n] = 1
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   189
                s.append(n)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   190
                if n in children:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   191
                    for c in children[n]:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   192
                        visit.insert(0, c)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   193
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   194
        return s
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   195
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   196
    def copy(self, rev):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   197
        p, a, d, t = self.commitcache[rev]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   198
        files = self.source.getchanges(rev)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   199
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   200
        for f,v,e in files:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   201
            try:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   202
                data = self.source.getfile(f, v)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   203
            except IOError, inst:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   204
                self.dest.delfile(f)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   205
            else:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   206
                self.dest.putfile(f, e, data)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   207
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   208
        r = [self.map[v] for v in p]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   209
        f = [f for f,v,e in files]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   210
        self.map[rev] = self.dest.putcommit(f, r, a, d, t)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   211
        file(self.mapfile, "a").write("%s %s\n" % (rev, self.map[rev]))
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   212
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   213
    def convert(self):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   214
        heads = self.source.getheads()
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   215
        parents = self.walktree(heads)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   216
        t = self.toposort(parents)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   217
        num = len(t)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   218
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   219
        for c in t:
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   220
            num -= 1
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   221
            if c in self.map: continue
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   222
            desc = self.commitcache[c][3].splitlines()[0]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   223
            print num, desc
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   224
            self.copy(c)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   225
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   226
gitpath, hgpath, mapfile = sys.argv[1:]
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   227
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   228
c = convert(convert_git(gitpath), convert_mercurial(hgpath), mapfile)
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   229
c.convert()