/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/rewrite/maptree.py

[merge] robertc's integration, updated tests to check for retcode=3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2007 by Jelmer Vernooij
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
"""Map Tree."""
17
 
 
18
 
from __future__ import absolute_import
19
 
 
20
 
def map_file_ids(repository, old_parents, new_parents):
21
 
    """Try to determine the equivalent file ids in two sets of parents.
22
 
 
23
 
    :param repository: Repository to use
24
 
    :param old_parents: List of revision ids of old parents
25
 
    :param new_parents: List of revision ids of new parents
26
 
    """
27
 
    assert len(old_parents) == len(new_parents)
28
 
    ret = {}
29
 
    for (oldp, newp) in zip(old_parents, new_parents):
30
 
        oldtree = repository.revision_tree(oldp)
31
 
        newtree = repository.revision_tree(newp)
32
 
        for path, ie in oldtree.iter_entries_by_dir():
33
 
            file_id = newtree.path2id(path)
34
 
            if file_id is not None:
35
 
                ret[ie.file_id] = file_id
36
 
    return ret
37
 
 
38
 
 
39
 
class MapTree(object):
40
 
    """Wrapper around a tree that translates file ids.
41
 
    """
42
 
 
43
 
    def __init__(self, oldtree, fileid_map):
44
 
        """Create a new MapTree.
45
 
 
46
 
        :param oldtree: Old tree to map to.
47
 
        :param fileid_map: Map with old -> new file ids.
48
 
        """
49
 
        self.oldtree = oldtree
50
 
        self.map = fileid_map
51
 
 
52
 
    def old_id(self, file_id):
53
 
        """Look up the original file id of a file.
54
 
 
55
 
        :param file_id: New file id
56
 
        :return: Old file id if mapped, otherwise new file id
57
 
        """
58
 
        for x in self.map:
59
 
            if self.map[x] == file_id:
60
 
                return x
61
 
        return file_id
62
 
 
63
 
    def new_id(self, file_id):
64
 
        """Look up the new file id of a file.
65
 
 
66
 
        :param file_id: Old file id
67
 
        :return: New file id
68
 
        """
69
 
        try:
70
 
            return self.map[file_id]
71
 
        except KeyError:
72
 
            return file_id
73
 
 
74
 
    def get_file_sha1(self, path, file_id=None):
75
 
        "See Tree.get_file_sha1()."""
76
 
        return self.oldtree.get_file_sha1(path)
77
 
 
78
 
    def get_file_with_stat(self, path, file_id=None):
79
 
        "See Tree.get_file_with_stat()."""
80
 
        if getattr(self.oldtree, "get_file_with_stat", None) is not None:
81
 
            return self.oldtree.get_file_with_stat(path=path)
82
 
        else:
83
 
            return self.get_file(path), None
84
 
 
85
 
    def get_file(self, path, file_id=None):
86
 
        "See Tree.get_file()."""
87
 
        return self.oldtree.get_file(path)
88
 
 
89
 
    def is_executable(self, path, file_id=None):
90
 
        "See Tree.is_executable()."""
91
 
        return self.oldtree.is_executable(path)
92
 
 
93
 
    def has_filename(self, filename):
94
 
        "See Tree.has_filename()."""
95
 
        return self.oldtree.has_filename(filename)
96
 
 
97
 
    def path_content_summary(self, path):
98
 
        "See Tree.path_content_summary()."""
99
 
        return self.oldtree.path_content_summary(path)
100
 
 
101
 
    def map_ie(self, ie):
102
 
        """Fix the references to old file ids in an inventory entry.
103
 
 
104
 
        :param ie: Inventory entry to map
105
 
        :return: New inventory entry
106
 
        """
107
 
        new_ie = ie.copy()
108
 
        new_ie.file_id = self.new_id(new_ie.file_id)
109
 
        new_ie.parent_id = self.new_id(new_ie.parent_id)
110
 
        return new_ie
111
 
 
112
 
    def iter_entries_by_dir(self):
113
 
        """See Tree.iter_entries_by_dir."""
114
 
        for path, ie in self.oldtree.iter_entries_by_dir():
115
 
            yield path, self.map_ie(ie)
116
 
 
117
 
    def path2id(self, path):
118
 
        file_id = self.oldtree.path2id(path)
119
 
        if file_id is None:
120
 
            return None
121
 
        return self.new_id(file_id)
122
 
 
123
 
    def id2path(self, file_id, recurse='down'):
124
 
        return self.oldtree.id2path(self.old_id(file_id=file_id), recurse=recurse)