hgext/convert/bzr.py
author Tobias Bell <tobias.bell@gmail.com>
Fri, 27 Mar 2009 18:17:24 +0100
changeset 7900 5c9c87ec4693
parent 7060 972cce34f345
child 7973 db3a68fd9387
child 8035 cb77c0fbec39
permissions -rw-r--r--
i18n: completed German translation of the children extension
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7053
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
     1
# bzr support for the convert extension
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
     2
# This module is for handling 'bzr', that was formerly known as Bazaar-NG;
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
     3
# it cannot access 'bar' repositories, but they were never used very much
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
     4
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
     5
import os
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
     6
from mercurial import demandimport
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
     7
# these do not work with demandimport, blacklist
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
     8
demandimport.ignore.extend([
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
     9
        'bzrlib.transactions',
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    10
        'bzrlib.urlutils',
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    11
    ])
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    12
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    13
from mercurial.i18n import _
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    14
from mercurial import util
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    15
from common import NoRepo, commit, converter_source
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    16
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    17
try:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    18
    # bazaar imports
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    19
    from bzrlib import branch, revision, errors
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    20
    from bzrlib.revisionspec import RevisionSpec
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    21
except ImportError:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    22
    pass
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    23
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    24
class bzr_source(converter_source):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    25
    """Reads Bazaar repositories by using the Bazaar Python libraries"""
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    26
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    27
    def __init__(self, ui, path, rev=None):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    28
        super(bzr_source, self).__init__(ui, path, rev=rev)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    29
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    30
        try:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    31
            # access bzrlib stuff
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    32
            branch
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    33
        except NameError:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    34
            raise NoRepo('Bazaar modules could not be loaded')
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    35
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    36
        if not os.path.exists(os.path.join(path, '.bzr')):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    37
            raise NoRepo('%s does not look like a Bazaar repo' % path)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    38
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    39
        path = os.path.abspath(path)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    40
        self.branch = branch.Branch.open(path)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    41
        self.sourcerepo = self.branch.repository
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    42
        self._parentids = {}
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    43
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    44
    def before(self):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    45
        """Before the conversion begins, acquire a read lock
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    46
        for all the operations that might need it. Fortunately
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    47
        read locks don't block other reads or writes to the
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    48
        repository, so this shouldn't have any impact on the usage of
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    49
        the source repository.
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    50
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    51
        The alternative would be locking on every operation that
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    52
        needs locks (there are currently two: getting the file and
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    53
        getting the parent map) and releasing immediately after,
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    54
        but this approach can take even 40% longer."""
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    55
        self.sourcerepo.lock_read()
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    56
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    57
    def after(self):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    58
        self.sourcerepo.unlock()
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    59
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    60
    def getheads(self):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    61
        if not self.rev:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    62
            return [self.branch.last_revision()]
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    63
        try:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    64
            r = RevisionSpec.from_string(self.rev)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    65
            info = r.in_history(self.branch)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    66
        except errors.BzrError:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    67
            raise util.Abort(_('%s is not a valid revision in current branch')
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    68
                             % self.rev)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    69
        return [info.rev_id]
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    70
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    71
    def getfile(self, name, rev):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    72
        revtree = self.sourcerepo.revision_tree(rev)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    73
        fileid = revtree.path2id(name)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    74
        if fileid is None:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    75
            # the file is not available anymore - was deleted
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    76
            raise IOError(_('%s is not available in %s anymore') %
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    77
                    (name, rev))
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    78
        sio = revtree.get_file(fileid)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    79
        return sio.read()
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    80
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    81
    def getmode(self, name, rev):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    82
        return self._modecache[(name, rev)]
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    83
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    84
    def getchanges(self, version):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    85
        # set up caches: modecache and revtree
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    86
        self._modecache = {}
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    87
        self._revtree = self.sourcerepo.revision_tree(version)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    88
        # get the parentids from the cache
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    89
        parentids = self._parentids.pop(version)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    90
        # only diff against first parent id
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    91
        prevtree = self.sourcerepo.revision_tree(parentids[0])
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    92
        return self._gettreechanges(self._revtree, prevtree)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    93
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    94
    def getcommit(self, version):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    95
        rev = self.sourcerepo.get_revision(version)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    96
        # populate parent id cache
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    97
        if not rev.parent_ids:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    98
            parents = []
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
    99
            self._parentids[version] = (revision.NULL_REVISION,)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   100
        else:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   101
            parents = self._filterghosts(rev.parent_ids)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   102
            self._parentids[version] = parents
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   103
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   104
        return commit(parents=parents,
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   105
                # bzr uses 1 second timezone precision
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   106
                date='%d %d' % (rev.timestamp, rev.timezone / 3600),
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   107
                author=self.recode(rev.committer),
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   108
                # bzr returns bytestrings or unicode, depending on the content
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   109
                desc=self.recode(rev.message),
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   110
                rev=version)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   111
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   112
    def gettags(self):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   113
        if not self.branch.supports_tags():
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   114
            return {}
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   115
        tagdict = self.branch.tags.get_tag_dict()
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   116
        bytetags = {}
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   117
        for name, rev in tagdict.iteritems():
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   118
            bytetags[self.recode(name)] = rev
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   119
        return bytetags
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   120
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   121
    def getchangedfiles(self, rev, i):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   122
        self._modecache = {}
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   123
        curtree = self.sourcerepo.revision_tree(rev)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   124
        parentids = self._parentids.pop(rev)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   125
        if i is not None:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   126
            parentid = parentids[i]
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   127
        else:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   128
            # no parent id, get the empty revision
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   129
            parentid = revision.NULL_REVISION
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   130
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   131
        prevtree = self.sourcerepo.revision_tree(parentid)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   132
        changes = [e[0] for e in self._gettreechanges(curtree, prevtree)[0]]
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   133
        return changes
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   134
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   135
    def _gettreechanges(self, current, origin):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   136
        revid = current._revision_id;
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   137
        changes = []
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   138
        renames = {}
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   139
        for (fileid, paths, changed_content, versioned, parent, name,
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   140
            kind, executable) in current.iter_changes(origin):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   141
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   142
            if paths[0] == u'' or paths[1] == u'':
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   143
                # ignore changes to tree root
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   144
                continue
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   145
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   146
            # bazaar tracks directories, mercurial does not, so
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   147
            # we have to rename the directory contents
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   148
            if kind[1] == 'directory':
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   149
                if None not in paths and paths[0] != paths[1]:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   150
                    # neither an add nor an delete - a move
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   151
                    # rename all directory contents manually
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   152
                    subdir = origin.inventory.path2id(paths[0])
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   153
                    # get all child-entries of the directory
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   154
                    for name, entry in origin.inventory.iter_entries(subdir):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   155
                        # hg does not track directory renames
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   156
                        if entry.kind == 'directory':
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   157
                            continue
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   158
                        frompath = self.recode(paths[0] + '/' + name)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   159
                        topath = self.recode(paths[1] + '/' + name)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   160
                        # register the files as changed
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   161
                        changes.append((frompath, revid))
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   162
                        changes.append((topath, revid))
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   163
                        # add to mode cache
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   164
                        mode = ((entry.executable and 'x') or (entry.kind == 'symlink' and 's')
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   165
                                or '')
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   166
                        self._modecache[(topath, revid)] = mode
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   167
                        # register the change as move
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   168
                        renames[topath] = frompath
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   169
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   170
                # no futher changes, go to the next change
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   171
                continue
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   172
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   173
            # we got unicode paths, need to convert them
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   174
            path, topath = [self.recode(part) for part in paths]
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   175
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   176
            if topath is None:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   177
                # file deleted
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   178
                changes.append((path, revid))
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   179
                continue
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   180
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   181
            # renamed
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   182
            if path and path != topath:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   183
                renames[topath] = path
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   184
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   185
            # populate the mode cache
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   186
            kind, executable = [e[1] for e in (kind, executable)]
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   187
            mode = ((executable and 'x') or (kind == 'symlink' and 's')
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   188
                    or '')
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   189
            self._modecache[(topath, revid)] = mode
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   190
            changes.append((topath, revid))
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   191
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   192
        return changes, renames
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   193
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   194
    def _filterghosts(self, ids):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   195
        """Filters out ghost revisions which hg does not support, see
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   196
        <http://bazaar-vcs.org/GhostRevision>
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   197
        """
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   198
        parentmap = self.sourcerepo.get_parent_map(ids)
7060
972cce34f345 convert: fixed python2.3 incompatibility in bzr source (generator expression)
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 7053
diff changeset
   199
        parents = tuple([parent for parent in ids if parent in parentmap])
7053
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   200
        return parents
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   201
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   202
    def recode(self, s, encoding=None):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   203
        """This version of recode tries to encode unicode to bytecode,
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   204
        and preferably using the UTF-8 codec.
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   205
        Other types than Unicode are silently returned, this is by
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   206
        intention, e.g. the None-type is not going to be encoded but instead
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   207
        just passed through
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   208
        """
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   209
        if not encoding:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   210
            encoding = self.encoding or 'utf-8'
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   211
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   212
        if isinstance(s, unicode):
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   213
            return s.encode(encoding)
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   214
        else:
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   215
            # leave it alone
209ef5f3534c convert: add bzr source
Marek Kubica <marek@xivilization.net>
parents:
diff changeset
   216
            return s