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
from __future__ import absolute_import
20
def map_file_ids(repository, old_parents, new_parents):
21
"""Try to determine the equivalent file ids in two sets of parents.
23
:param repository: Repository to use
24
:param old_parents: List of revision ids of old parents
25
:param new_parents: List of revision ids of new parents
27
assert len(old_parents) == len(new_parents)
29
for (oldp, newp) in zip(old_parents, new_parents):
30
oldtree = repository.revision_tree(oldp)
31
newtree = repository.revision_tree(newp)
32
for path, ie in oldtree.iter_entries_by_dir():
33
file_id = newtree.path2id(path)
34
if file_id is not None:
35
ret[ie.file_id] = file_id
39
class MapTree(object):
40
"""Wrapper around a tree that translates file ids.
43
def __init__(self, oldtree, fileid_map):
44
"""Create a new MapTree.
46
:param oldtree: Old tree to map to.
47
:param fileid_map: Map with old -> new file ids.
49
self.oldtree = oldtree
52
def old_id(self, file_id):
53
"""Look up the original file id of a file.
55
:param file_id: New file id
56
:return: Old file id if mapped, otherwise new file id
59
if self.map[x] == file_id:
63
def new_id(self, file_id):
64
"""Look up the new file id of a file.
66
:param file_id: Old file id
70
return self.map[file_id]
74
def get_file_sha1(self, path, file_id=None):
75
"See Tree.get_file_sha1()."""
76
return self.oldtree.get_file_sha1(path)
78
def get_file_with_stat(self, path, file_id=None):
79
"See Tree.get_file_with_stat()."""
80
if getattr(self.oldtree, "get_file_with_stat", None) is not None:
81
return self.oldtree.get_file_with_stat(path=path)
83
return self.get_file(path), None
85
def get_file(self, path, file_id=None):
86
"See Tree.get_file()."""
87
return self.oldtree.get_file(path)
89
def is_executable(self, path, file_id=None):
90
"See Tree.is_executable()."""
91
return self.oldtree.is_executable(path)
93
def has_filename(self, filename):
94
"See Tree.has_filename()."""
95
return self.oldtree.has_filename(filename)
97
def path_content_summary(self, path):
98
"See Tree.path_content_summary()."""
99
return self.oldtree.path_content_summary(path)
101
def map_ie(self, ie):
102
"""Fix the references to old file ids in an inventory entry.
104
:param ie: Inventory entry to map
105
:return: New inventory entry
108
new_ie.file_id = self.new_id(new_ie.file_id)
109
new_ie.parent_id = self.new_id(new_ie.parent_id)
112
def iter_entries_by_dir(self):
113
"""See Tree.iter_entries_by_dir."""
114
for path, ie in self.oldtree.iter_entries_by_dir():
115
yield path, self.map_ie(ie)
117
def path2id(self, path):
118
file_id = self.oldtree.path2id(path)
121
return self.new_id(file_id)
123
def id2path(self, file_id, recurse='down'):
124
return self.oldtree.id2path(self.old_id(file_id=file_id), recurse=recurse)