| 1 |
# darcs.py - darcs support for the convert extension |
| 2 |
# |
| 3 |
# Copyright 2007-2009 Olivia Mackall <[email protected]> and others |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import os |
| 11 |
import re |
| 12 |
import shutil |
| 13 |
from xml.etree.ElementTree import ( |
| 14 |
ElementTree, |
| 15 |
XMLParser, |
| 16 |
) |
| 17 |
|
| 18 |
from mercurial.i18n import _ |
| 19 |
from mercurial import ( |
| 20 |
error, |
| 21 |
pycompat, |
| 22 |
util, |
| 23 |
) |
| 24 |
from mercurial.utils import dateutil |
| 25 |
from . import common |
| 26 |
|
| 27 |
NoRepo = common.NoRepo |
| 28 |
|
| 29 |
|
| 30 |
class darcs_source(common.converter_source, common.commandline): |
| 31 |
def __init__(self, ui, repotype, path, revs=None): |
| 32 |
common.converter_source.__init__(self, ui, repotype, path, revs=revs) |
| 33 |
common.commandline.__init__(self, ui, b'darcs') |
| 34 |
|
| 35 |
# check for _darcs, ElementTree so that we can easily skip |
| 36 |
# test-convert-darcs if ElementTree is not around |
| 37 |
if not os.path.exists(os.path.join(path, b'_darcs')): |
| 38 |
raise NoRepo(_(b"%s does not look like a darcs repository") % path) |
| 39 |
|
| •40 |
common.checktool(b'darcs', debname=b'darcs') |
| 41 |
version = self.run0(b'--version').splitlines()[0].strip() |
| 42 |
if version < b'2.1': |
| 43 |
raise error.Abort( |
| 44 |
_(b'darcs version 2.1 or newer needed (found %r)') % version |
| 45 |
) |
| 46 |
|
| 47 |
if "ElementTree" not in globals(): |
| 48 |
raise error.Abort(_(b"Python ElementTree module is not available")) |
| 49 |
|
| 50 |
self.path = os.path.realpath(path) |
| 51 |
|
| 52 |
self.lastrev = None |
| 53 |
self.changes = {} |
| 54 |
self.parents = {} |
| 55 |
self.tags = {} |
| 56 |
|
| 57 |
# Check darcs repository format |
| 58 |
format = self.format() |
| 59 |
if format: |
| 60 |
if format in (b'darcs-1.0', b'hashed'): |
| 61 |
raise NoRepo( |
| 62 |
_( |
| 63 |
b"%s repository format is unsupported, " |
| 64 |
b"please upgrade" |
| 65 |
) |
| 66 |
% format |
| 67 |
) |
| 68 |
else: |
| 69 |
self.ui.warn(_(b'failed to detect repository format!')) |
| 70 |
|
| 71 |
def before(self): |
| 72 |
self.tmppath = pycompat.mkdtemp( |
| 73 |
prefix=b'convert-' + os.path.basename(self.path) + b'-' |
| 74 |
) |
| 75 |
output, status = self.run(b'init', repodir=self.tmppath) |
| 76 |
self.checkexit(status) |
| 77 |
|
| 78 |
tree = self.xml( |
| 79 |
b'changes', xml_output=True, summary=True, repodir=self.path |
| 80 |
) |
| 81 |
tagname = None |
| 82 |
child = None |
| 83 |
for elt in tree.findall('patch'): |
| 84 |
node = self.recode(elt.get('hash')) |
| 85 |
name = self.recode(elt.findtext('name', '')) |
| 86 |
if name.startswith(b'TAG '): |
| 87 |
tagname = name[4:].strip() |
| 88 |
elif tagname is not None: |
| 89 |
self.tags[tagname] = node |
| 90 |
tagname = None |
| 91 |
self.changes[node] = elt |
| 92 |
self.parents[child] = [node] |
| 93 |
child = node |
| 94 |
self.parents[child] = [] |
| 95 |
|
| 96 |
def after(self): |
| 97 |
self.ui.debug(b'cleaning up %s\n' % self.tmppath) |
| 98 |
shutil.rmtree(self.tmppath, ignore_errors=True) |
| 99 |
|
| 100 |
def recode(self, s, encoding=None): |
| 101 |
if isinstance(s, str): |
| 102 |
# XMLParser returns unicode objects for anything it can't |
| 103 |
# encode into ASCII. We convert them back to str to get |
| 104 |
# recode's normal conversion behavior. |
| 105 |
s = s.encode('latin-1') |
| 106 |
return super().recode(s, encoding) |
| 107 |
|
| 108 |
def xml(self, cmd, **kwargs): |
| 109 |
# NOTE: darcs is currently encoding agnostic and will print |
| 110 |
# patch metadata byte-for-byte, even in the XML changelog. |
| 111 |
etree = ElementTree() |
| 112 |
# While we are decoding the XML as latin-1 to be as liberal as |
| 113 |
# possible, etree will still raise an exception if any |
| 114 |
# non-printable characters are in the XML changelog. |
| 115 |
parser = XMLParser(encoding='latin-1') |
| 116 |
p = self._run(cmd, **kwargs) |
| 117 |
etree.parse(p.stdout, parser=parser) |
| 118 |
p.wait() |
| 119 |
self.checkexit(p.returncode) |
| 120 |
return etree.getroot() |
| 121 |
|
| 122 |
def format(self): |
| 123 |
output, status = self.run(b'show', b'repo', repodir=self.path) |
| 124 |
self.checkexit(status) |
| 125 |
m = re.search(br'^\s*Format:\s*(.*)$', output, re.MULTILINE) |
| 126 |
if not m: |
| 127 |
return None |
| 128 |
return b','.join(sorted(f.strip() for f in m.group(1).split(b','))) |
| 129 |
|
| 130 |
def manifest(self): |
| 131 |
man = [] |
| 132 |
output, status = self.run( |
| 133 |
b'show', b'files', no_directories=True, repodir=self.tmppath |
| 134 |
) |
| 135 |
self.checkexit(status) |
| 136 |
for line in output.split(b'\n'): |
| 137 |
path = line[2:] |
| 138 |
if path: |
| 139 |
man.append(path) |
| 140 |
return man |
| 141 |
|
| 142 |
def getheads(self): |
| 143 |
return self.parents[None] |
| 144 |
|
| 145 |
def getcommit(self, rev): |
| 146 |
elt = self.changes[rev] |
| 147 |
dateformat = b'%a %b %d %H:%M:%S %Z %Y' |
| 148 |
date = dateutil.strdate(self.recode(elt.get('local_date')), dateformat) |
| 149 |
desc = elt.findtext('name') + '\n' + elt.findtext('comment', '') |
| 150 |
# etree can return unicode objects for name, comment, and author, |
| 151 |
# so recode() is used to ensure str objects are emitted. |
| 152 |
newdateformat = b'%Y-%m-%d %H:%M:%S %1%2' |
| 153 |
return common.commit( |
| 154 |
author=self.recode(elt.get('author')), |
| 155 |
date=dateutil.datestr(date, newdateformat), |
| 156 |
desc=self.recode(desc).strip(), |
| 157 |
parents=self.parents[rev], |
| 158 |
) |
| 159 |
|
| 160 |
def pull(self, rev): |
| 161 |
output, status = self.run( |
| 162 |
b'pull', |
| 163 |
self.path, |
| 164 |
all=True, |
| 165 |
match=b'hash %s' % self.recode(rev), |
| 166 |
no_test=True, |
| 167 |
no_posthook=True, |
| 168 |
external_merge=b'/bin/false', |
| 169 |
repodir=self.tmppath, |
| 170 |
) |
| 171 |
if status: |
| 172 |
if output.find(b'We have conflicts in') == -1: |
| 173 |
self.checkexit(status, output) |
| 174 |
output, status = self.run(b'revert', all=True, repodir=self.tmppath) |
| 175 |
self.checkexit(status, output) |
| 176 |
|
| 177 |
def getchanges(self, rev, full): |
| 178 |
if full: |
| 179 |
raise error.Abort(_(b"convert from darcs does not support --full")) |
| 180 |
copies = {} |
| 181 |
changes = [] |
| 182 |
man = None |
| 183 |
for elt in self.changes[rev].find('summary'): |
| 184 |
if elt.tag in ('add_directory', 'remove_directory'): |
| 185 |
continue |
| 186 |
if elt.tag == 'move': |
| 187 |
if man is None: |
| 188 |
man = self.manifest() |
| 189 |
source = self.recode(elt.get('from')) |
| 190 |
dest = self.recode(elt.get('to')) |
| 191 |
if source in man: |
| 192 |
# File move |
| 193 |
changes.append((source, rev)) |
| 194 |
changes.append((dest, rev)) |
| 195 |
copies[dest] = source |
| 196 |
else: |
| 197 |
# Directory move, deduce file moves from manifest |
| 198 |
source = source + b'/' |
| 199 |
for f in man: |
| 200 |
if not f.startswith(source): |
| 201 |
continue |
| 202 |
fdest = dest + b'/' + f[len(source) :] |
| 203 |
changes.append((f, rev)) |
| 204 |
changes.append((fdest, rev)) |
| 205 |
copies[fdest] = f |
| 206 |
else: |
| 207 |
changes.append((self.recode(elt.text.strip()), rev)) |
| 208 |
self.pull(rev) |
| 209 |
self.lastrev = rev |
| 210 |
return sorted(changes), copies, set() |
| 211 |
|
| 212 |
def getfile(self, name, rev): |
| 213 |
if rev != self.lastrev: |
| 214 |
raise error.Abort(_(b'internal calling inconsistency')) |
| 215 |
path = os.path.join(self.tmppath, name) |
| 216 |
try: |
| 217 |
data = util.readfile(path) |
| 218 |
mode = os.lstat(path).st_mode |
| 219 |
except FileNotFoundError: |
| 220 |
return None, None |
| 221 |
mode = (mode & 0o111) and b'x' or b'' |
| 222 |
return data, mode |
| 223 |
|
| 224 |
def gettags(self): |
| 225 |
return self.tags |