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