1
# Copyright (C) 2006-2007 by Jelmer Vernooij
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.
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.
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
18
def map_file_ids(repository, old_parents, new_parents):
19
"""Try to determine the equivalent file ids in two sets of parents.
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
25
assert len(old_parents) == len(new_parents)
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
37
class MapTree(object):
38
"""Wrapper around a tree that translates file ids.
41
def __init__(self, oldtree, fileid_map):
42
"""Create a new MapTree.
44
:param oldtree: Old tree to map to.
45
:param fileid_map: Map with old -> new file ids.
47
self.oldtree = oldtree
50
def old_id(self, file_id):
51
"""Look up the original file id of a file.
53
:param file_id: New file id
54
:return: Old file id if mapped, otherwise new file id
57
if self.map[x] == file_id:
61
def new_id(self, file_id):
62
"""Look up the new file id of a file.
64
:param file_id: Old file id
68
return self.map[file_id]
72
def get_file_sha1(self, path, file_id=None):
73
"See Tree.get_file_sha1()."""
74
return self.oldtree.get_file_sha1(path)
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)
81
return self.get_file(path), None
83
def get_file(self, path, file_id=None):
84
"See Tree.get_file()."""
85
return self.oldtree.get_file(path)
87
def is_executable(self, path, file_id=None):
88
"See Tree.is_executable()."""
89
return self.oldtree.is_executable(path)
91
def has_filename(self, filename):
92
"See Tree.has_filename()."""
93
return self.oldtree.has_filename(filename)
95
def path_content_summary(self, path):
96
"See Tree.path_content_summary()."""
97
return self.oldtree.path_content_summary(path)
100
"""Fix the references to old file ids in an inventory entry.
102
:param ie: Inventory entry to map
103
:return: New inventory entry
106
new_ie.file_id = self.new_id(new_ie.file_id)
107
new_ie.parent_id = self.new_id(new_ie.parent_id)
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)
115
def path2id(self, path):
116
file_id = self.oldtree.path2id(path)
119
return self.new_id(file_id)
121
def id2path(self, file_id, recurse='down'):
122
return self.oldtree.id2path(self.old_id(file_id=file_id), recurse=recurse)