File: maptree.py

package info (click to toggle)
bzr-rewrite 0.6.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 252 kB
  • ctags: 256
  • sloc: python: 1,950; makefile: 45
file content (144 lines) | stat: -rw-r--r-- 4,934 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Copyright (C) 2006-2007 by Jelmer Vernooij
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
"""Map Tree."""


def map_file_ids(repository, old_parents, new_parents):
    """Try to determine the equivalent file ids in two sets of parents.

    :param repository: Repository to use
    :param old_parents: List of revision ids of old parents
    :param new_parents: List of revision ids of new parents
    """
    assert len(old_parents) == len(new_parents)
    ret = {}
    for (oldp, newp) in zip(old_parents, new_parents):
        oldinv = repository.get_revision_inventory(oldp)
        newinv = repository.get_revision_inventory(newp)
        for path, ie in oldinv.iter_entries():
            if newinv.has_filename(path):
                ret[ie.file_id] = newinv.path2id(path)
    return ret


class MapInventory:
    """Maps the file ids in an inventory."""

    def __init__(self, oldinv, maptree):
        self.oldinv = oldinv
        self.maptree = maptree

    def map_ie(self, ie):
        """Fix the references to old file ids in an inventory entry.

        :param ie: Inventory entry to map
        :return: New inventory entry
        """
        new_ie = ie.copy()
        new_ie.file_id = self.maptree.new_id(new_ie.file_id)
        new_ie.parent_id = self.maptree.new_id(new_ie.parent_id)
        return new_ie

    def __len__(self):
        """See Inventory.__len__()."""
        return len(self.oldinv)

    def iter_entries(self):
        """See Inventory.iter_entries()."""
        for path, ie in self.oldinv.iter_entries():
            yield path, self.map_ie(ie)

    def path2id(self, path):
        """See Inventory.path2id()."""
        return self.maptree.new_id(self.oldinv.path2id(path))

    def id2path(self, id):
        """See Inventory.id2path()."""
        return self.oldinv.id2path(self.maptree.old_id(id))

    def has_id(self, id):
        """See Inventory.has_id()."""
        return self.oldinv.has_id(self.maptree.old_id(id))


class MapTree:
    """Wrapper around a tree that translates file ids.
    """

    def __init__(self, oldtree, fileid_map):
        """Create a new MapTree.

        :param oldtree: Old tree to map to.
        :param fileid_map: Map with old -> new file ids.
        """
        self.oldtree = oldtree
        self.map = fileid_map
        self.inventory = MapInventory(self.oldtree.inventory, self)

    def old_id(self, file_id):
        """Look up the original file id of a file.

        :param file_id: New file id
        :return: Old file id if mapped, otherwise new file id
        """
        for x in self.map:
            if self.map[x] == file_id:
                return x
        return file_id

    def new_id(self, file_id):
        """Look up the new file id of a file.

        :param file_id: Old file id
        :return: New file id
        """
        try:
            return self.map[file_id]
        except KeyError:
            return file_id

    def get_file_sha1(self, file_id, path=None):
        "See Tree.get_file_sha1()."""
        return self.oldtree.get_file_sha1(file_id=self.old_id(file_id),
                                          path=path)

    def get_file_with_stat(self, file_id, path=None):
        "See Tree.get_file_with_stat()."""
        if getattr(self.oldtree, "get_file_with_stat", None) is not None:
            return self.oldtree.get_file_with_stat(file_id=self.old_id(file_id),
                                               path=path)
        else:
            return self.get_file(file_id, path), None

    def get_file(self, file_id, path=None):
        "See Tree.get_file()."""
        if path is None:
            return self.oldtree.get_file(self.old_id(file_id=file_id))
        else:
            return self.oldtree.get_file(self.old_id(file_id=file_id), path)

    def is_executable(self, file_id, path=None):
        "See Tree.is_executable()."""
        return self.oldtree.is_executable(self.old_id(file_id=file_id),
                                          path=path)

    def has_filename(self, filename):
        "See Tree.has_filename()."""
        return self.oldtree.has_filename(filename)

    def path_content_summary(self, path):
        "See Tree.path_content_summary()."""
        return self.oldtree.path_content_summary(path)