hgext/convert/darcs.py
author Christian Ebert <blacktrash@gmx.net>
Fri, 04 Sep 2009 11:51:28 +0200
changeset 9421 c8e4dc218aaf
parent 9242 6fd3f795e908
child 9467 4c041f1ee1b4
child 9526 457a30ed8ffb
permissions -rw-r--r--
log: prevent negative date range from displaying entire log (issue1805)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8250
1b60efdb8bc5 convert: add copyright and license headers to back-ends
Martin Geisler <mg@lazybytes.net>
parents: 8209
diff changeset
     1
# darcs.py - darcs support for the convert extension
1b60efdb8bc5 convert: add copyright and license headers to back-ends
Martin Geisler <mg@lazybytes.net>
parents: 8209
diff changeset
     2
#
1b60efdb8bc5 convert: add copyright and license headers to back-ends
Martin Geisler <mg@lazybytes.net>
parents: 8209
diff changeset
     3
#  Copyright 2007-2009 Matt Mackall <mpm@selenic.com> and others
1b60efdb8bc5 convert: add copyright and license headers to back-ends
Martin Geisler <mg@lazybytes.net>
parents: 8209
diff changeset
     4
#
1b60efdb8bc5 convert: add copyright and license headers to back-ends
Martin Geisler <mg@lazybytes.net>
parents: 8209
diff changeset
     5
# This software may be used and distributed according to the terms of the
1b60efdb8bc5 convert: add copyright and license headers to back-ends
Martin Geisler <mg@lazybytes.net>
parents: 8209
diff changeset
     6
# GNU General Public License version 2, incorporated herein by reference.
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     7
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
     8
from common import NoRepo, checktool, commandline, commit, converter_source
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     9
from mercurial.i18n import _
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    10
from mercurial import util
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    11
import os, shutil, tempfile
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    12
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    13
# The naming drift of ElementTree is fun!
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    14
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    15
try: from xml.etree.cElementTree import ElementTree
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    16
except ImportError:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    17
    try: from xml.etree.ElementTree import ElementTree
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    18
    except ImportError:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    19
        try: from elementtree.cElementTree import ElementTree
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    20
        except ImportError:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    21
            try: from elementtree.ElementTree import ElementTree
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    22
            except ImportError: ElementTree = None
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    23
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    24
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    25
class darcs_source(converter_source, commandline):
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    26
    def __init__(self, ui, path, rev=None):
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    27
        converter_source.__init__(self, ui, path, rev=rev)
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    28
        commandline.__init__(self, ui, 'darcs')
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    29
5520
cc3af86ab6fe test-convert-darcs: skip if we can't find the elementtree module
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5481
diff changeset
    30
        # check for _darcs, ElementTree, _darcs/inventory so that we can
cc3af86ab6fe test-convert-darcs: skip if we can't find the elementtree module
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5481
diff changeset
    31
        # easily skip test-convert-darcs if ElementTree is not around
7973
db3a68fd9387 convert: attempt to check repo type before checking for tool
Matt Mackall <mpm@selenic.com>
parents: 7609
diff changeset
    32
        if not os.path.exists(os.path.join(path, '_darcs', 'inventories')):
db3a68fd9387 convert: attempt to check repo type before checking for tool
Matt Mackall <mpm@selenic.com>
parents: 7609
diff changeset
    33
            raise NoRepo("%s does not look like a darcs repo" % path)
db3a68fd9387 convert: attempt to check repo type before checking for tool
Matt Mackall <mpm@selenic.com>
parents: 7609
diff changeset
    34
5520
cc3af86ab6fe test-convert-darcs: skip if we can't find the elementtree module
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5481
diff changeset
    35
        if not os.path.exists(os.path.join(path, '_darcs')):
5521
03496d4fa509 convert: display all errors if we couldn't open the source repo
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5520
diff changeset
    36
            raise NoRepo("%s does not look like a darcs repo" % path)
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    37
5497
f0a3918abd42 convert: fail if an external required tool is not found
Patrick Mezard <pmezard@gmail.com>
parents: 5412
diff changeset
    38
        checktool('darcs')
9242
6fd3f795e908 issue1251: bail if darcs version is too old
Bryan O'Sullivan <bos@serpentine.com>
parents: 8250
diff changeset
    39
        version = self.run0('--version').splitlines()[0].strip()
6fd3f795e908 issue1251: bail if darcs version is too old
Bryan O'Sullivan <bos@serpentine.com>
parents: 8250
diff changeset
    40
        if version < '2.1':
6fd3f795e908 issue1251: bail if darcs version is too old
Bryan O'Sullivan <bos@serpentine.com>
parents: 8250
diff changeset
    41
            raise util.Abort(_('darcs version 2.1 or newer needed (found %r)') %
6fd3f795e908 issue1251: bail if darcs version is too old
Bryan O'Sullivan <bos@serpentine.com>
parents: 8250
diff changeset
    42
                             version)
5497
f0a3918abd42 convert: fail if an external required tool is not found
Patrick Mezard <pmezard@gmail.com>
parents: 5412
diff changeset
    43
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    44
        if ElementTree is None:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    45
            raise util.Abort(_("Python ElementTree module is not available"))
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    46
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    47
        self.path = os.path.realpath(path)
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    48
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    49
        self.lastrev = None
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    50
        self.changes = {}
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    51
        self.parents = {}
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    52
        self.tags = {}
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    53
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    54
    def before(self):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    55
        self.tmppath = tempfile.mkdtemp(
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    56
            prefix='convert-' + os.path.basename(self.path) + '-')
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    57
        output, status = self.run('init', repodir=self.tmppath)
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    58
        self.checkexit(status)
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    59
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    60
        tree = self.xml('changes', xml_output=True, summary=True,
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    61
                        repodir=self.path)
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    62
        tagname = None
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    63
        child = None
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    64
        for elt in tree.findall('patch'):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    65
            node = elt.get('hash')
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    66
            name = elt.findtext('name', '')
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    67
            if name.startswith('TAG '):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    68
                tagname = name[4:].strip()
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    69
            elif tagname is not None:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    70
                self.tags[tagname] = node
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    71
                tagname = None
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    72
            self.changes[node] = elt
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    73
            self.parents[child] = [node]
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    74
            child = node
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    75
        self.parents[child] = []
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    76
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    77
    def after(self):
6956
12472a240398 i18n: mark strings for translation in convert extension
Martin Geisler <mg@daimi.au.dk>
parents: 6762
diff changeset
    78
        self.ui.debug(_('cleaning up %s\n') % self.tmppath)
5362
4ad2a18aff42 convert: fix a few residual bugs in darcs importer
Bryan O'Sullivan <bos@serpentine.com>
parents: 5359
diff changeset
    79
        shutil.rmtree(self.tmppath, ignore_errors=True)
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    80
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    81
    def xml(self, cmd, **kwargs):
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    82
        etree = ElementTree()
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    83
        fp = self._run(cmd, **kwargs)
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    84
        etree.parse(fp)
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    85
        self.checkexit(fp.close())
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    86
        return etree.getroot()
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    87
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    88
    def getheads(self):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    89
        return self.parents[None]
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    90
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    91
    def getcommit(self, rev):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    92
        elt = self.changes[rev]
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    93
        date = util.strdate(elt.get('local_date'), '%a %b %d %H:%M:%S %Z %Y')
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    94
        desc = elt.findtext('name') + '\n' + elt.findtext('comment', '')
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    95
        return commit(author=elt.get('author'), date=util.datestr(date),
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    96
                      desc=desc.strip(), parents=self.parents[rev])
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    97
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    98
    def pull(self, rev):
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    99
        output, status = self.run('pull', self.path, all=True,
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
   100
                                  match='hash %s' % rev,
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
   101
                                  no_test=True, no_posthook=True,
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
   102
                                  external_merge='/bin/false',
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   103
                                  repodir=self.tmppath)
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   104
        if status:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   105
            if output.find('We have conflicts in') == -1:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   106
                self.checkexit(status, output)
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
   107
            output, status = self.run('revert', all=True, repodir=self.tmppath)
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   108
            self.checkexit(status, output)
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   109
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   110
    def getchanges(self, rev):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   111
        self.pull(rev)
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   112
        copies = {}
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   113
        changes = []
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   114
        for elt in self.changes[rev].find('summary').getchildren():
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   115
            if elt.tag in ('add_directory', 'remove_directory'):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   116
                continue
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   117
            if elt.tag == 'move':
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   118
                changes.append((elt.get('from'), rev))
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   119
                copies[elt.get('from')] = elt.get('to')
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   120
            else:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   121
                changes.append((elt.text.strip(), rev))
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   122
        self.lastrev = rev
8209
a1a5a57efe90 replace util.sort with sorted built-in
Matt Mackall <mpm@selenic.com>
parents: 7973
diff changeset
   123
        return sorted(changes), copies
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   124
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   125
    def getfile(self, name, rev):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   126
        if rev != self.lastrev:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   127
            raise util.Abort(_('internal calling inconsistency'))
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   128
        return open(os.path.join(self.tmppath, name), 'rb').read()
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   129
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   130
    def getmode(self, name, rev):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   131
        mode = os.lstat(os.path.join(self.tmppath, name)).st_mode
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   132
        return (mode & 0111) and 'x' or ''
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   133
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   134
    def gettags(self):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   135
        return self.tags