##// END OF EJS Templates
convert: make sure mercurial_source has a local hg repo
Alexis S. L. Carvalho -
r5522:f5345a2d default
parent child Browse files
Show More
@@ -1,259 +1,260 b''
1 # hg backend for convert extension
1 # hg backend for convert extension
2
2
3 # Note for hg->hg conversion: Old versions of Mercurial didn't trim
3 # Note for hg->hg conversion: Old versions of Mercurial didn't trim
4 # the whitespace from the ends of commit messages, but new versions
4 # the whitespace from the ends of commit messages, but new versions
5 # do. Changesets created by those older versions, then converted, may
5 # do. Changesets created by those older versions, then converted, may
6 # thus have different hashes for changesets that are otherwise
6 # thus have different hashes for changesets that are otherwise
7 # identical.
7 # identical.
8
8
9
9
10 import os, time
10 import os, time
11 from mercurial.i18n import _
11 from mercurial.i18n import _
12 from mercurial.node import *
12 from mercurial.node import *
13 from mercurial import hg, lock, revlog, util
13 from mercurial import hg, lock, revlog, util
14
14
15 from common import NoRepo, commit, converter_source, converter_sink
15 from common import NoRepo, commit, converter_source, converter_sink
16
16
17 class mercurial_sink(converter_sink):
17 class mercurial_sink(converter_sink):
18 def __init__(self, ui, path):
18 def __init__(self, ui, path):
19 converter_sink.__init__(self, ui, path)
19 converter_sink.__init__(self, ui, path)
20 self.branchnames = ui.configbool('convert', 'hg.usebranchnames', True)
20 self.branchnames = ui.configbool('convert', 'hg.usebranchnames', True)
21 self.clonebranches = ui.configbool('convert', 'hg.clonebranches', False)
21 self.clonebranches = ui.configbool('convert', 'hg.clonebranches', False)
22 self.tagsbranch = ui.config('convert', 'hg.tagsbranch', 'default')
22 self.tagsbranch = ui.config('convert', 'hg.tagsbranch', 'default')
23 self.lastbranch = None
23 self.lastbranch = None
24 if os.path.isdir(path) and len(os.listdir(path)) > 0:
24 if os.path.isdir(path) and len(os.listdir(path)) > 0:
25 try:
25 try:
26 self.repo = hg.repository(self.ui, path)
26 self.repo = hg.repository(self.ui, path)
27 ui.status(_('destination %s is a Mercurial repository\n') %
27 ui.status(_('destination %s is a Mercurial repository\n') %
28 path)
28 path)
29 except hg.RepoError, err:
29 except hg.RepoError, err:
30 ui.print_exc()
30 ui.print_exc()
31 raise NoRepo(err.args[0])
31 raise NoRepo(err.args[0])
32 else:
32 else:
33 try:
33 try:
34 ui.status(_('initializing destination %s repository\n') % path)
34 ui.status(_('initializing destination %s repository\n') % path)
35 self.repo = hg.repository(self.ui, path, create=True)
35 self.repo = hg.repository(self.ui, path, create=True)
36 self.created.append(path)
36 self.created.append(path)
37 except hg.RepoError, err:
37 except hg.RepoError, err:
38 ui.print_exc()
38 ui.print_exc()
39 raise NoRepo("could not create hg repo %s as sink" % path)
39 raise NoRepo("could not create hg repo %s as sink" % path)
40 self.lock = None
40 self.lock = None
41 self.wlock = None
41 self.wlock = None
42 self.filemapmode = False
42 self.filemapmode = False
43
43
44 def before(self):
44 def before(self):
45 self.wlock = self.repo.wlock()
45 self.wlock = self.repo.wlock()
46 self.lock = self.repo.lock()
46 self.lock = self.repo.lock()
47 self.repo.dirstate.clear()
47 self.repo.dirstate.clear()
48
48
49 def after(self):
49 def after(self):
50 self.repo.dirstate.invalidate()
50 self.repo.dirstate.invalidate()
51 self.lock = None
51 self.lock = None
52 self.wlock = None
52 self.wlock = None
53
53
54 def revmapfile(self):
54 def revmapfile(self):
55 return os.path.join(self.path, ".hg", "shamap")
55 return os.path.join(self.path, ".hg", "shamap")
56
56
57 def authorfile(self):
57 def authorfile(self):
58 return os.path.join(self.path, ".hg", "authormap")
58 return os.path.join(self.path, ".hg", "authormap")
59
59
60 def getheads(self):
60 def getheads(self):
61 h = self.repo.changelog.heads()
61 h = self.repo.changelog.heads()
62 return [ hex(x) for x in h ]
62 return [ hex(x) for x in h ]
63
63
64 def putfile(self, f, e, data):
64 def putfile(self, f, e, data):
65 self.repo.wwrite(f, data, e)
65 self.repo.wwrite(f, data, e)
66 if f not in self.repo.dirstate:
66 if f not in self.repo.dirstate:
67 self.repo.dirstate.normallookup(f)
67 self.repo.dirstate.normallookup(f)
68
68
69 def copyfile(self, source, dest):
69 def copyfile(self, source, dest):
70 self.repo.copy(source, dest)
70 self.repo.copy(source, dest)
71
71
72 def delfile(self, f):
72 def delfile(self, f):
73 try:
73 try:
74 util.unlink(self.repo.wjoin(f))
74 util.unlink(self.repo.wjoin(f))
75 #self.repo.remove([f])
75 #self.repo.remove([f])
76 except OSError:
76 except OSError:
77 pass
77 pass
78
78
79 def setbranch(self, branch, pbranch, parents):
79 def setbranch(self, branch, pbranch, parents):
80 if (not self.clonebranches) or (branch == self.lastbranch):
80 if (not self.clonebranches) or (branch == self.lastbranch):
81 return
81 return
82
82
83 self.lastbranch = branch
83 self.lastbranch = branch
84 self.after()
84 self.after()
85 if not branch:
85 if not branch:
86 branch = 'default'
86 branch = 'default'
87 if not pbranch:
87 if not pbranch:
88 pbranch = 'default'
88 pbranch = 'default'
89
89
90 branchpath = os.path.join(self.path, branch)
90 branchpath = os.path.join(self.path, branch)
91 try:
91 try:
92 self.repo = hg.repository(self.ui, branchpath)
92 self.repo = hg.repository(self.ui, branchpath)
93 except:
93 except:
94 if not parents:
94 if not parents:
95 self.repo = hg.repository(self.ui, branchpath, create=True)
95 self.repo = hg.repository(self.ui, branchpath, create=True)
96 else:
96 else:
97 self.ui.note(_('cloning branch %s to %s\n') % (pbranch, branch))
97 self.ui.note(_('cloning branch %s to %s\n') % (pbranch, branch))
98 hg.clone(self.ui, os.path.join(self.path, pbranch),
98 hg.clone(self.ui, os.path.join(self.path, pbranch),
99 branchpath, rev=parents, update=False,
99 branchpath, rev=parents, update=False,
100 stream=True)
100 stream=True)
101 self.repo = hg.repository(self.ui, branchpath)
101 self.repo = hg.repository(self.ui, branchpath)
102 self.before()
102 self.before()
103
103
104 def putcommit(self, files, parents, commit):
104 def putcommit(self, files, parents, commit):
105 seen = {}
105 seen = {}
106 pl = []
106 pl = []
107 for p in parents:
107 for p in parents:
108 if p not in seen:
108 if p not in seen:
109 pl.append(p)
109 pl.append(p)
110 seen[p] = 1
110 seen[p] = 1
111 parents = pl
111 parents = pl
112 nparents = len(parents)
112 nparents = len(parents)
113 if self.filemapmode and nparents == 1:
113 if self.filemapmode and nparents == 1:
114 m1node = self.repo.changelog.read(bin(parents[0]))[0]
114 m1node = self.repo.changelog.read(bin(parents[0]))[0]
115 parent = parents[0]
115 parent = parents[0]
116
116
117 if len(parents) < 2: parents.append("0" * 40)
117 if len(parents) < 2: parents.append("0" * 40)
118 if len(parents) < 2: parents.append("0" * 40)
118 if len(parents) < 2: parents.append("0" * 40)
119 p2 = parents.pop(0)
119 p2 = parents.pop(0)
120
120
121 text = commit.desc
121 text = commit.desc
122 extra = commit.extra.copy()
122 extra = commit.extra.copy()
123 if self.branchnames and commit.branch:
123 if self.branchnames and commit.branch:
124 extra['branch'] = commit.branch
124 extra['branch'] = commit.branch
125 if commit.rev:
125 if commit.rev:
126 extra['convert_revision'] = commit.rev
126 extra['convert_revision'] = commit.rev
127
127
128 while parents:
128 while parents:
129 p1 = p2
129 p1 = p2
130 p2 = parents.pop(0)
130 p2 = parents.pop(0)
131 a = self.repo.rawcommit(files, text, commit.author, commit.date,
131 a = self.repo.rawcommit(files, text, commit.author, commit.date,
132 bin(p1), bin(p2), extra=extra)
132 bin(p1), bin(p2), extra=extra)
133 self.repo.dirstate.clear()
133 self.repo.dirstate.clear()
134 text = "(octopus merge fixup)\n"
134 text = "(octopus merge fixup)\n"
135 p2 = hg.hex(self.repo.changelog.tip())
135 p2 = hg.hex(self.repo.changelog.tip())
136
136
137 if self.filemapmode and nparents == 1:
137 if self.filemapmode and nparents == 1:
138 man = self.repo.manifest
138 man = self.repo.manifest
139 mnode = self.repo.changelog.read(bin(p2))[0]
139 mnode = self.repo.changelog.read(bin(p2))[0]
140 if not man.cmp(m1node, man.revision(mnode)):
140 if not man.cmp(m1node, man.revision(mnode)):
141 self.repo.rollback()
141 self.repo.rollback()
142 self.repo.dirstate.clear()
142 self.repo.dirstate.clear()
143 return parent
143 return parent
144 return p2
144 return p2
145
145
146 def puttags(self, tags):
146 def puttags(self, tags):
147 try:
147 try:
148 old = self.repo.wfile(".hgtags").read()
148 old = self.repo.wfile(".hgtags").read()
149 oldlines = old.splitlines(1)
149 oldlines = old.splitlines(1)
150 oldlines.sort()
150 oldlines.sort()
151 except:
151 except:
152 oldlines = []
152 oldlines = []
153
153
154 k = tags.keys()
154 k = tags.keys()
155 k.sort()
155 k.sort()
156 newlines = []
156 newlines = []
157 for tag in k:
157 for tag in k:
158 newlines.append("%s %s\n" % (tags[tag], tag))
158 newlines.append("%s %s\n" % (tags[tag], tag))
159
159
160 newlines.sort()
160 newlines.sort()
161
161
162 if newlines != oldlines:
162 if newlines != oldlines:
163 self.ui.status("updating tags\n")
163 self.ui.status("updating tags\n")
164 f = self.repo.wfile(".hgtags", "w")
164 f = self.repo.wfile(".hgtags", "w")
165 f.write("".join(newlines))
165 f.write("".join(newlines))
166 f.close()
166 f.close()
167 if not oldlines: self.repo.add([".hgtags"])
167 if not oldlines: self.repo.add([".hgtags"])
168 date = "%s 0" % int(time.mktime(time.gmtime()))
168 date = "%s 0" % int(time.mktime(time.gmtime()))
169 extra = {}
169 extra = {}
170 if self.tagsbranch != 'default':
170 if self.tagsbranch != 'default':
171 extra['branch'] = self.tagsbranch
171 extra['branch'] = self.tagsbranch
172 try:
172 try:
173 tagparent = self.repo.changectx(self.tagsbranch).node()
173 tagparent = self.repo.changectx(self.tagsbranch).node()
174 except hg.RepoError, inst:
174 except hg.RepoError, inst:
175 tagparent = nullid
175 tagparent = nullid
176 self.repo.rawcommit([".hgtags"], "update tags", "convert-repo",
176 self.repo.rawcommit([".hgtags"], "update tags", "convert-repo",
177 date, tagparent, nullid)
177 date, tagparent, nullid)
178 return hex(self.repo.changelog.tip())
178 return hex(self.repo.changelog.tip())
179
179
180 def setfilemapmode(self, active):
180 def setfilemapmode(self, active):
181 self.filemapmode = active
181 self.filemapmode = active
182
182
183 class mercurial_source(converter_source):
183 class mercurial_source(converter_source):
184 def __init__(self, ui, path, rev=None):
184 def __init__(self, ui, path, rev=None):
185 converter_source.__init__(self, ui, path, rev)
185 converter_source.__init__(self, ui, path, rev)
186 try:
186 try:
187 self.repo = hg.repository(self.ui, path)
187 self.repo = hg.repository(self.ui, path)
188 # try to provoke an exception if this isn't really a hg
188 # try to provoke an exception if this isn't really a hg
189 # repo, but some other bogus compatible-looking url
189 # repo, but some other bogus compatible-looking url
190 self.repo.heads()
190 if not self.repo.local():
191 raise hg.RepoError()
191 except hg.RepoError:
192 except hg.RepoError:
192 ui.print_exc()
193 ui.print_exc()
193 raise NoRepo("%s does not look like a Mercurial repo" % path)
194 raise NoRepo("%s is not a local Mercurial repo" % path)
194 self.lastrev = None
195 self.lastrev = None
195 self.lastctx = None
196 self.lastctx = None
196 self._changescache = None
197 self._changescache = None
197
198
198 def changectx(self, rev):
199 def changectx(self, rev):
199 if self.lastrev != rev:
200 if self.lastrev != rev:
200 self.lastctx = self.repo.changectx(rev)
201 self.lastctx = self.repo.changectx(rev)
201 self.lastrev = rev
202 self.lastrev = rev
202 return self.lastctx
203 return self.lastctx
203
204
204 def getheads(self):
205 def getheads(self):
205 if self.rev:
206 if self.rev:
206 return [hex(self.repo.changectx(self.rev).node())]
207 return [hex(self.repo.changectx(self.rev).node())]
207 else:
208 else:
208 return [hex(node) for node in self.repo.heads()]
209 return [hex(node) for node in self.repo.heads()]
209
210
210 def getfile(self, name, rev):
211 def getfile(self, name, rev):
211 try:
212 try:
212 return self.changectx(rev).filectx(name).data()
213 return self.changectx(rev).filectx(name).data()
213 except revlog.LookupError, err:
214 except revlog.LookupError, err:
214 raise IOError(err)
215 raise IOError(err)
215
216
216 def getmode(self, name, rev):
217 def getmode(self, name, rev):
217 m = self.changectx(rev).manifest()
218 m = self.changectx(rev).manifest()
218 return (m.execf(name) and 'x' or '') + (m.linkf(name) and 'l' or '')
219 return (m.execf(name) and 'x' or '') + (m.linkf(name) and 'l' or '')
219
220
220 def getchanges(self, rev):
221 def getchanges(self, rev):
221 ctx = self.changectx(rev)
222 ctx = self.changectx(rev)
222 if self._changescache and self._changescache[0] == rev:
223 if self._changescache and self._changescache[0] == rev:
223 m, a, r = self._changescache[1]
224 m, a, r = self._changescache[1]
224 else:
225 else:
225 m, a, r = self.repo.status(ctx.parents()[0].node(), ctx.node())[:3]
226 m, a, r = self.repo.status(ctx.parents()[0].node(), ctx.node())[:3]
226 changes = [(name, rev) for name in m + a + r]
227 changes = [(name, rev) for name in m + a + r]
227 changes.sort()
228 changes.sort()
228 return (changes, self.getcopies(ctx, m + a))
229 return (changes, self.getcopies(ctx, m + a))
229
230
230 def getcopies(self, ctx, files):
231 def getcopies(self, ctx, files):
231 copies = {}
232 copies = {}
232 for name in files:
233 for name in files:
233 try:
234 try:
234 copies[name] = ctx.filectx(name).renamed()[0]
235 copies[name] = ctx.filectx(name).renamed()[0]
235 except TypeError:
236 except TypeError:
236 pass
237 pass
237 return copies
238 return copies
238
239
239 def getcommit(self, rev):
240 def getcommit(self, rev):
240 ctx = self.changectx(rev)
241 ctx = self.changectx(rev)
241 parents = [hex(p.node()) for p in ctx.parents() if p.node() != nullid]
242 parents = [hex(p.node()) for p in ctx.parents() if p.node() != nullid]
242 return commit(author=ctx.user(), date=util.datestr(ctx.date()),
243 return commit(author=ctx.user(), date=util.datestr(ctx.date()),
243 desc=ctx.description(), parents=parents,
244 desc=ctx.description(), parents=parents,
244 branch=ctx.branch(), extra=ctx.extra())
245 branch=ctx.branch(), extra=ctx.extra())
245
246
246 def gettags(self):
247 def gettags(self):
247 tags = [t for t in self.repo.tagslist() if t[0] != 'tip']
248 tags = [t for t in self.repo.tagslist() if t[0] != 'tip']
248 return dict([(name, hex(node)) for name, node in tags])
249 return dict([(name, hex(node)) for name, node in tags])
249
250
250 def getchangedfiles(self, rev, i):
251 def getchangedfiles(self, rev, i):
251 ctx = self.changectx(rev)
252 ctx = self.changectx(rev)
252 i = i or 0
253 i = i or 0
253 changes = self.repo.status(ctx.parents()[i].node(), ctx.node())[:3]
254 changes = self.repo.status(ctx.parents()[i].node(), ctx.node())[:3]
254
255
255 if i == 0:
256 if i == 0:
256 self._changescache = (rev, changes)
257 self._changescache = (rev, changes)
257
258
258 return changes[0] + changes[1] + changes[2]
259 return changes[0] + changes[1] + changes[2]
259
260
General Comments 0
You need to be logged in to leave comments. Login now