/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/rewrite/maptree.py

  • Committer: Jelmer Vernooij
  • Date: 2020-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
5
# the Free Software Foundation; either version 2 of the License, or
 
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):
 
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)
 
26
    ret = {}
 
27
    for (oldp, newp) in zip(old_parents, new_parents):
 
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
 
34
    return ret
 
35
 
 
36
 
 
37
class MapTree(object):
 
38
    """Wrapper around a tree that translates file ids.
 
39
    """
 
40
 
 
41
    def __init__(self, oldtree, fileid_map):
 
42
        """Create a new MapTree.
 
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
 
 
72
    def get_file_sha1(self, path, file_id=None):
 
73
        "See Tree.get_file_sha1()."""
 
74
        return self.oldtree.get_file_sha1(path)
 
75
 
 
76
    def get_file_with_stat(self, path, file_id=None):
 
77
        "See Tree.get_file_with_stat()."""
 
78
        if getattr(self.oldtree, "get_file_with_stat", None) is not None:
 
79
            return self.oldtree.get_file_with_stat(path=path)
 
80
        else:
 
81
            return self.get_file(path), None
 
82
 
 
83
    def get_file(self, path, file_id=None):
 
84
        "See Tree.get_file()."""
 
85
        return self.oldtree.get_file(path)
 
86
 
 
87
    def is_executable(self, path, file_id=None):
 
88
        "See Tree.is_executable()."""
 
89
        return self.oldtree.is_executable(path)
 
90
 
 
91
    def has_filename(self, filename):
 
92
        "See Tree.has_filename()."""
 
93
        return self.oldtree.has_filename(filename)
 
94
 
 
95
    def path_content_summary(self, path):
 
96
        "See Tree.path_content_summary()."""
 
97
        return self.oldtree.path_content_summary(path)
 
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
 
 
121
    def id2path(self, file_id, recurse='down'):
 
122
        return self.oldtree.id2path(self.old_id(file_id=file_id), recurse=recurse)