Show More
@@ -1,375 +1,375 b'' | |||
|
1 | 1 | # gnuarch.py - GNU Arch support for the convert extension |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2008, 2009 Aleix Conchillo Flaque <aleix@member.fsf.org> |
|
4 | 4 | # and others |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms of the |
|
7 | 7 | # GNU General Public License version 2 or any later version. |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import os |
|
11 | 11 | import shutil |
|
12 | 12 | import stat |
|
13 | 13 | import tempfile |
|
14 | 14 | |
|
15 | 15 | from mercurial.i18n import _ |
|
16 | 16 | from mercurial import ( |
|
17 | 17 | encoding, |
|
18 | 18 | error, |
|
19 | 19 | mail, |
|
20 | 20 | pycompat, |
|
21 | 21 | util, |
|
22 | 22 | ) |
|
23 | 23 | from mercurial.utils import ( |
|
24 | 24 | dateutil, |
|
25 | 25 | procutil, |
|
26 | 26 | ) |
|
27 | 27 | from . import common |
|
28 | 28 | |
|
29 | 29 | |
|
30 | 30 | class gnuarch_source(common.converter_source, common.commandline): |
|
31 | 31 | class gnuarch_rev(object): |
|
32 | 32 | def __init__(self, rev): |
|
33 | 33 | self.rev = rev |
|
34 | 34 | self.summary = b'' |
|
35 | 35 | self.date = None |
|
36 | 36 | self.author = b'' |
|
37 | 37 | self.continuationof = None |
|
38 | 38 | self.add_files = [] |
|
39 | 39 | self.mod_files = [] |
|
40 | 40 | self.del_files = [] |
|
41 | 41 | self.ren_files = {} |
|
42 | 42 | self.ren_dirs = {} |
|
43 | 43 | |
|
44 | 44 | def __init__(self, ui, repotype, path, revs=None): |
|
45 | 45 | super(gnuarch_source, self).__init__(ui, repotype, path, revs=revs) |
|
46 | 46 | |
|
47 | 47 | if not os.path.exists(os.path.join(path, b'{arch}')): |
|
48 | 48 | raise common.NoRepo( |
|
49 | 49 | _(b"%s does not look like a GNU Arch repository") % path |
|
50 | 50 | ) |
|
51 | 51 | |
|
52 | 52 | # Could use checktool, but we want to check for baz or tla. |
|
53 | 53 | self.execmd = None |
|
54 | 54 | if procutil.findexe(b'baz'): |
|
55 | 55 | self.execmd = b'baz' |
|
56 | 56 | else: |
|
57 | 57 | if procutil.findexe(b'tla'): |
|
58 | 58 | self.execmd = b'tla' |
|
59 | 59 | else: |
|
60 | 60 | raise error.Abort(_(b'cannot find a GNU Arch tool')) |
|
61 | 61 | |
|
62 | 62 | common.commandline.__init__(self, ui, self.execmd) |
|
63 | 63 | |
|
64 | 64 | self.path = os.path.realpath(path) |
|
65 | 65 | self.tmppath = None |
|
66 | 66 | |
|
67 | 67 | self.treeversion = None |
|
68 | 68 | self.lastrev = None |
|
69 | 69 | self.changes = {} |
|
70 | 70 | self.parents = {} |
|
71 | 71 | self.tags = {} |
|
72 | 72 | self.encoding = encoding.encoding |
|
73 | 73 | self.archives = [] |
|
74 | 74 | |
|
75 | 75 | def before(self): |
|
76 | 76 | # Get registered archives |
|
77 | 77 | self.archives = [ |
|
78 | 78 | i.rstrip(b'\n') for i in self.runlines0(b'archives', b'-n') |
|
79 | 79 | ] |
|
80 | 80 | |
|
81 | 81 | if self.execmd == b'tla': |
|
82 | 82 | output = self.run0(b'tree-version', self.path) |
|
83 | 83 | else: |
|
84 | 84 | output = self.run0(b'tree-version', b'-d', self.path) |
|
85 | 85 | self.treeversion = output.strip() |
|
86 | 86 | |
|
87 | 87 | # Get name of temporary directory |
|
88 | 88 | version = self.treeversion.split(b'/') |
|
89 | 89 | self.tmppath = os.path.join( |
|
90 | 90 | pycompat.fsencode(tempfile.gettempdir()), b'hg-%s' % version[1] |
|
91 | 91 | ) |
|
92 | 92 | |
|
93 | 93 | # Generate parents dictionary |
|
94 | 94 | self.parents[None] = [] |
|
95 | 95 | treeversion = self.treeversion |
|
96 | 96 | child = None |
|
97 | 97 | while treeversion: |
|
98 | 98 | self.ui.status(_(b'analyzing tree version %s...\n') % treeversion) |
|
99 | 99 | |
|
100 | 100 | archive = treeversion.split(b'/')[0] |
|
101 | 101 | if archive not in self.archives: |
|
102 | 102 | self.ui.status( |
|
103 | 103 | _( |
|
104 | 104 | b'tree analysis stopped because it points to ' |
|
105 | 105 | b'an unregistered archive %s...\n' |
|
106 | 106 | ) |
|
107 | 107 | % archive |
|
108 | 108 | ) |
|
109 | 109 | break |
|
110 | 110 | |
|
111 | 111 | # Get the complete list of revisions for that tree version |
|
112 | 112 | output, status = self.runlines( |
|
113 | 113 | b'revisions', b'-r', b'-f', treeversion |
|
114 | 114 | ) |
|
115 | 115 | self.checkexit( |
|
116 | 116 | status, b'failed retrieving revisions for %s' % treeversion |
|
117 | 117 | ) |
|
118 | 118 | |
|
119 | 119 | # No new iteration unless a revision has a continuation-of header |
|
120 | 120 | treeversion = None |
|
121 | 121 | |
|
122 | 122 | for l in output: |
|
123 | 123 | rev = l.strip() |
|
124 | 124 | self.changes[rev] = self.gnuarch_rev(rev) |
|
125 | 125 | self.parents[rev] = [] |
|
126 | 126 | |
|
127 | 127 | # Read author, date and summary |
|
128 | 128 | catlog, status = self.run(b'cat-log', b'-d', self.path, rev) |
|
129 | 129 | if status: |
|
130 | 130 | catlog = self.run0(b'cat-archive-log', rev) |
|
131 | 131 | self._parsecatlog(catlog, rev) |
|
132 | 132 | |
|
133 | 133 | # Populate the parents map |
|
134 | 134 | self.parents[child].append(rev) |
|
135 | 135 | |
|
136 | 136 | # Keep track of the current revision as the child of the next |
|
137 | 137 | # revision scanned |
|
138 | 138 | child = rev |
|
139 | 139 | |
|
140 | 140 | # Check if we have to follow the usual incremental history |
|
141 | 141 | # or if we have to 'jump' to a different treeversion given |
|
142 | 142 | # by the continuation-of header. |
|
143 | 143 | if self.changes[rev].continuationof: |
|
144 | 144 | treeversion = b'--'.join( |
|
145 | 145 | self.changes[rev].continuationof.split(b'--')[:-1] |
|
146 | 146 | ) |
|
147 | 147 | break |
|
148 | 148 | |
|
149 | 149 | # If we reached a base-0 revision w/o any continuation-of |
|
150 | 150 | # header, it means the tree history ends here. |
|
151 | 151 | if rev[-6:] == b'base-0': |
|
152 | 152 | break |
|
153 | 153 | |
|
154 | 154 | def after(self): |
|
155 | 155 | self.ui.debug(b'cleaning up %s\n' % self.tmppath) |
|
156 | 156 | shutil.rmtree(self.tmppath, ignore_errors=True) |
|
157 | 157 | |
|
158 | 158 | def getheads(self): |
|
159 | 159 | return self.parents[None] |
|
160 | 160 | |
|
161 | 161 | def getfile(self, name, rev): |
|
162 | 162 | if rev != self.lastrev: |
|
163 | 163 | raise error.Abort(_(b'internal calling inconsistency')) |
|
164 | 164 | |
|
165 | 165 | if not os.path.lexists(os.path.join(self.tmppath, name)): |
|
166 | 166 | return None, None |
|
167 | 167 | |
|
168 | 168 | return self._getfile(name, rev) |
|
169 | 169 | |
|
170 | 170 | def getchanges(self, rev, full): |
|
171 | 171 | if full: |
|
172 | 172 | raise error.Abort(_(b"convert from arch does not support --full")) |
|
173 | 173 | self._update(rev) |
|
174 | 174 | changes = [] |
|
175 | 175 | copies = {} |
|
176 | 176 | |
|
177 | 177 | for f in self.changes[rev].add_files: |
|
178 | 178 | changes.append((f, rev)) |
|
179 | 179 | |
|
180 | 180 | for f in self.changes[rev].mod_files: |
|
181 | 181 | changes.append((f, rev)) |
|
182 | 182 | |
|
183 | 183 | for f in self.changes[rev].del_files: |
|
184 | 184 | changes.append((f, rev)) |
|
185 | 185 | |
|
186 | 186 | for src in self.changes[rev].ren_files: |
|
187 | 187 | to = self.changes[rev].ren_files[src] |
|
188 | 188 | changes.append((src, rev)) |
|
189 | 189 | changes.append((to, rev)) |
|
190 | 190 | copies[to] = src |
|
191 | 191 | |
|
192 | 192 | for src in self.changes[rev].ren_dirs: |
|
193 | 193 | to = self.changes[rev].ren_dirs[src] |
|
194 | 194 | chgs, cps = self._rendirchanges(src, to) |
|
195 | 195 | changes += [(f, rev) for f in chgs] |
|
196 | 196 | copies.update(cps) |
|
197 | 197 | |
|
198 | 198 | self.lastrev = rev |
|
199 | 199 | return sorted(set(changes)), copies, set() |
|
200 | 200 | |
|
201 | 201 | def getcommit(self, rev): |
|
202 | 202 | changes = self.changes[rev] |
|
203 | 203 | return common.commit( |
|
204 | 204 | author=changes.author, |
|
205 | 205 | date=changes.date, |
|
206 | 206 | desc=changes.summary, |
|
207 | 207 | parents=self.parents[rev], |
|
208 | 208 | rev=rev, |
|
209 | 209 | ) |
|
210 | 210 | |
|
211 | 211 | def gettags(self): |
|
212 | 212 | return self.tags |
|
213 | 213 | |
|
214 | 214 | def _execute(self, cmd, *args, **kwargs): |
|
215 | 215 | cmdline = [self.execmd, cmd] |
|
216 | 216 | cmdline += args |
|
217 | 217 | cmdline = [procutil.shellquote(arg) for arg in cmdline] |
|
218 | 218 | bdevnull = pycompat.bytestr(os.devnull) |
|
219 | 219 | cmdline += [b'>', bdevnull, b'2>', bdevnull] |
|
220 | 220 | cmdline = procutil.quotecommand(b' '.join(cmdline)) |
|
221 | 221 | self.ui.debug(cmdline, b'\n') |
|
222 | 222 | return os.system(pycompat.rapply(procutil.tonativestr, cmdline)) |
|
223 | 223 | |
|
224 | 224 | def _update(self, rev): |
|
225 | 225 | self.ui.debug(b'applying revision %s...\n' % rev) |
|
226 | 226 | changeset, status = self.runlines(b'replay', b'-d', self.tmppath, rev) |
|
227 | 227 | if status: |
|
228 | 228 | # Something went wrong while merging (baz or tla |
|
229 | 229 | # issue?), get latest revision and try from there |
|
230 | 230 | shutil.rmtree(self.tmppath, ignore_errors=True) |
|
231 | 231 | self._obtainrevision(rev) |
|
232 | 232 | else: |
|
233 | 233 | old_rev = self.parents[rev][0] |
|
234 | 234 | self.ui.debug( |
|
235 | 235 | b'computing changeset between %s and %s...\n' % (old_rev, rev) |
|
236 | 236 | ) |
|
237 | 237 | self._parsechangeset(changeset, rev) |
|
238 | 238 | |
|
239 | 239 | def _getfile(self, name, rev): |
|
240 | 240 | mode = os.lstat(os.path.join(self.tmppath, name)).st_mode |
|
241 | 241 | if stat.S_ISLNK(mode): |
|
242 | 242 | data = util.readlink(os.path.join(self.tmppath, name)) |
|
243 | 243 | if mode: |
|
244 | 244 | mode = b'l' |
|
245 | 245 | else: |
|
246 | 246 | mode = b'' |
|
247 | 247 | else: |
|
248 | 248 | data = util.readfile(os.path.join(self.tmppath, name)) |
|
249 | 249 | mode = (mode & 0o111) and b'x' or b'' |
|
250 | 250 | return data, mode |
|
251 | 251 | |
|
252 | 252 | def _exclude(self, name): |
|
253 | 253 | exclude = [b'{arch}', b'.arch-ids', b'.arch-inventory'] |
|
254 | 254 | for exc in exclude: |
|
255 | 255 | if name.find(exc) != -1: |
|
256 | 256 | return True |
|
257 | 257 | return False |
|
258 | 258 | |
|
259 | 259 | def _readcontents(self, path): |
|
260 | 260 | files = [] |
|
261 | 261 | contents = os.listdir(path) |
|
262 | 262 | while len(contents) > 0: |
|
263 | 263 | c = contents.pop() |
|
264 | 264 | p = os.path.join(path, c) |
|
265 | 265 | # os.walk could be used, but here we avoid internal GNU |
|
266 | 266 | # Arch files and directories, thus saving a lot time. |
|
267 | 267 | if not self._exclude(p): |
|
268 | 268 | if os.path.isdir(p): |
|
269 | 269 | contents += [os.path.join(c, f) for f in os.listdir(p)] |
|
270 | 270 | else: |
|
271 | 271 | files.append(c) |
|
272 | 272 | return files |
|
273 | 273 | |
|
274 | 274 | def _rendirchanges(self, src, dest): |
|
275 | 275 | changes = [] |
|
276 | 276 | copies = {} |
|
277 | 277 | files = self._readcontents(os.path.join(self.tmppath, dest)) |
|
278 | 278 | for f in files: |
|
279 | 279 | s = os.path.join(src, f) |
|
280 | 280 | d = os.path.join(dest, f) |
|
281 | 281 | changes.append(s) |
|
282 | 282 | changes.append(d) |
|
283 | 283 | copies[d] = s |
|
284 | 284 | return changes, copies |
|
285 | 285 | |
|
286 | 286 | def _obtainrevision(self, rev): |
|
287 | 287 | self.ui.debug(b'obtaining revision %s...\n' % rev) |
|
288 | 288 | output = self._execute(b'get', rev, self.tmppath) |
|
289 | 289 | self.checkexit(output) |
|
290 | 290 | self.ui.debug(b'analyzing revision %s...\n' % rev) |
|
291 | 291 | files = self._readcontents(self.tmppath) |
|
292 | 292 | self.changes[rev].add_files += files |
|
293 | 293 | |
|
294 | 294 | def _stripbasepath(self, path): |
|
295 | 295 | if path.startswith(b'./'): |
|
296 | 296 | return path[2:] |
|
297 | 297 | return path |
|
298 | 298 | |
|
299 | 299 | def _parsecatlog(self, data, rev): |
|
300 | 300 | try: |
|
301 | 301 | catlog = mail.parsebytes(data) |
|
302 | 302 | |
|
303 | 303 | # Commit date |
|
304 | 304 | self.changes[rev].date = dateutil.datestr( |
|
305 |
dateutil.strdate(catlog[ |
|
|
305 | dateutil.strdate(catlog[r'Standard-date'], b'%Y-%m-%d %H:%M:%S') | |
|
306 | 306 | ) |
|
307 | 307 | |
|
308 | 308 | # Commit author |
|
309 |
self.changes[rev].author = self.recode(catlog[ |
|
|
309 | self.changes[rev].author = self.recode(catlog[r'Creator']) | |
|
310 | 310 | |
|
311 | 311 | # Commit description |
|
312 | 312 | self.changes[rev].summary = b'\n\n'.join( |
|
313 |
(catlog[ |
|
|
313 | (catlog[r'Summary'], catlog.get_payload()) | |
|
314 | 314 | ) |
|
315 | 315 | self.changes[rev].summary = self.recode(self.changes[rev].summary) |
|
316 | 316 | |
|
317 | 317 | # Commit revision origin when dealing with a branch or tag |
|
318 |
if |
|
|
318 | if r'Continuation-of' in catlog: | |
|
319 | 319 | self.changes[rev].continuationof = self.recode( |
|
320 |
catlog[ |
|
|
320 | catlog[r'Continuation-of'] | |
|
321 | 321 | ) |
|
322 | 322 | except Exception: |
|
323 | 323 | raise error.Abort(_(b'could not parse cat-log of %s') % rev) |
|
324 | 324 | |
|
325 | 325 | def _parsechangeset(self, data, rev): |
|
326 | 326 | for l in data: |
|
327 | 327 | l = l.strip() |
|
328 | 328 | # Added file (ignore added directory) |
|
329 | 329 | if l.startswith(b'A') and not l.startswith(b'A/'): |
|
330 | 330 | file = self._stripbasepath(l[1:].strip()) |
|
331 | 331 | if not self._exclude(file): |
|
332 | 332 | self.changes[rev].add_files.append(file) |
|
333 | 333 | # Deleted file (ignore deleted directory) |
|
334 | 334 | elif l.startswith(b'D') and not l.startswith(b'D/'): |
|
335 | 335 | file = self._stripbasepath(l[1:].strip()) |
|
336 | 336 | if not self._exclude(file): |
|
337 | 337 | self.changes[rev].del_files.append(file) |
|
338 | 338 | # Modified binary file |
|
339 | 339 | elif l.startswith(b'Mb'): |
|
340 | 340 | file = self._stripbasepath(l[2:].strip()) |
|
341 | 341 | if not self._exclude(file): |
|
342 | 342 | self.changes[rev].mod_files.append(file) |
|
343 | 343 | # Modified link |
|
344 | 344 | elif l.startswith(b'M->'): |
|
345 | 345 | file = self._stripbasepath(l[3:].strip()) |
|
346 | 346 | if not self._exclude(file): |
|
347 | 347 | self.changes[rev].mod_files.append(file) |
|
348 | 348 | # Modified file |
|
349 | 349 | elif l.startswith(b'M'): |
|
350 | 350 | file = self._stripbasepath(l[1:].strip()) |
|
351 | 351 | if not self._exclude(file): |
|
352 | 352 | self.changes[rev].mod_files.append(file) |
|
353 | 353 | # Renamed file (or link) |
|
354 | 354 | elif l.startswith(b'=>'): |
|
355 | 355 | files = l[2:].strip().split(b' ') |
|
356 | 356 | if len(files) == 1: |
|
357 | 357 | files = l[2:].strip().split(b'\t') |
|
358 | 358 | src = self._stripbasepath(files[0]) |
|
359 | 359 | dst = self._stripbasepath(files[1]) |
|
360 | 360 | if not self._exclude(src) and not self._exclude(dst): |
|
361 | 361 | self.changes[rev].ren_files[src] = dst |
|
362 | 362 | # Conversion from file to link or from link to file (modified) |
|
363 | 363 | elif l.startswith(b'ch'): |
|
364 | 364 | file = self._stripbasepath(l[2:].strip()) |
|
365 | 365 | if not self._exclude(file): |
|
366 | 366 | self.changes[rev].mod_files.append(file) |
|
367 | 367 | # Renamed directory |
|
368 | 368 | elif l.startswith(b'/>'): |
|
369 | 369 | dirs = l[2:].strip().split(b' ') |
|
370 | 370 | if len(dirs) == 1: |
|
371 | 371 | dirs = l[2:].strip().split(b'\t') |
|
372 | 372 | src = self._stripbasepath(dirs[0]) |
|
373 | 373 | dst = self._stripbasepath(dirs[1]) |
|
374 | 374 | if not self._exclude(src) and not self._exclude(dst): |
|
375 | 375 | self.changes[rev].ren_dirs[src] = dst |
General Comments 0
You need to be logged in to leave comments.
Login now