1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
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
27
from bzrlib.plugins.git.inventory import (
30
from bzrlib.plugins.git.mapping import (
36
class GitRevisionTree(revisiontree.RevisionTree):
38
def __init__(self, repository, revision_id):
39
self._revision_id = revision_id
40
self._repository = repository
41
store = repository._git.object_store
42
assert isinstance(revision_id, str)
43
git_id, self.mapping = repository.lookup_bzr_revision_id(revision_id)
45
commit = store[git_id]
47
raise errors.NoSuchRevision(repository, revision_id)
48
self.tree = commit.tree
49
self._inventory = GitInventory(self.tree, self.mapping, store,
52
def get_revision_id(self):
53
return self._revision_id
55
def get_file_text(self, file_id, path=None):
57
entry = self._inventory._get_ie(path)
59
entry = self._inventory[file_id]
60
if entry.kind == 'directory': return ""
61
return entry.object.data
64
def tree_delta_from_git_changes(changes, mapping, specific_file=None,
65
require_versioned=False):
66
"""Create a TreeDelta from two git trees.
68
source and target are iterators over tuples with:
71
ret = delta.TreeDelta()
72
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
74
ret.added.append((newpath, mapping.generate_file_id(newpath), mode_kind(newmode)))
76
ret.removed.append((oldpath, mapping.generate_file_id(oldpath), mode_kind(oldmode)))
77
elif oldpath != newpath:
78
ret.renamed.append((oldpath, newpath, mapping.generate_file_id(oldpath), mode_kind(newmode), (oldsha != newsha), (oldmode != newmode)))
79
elif mode_kind(oldmode) != mode_kind(newmode):
80
ret.kind_changed.append((newpath, mapping.generate_file_id(newpath), mode_kind(oldmode), mode_kind(newmode)))
81
elif oldsha != newsha or oldmode != newmode:
82
ret.modified.append((newpath, mapping.generate_file_id(newpath), mode_kind(newmode), (oldsha != newsha), (oldmode != newmode)))
84
ret.unchanged.append((newpath, mapping.generate_file_id(newpath), mode_kind(newmode)))
88
def changes_from_git_changes(changes, mapping, specific_file=None,
89
require_versioned=False):
90
"""Create a iter_changes-like generator from a git stream.
92
source and target are iterators over tuples with:
95
for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
96
path = (oldpath, newpath)
98
fileid = mapping.generate_file_id(newpath)
104
oldexe = mode_is_executable(oldmode)
105
oldkind = mode_kind(oldmode)
107
(oldparentpath, oldname) = oldpath.rsplit("/", 1)
112
oldparent = mapping.generate_file_id(oldparentpath)
113
fileid = mapping.generate_file_id(oldpath)
120
newexe = mode_is_executable(newmode)
121
newkind = mode_kind(newmode)
123
newparentpath, newname = newpath.rsplit("/", 1)
128
newparent = mapping.generate_file_id(newparentpath)
129
yield fileid, (oldpath, newpath), (oldsha != newsha), (oldpath is not None, newpath is not None), (oldparent, newparent), (oldname, newname), (oldkind, newkind), (oldexe, newexe)
132
class InterGitRevisionTrees(tree.InterTree):
133
"""InterTree that works between two git revision trees."""
135
_matching_from_tree_format = None
136
_matching_to_tree_format = None
137
_test_mutable_trees_to_test_trees = None
140
def is_compatible(cls, source, target):
141
return (isinstance(source, GitRevisionTree) and
142
isinstance(target, GitRevisionTree))
144
def compare(self, want_unchanged=False, specific_files=None,
145
extra_trees=None, require_versioned=False, include_root=False,
146
want_unversioned=False):
147
if self.source._repository._git.object_store != self.target._repository._git.object_store:
149
changes = self.source._repository._git.object_store.tree_changes(
150
self.source.tree, self.target.tree, want_unchanged=want_unchanged)
151
return tree_delta_from_git_changes(changes, self.target.mapping,
152
specific_file=specific_files)
154
def iter_changes(self, include_unchanged=False, specific_files=None,
155
pb=None, extra_trees=[], require_versioned=True, want_unversioned=False):
156
if self.source._repository._git.object_store != self.target._repository._git.object_store:
158
changes = self.source._repository._git.object_store.tree_changes(
159
self.source.tree, self.target.tree,
160
want_unchanged=include_unchanged)
161
return changes_from_git_changes(changes, self.target.mapping,
162
specific_file=specific_files)
165
tree.InterTree.register_optimiser(InterGitRevisionTrees)