1 # darcs.py - darcs support for the convert extension
3 # Copyright 2007-2009 Matt Mackall <mpm@selenic.com> and others
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2, incorporated herein by reference.
8 from common import NoRepo, checktool, commandline, commit, converter_source
9 from mercurial.i18n import _
10 from mercurial import util
11 import os, shutil, tempfile
13 # The naming drift of ElementTree is fun!
15 try: from xml.etree.cElementTree import ElementTree
17 try: from xml.etree.ElementTree import ElementTree
19 try: from elementtree.cElementTree import ElementTree
21 try: from elementtree.ElementTree import ElementTree
22 except ImportError: ElementTree = None
25 class darcs_source(converter_source, commandline):
26 def __init__(self, ui, path, rev=None):
27 converter_source.__init__(self, ui, path, rev=rev)
28 commandline.__init__(self, ui, 'darcs')
30 # check for _darcs, ElementTree, _darcs/inventory so that we can
31 # easily skip test-convert-darcs if ElementTree is not around
32 if not os.path.exists(os.path.join(path, '_darcs', 'inventories')):
33 raise NoRepo("%s does not look like a darcs repo" % path)
35 if not os.path.exists(os.path.join(path, '_darcs')):
36 raise NoRepo("%s does not look like a darcs repo" % path)
39 version = self.run0('--version').splitlines()[0].strip()
41 raise util.Abort(_('darcs version 2.1 or newer needed (found %r)') %
44 if ElementTree is None:
45 raise util.Abort(_("Python ElementTree module is not available"))
47 self.path = os.path.realpath(path)
55 self.tmppath = tempfile.mkdtemp(
56 prefix='convert-' + os.path.basename(self.path) + '-')
57 output, status = self.run('init', repodir=self.tmppath)
58 self.checkexit(status)
60 tree = self.xml('changes', xml_output=True, summary=True,
64 for elt in tree.findall('patch'):
65 node = elt.get('hash')
66 name = elt.findtext('name', '')
67 if name.startswith('TAG '):
68 tagname = name[4:].strip()
69 elif tagname is not None:
70 self.tags[tagname] = node
72 self.changes[node] = elt
73 self.parents[child] = [node]
75 self.parents[child] = []
78 self.ui.debug(_('cleaning up %s\n') % self.tmppath)
79 shutil.rmtree(self.tmppath, ignore_errors=True)
81 def xml(self, cmd, **kwargs):
83 fp = self._run(cmd, **kwargs)
85 self.checkexit(fp.close())
86 return etree.getroot()
89 return self.parents[None]
91 def getcommit(self, rev):
92 elt = self.changes[rev]
93 date = util.strdate(elt.get('local_date'), '%a %b %d %H:%M:%S %Z %Y')
94 desc = elt.findtext('name') + '\n' + elt.findtext('comment', '')
95 return commit(author=elt.get('author'), date=util.datestr(date),
96 desc=desc.strip(), parents=self.parents[rev])
99 output, status = self.run('pull', self.path, all=True,
100 match='hash %s' % rev,
101 no_test=True, no_posthook=True,
102 external_merge='/bin/false',
103 repodir=self.tmppath)
105 if output.find('We have conflicts in') == -1:
106 self.checkexit(status, output)
107 output, status = self.run('revert', all=True, repodir=self.tmppath)
108 self.checkexit(status, output)
110 def getchanges(self, rev):
114 for elt in self.changes[rev].find('summary').getchildren():
115 if elt.tag in ('add_directory', 'remove_directory'):
117 if elt.tag == 'move':
118 changes.append((elt.get('from'), rev))
119 copies[elt.get('from')] = elt.get('to')
121 changes.append((elt.text.strip(), rev))
123 return sorted(changes), copies
125 def getfile(self, name, rev):
126 if rev != self.lastrev:
127 raise util.Abort(_('internal calling inconsistency'))
128 return open(os.path.join(self.tmppath, name), 'rb').read()
130 def getmode(self, name, rev):
131 mode = os.lstat(os.path.join(self.tmppath, name)).st_mode
132 return (mode & 0111) and 'x' or ''