2009-04-26 03:47:44 +04:00
|
|
|
# gnuarch.py - GNU Arch support for the convert extension
|
|
|
|
#
|
|
|
|
# Copyright 2008, 2009 Aleix Conchillo Flaque <aleix@member.fsf.org>
|
|
|
|
# and others
|
|
|
|
#
|
|
|
|
# This software may be used and distributed according to the terms of the
|
2010-01-20 07:20:08 +03:00
|
|
|
# GNU General Public License version 2 or any later version.
|
2016-03-02 11:58:01 +03:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
import email
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import stat
|
|
|
|
import tempfile
|
2016-05-14 08:03:12 +03:00
|
|
|
|
|
|
|
from mercurial.i18n import _
|
2016-03-02 11:58:01 +03:00
|
|
|
from mercurial import (
|
|
|
|
encoding,
|
|
|
|
error,
|
|
|
|
util,
|
|
|
|
)
|
|
|
|
from . import common
|
2008-02-05 11:30:08 +03:00
|
|
|
|
2016-03-02 11:58:01 +03:00
|
|
|
class gnuarch_source(common.converter_source, common.commandline):
|
2008-02-05 11:30:08 +03:00
|
|
|
|
2009-06-10 17:10:21 +04:00
|
|
|
class gnuarch_rev(object):
|
2008-02-05 11:30:08 +03:00
|
|
|
def __init__(self, rev):
|
|
|
|
self.rev = rev
|
|
|
|
self.summary = ''
|
|
|
|
self.date = None
|
|
|
|
self.author = ''
|
2009-01-04 04:36:48 +03:00
|
|
|
self.continuationof = None
|
2008-02-05 11:30:08 +03:00
|
|
|
self.add_files = []
|
|
|
|
self.mod_files = []
|
|
|
|
self.del_files = []
|
|
|
|
self.ren_files = {}
|
|
|
|
self.ren_dirs = {}
|
|
|
|
|
2015-07-08 20:27:43 +03:00
|
|
|
def __init__(self, ui, path, revs=None):
|
|
|
|
super(gnuarch_source, self).__init__(ui, path, revs=revs)
|
2008-02-05 11:30:08 +03:00
|
|
|
|
|
|
|
if not os.path.exists(os.path.join(path, '{arch}')):
|
2016-03-02 11:58:01 +03:00
|
|
|
raise common.NoRepo(_("%s does not look like a GNU Arch repository")
|
2010-04-18 17:47:49 +04:00
|
|
|
% path)
|
2008-02-05 11:30:08 +03:00
|
|
|
|
|
|
|
# Could use checktool, but we want to check for baz or tla.
|
|
|
|
self.execmd = None
|
2011-05-08 22:35:46 +04:00
|
|
|
if util.findexe('baz'):
|
2008-02-13 01:35:35 +03:00
|
|
|
self.execmd = 'baz'
|
2008-02-05 11:30:08 +03:00
|
|
|
else:
|
2011-05-08 22:35:46 +04:00
|
|
|
if util.findexe('tla'):
|
2008-02-13 01:35:35 +03:00
|
|
|
self.execmd = 'tla'
|
2008-02-05 11:30:08 +03:00
|
|
|
else:
|
2015-10-08 22:55:45 +03:00
|
|
|
raise error.Abort(_('cannot find a GNU Arch tool'))
|
2008-02-05 11:30:08 +03:00
|
|
|
|
2016-03-02 11:58:01 +03:00
|
|
|
common.commandline.__init__(self, ui, self.execmd)
|
2008-02-05 11:30:08 +03:00
|
|
|
|
2011-10-29 20:02:23 +04:00
|
|
|
self.path = os.path.realpath(path)
|
2008-02-05 11:30:08 +03:00
|
|
|
self.tmppath = None
|
|
|
|
|
|
|
|
self.treeversion = None
|
|
|
|
self.lastrev = None
|
|
|
|
self.changes = {}
|
|
|
|
self.parents = {}
|
|
|
|
self.tags = {}
|
2016-03-02 11:58:01 +03:00
|
|
|
self.catlogparser = email.Parser.Parser()
|
2010-08-18 18:53:52 +04:00
|
|
|
self.encoding = encoding.encoding
|
2009-01-04 04:36:48 +03:00
|
|
|
self.archives = []
|
2008-02-05 11:30:08 +03:00
|
|
|
|
|
|
|
def before(self):
|
2009-01-04 04:36:48 +03:00
|
|
|
# Get registered archives
|
|
|
|
self.archives = [i.rstrip('\n')
|
|
|
|
for i in self.runlines0('archives', '-n')]
|
|
|
|
|
2008-02-05 11:30:08 +03:00
|
|
|
if self.execmd == 'tla':
|
|
|
|
output = self.run0('tree-version', self.path)
|
|
|
|
else:
|
|
|
|
output = self.run0('tree-version', '-d', self.path)
|
|
|
|
self.treeversion = output.strip()
|
|
|
|
|
|
|
|
# Get name of temporary directory
|
|
|
|
version = self.treeversion.split('/')
|
|
|
|
self.tmppath = os.path.join(tempfile.gettempdir(),
|
|
|
|
'hg-%s' % version[1])
|
|
|
|
|
|
|
|
# Generate parents dictionary
|
2009-01-04 04:36:48 +03:00
|
|
|
self.parents[None] = []
|
|
|
|
treeversion = self.treeversion
|
|
|
|
child = None
|
|
|
|
while treeversion:
|
|
|
|
self.ui.status(_('analyzing tree version %s...\n') % treeversion)
|
|
|
|
|
|
|
|
archive = treeversion.split('/')[0]
|
|
|
|
if archive not in self.archives:
|
2009-05-31 03:29:30 +04:00
|
|
|
self.ui.status(_('tree analysis stopped because it points to '
|
|
|
|
'an unregistered archive %s...\n') % archive)
|
2008-02-05 11:30:08 +03:00
|
|
|
break
|
2009-01-04 04:36:48 +03:00
|
|
|
|
|
|
|
# Get the complete list of revisions for that tree version
|
|
|
|
output, status = self.runlines('revisions', '-r', '-f', treeversion)
|
2012-08-16 00:38:42 +04:00
|
|
|
self.checkexit(status, 'failed retrieving revisions for %s'
|
2010-01-25 09:05:27 +03:00
|
|
|
% treeversion)
|
2009-01-04 04:36:48 +03:00
|
|
|
|
|
|
|
# No new iteration unless a revision has a continuation-of header
|
|
|
|
treeversion = None
|
|
|
|
|
|
|
|
for l in output:
|
|
|
|
rev = l.strip()
|
|
|
|
self.changes[rev] = self.gnuarch_rev(rev)
|
|
|
|
self.parents[rev] = []
|
|
|
|
|
|
|
|
# Read author, date and summary
|
|
|
|
catlog, status = self.run('cat-log', '-d', self.path, rev)
|
|
|
|
if status:
|
|
|
|
catlog = self.run0('cat-archive-log', rev)
|
|
|
|
self._parsecatlog(catlog, rev)
|
|
|
|
|
|
|
|
# Populate the parents map
|
|
|
|
self.parents[child].append(rev)
|
|
|
|
|
|
|
|
# Keep track of the current revision as the child of the next
|
|
|
|
# revision scanned
|
|
|
|
child = rev
|
|
|
|
|
|
|
|
# Check if we have to follow the usual incremental history
|
|
|
|
# or if we have to 'jump' to a different treeversion given
|
|
|
|
# by the continuation-of header.
|
|
|
|
if self.changes[rev].continuationof:
|
2010-01-25 09:05:27 +03:00
|
|
|
treeversion = '--'.join(
|
|
|
|
self.changes[rev].continuationof.split('--')[:-1])
|
2009-01-04 04:36:48 +03:00
|
|
|
break
|
|
|
|
|
|
|
|
# If we reached a base-0 revision w/o any continuation-of
|
|
|
|
# header, it means the tree history ends here.
|
|
|
|
if rev[-6:] == 'base-0':
|
|
|
|
break
|
2008-02-05 11:30:08 +03:00
|
|
|
|
|
|
|
def after(self):
|
2009-09-19 03:15:38 +04:00
|
|
|
self.ui.debug('cleaning up %s\n' % self.tmppath)
|
2008-02-05 11:30:08 +03:00
|
|
|
shutil.rmtree(self.tmppath, ignore_errors=True)
|
|
|
|
|
|
|
|
def getheads(self):
|
|
|
|
return self.parents[None]
|
|
|
|
|
|
|
|
def getfile(self, name, rev):
|
|
|
|
if rev != self.lastrev:
|
2015-10-08 22:55:45 +03:00
|
|
|
raise error.Abort(_('internal calling inconsistency'))
|
2008-02-05 11:30:08 +03:00
|
|
|
|
2010-09-20 23:46:56 +04:00
|
|
|
if not os.path.lexists(os.path.join(self.tmppath, name)):
|
2014-08-27 00:03:32 +04:00
|
|
|
return None, None
|
2008-02-05 11:30:08 +03:00
|
|
|
|
2010-05-09 23:52:34 +04:00
|
|
|
return self._getfile(name, rev)
|
2008-02-05 11:30:08 +03:00
|
|
|
|
convert: introduce --full for converting all files
Convert will normally only process files that were changed in a source
revision, apply the filemap, and record it has a change in the target
repository. (If it ends up not really changing anything, nothing changes.)
That means that _if_ the filemap is changed before continuing an incremental
convert, the change will only kick in when the files it affects are modified in
a source revision and thus processed.
With --full, convert will make a full conversion every time and process
all files in the source repo and remove target repo files that shouldn't be
there. Filemap changes will thus kick in on the first converted revision, no
matter what is changed.
This flag should in most cases not make any difference but will make convert
significantly slower.
Other names has been considered for this feature, such as "resync", "sync",
"checkunmodified", "all" or "allfiles", but I found that they were less obvious
and required more explanation than "full" and were harder to describe
consistently.
2014-08-27 00:03:32 +04:00
|
|
|
def getchanges(self, rev, full):
|
|
|
|
if full:
|
2015-10-14 09:06:54 +03:00
|
|
|
raise error.Abort(_("convert from arch does not support --full"))
|
2008-02-05 11:30:08 +03:00
|
|
|
self._update(rev)
|
|
|
|
changes = []
|
|
|
|
copies = {}
|
|
|
|
|
|
|
|
for f in self.changes[rev].add_files:
|
|
|
|
changes.append((f, rev))
|
|
|
|
|
|
|
|
for f in self.changes[rev].mod_files:
|
|
|
|
changes.append((f, rev))
|
|
|
|
|
|
|
|
for f in self.changes[rev].del_files:
|
|
|
|
changes.append((f, rev))
|
|
|
|
|
|
|
|
for src in self.changes[rev].ren_files:
|
|
|
|
to = self.changes[rev].ren_files[src]
|
|
|
|
changes.append((src, rev))
|
|
|
|
changes.append((to, rev))
|
2009-01-05 16:34:20 +03:00
|
|
|
copies[to] = src
|
2008-02-05 11:30:08 +03:00
|
|
|
|
|
|
|
for src in self.changes[rev].ren_dirs:
|
|
|
|
to = self.changes[rev].ren_dirs[src]
|
2010-02-08 17:36:34 +03:00
|
|
|
chgs, cps = self._rendirchanges(src, to)
|
2008-02-05 11:30:08 +03:00
|
|
|
changes += [(f, rev) for f in chgs]
|
2009-01-05 16:34:20 +03:00
|
|
|
copies.update(cps)
|
2008-02-05 11:30:08 +03:00
|
|
|
|
|
|
|
self.lastrev = rev
|
2015-03-19 19:40:19 +03:00
|
|
|
return sorted(set(changes)), copies, set()
|
2008-02-05 11:30:08 +03:00
|
|
|
|
|
|
|
def getcommit(self, rev):
|
|
|
|
changes = self.changes[rev]
|
2016-03-02 11:58:01 +03:00
|
|
|
return common.commit(author=changes.author, date=changes.date,
|
|
|
|
desc=changes.summary, parents=self.parents[rev],
|
|
|
|
rev=rev)
|
2008-02-05 11:30:08 +03:00
|
|
|
|
|
|
|
def gettags(self):
|
|
|
|
return self.tags
|
|
|
|
|
|
|
|
def _execute(self, cmd, *args, **kwargs):
|
|
|
|
cmdline = [self.execmd, cmd]
|
|
|
|
cmdline += args
|
|
|
|
cmdline = [util.shellquote(arg) for arg in cmdline]
|
2012-08-04 09:14:40 +04:00
|
|
|
cmdline += ['>', os.devnull, '2>', os.devnull]
|
2008-02-05 11:30:08 +03:00
|
|
|
cmdline = util.quotecommand(' '.join(cmdline))
|
|
|
|
self.ui.debug(cmdline, '\n')
|
|
|
|
return os.system(cmdline)
|
|
|
|
|
|
|
|
def _update(self, rev):
|
2009-09-19 03:15:38 +04:00
|
|
|
self.ui.debug('applying revision %s...\n' % rev)
|
2009-01-04 04:36:48 +03:00
|
|
|
changeset, status = self.runlines('replay', '-d', self.tmppath,
|
|
|
|
rev)
|
|
|
|
if status:
|
|
|
|
# Something went wrong while merging (baz or tla
|
|
|
|
# issue?), get latest revision and try from there
|
|
|
|
shutil.rmtree(self.tmppath, ignore_errors=True)
|
2008-02-08 15:56:29 +03:00
|
|
|
self._obtainrevision(rev)
|
2008-02-05 11:30:08 +03:00
|
|
|
else:
|
2009-01-04 04:36:48 +03:00
|
|
|
old_rev = self.parents[rev][0]
|
2009-09-19 03:15:38 +04:00
|
|
|
self.ui.debug('computing changeset between %s and %s...\n'
|
2009-01-04 04:36:48 +03:00
|
|
|
% (old_rev, rev))
|
|
|
|
self._parsechangeset(changeset, rev)
|
2008-02-05 11:30:08 +03:00
|
|
|
|
|
|
|
def _getfile(self, name, rev):
|
|
|
|
mode = os.lstat(os.path.join(self.tmppath, name)).st_mode
|
|
|
|
if stat.S_ISLNK(mode):
|
|
|
|
data = os.readlink(os.path.join(self.tmppath, name))
|
2015-03-14 00:00:06 +03:00
|
|
|
if mode:
|
|
|
|
mode = 'l'
|
|
|
|
else:
|
|
|
|
mode = ''
|
2008-02-05 11:30:08 +03:00
|
|
|
else:
|
|
|
|
data = open(os.path.join(self.tmppath, name), 'rb').read()
|
2015-06-24 08:30:33 +03:00
|
|
|
mode = (mode & 0o111) and 'x' or ''
|
2008-02-05 11:30:08 +03:00
|
|
|
return data, mode
|
|
|
|
|
|
|
|
def _exclude(self, name):
|
2010-01-25 09:05:27 +03:00
|
|
|
exclude = ['{arch}', '.arch-ids', '.arch-inventory']
|
2008-02-05 11:30:08 +03:00
|
|
|
for exc in exclude:
|
|
|
|
if name.find(exc) != -1:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _readcontents(self, path):
|
|
|
|
files = []
|
|
|
|
contents = os.listdir(path)
|
|
|
|
while len(contents) > 0:
|
|
|
|
c = contents.pop()
|
|
|
|
p = os.path.join(path, c)
|
2008-02-07 14:31:29 +03:00
|
|
|
# os.walk could be used, but here we avoid internal GNU
|
|
|
|
# Arch files and directories, thus saving a lot time.
|
2008-02-05 11:30:08 +03:00
|
|
|
if not self._exclude(p):
|
|
|
|
if os.path.isdir(p):
|
|
|
|
contents += [os.path.join(c, f) for f in os.listdir(p)]
|
|
|
|
else:
|
|
|
|
files.append(c)
|
|
|
|
return files
|
|
|
|
|
|
|
|
def _rendirchanges(self, src, dest):
|
|
|
|
changes = []
|
|
|
|
copies = {}
|
|
|
|
files = self._readcontents(os.path.join(self.tmppath, dest))
|
|
|
|
for f in files:
|
|
|
|
s = os.path.join(src, f)
|
|
|
|
d = os.path.join(dest, f)
|
|
|
|
changes.append(s)
|
|
|
|
changes.append(d)
|
2009-01-05 16:34:20 +03:00
|
|
|
copies[d] = s
|
2008-02-05 11:30:08 +03:00
|
|
|
return changes, copies
|
|
|
|
|
2008-02-08 15:56:29 +03:00
|
|
|
def _obtainrevision(self, rev):
|
2009-09-19 03:15:38 +04:00
|
|
|
self.ui.debug('obtaining revision %s...\n' % rev)
|
2009-01-04 04:36:48 +03:00
|
|
|
output = self._execute('get', rev, self.tmppath)
|
2008-02-08 15:56:29 +03:00
|
|
|
self.checkexit(output)
|
2009-09-19 03:15:38 +04:00
|
|
|
self.ui.debug('analyzing revision %s...\n' % rev)
|
2008-02-08 15:56:29 +03:00
|
|
|
files = self._readcontents(self.tmppath)
|
|
|
|
self.changes[rev].add_files += files
|
|
|
|
|
2008-02-12 13:35:06 +03:00
|
|
|
def _stripbasepath(self, path):
|
|
|
|
if path.startswith('./'):
|
|
|
|
return path[2:]
|
|
|
|
return path
|
|
|
|
|
2008-02-05 11:30:08 +03:00
|
|
|
def _parsecatlog(self, data, rev):
|
2009-01-04 04:36:48 +03:00
|
|
|
try:
|
|
|
|
catlog = self.catlogparser.parsestr(data)
|
2009-01-04 20:44:39 +03:00
|
|
|
|
|
|
|
# Commit date
|
2009-01-04 04:36:48 +03:00
|
|
|
self.changes[rev].date = util.datestr(
|
|
|
|
util.strdate(catlog['Standard-date'],
|
|
|
|
'%Y-%m-%d %H:%M:%S'))
|
2009-01-04 20:44:39 +03:00
|
|
|
|
|
|
|
# Commit author
|
|
|
|
self.changes[rev].author = self.recode(catlog['Creator'])
|
|
|
|
|
|
|
|
# Commit description
|
|
|
|
self.changes[rev].summary = '\n\n'.join((catlog['Summary'],
|
|
|
|
catlog.get_payload()))
|
|
|
|
self.changes[rev].summary = self.recode(self.changes[rev].summary)
|
|
|
|
|
|
|
|
# Commit revision origin when dealing with a branch or tag
|
2009-08-24 23:00:34 +04:00
|
|
|
if 'Continuation-of' in catlog:
|
2010-01-25 09:05:27 +03:00
|
|
|
self.changes[rev].continuationof = self.recode(
|
|
|
|
catlog['Continuation-of'])
|
2009-03-23 15:13:06 +03:00
|
|
|
except Exception:
|
2015-10-08 22:55:45 +03:00
|
|
|
raise error.Abort(_('could not parse cat-log of %s') % rev)
|
2008-02-06 13:42:04 +03:00
|
|
|
|
2008-02-08 15:56:29 +03:00
|
|
|
def _parsechangeset(self, data, rev):
|
2008-02-05 11:30:08 +03:00
|
|
|
for l in data:
|
|
|
|
l = l.strip()
|
2008-02-09 19:36:42 +03:00
|
|
|
# Added file (ignore added directory)
|
2008-02-05 11:30:08 +03:00
|
|
|
if l.startswith('A') and not l.startswith('A/'):
|
2008-02-12 13:35:06 +03:00
|
|
|
file = self._stripbasepath(l[1:].strip())
|
2008-02-05 11:30:08 +03:00
|
|
|
if not self._exclude(file):
|
|
|
|
self.changes[rev].add_files.append(file)
|
2008-02-09 19:36:42 +03:00
|
|
|
# Deleted file (ignore deleted directory)
|
|
|
|
elif l.startswith('D') and not l.startswith('D/'):
|
2008-02-12 13:35:06 +03:00
|
|
|
file = self._stripbasepath(l[1:].strip())
|
2008-02-05 11:30:08 +03:00
|
|
|
if not self._exclude(file):
|
2008-02-09 19:36:42 +03:00
|
|
|
self.changes[rev].del_files.append(file)
|
|
|
|
# Modified binary file
|
|
|
|
elif l.startswith('Mb'):
|
2008-02-12 13:35:06 +03:00
|
|
|
file = self._stripbasepath(l[2:].strip())
|
2008-02-05 11:30:08 +03:00
|
|
|
if not self._exclude(file):
|
|
|
|
self.changes[rev].mod_files.append(file)
|
2008-02-09 19:36:42 +03:00
|
|
|
# Modified link
|
|
|
|
elif l.startswith('M->'):
|
2008-02-12 13:35:06 +03:00
|
|
|
file = self._stripbasepath(l[3:].strip())
|
2008-02-09 19:36:42 +03:00
|
|
|
if not self._exclude(file):
|
|
|
|
self.changes[rev].mod_files.append(file)
|
|
|
|
# Modified file
|
|
|
|
elif l.startswith('M'):
|
2008-02-12 13:35:06 +03:00
|
|
|
file = self._stripbasepath(l[1:].strip())
|
2008-02-05 11:30:08 +03:00
|
|
|
if not self._exclude(file):
|
2008-02-09 19:36:42 +03:00
|
|
|
self.changes[rev].mod_files.append(file)
|
|
|
|
# Renamed file (or link)
|
2008-02-05 11:30:08 +03:00
|
|
|
elif l.startswith('=>'):
|
|
|
|
files = l[2:].strip().split(' ')
|
|
|
|
if len(files) == 1:
|
|
|
|
files = l[2:].strip().split('\t')
|
2008-02-12 13:35:06 +03:00
|
|
|
src = self._stripbasepath(files[0])
|
|
|
|
dst = self._stripbasepath(files[1])
|
|
|
|
if not self._exclude(src) and not self._exclude(dst):
|
|
|
|
self.changes[rev].ren_files[src] = dst
|
2008-02-09 19:36:42 +03:00
|
|
|
# Conversion from file to link or from link to file (modified)
|
|
|
|
elif l.startswith('ch'):
|
2008-02-12 13:35:06 +03:00
|
|
|
file = self._stripbasepath(l[2:].strip())
|
2008-02-09 19:36:42 +03:00
|
|
|
if not self._exclude(file):
|
|
|
|
self.changes[rev].mod_files.append(file)
|
|
|
|
# Renamed directory
|
|
|
|
elif l.startswith('/>'):
|
|
|
|
dirs = l[2:].strip().split(' ')
|
|
|
|
if len(dirs) == 1:
|
|
|
|
dirs = l[2:].strip().split('\t')
|
2008-02-12 13:35:06 +03:00
|
|
|
src = self._stripbasepath(dirs[0])
|
|
|
|
dst = self._stripbasepath(dirs[1])
|
|
|
|
if not self._exclude(src) and not self._exclude(dst):
|
|
|
|
self.changes[rev].ren_dirs[src] = dst
|