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