##// END OF EJS Templates
convert: fail properly if we can't read a source hg repository
Bryan O'Sullivan -
r5358:4fbd27bf default
parent child Browse files
Show More
@@ -1,210 +1,213
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 self.path = path
19 self.path = path
20 self.ui = ui
20 self.ui = ui
21 self.branchnames = ui.configbool('convert', 'hg.usebranchnames', True)
21 self.branchnames = ui.configbool('convert', 'hg.usebranchnames', True)
22 self.clonebranches = ui.configbool('convert', 'hg.clonebranches', False)
22 self.clonebranches = ui.configbool('convert', 'hg.clonebranches', False)
23 self.tagsbranch = ui.config('convert', 'hg.tagsbranch', 'default')
23 self.tagsbranch = ui.config('convert', 'hg.tagsbranch', 'default')
24 self.lastbranch = None
24 self.lastbranch = None
25 try:
25 try:
26 self.repo = hg.repository(self.ui, path)
26 self.repo = hg.repository(self.ui, path)
27 except:
27 except:
28 raise NoRepo("could not open hg repo %s as sink" % path)
28 raise NoRepo("could not open hg repo %s as sink" % path)
29 self.lock = None
29 self.lock = None
30 self.wlock = None
30 self.wlock = None
31
31
32 def before(self):
32 def before(self):
33 self.wlock = self.repo.wlock()
33 self.wlock = self.repo.wlock()
34 self.lock = self.repo.lock()
34 self.lock = self.repo.lock()
35 self.repo.dirstate.clear()
35 self.repo.dirstate.clear()
36
36
37 def after(self):
37 def after(self):
38 self.repo.dirstate.invalidate()
38 self.repo.dirstate.invalidate()
39 self.lock = None
39 self.lock = None
40 self.wlock = None
40 self.wlock = None
41
41
42 def revmapfile(self):
42 def revmapfile(self):
43 return os.path.join(self.path, ".hg", "shamap")
43 return os.path.join(self.path, ".hg", "shamap")
44
44
45 def authorfile(self):
45 def authorfile(self):
46 return os.path.join(self.path, ".hg", "authormap")
46 return os.path.join(self.path, ".hg", "authormap")
47
47
48 def getheads(self):
48 def getheads(self):
49 h = self.repo.changelog.heads()
49 h = self.repo.changelog.heads()
50 return [ hex(x) for x in h ]
50 return [ hex(x) for x in h ]
51
51
52 def putfile(self, f, e, data):
52 def putfile(self, f, e, data):
53 self.repo.wwrite(f, data, e)
53 self.repo.wwrite(f, data, e)
54 if f not in self.repo.dirstate:
54 if f not in self.repo.dirstate:
55 self.repo.dirstate.normallookup(f)
55 self.repo.dirstate.normallookup(f)
56
56
57 def copyfile(self, source, dest):
57 def copyfile(self, source, dest):
58 self.repo.copy(source, dest)
58 self.repo.copy(source, dest)
59
59
60 def delfile(self, f):
60 def delfile(self, f):
61 try:
61 try:
62 util.unlink(self.repo.wjoin(f))
62 util.unlink(self.repo.wjoin(f))
63 #self.repo.remove([f])
63 #self.repo.remove([f])
64 except:
64 except:
65 pass
65 pass
66
66
67 def setbranch(self, branch, pbranch, parents):
67 def setbranch(self, branch, pbranch, parents):
68 if (not self.clonebranches) or (branch == self.lastbranch):
68 if (not self.clonebranches) or (branch == self.lastbranch):
69 return
69 return
70
70
71 self.lastbranch = branch
71 self.lastbranch = branch
72 self.after()
72 self.after()
73 if not branch:
73 if not branch:
74 branch = 'default'
74 branch = 'default'
75 if not pbranch:
75 if not pbranch:
76 pbranch = 'default'
76 pbranch = 'default'
77
77
78 branchpath = os.path.join(self.path, branch)
78 branchpath = os.path.join(self.path, branch)
79 try:
79 try:
80 self.repo = hg.repository(self.ui, branchpath)
80 self.repo = hg.repository(self.ui, branchpath)
81 except:
81 except:
82 if not parents:
82 if not parents:
83 self.repo = hg.repository(self.ui, branchpath, create=True)
83 self.repo = hg.repository(self.ui, branchpath, create=True)
84 else:
84 else:
85 self.ui.note(_('cloning branch %s to %s\n') % (pbranch, branch))
85 self.ui.note(_('cloning branch %s to %s\n') % (pbranch, branch))
86 hg.clone(self.ui, os.path.join(self.path, pbranch),
86 hg.clone(self.ui, os.path.join(self.path, pbranch),
87 branchpath, rev=parents, update=False,
87 branchpath, rev=parents, update=False,
88 stream=True)
88 stream=True)
89 self.repo = hg.repository(self.ui, branchpath)
89 self.repo = hg.repository(self.ui, branchpath)
90
90
91 def putcommit(self, files, parents, commit):
91 def putcommit(self, files, parents, commit):
92 seen = {}
92 seen = {}
93 pl = []
93 pl = []
94 for p in parents:
94 for p in parents:
95 if p not in seen:
95 if p not in seen:
96 pl.append(p)
96 pl.append(p)
97 seen[p] = 1
97 seen[p] = 1
98 parents = pl
98 parents = pl
99
99
100 if len(parents) < 2: parents.append("0" * 40)
100 if len(parents) < 2: parents.append("0" * 40)
101 if len(parents) < 2: parents.append("0" * 40)
101 if len(parents) < 2: parents.append("0" * 40)
102 p2 = parents.pop(0)
102 p2 = parents.pop(0)
103
103
104 text = commit.desc
104 text = commit.desc
105 extra = {}
105 extra = {}
106 if self.branchnames and commit.branch:
106 if self.branchnames and commit.branch:
107 extra['branch'] = commit.branch
107 extra['branch'] = commit.branch
108 if commit.rev:
108 if commit.rev:
109 extra['convert_revision'] = commit.rev
109 extra['convert_revision'] = commit.rev
110
110
111 while parents:
111 while parents:
112 p1 = p2
112 p1 = p2
113 p2 = parents.pop(0)
113 p2 = parents.pop(0)
114 a = self.repo.rawcommit(files, text, commit.author, commit.date,
114 a = self.repo.rawcommit(files, text, commit.author, commit.date,
115 bin(p1), bin(p2), extra=extra)
115 bin(p1), bin(p2), extra=extra)
116 self.repo.dirstate.clear()
116 self.repo.dirstate.clear()
117 text = "(octopus merge fixup)\n"
117 text = "(octopus merge fixup)\n"
118 p2 = hg.hex(self.repo.changelog.tip())
118 p2 = hg.hex(self.repo.changelog.tip())
119
119
120 return p2
120 return p2
121
121
122 def puttags(self, tags):
122 def puttags(self, tags):
123 try:
123 try:
124 old = self.repo.wfile(".hgtags").read()
124 old = self.repo.wfile(".hgtags").read()
125 oldlines = old.splitlines(1)
125 oldlines = old.splitlines(1)
126 oldlines.sort()
126 oldlines.sort()
127 except:
127 except:
128 oldlines = []
128 oldlines = []
129
129
130 k = tags.keys()
130 k = tags.keys()
131 k.sort()
131 k.sort()
132 newlines = []
132 newlines = []
133 for tag in k:
133 for tag in k:
134 newlines.append("%s %s\n" % (tags[tag], tag))
134 newlines.append("%s %s\n" % (tags[tag], tag))
135
135
136 newlines.sort()
136 newlines.sort()
137
137
138 if newlines != oldlines:
138 if newlines != oldlines:
139 self.ui.status("updating tags\n")
139 self.ui.status("updating tags\n")
140 f = self.repo.wfile(".hgtags", "w")
140 f = self.repo.wfile(".hgtags", "w")
141 f.write("".join(newlines))
141 f.write("".join(newlines))
142 f.close()
142 f.close()
143 if not oldlines: self.repo.add([".hgtags"])
143 if not oldlines: self.repo.add([".hgtags"])
144 date = "%s 0" % int(time.mktime(time.gmtime()))
144 date = "%s 0" % int(time.mktime(time.gmtime()))
145 extra = {}
145 extra = {}
146 if self.tagsbranch != 'default':
146 if self.tagsbranch != 'default':
147 extra['branch'] = self.tagsbranch
147 extra['branch'] = self.tagsbranch
148 try:
148 try:
149 tagparent = self.repo.changectx(self.tagsbranch).node()
149 tagparent = self.repo.changectx(self.tagsbranch).node()
150 except hg.RepoError, inst:
150 except hg.RepoError, inst:
151 tagparent = nullid
151 tagparent = nullid
152 self.repo.rawcommit([".hgtags"], "update tags", "convert-repo",
152 self.repo.rawcommit([".hgtags"], "update tags", "convert-repo",
153 date, tagparent, nullid)
153 date, tagparent, nullid)
154 return hex(self.repo.changelog.tip())
154 return hex(self.repo.changelog.tip())
155
155
156 class mercurial_source(converter_source):
156 class mercurial_source(converter_source):
157 def __init__(self, ui, path, rev=None):
157 def __init__(self, ui, path, rev=None):
158 converter_source.__init__(self, ui, path, rev)
158 converter_source.__init__(self, ui, path, rev)
159 try:
159 self.repo = hg.repository(self.ui, path)
160 self.repo = hg.repository(self.ui, path)
161 except:
162 raise NoRepo("could not open hg repo %s as source" % path)
160 self.lastrev = None
163 self.lastrev = None
161 self.lastctx = None
164 self.lastctx = None
162
165
163 def changectx(self, rev):
166 def changectx(self, rev):
164 if self.lastrev != rev:
167 if self.lastrev != rev:
165 self.lastctx = self.repo.changectx(rev)
168 self.lastctx = self.repo.changectx(rev)
166 self.lastrev = rev
169 self.lastrev = rev
167 return self.lastctx
170 return self.lastctx
168
171
169 def getheads(self):
172 def getheads(self):
170 if self.rev:
173 if self.rev:
171 return [hex(self.repo.changectx(self.rev).node())]
174 return [hex(self.repo.changectx(self.rev).node())]
172 else:
175 else:
173 return [hex(node) for node in self.repo.heads()]
176 return [hex(node) for node in self.repo.heads()]
174
177
175 def getfile(self, name, rev):
178 def getfile(self, name, rev):
176 try:
179 try:
177 return self.changectx(rev).filectx(name).data()
180 return self.changectx(rev).filectx(name).data()
178 except revlog.LookupError, err:
181 except revlog.LookupError, err:
179 raise IOError(err)
182 raise IOError(err)
180
183
181 def getmode(self, name, rev):
184 def getmode(self, name, rev):
182 m = self.changectx(rev).manifest()
185 m = self.changectx(rev).manifest()
183 return (m.execf(name) and 'x' or '') + (m.linkf(name) and 'l' or '')
186 return (m.execf(name) and 'x' or '') + (m.linkf(name) and 'l' or '')
184
187
185 def getchanges(self, rev):
188 def getchanges(self, rev):
186 ctx = self.changectx(rev)
189 ctx = self.changectx(rev)
187 m, a, r = self.repo.status(ctx.parents()[0].node(), ctx.node())[:3]
190 m, a, r = self.repo.status(ctx.parents()[0].node(), ctx.node())[:3]
188 changes = [(name, rev) for name in m + a + r]
191 changes = [(name, rev) for name in m + a + r]
189 changes.sort()
192 changes.sort()
190 return (changes, self.getcopies(ctx, m + a))
193 return (changes, self.getcopies(ctx, m + a))
191
194
192 def getcopies(self, ctx, files):
195 def getcopies(self, ctx, files):
193 copies = {}
196 copies = {}
194 for name in files:
197 for name in files:
195 try:
198 try:
196 copies[name] = ctx.filectx(name).renamed()[0]
199 copies[name] = ctx.filectx(name).renamed()[0]
197 except TypeError:
200 except TypeError:
198 pass
201 pass
199 return copies
202 return copies
200
203
201 def getcommit(self, rev):
204 def getcommit(self, rev):
202 ctx = self.changectx(rev)
205 ctx = self.changectx(rev)
203 parents = [hex(p.node()) for p in ctx.parents() if p.node() != nullid]
206 parents = [hex(p.node()) for p in ctx.parents() if p.node() != nullid]
204 return commit(author=ctx.user(), date=util.datestr(ctx.date()),
207 return commit(author=ctx.user(), date=util.datestr(ctx.date()),
205 desc=ctx.description(), parents=parents,
208 desc=ctx.description(), parents=parents,
206 branch=ctx.branch())
209 branch=ctx.branch())
207
210
208 def gettags(self):
211 def gettags(self):
209 tags = [t for t in self.repo.tagslist() if t[0] != 'tip']
212 tags = [t for t in self.repo.tagslist() if t[0] != 'tip']
210 return dict([(name, hex(node)) for name, node in tags])
213 return dict([(name, hex(node)) for name, node in tags])
General Comments 0
You need to be logged in to leave comments. Login now