/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
1
# Copyright (C) 2006-2007 by Jelmer Vernooij
0.436.177 by Jelmer Vernooij
Remove trailing whitespace.
2
#
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
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
0.436.131 by Jelmer Vernooij
Change license back to GPLv2+
5
# the Free Software Foundation; either version 2 of the License, or
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
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
def map_file_ids(repository, old_parents, new_parents):
0.436.21 by Jelmer Vernooij
Tests.
19
    """Try to determine the equivalent file ids in two sets of parents.
20
21
    :param repository: Repository to use
22
    :param old_parents: List of revision ids of old parents
23
    :param new_parents: List of revision ids of new parents
24
    """
25
    assert len(old_parents) == len(new_parents)
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
26
    ret = {}
27
    for (oldp, newp) in zip(old_parents, new_parents):
0.436.243 by Jelmer Vernooij
Avoid deprecated methods, use Tree API only.
28
        oldtree = repository.revision_tree(oldp)
29
        newtree = repository.revision_tree(newp)
30
        for path, ie in oldtree.iter_entries_by_dir():
31
            file_id = newtree.path2id(path)
32
            if file_id is not None:
33
                ret[ie.file_id] = file_id
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
34
    return ret
35
36
0.436.228 by Jelmer Vernooij
use new style classes.
37
class MapTree(object):
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
38
    """Wrapper around a tree that translates file ids.
39
    """
0.436.114 by Jelmer Vernooij
Some more docstrings.
40
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
41
    def __init__(self, oldtree, fileid_map):
0.436.177 by Jelmer Vernooij
Remove trailing whitespace.
42
        """Create a new MapTree.
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
43
44
        :param oldtree: Old tree to map to.
45
        :param fileid_map: Map with old -> new file ids.
46
        """
47
        self.oldtree = oldtree
48
        self.map = fileid_map
49
50
    def old_id(self, file_id):
51
        """Look up the original file id of a file.
52
53
        :param file_id: New file id
54
        :return: Old file id if mapped, otherwise new file id
55
        """
56
        for x in self.map:
57
            if self.map[x] == file_id:
58
                return x
59
        return file_id
60
61
    def new_id(self, file_id):
62
        """Look up the new file id of a file.
63
64
        :param file_id: Old file id
65
        :return: New file id
66
        """
67
        try:
68
            return self.map[file_id]
69
        except KeyError:
70
            return file_id
71
7406.3.3 by Jelmer Vernooij
Fix python3 compatibility.
72
    def get_file_sha1(self, path, file_id=None):
0.436.114 by Jelmer Vernooij
Some more docstrings.
73
        "See Tree.get_file_sha1()."""
7406.3.3 by Jelmer Vernooij
Fix python3 compatibility.
74
        return self.oldtree.get_file_sha1(path)
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
75
7406.3.3 by Jelmer Vernooij
Fix python3 compatibility.
76
    def get_file_with_stat(self, path, file_id=None):
0.436.114 by Jelmer Vernooij
Some more docstrings.
77
        "See Tree.get_file_with_stat()."""
0.436.108 by Jelmer Vernooij
Allow backing MapTree onto a tree that is not a MutableTree.
78
        if getattr(self.oldtree, "get_file_with_stat", None) is not None:
7406.3.3 by Jelmer Vernooij
Fix python3 compatibility.
79
            return self.oldtree.get_file_with_stat(path=path)
0.436.108 by Jelmer Vernooij
Allow backing MapTree onto a tree that is not a MutableTree.
80
        else:
7406.3.3 by Jelmer Vernooij
Fix python3 compatibility.
81
            return self.get_file(path), None
0.436.106 by Jelmer Vernooij
Provide get_file_with_stat.
82
7406.3.3 by Jelmer Vernooij
Fix python3 compatibility.
83
    def get_file(self, path, file_id=None):
0.436.114 by Jelmer Vernooij
Some more docstrings.
84
        "See Tree.get_file()."""
7406.3.3 by Jelmer Vernooij
Fix python3 compatibility.
85
        return self.oldtree.get_file(path)
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
86
7406.3.3 by Jelmer Vernooij
Fix python3 compatibility.
87
    def is_executable(self, path, file_id=None):
0.436.114 by Jelmer Vernooij
Some more docstrings.
88
        "See Tree.is_executable()."""
7406.3.3 by Jelmer Vernooij
Fix python3 compatibility.
89
        return self.oldtree.is_executable(path)
0.436.18 by Jelmer Vernooij
More tests, extend MapTree a bit.
90
91
    def has_filename(self, filename):
0.436.114 by Jelmer Vernooij
Some more docstrings.
92
        "See Tree.has_filename()."""
0.436.18 by Jelmer Vernooij
More tests, extend MapTree a bit.
93
        return self.oldtree.has_filename(filename)
0.436.47 by Jelmer Vernooij
Add progress bar, fix compatibility with bzr.dev.
94
95
    def path_content_summary(self, path):
0.436.114 by Jelmer Vernooij
Some more docstrings.
96
        "See Tree.path_content_summary()."""
0.436.47 by Jelmer Vernooij
Add progress bar, fix compatibility with bzr.dev.
97
        return self.oldtree.path_content_summary(path)
0.436.243 by Jelmer Vernooij
Avoid deprecated methods, use Tree API only.
98
99
    def map_ie(self, ie):
100
        """Fix the references to old file ids in an inventory entry.
101
102
        :param ie: Inventory entry to map
103
        :return: New inventory entry
104
        """
105
        new_ie = ie.copy()
106
        new_ie.file_id = self.new_id(new_ie.file_id)
107
        new_ie.parent_id = self.new_id(new_ie.parent_id)
108
        return new_ie
109
110
    def iter_entries_by_dir(self):
111
        """See Tree.iter_entries_by_dir."""
112
        for path, ie in self.oldtree.iter_entries_by_dir():
113
            yield path, self.map_ie(ie)
114
115
    def path2id(self, path):
116
        file_id = self.oldtree.path2id(path)
117
        if file_id is None:
118
            return None
119
        return self.new_id(file_id)
120
7450.1.1 by Jelmer Vernooij
Support recurse argument to id2path.
121
    def id2path(self, file_id, recurse='down'):
122
        return self.oldtree.id2path(self.old_id(file_id=file_id), recurse=recurse)