hgext/convert/darcs.py
author Dirkjan Ochtman <dirkjan@ochtman.nl>
Thu, 28 Feb 2008 19:08:04 +0100
changeset 6182 07f2236c4dee
parent 5528 6ffca2bf23da
child 6762 f67d1468ac50
permissions -rw-r--r--
add a blame alias for annotate The original cvs command was called blame. SVN has both, and other VC systems typically also have this as an alias. Makes things easier for converts.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     1
# darcs support for the convert extension
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     2
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
     3
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
     4
from mercurial.i18n import _
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     5
from mercurial import util
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     6
import os, shutil, tempfile
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     7
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     8
# The naming drift of ElementTree is fun!
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
     9
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    10
try: from xml.etree.cElementTree import ElementTree
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    11
except ImportError:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    12
    try: from xml.etree.ElementTree import ElementTree
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    13
    except ImportError:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    14
        try: from elementtree.cElementTree import ElementTree
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    15
        except ImportError:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    16
            try: from elementtree.ElementTree import ElementTree
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    17
            except ImportError: ElementTree = None
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    18
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    19
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    20
class darcs_source(converter_source, commandline):
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    21
    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
    22
        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
    23
        commandline.__init__(self, ui, 'darcs')
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    24
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
    25
        # 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
    26
        # easily skip test-convert-darcs if ElementTree is not around
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
    27
        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
    28
            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
    29
5497
f0a3918abd42 convert: fail if an external required tool is not found
Patrick Mezard <pmezard@gmail.com>
parents: 5412
diff changeset
    30
        checktool('darcs')
f0a3918abd42 convert: fail if an external required tool is not found
Patrick Mezard <pmezard@gmail.com>
parents: 5412
diff changeset
    31
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    32
        if ElementTree is None:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    33
            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
    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', 'inventory')):
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)
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
    37
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    38
        self.path = os.path.realpath(path)
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    39
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    40
        self.lastrev = None
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    41
        self.changes = {}
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    42
        self.parents = {}
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    43
        self.tags = {}
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    44
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    45
    def before(self):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    46
        self.tmppath = tempfile.mkdtemp(
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    47
            prefix='convert-' + os.path.basename(self.path) + '-')
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    48
        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
    49
        self.checkexit(status)
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    50
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    51
        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
    52
                        repodir=self.path)
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    53
        tagname = None
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    54
        child = None
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    55
        for elt in tree.findall('patch'):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    56
            node = elt.get('hash')
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    57
            name = elt.findtext('name', '')
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    58
            if name.startswith('TAG '):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    59
                tagname = name[4:].strip()
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    60
            elif tagname is not None:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    61
                self.tags[tagname] = node
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
            self.changes[node] = elt
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    64
            self.parents[child] = [node]
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    65
            child = node
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    66
        self.parents[child] = []
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    67
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    68
    def after(self):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    69
        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
    70
        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
    71
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    72
    def xml(self, cmd, **kwargs):
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    73
        etree = ElementTree()
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    74
        fp = self._run(cmd, **kwargs)
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    75
        etree.parse(fp)
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    76
        self.checkexit(fp.close())
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    77
        return etree.getroot()
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    78
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    79
    def getheads(self):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    80
        return self.parents[None]
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    81
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    82
    def getcommit(self, rev):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    83
        elt = self.changes[rev]
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    84
        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
    85
        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
    86
        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
    87
                      desc=desc.strip(), parents=self.parents[rev])
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    88
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    89
    def pull(self, rev):
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    90
        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
    91
                                  match='hash %s' % rev,
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    92
                                  no_test=True, no_posthook=True,
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    93
                                  external_merge='/bin/false',
5359
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    94
                                  repodir=self.tmppath)
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    95
        if status:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
    96
            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
    97
                self.checkexit(status, output)
5512
8cd26ccc68f8 convert: abstract darcs's commandline handling
Bryan O'Sullivan <bos@serpentine.com>
parents: 5498
diff changeset
    98
            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
    99
            self.checkexit(status, output)
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   100
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   101
    def getchanges(self, rev):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   102
        self.pull(rev)
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   103
        copies = {}
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   104
        changes = []
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   105
        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
   106
            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
   107
                continue
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   108
            if elt.tag == 'move':
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   109
                changes.append((elt.get('from'), rev))
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   110
                copies[elt.get('from')] = elt.get('to')
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   111
            else:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   112
                changes.append((elt.text.strip(), rev))
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   113
        changes.sort()
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   114
        self.lastrev = rev
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   115
        return changes, copies
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   116
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   117
    def getfile(self, name, rev):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   118
        if rev != self.lastrev:
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   119
            raise util.Abort(_('internal calling inconsistency'))
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   120
        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
   121
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   122
    def getmode(self, name, rev):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   123
        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
   124
        return (mode & 0111) and 'x' or ''
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   125
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   126
    def gettags(self):
6b6104430964 convert: support darcs as a source repo
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff changeset
   127
        return self.tags