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