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