/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.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
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.114 by Jelmer Vernooij
Some more docstrings.
47
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
48
    def __init__(self, oldinv, maptree):
49
        self.oldinv = oldinv
50
        self.maptree = maptree
51
52
    def map_ie(self, ie):
53
        """Fix the references to old file ids in an inventory entry.
54
55
        :param ie: Inventory entry to map
56
        :return: New inventory entry
57
        """
58
        new_ie = ie.copy()
59
        new_ie.file_id = self.maptree.new_id(new_ie.file_id)
60
        new_ie.parent_id = self.maptree.new_id(new_ie.parent_id)
61
        return new_ie
62
63
    def __len__(self):
0.436.114 by Jelmer Vernooij
Some more docstrings.
64
        """See Inventory.__len__()."""
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
65
        return len(self.oldinv)
66
67
    def iter_entries(self):
0.436.114 by Jelmer Vernooij
Some more docstrings.
68
        """See Inventory.iter_entries()."""
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
69
        for path, ie in self.oldinv.iter_entries():
70
            yield path, self.map_ie(ie)
71
0.436.18 by Jelmer Vernooij
More tests, extend MapTree a bit.
72
    def path2id(self, path):
0.436.114 by Jelmer Vernooij
Some more docstrings.
73
        """See Inventory.path2id()."""
0.436.18 by Jelmer Vernooij
More tests, extend MapTree a bit.
74
        return self.maptree.new_id(self.oldinv.path2id(path))
75
76
    def id2path(self, id):
0.436.114 by Jelmer Vernooij
Some more docstrings.
77
        """See Inventory.id2path()."""
0.436.18 by Jelmer Vernooij
More tests, extend MapTree a bit.
78
        return self.oldinv.id2path(self.maptree.old_id(id))
79
80
    def has_id(self, id):
0.436.114 by Jelmer Vernooij
Some more docstrings.
81
        """See Inventory.has_id()."""
0.436.18 by Jelmer Vernooij
More tests, extend MapTree a bit.
82
        return self.oldinv.has_id(self.maptree.old_id(id))
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
83
0.436.21 by Jelmer Vernooij
Tests.
84
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
85
class MapTree:
86
    """Wrapper around a tree that translates file ids.
87
    """
0.436.114 by Jelmer Vernooij
Some more docstrings.
88
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
89
    def __init__(self, oldtree, fileid_map):
90
        """Create a new MapTree. 
91
92
        :param oldtree: Old tree to map to.
93
        :param fileid_map: Map with old -> new file ids.
94
        """
95
        self.oldtree = oldtree
96
        self.map = fileid_map
97
        self.inventory = MapInventory(self.oldtree.inventory, self)
98
99
    def old_id(self, file_id):
100
        """Look up the original file id of a file.
101
102
        :param file_id: New file id
103
        :return: Old file id if mapped, otherwise new file id
104
        """
105
        for x in self.map:
106
            if self.map[x] == file_id:
107
                return x
108
        return file_id
109
110
    def new_id(self, file_id):
111
        """Look up the new file id of a file.
112
113
        :param file_id: Old file id
114
        :return: New file id
115
        """
116
        try:
117
            return self.map[file_id]
118
        except KeyError:
119
            return file_id
120
121
    def get_file_sha1(self, file_id, path=None):
0.436.114 by Jelmer Vernooij
Some more docstrings.
122
        "See Tree.get_file_sha1()."""
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
123
        return self.oldtree.get_file_sha1(file_id=self.old_id(file_id), 
124
                                          path=path)
125
0.436.106 by Jelmer Vernooij
Provide get_file_with_stat.
126
    def get_file_with_stat(self, file_id, path=None):
0.436.114 by Jelmer Vernooij
Some more docstrings.
127
        "See Tree.get_file_with_stat()."""
0.436.108 by Jelmer Vernooij
Allow backing MapTree onto a tree that is not a MutableTree.
128
        if getattr(self.oldtree, "get_file_with_stat", None) is not None:
129
            return self.oldtree.get_file_with_stat(file_id=self.old_id(file_id),
0.436.107 by Jelmer Vernooij
Fix syntax error.
130
                                               path=path)
0.436.108 by Jelmer Vernooij
Allow backing MapTree onto a tree that is not a MutableTree.
131
        else:
132
            return self.get_file(file_id, path), None
0.436.106 by Jelmer Vernooij
Provide get_file_with_stat.
133
0.436.44 by Jelmer Vernooij
Update for newer API.
134
    def get_file(self, file_id, path=None):
0.436.114 by Jelmer Vernooij
Some more docstrings.
135
        "See Tree.get_file()."""
0.436.45 by Jelmer Vernooij
Fix 0.90 compatibility.
136
        if path is None:
137
            return self.oldtree.get_file(self.old_id(file_id=file_id))
138
        else:
139
            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.
140
141
    def is_executable(self, file_id, path=None):
0.436.114 by Jelmer Vernooij
Some more docstrings.
142
        "See Tree.is_executable()."""
0.436.17 by Jelmer Vernooij
Move maptree code to separate files.
143
        return self.oldtree.is_executable(self.old_id(file_id=file_id), 
144
                                          path=path)
0.436.18 by Jelmer Vernooij
More tests, extend MapTree a bit.
145
146
    def has_filename(self, filename):
0.436.114 by Jelmer Vernooij
Some more docstrings.
147
        "See Tree.has_filename()."""
0.436.18 by Jelmer Vernooij
More tests, extend MapTree a bit.
148
        return self.oldtree.has_filename(filename)
0.436.47 by Jelmer Vernooij
Add progress bar, fix compatibility with bzr.dev.
149
150
    def path_content_summary(self, path):
0.436.114 by Jelmer Vernooij
Some more docstrings.
151
        "See Tree.path_content_summary()."""
0.436.47 by Jelmer Vernooij
Add progress bar, fix compatibility with bzr.dev.
152
        return self.oldtree.path_content_summary(path)