author | Matt Mackall <mpm@selenic.com> |
Wed, 20 Dec 2006 16:31:46 -0600 | |
changeset 3947 | 0fab73b3f453 |
parent 3916 | 645e1dd4b8ae |
child 3948 | b58c1681d23b |
permissions | -rwxr-xr-x |
316 | 1 |
#!/usr/bin/env python |
2 |
# |
|
3 |
# This is a generalized framework for converting between SCM |
|
4 |
# repository formats. |
|
5 |
# |
|
6 |
# In its current form, it's hardcoded to convert incrementally between |
|
7 |
# git and Mercurial. |
|
8 |
# |
|
3916
645e1dd4b8ae
convert-repo: update usage information
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3911
diff
changeset
|
9 |
# To use, run: |
645e1dd4b8ae
convert-repo: update usage information
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3911
diff
changeset
|
10 |
# |
645e1dd4b8ae
convert-repo: update usage information
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3911
diff
changeset
|
11 |
# convert-repo <git-dir> <hg-dir> <mapfile> |
645e1dd4b8ae
convert-repo: update usage information
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3911
diff
changeset
|
12 |
# |
645e1dd4b8ae
convert-repo: update usage information
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3911
diff
changeset
|
13 |
# (don't forget to create the <hg-dir> repository beforehand) |
645e1dd4b8ae
convert-repo: update usage information
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3911
diff
changeset
|
14 |
# |
645e1dd4b8ae
convert-repo: update usage information
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3911
diff
changeset
|
15 |
# The <mapfile> is a simple text file that maps a git commit hash to |
645e1dd4b8ae
convert-repo: update usage information
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3911
diff
changeset
|
16 |
# the hash in Mercurial for that version, like so: |
316 | 17 |
# |
18 |
# <git hash> <mercurial hash> |
|
19 |
# |
|
3916
645e1dd4b8ae
convert-repo: update usage information
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3911
diff
changeset
|
20 |
# If the file doesn't exist, it's automatically created. It's updated |
645e1dd4b8ae
convert-repo: update usage information
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3911
diff
changeset
|
21 |
# on each commit copied, so convert-repo can be interrupted and can |
645e1dd4b8ae
convert-repo: update usage information
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
3911
diff
changeset
|
22 |
# be run repeatedly to copy new commits. |
316 | 23 |
|
694 | 24 |
import sys, os, zlib, sha, time |
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
25 |
os.environ["HGENCODING"] = "utf-8" |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
26 |
from mercurial import hg, ui, util, fancyopts |
3825
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2649
diff
changeset
|
27 |
|
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
28 |
class Abort(Exception): pass |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
29 |
|
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
30 |
quiet = 0 |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
31 |
def status(msg): |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
32 |
if not quiet: sys.stdout.write(str(msg)) |
3825
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2649
diff
changeset
|
33 |
|
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
34 |
def warn(msg): |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
35 |
sys.stderr.write(str(msg)) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
36 |
|
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
37 |
def abort(msg): |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
38 |
raise Abort(msg) |
316 | 39 |
|
3825
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2649
diff
changeset
|
40 |
def recode(s): |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2649
diff
changeset
|
41 |
try: |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2649
diff
changeset
|
42 |
return s.decode("utf-8").encode("utf-8") |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2649
diff
changeset
|
43 |
except: |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2649
diff
changeset
|
44 |
try: |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2649
diff
changeset
|
45 |
return s.decode("latin-1").encode("utf-8") |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2649
diff
changeset
|
46 |
except: |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2649
diff
changeset
|
47 |
return s.decode("utf-8", "replace").encode("utf-8") |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2649
diff
changeset
|
48 |
|
316 | 49 |
class convert_git: |
50 |
def __init__(self, path): |
|
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
51 |
if os.path.isdir(path + "/.git"): |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
52 |
path += "/.git" |
316 | 53 |
self.path = path |
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
54 |
if not os.path.exists(path + "/HEAD"): |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
55 |
raise TypeError("couldn't open GIT repo %s" % path) |
316 | 56 |
|
57 |
def getheads(self): |
|
2649
e6a7a6a33a62
make convert-repo deal with git symbolic refs.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2093
diff
changeset
|
58 |
fh = os.popen("GIT_DIR=%s git-rev-parse --verify HEAD" % self.path) |
e6a7a6a33a62
make convert-repo deal with git symbolic refs.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2093
diff
changeset
|
59 |
return [fh.read()[:-1]] |
316 | 60 |
|
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
61 |
def catfile(self, rev, type): |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
62 |
if rev == "0" * 40: raise IOError() |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
63 |
fh = os.popen("GIT_DIR=%s git-cat-file %s %s 2>/dev/null" % (self.path, type, rev)) |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
64 |
return fh.read() |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
65 |
|
316 | 66 |
def getfile(self, name, rev): |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
67 |
return self.catfile(rev, "blob") |
316 | 68 |
|
69 |
def getchanges(self, version): |
|
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
70 |
fh = os.popen("GIT_DIR=%s git-diff-tree --root -m -r %s" % (self.path, version)) |
316 | 71 |
changes = [] |
72 |
for l in fh: |
|
73 |
if "\t" not in l: continue |
|
74 |
m, f = l[:-1].split("\t") |
|
75 |
m = m.split() |
|
76 |
h = m[3] |
|
77 |
p = (m[1] == "100755") |
|
78 |
changes.append((f, h, p)) |
|
79 |
return changes |
|
80 |
||
81 |
def getcommit(self, version): |
|
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
82 |
c = self.catfile(version, "commit") # read the commit hash |
316 | 83 |
end = c.find("\n\n") |
84 |
message = c[end+2:] |
|
3825
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2649
diff
changeset
|
85 |
message = recode(message) |
316 | 86 |
l = c[:end].splitlines() |
87 |
manifest = l[0].split()[1] |
|
88 |
parents = [] |
|
89 |
for e in l[1:]: |
|
90 |
n,v = e.split(" ", 1) |
|
91 |
if n == "author": |
|
92 |
p = v.split() |
|
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
93 |
tm, tz = p[-2:] |
316 | 94 |
author = " ".join(p[:-2]) |
95 |
if author[0] == "<": author = author[1:-1] |
|
3825
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2649
diff
changeset
|
96 |
author = recode(author) |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
97 |
if n == "committer": |
431 | 98 |
p = v.split() |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
99 |
tm, tz = p[-2:] |
431 | 100 |
committer = " ".join(p[:-2]) |
101 |
if committer[0] == "<": committer = committer[1:-1] |
|
3825
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2649
diff
changeset
|
102 |
committer = recode(committer) |
3910
4bc5a2405b12
convert-repo: fix recoding of committer
Matt Mackall <mpm@selenic.com>
parents:
3825
diff
changeset
|
103 |
message += "\ncommitter: %s\n" % committer |
316 | 104 |
if n == "parent": parents.append(v) |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
105 |
|
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
106 |
tzs, tzh, tzm = tz[-5:-4] + "1", tz[-4:-2], tz[-2:] |
2093
5cc414722587
convert-repo: fix reversed time zone offset
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1715
diff
changeset
|
107 |
tz = -int(tzs) * (int(tzh) * 3600 + int(tzm)) |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
108 |
date = tm + " " + str(tz) |
316 | 109 |
return (parents, author, date, message) |
110 |
||
694 | 111 |
def gettags(self): |
112 |
tags = {} |
|
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
113 |
for f in os.listdir(self.path + "/refs/tags"): |
694 | 114 |
try: |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
115 |
h = file(self.path + "/refs/tags/" + f).read().strip() |
1386
a1040345fdda
convert-repo: retrieve the commit hash from the tag object for tag import
Matt Mackall <mpm@selenic.com>
parents:
1385
diff
changeset
|
116 |
c = self.catfile(h, "tag") # read the commit hash |
a1040345fdda
convert-repo: retrieve the commit hash from the tag object for tag import
Matt Mackall <mpm@selenic.com>
parents:
1385
diff
changeset
|
117 |
h = c.splitlines()[0].split()[1] |
1237 | 118 |
tags[f] = h |
694 | 119 |
except: |
120 |
pass |
|
121 |
return tags |
|
122 |
||
316 | 123 |
class convert_mercurial: |
124 |
def __init__(self, path): |
|
125 |
self.path = path |
|
126 |
u = ui.ui() |
|
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
127 |
try: |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
128 |
self.repo = hg.repository(u, path) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
129 |
except: |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
130 |
raise TypeError("could open hg repo %s" % path) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
131 |
|
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
132 |
def mapfile(self): |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
133 |
return os.path.join(self.path, ".hg", "shamap") |
316 | 134 |
|
135 |
def getheads(self): |
|
136 |
h = self.repo.changelog.heads() |
|
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
137 |
return [ hg.hex(x) for x in h ] |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
138 |
|
316 | 139 |
def putfile(self, f, e, data): |
140 |
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
|
141 |
if self.repo.dirstate.state(f) == '?': |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
142 |
self.repo.dirstate.update([f], "a") |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
143 |
|
450 | 144 |
util.set_exec(self.repo.wjoin(f), e) |
316 | 145 |
|
146 |
def delfile(self, f): |
|
147 |
try: |
|
148 |
os.unlink(self.repo.wjoin(f)) |
|
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
149 |
#self.repo.remove([f]) |
316 | 150 |
except: |
151 |
pass |
|
152 |
||
1715
40346aa66b0f
Revert convert-repo changes
Matt Mackall <mpm@selenic.com>
parents:
1656
diff
changeset
|
153 |
def putcommit(self, files, parents, author, dest, text): |
431 | 154 |
seen = {} |
155 |
pl = [] |
|
156 |
for p in parents: |
|
157 |
if p not in seen: |
|
158 |
pl.append(p) |
|
159 |
seen[p] = 1 |
|
160 |
parents = pl |
|
316 | 161 |
|
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
162 |
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
|
163 |
if len(parents) < 2: parents.append("0" * 40) |
431 | 164 |
p2 = parents.pop(0) |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
165 |
|
431 | 166 |
while parents: |
167 |
p1 = p2 |
|
168 |
p2 = parents.pop(0) |
|
1715
40346aa66b0f
Revert convert-repo changes
Matt Mackall <mpm@selenic.com>
parents:
1656
diff
changeset
|
169 |
self.repo.rawcommit(files, text, author, dest, |
40346aa66b0f
Revert convert-repo changes
Matt Mackall <mpm@selenic.com>
parents:
1656
diff
changeset
|
170 |
hg.bin(p1), hg.bin(p2)) |
431 | 171 |
text = "(octopus merge fixup)\n" |
1389
9b3ef6f3cef5
convert-repo: fix up octopus merge conversion
Matt Mackall <mpm@selenic.com>
parents:
1388
diff
changeset
|
172 |
p2 = hg.hex(self.repo.changelog.tip()) |
431 | 173 |
|
1389
9b3ef6f3cef5
convert-repo: fix up octopus merge conversion
Matt Mackall <mpm@selenic.com>
parents:
1388
diff
changeset
|
174 |
return p2 |
316 | 175 |
|
694 | 176 |
def puttags(self, tags): |
177 |
try: |
|
178 |
old = self.repo.wfile(".hgtags").read() |
|
179 |
oldlines = old.splitlines(1) |
|
180 |
oldlines.sort() |
|
181 |
except: |
|
182 |
oldlines = [] |
|
183 |
||
184 |
k = tags.keys() |
|
185 |
k.sort() |
|
186 |
newlines = [] |
|
187 |
for tag in k: |
|
188 |
newlines.append("%s %s\n" % (tags[tag], tag)) |
|
189 |
||
190 |
newlines.sort() |
|
191 |
||
192 |
if newlines != oldlines: |
|
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
193 |
status("updating tags\n") |
694 | 194 |
f = self.repo.wfile(".hgtags", "w") |
195 |
f.write("".join(newlines)) |
|
196 |
f.close() |
|
197 |
if not oldlines: self.repo.add([".hgtags"]) |
|
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
198 |
date = "%s 0" % int(time.mktime(time.gmtime())) |
694 | 199 |
self.repo.rawcommit([".hgtags"], "update tags", "convert-repo", |
200 |
date, self.repo.changelog.tip(), hg.nullid) |
|
1387
0c7e8d345564
convert-repo: linearize the tag commit
Matt Mackall <mpm@selenic.com>
parents:
1386
diff
changeset
|
201 |
return hg.hex(self.repo.changelog.tip()) |
694 | 202 |
|
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
203 |
converters = [convert_git, convert_mercurial] |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
204 |
|
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
205 |
def converter(path): |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
206 |
if not os.path.isdir(path): |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
207 |
abort("%s: not a directory\n" % path) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
208 |
for c in converters: |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
209 |
try: |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
210 |
return c(path) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
211 |
except TypeError: |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
212 |
pass |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
213 |
abort("%s: unknown repository type\n" % path) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
214 |
|
316 | 215 |
class convert: |
216 |
def __init__(self, source, dest, mapfile): |
|
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
217 |
|
316 | 218 |
self.source = source |
219 |
self.dest = dest |
|
220 |
self.mapfile = mapfile |
|
221 |
self.commitcache = {} |
|
222 |
||
223 |
self.map = {} |
|
1655
7bfd4724932a
convert-repo: automatically create empty map file
Matt Mackall <mpm@selenic.com>
parents:
1389
diff
changeset
|
224 |
try: |
7bfd4724932a
convert-repo: automatically create empty map file
Matt Mackall <mpm@selenic.com>
parents:
1389
diff
changeset
|
225 |
for l in file(self.mapfile): |
7bfd4724932a
convert-repo: automatically create empty map file
Matt Mackall <mpm@selenic.com>
parents:
1389
diff
changeset
|
226 |
sv, dv = l[:-1].split() |
7bfd4724932a
convert-repo: automatically create empty map file
Matt Mackall <mpm@selenic.com>
parents:
1389
diff
changeset
|
227 |
self.map[sv] = dv |
7bfd4724932a
convert-repo: automatically create empty map file
Matt Mackall <mpm@selenic.com>
parents:
1389
diff
changeset
|
228 |
except IOError: |
7bfd4724932a
convert-repo: automatically create empty map file
Matt Mackall <mpm@selenic.com>
parents:
1389
diff
changeset
|
229 |
pass |
316 | 230 |
|
231 |
def walktree(self, heads): |
|
232 |
visit = heads |
|
233 |
known = {} |
|
234 |
parents = {} |
|
235 |
while visit: |
|
236 |
n = visit.pop(0) |
|
237 |
if n in known or n in self.map: continue |
|
238 |
known[n] = 1 |
|
239 |
self.commitcache[n] = self.source.getcommit(n) |
|
240 |
cp = self.commitcache[n][0] |
|
241 |
for p in cp: |
|
242 |
parents.setdefault(n, []).append(p) |
|
243 |
visit.append(p) |
|
244 |
||
245 |
return parents |
|
246 |
||
247 |
def toposort(self, parents): |
|
248 |
visit = parents.keys() |
|
249 |
seen = {} |
|
250 |
children = {} |
|
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
251 |
|
316 | 252 |
while visit: |
253 |
n = visit.pop(0) |
|
254 |
if n in seen: continue |
|
255 |
seen[n] = 1 |
|
256 |
pc = 0 |
|
257 |
if n in parents: |
|
258 |
for p in parents[n]: |
|
259 |
if p not in self.map: pc += 1 |
|
260 |
visit.append(p) |
|
261 |
children.setdefault(p, []).append(n) |
|
262 |
if not pc: root = n |
|
263 |
||
264 |
s = [] |
|
265 |
removed = {} |
|
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
266 |
visit = children.keys() |
316 | 267 |
while visit: |
268 |
n = visit.pop(0) |
|
269 |
if n in removed: continue |
|
270 |
dep = 0 |
|
271 |
if n in parents: |
|
272 |
for p in parents[n]: |
|
273 |
if p in self.map: continue |
|
274 |
if p not in removed: |
|
275 |
# we're still dependent |
|
276 |
visit.append(n) |
|
277 |
dep = 1 |
|
278 |
break |
|
279 |
||
280 |
if not dep: |
|
281 |
# all n's parents are in the list |
|
282 |
removed[n] = 1 |
|
283 |
s.append(n) |
|
284 |
if n in children: |
|
285 |
for c in children[n]: |
|
286 |
visit.insert(0, c) |
|
287 |
||
288 |
return s |
|
289 |
||
290 |
def copy(self, rev): |
|
291 |
p, a, d, t = self.commitcache[rev] |
|
292 |
files = self.source.getchanges(rev) |
|
293 |
||
294 |
for f,v,e in files: |
|
295 |
try: |
|
296 |
data = self.source.getfile(f, v) |
|
297 |
except IOError, inst: |
|
298 |
self.dest.delfile(f) |
|
299 |
else: |
|
300 |
self.dest.putfile(f, e, data) |
|
301 |
||
302 |
r = [self.map[v] for v in p] |
|
303 |
f = [f for f,v,e in files] |
|
304 |
self.map[rev] = self.dest.putcommit(f, r, a, d, t) |
|
305 |
file(self.mapfile, "a").write("%s %s\n" % (rev, self.map[rev])) |
|
306 |
||
307 |
def convert(self): |
|
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
308 |
status("scanning source...\n") |
316 | 309 |
heads = self.source.getheads() |
310 |
parents = self.walktree(heads) |
|
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
311 |
status("sorting...\n") |
316 | 312 |
t = self.toposort(parents) |
1388
5eb2d3c54165
convert-repo: change duplicate elimination
Matt Mackall <mpm@selenic.com>
parents:
1387
diff
changeset
|
313 |
t = [n for n in t if n not in self.map] |
316 | 314 |
num = len(t) |
1715
40346aa66b0f
Revert convert-repo changes
Matt Mackall <mpm@selenic.com>
parents:
1656
diff
changeset
|
315 |
c = None |
316 | 316 |
|
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
317 |
status("converting...\n") |
316 | 318 |
for c in t: |
319 |
num -= 1 |
|
320 |
desc = self.commitcache[c][3].splitlines()[0] |
|
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
321 |
status("%d %s\n" % (num, desc)) |
316 | 322 |
self.copy(c) |
323 |
||
694 | 324 |
tags = self.source.gettags() |
325 |
ctags = {} |
|
326 |
for k in tags: |
|
327 |
v = tags[k] |
|
328 |
if v in self.map: |
|
329 |
ctags[k] = self.map[v] |
|
330 |
||
1715
40346aa66b0f
Revert convert-repo changes
Matt Mackall <mpm@selenic.com>
parents:
1656
diff
changeset
|
331 |
if c and ctags: |
1387
0c7e8d345564
convert-repo: linearize the tag commit
Matt Mackall <mpm@selenic.com>
parents:
1386
diff
changeset
|
332 |
nrev = self.dest.puttags(ctags) |
0c7e8d345564
convert-repo: linearize the tag commit
Matt Mackall <mpm@selenic.com>
parents:
1386
diff
changeset
|
333 |
# write another hash correspondence to override the previous |
0c7e8d345564
convert-repo: linearize the tag commit
Matt Mackall <mpm@selenic.com>
parents:
1386
diff
changeset
|
334 |
# one so we don't end up with extra tag heads |
3911
fe075ddf3272
convert-repo: avoid adding bogus value to shamap on tag update
Matt Mackall <mpm@selenic.com>
parents:
3910
diff
changeset
|
335 |
if nrev: |
fe075ddf3272
convert-repo: avoid adding bogus value to shamap on tag update
Matt Mackall <mpm@selenic.com>
parents:
3910
diff
changeset
|
336 |
file(self.mapfile, "a").write("%s %s\n" % (c, nrev)) |
694 | 337 |
|
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
338 |
def command(src, dest=None, mapfile=None, **opts): |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
339 |
srcc = converter(src) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
340 |
if not hasattr(srcc, "getcommit"): |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
341 |
abort("%s: can't read from this repo type\n" % src) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
342 |
|
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
343 |
if not dest: |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
344 |
dest = src + "-hg" |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
345 |
status("assuming destination %s\n" % dest) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
346 |
if not os.path.isdir(dest): |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
347 |
status("creating repository %s\n" % dest) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
348 |
os.system("hg init " + dest) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
349 |
destc = converter(dest) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
350 |
if not hasattr(destc, "putcommit"): |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
351 |
abort("%s: can't write to this repo type\n" % src) |
316 | 352 |
|
3947
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
353 |
if not mapfile: |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
354 |
try: |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
355 |
mapfile = destc.mapfile() |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
356 |
except: |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
357 |
mapfile = os.path.join(destc, "map") |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
358 |
|
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
359 |
c = convert(srcc, destc, mapfile) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
360 |
c.convert() |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
361 |
|
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
362 |
options = [('q', 'quiet', None, 'suppress output')] |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
363 |
opts = {} |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
364 |
args = fancyopts.fancyopts(sys.argv[1:], options, opts) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
365 |
|
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
366 |
if opts['quiet']: |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
367 |
quiet = 1 |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
368 |
|
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
369 |
try: |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
370 |
command(*args, **opts) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
371 |
except Abort, inst: |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
372 |
warn(inst) |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
373 |
except KeyboardInterrupt: |
0fab73b3f453
convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents:
3916
diff
changeset
|
374 |
status("interrupted\n") |