/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
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
0.436.81 by Jelmer Vernooij
Change license to GPLv3+
5
# the Free Software Foundation; either version 3 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
from bzrlib.config import Config
19
from bzrlib.errors import BzrError, NoSuchFile, UnknownFormatError
20
from bzrlib.generate_ids import gen_revision_id
21
from bzrlib import osutils
22
from bzrlib.revision import NULL_REVISION
23
from bzrlib.trace import mutter
24
import bzrlib.ui as ui
25
26
27
def map_file_ids(repository, old_parents, new_parents):
0.436.21 by Jelmer Vernooij
Tests.
28
    """Try to determine the equivalent file ids in two sets of parents.
29
30
    :param repository: Repository to use
31
    :param old_parents: List of revision ids of old parents
32
    :param new_parents: List of revision ids of new parents
33
    """
34
    assert len(old_parents) == len(new_parents)
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
35
    ret = {}
36
    for (oldp, newp) in zip(old_parents, new_parents):
37
        oldinv = repository.get_revision_inventory(oldp)
38
        newinv = repository.get_revision_inventory(newp)
39
        for path, ie in oldinv.iter_entries():
40
            if newinv.has_filename(path):
41
                ret[ie.file_id] = newinv.path2id(path)
42
    return ret
43
44
45
class MapInventory:
0.436.32 by Jelmer Vernooij
Properly detect invalid snapshot replays.
46
    """Maps the file ids in an inventory."""
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
47
    def __init__(self, oldinv, maptree):
48
        self.oldinv = oldinv
49
        self.maptree = maptree
50
51
    def map_ie(self, ie):
52
        """Fix the references to old file ids in an inventory entry.
53
54
        :param ie: Inventory entry to map
55
        :return: New inventory entry
56
        """
57
        new_ie = ie.copy()
58
        new_ie.file_id = self.maptree.new_id(new_ie.file_id)
59
        new_ie.parent_id = self.maptree.new_id(new_ie.parent_id)
60
        return new_ie
61
62
    def __len__(self):
63
        return len(self.oldinv)
64
65
    def iter_entries(self):
66
        for path, ie in self.oldinv.iter_entries():
67
            yield path, self.map_ie(ie)
68
0.436.18 by Jelmer Vernooij
More tests, extend MapTree a bit.
69
    def path2id(self, path):
70
        return self.maptree.new_id(self.oldinv.path2id(path))
71
72
    def id2path(self, id):
73
        return self.oldinv.id2path(self.maptree.old_id(id))
74
75
    def has_id(self, id):
76
        return self.oldinv.has_id(self.maptree.old_id(id))
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
77
0.436.21 by Jelmer Vernooij
Tests.
78
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
79
class MapTree:
80
    """Wrapper around a tree that translates file ids.
81
    """
82
    # TODO: Inventory
83
    def __init__(self, oldtree, fileid_map):
84
        """Create a new MapTree. 
85
86
        :param oldtree: Old tree to map to.
87
        :param fileid_map: Map with old -> new file ids.
88
        """
89
        self.oldtree = oldtree
90
        self.map = fileid_map
91
        self.inventory = MapInventory(self.oldtree.inventory, self)
92
93
    def old_id(self, file_id):
94
        """Look up the original file id of a file.
95
96
        :param file_id: New file id
97
        :return: Old file id if mapped, otherwise new file id
98
        """
99
        for x in self.map:
100
            if self.map[x] == file_id:
101
                return x
102
        return file_id
103
104
    def new_id(self, file_id):
105
        """Look up the new file id of a file.
106
107
        :param file_id: Old file id
108
        :return: New file id
109
        """
110
        try:
111
            return self.map[file_id]
112
        except KeyError:
113
            return file_id
114
115
    def get_file_sha1(self, file_id, path=None):
116
        return self.oldtree.get_file_sha1(file_id=self.old_id(file_id), 
117
                                          path=path)
118
0.436.44 by Jelmer Vernooij
Update for newer API.
119
    def get_file(self, file_id, path=None):
0.436.45 by Jelmer Vernooij
Fix 0.90 compatibility.
120
        if path is None:
121
            return self.oldtree.get_file(self.old_id(file_id=file_id))
122
        else:
123
            return self.oldtree.get_file(self.old_id(file_id=file_id), path)
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
124
125
    def is_executable(self, file_id, path=None):
126
        return self.oldtree.is_executable(self.old_id(file_id=file_id), 
127
                                          path=path)
0.436.18 by Jelmer Vernooij
More tests, extend MapTree a bit.
128
129
    def has_filename(self, filename):
130
        return self.oldtree.has_filename(filename)
0.436.47 by Jelmer Vernooij
Add progress bar, fix compatibility with bzr.dev.
131
132
    def path_content_summary(self, path):
133
        return self.oldtree.path_content_summary(path)