/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.617 by Jelmer Vernooij
Add custom InterTree for use between git revision trees.
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
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
17
18
"""Git Trees."""
19
20
from bzrlib import (
21
    delta,
22
    errors,
23
    revisiontree,
24
    tree,
25
    )
26
27
from bzrlib.plugins.git.inventory import (
28
    GitInventory,
29
    )
30
from bzrlib.plugins.git.mapping import (
31
    mode_kind,
32
    )
33
34
35
class GitRevisionTree(revisiontree.RevisionTree):
36
37
    def __init__(self, repository, revision_id):
38
        self._revision_id = revision_id
39
        self._repository = repository
40
        store = repository._git.object_store
41
        assert isinstance(revision_id, str)
42
        git_id, self.mapping = repository.lookup_git_revid(revision_id)
43
        try:
44
            commit = store[git_id]
45
        except KeyError, r:
46
            raise errors.NoSuchRevision(repository, revision_id)
47
        self.tree = commit.tree
48
        self._inventory = GitInventory(self.tree, self.mapping, store, 
49
                                       revision_id)
50
51
    def get_revision_id(self):
52
        return self._revision_id
53
54
    def get_file_text(self, file_id, path=None):
55
        if path is not None:
56
            entry = self._inventory._get_ie(path)
57
        else:
58
            entry = self._inventory[file_id]
59
        if entry.kind == 'directory': return ""
60
        return entry.object.data
61
62
63
def tree_delta_from_git_changes(changes, mapping, specific_file=None, 
64
                                require_versioned=False):
65
    """Create a TreeDelta from two git trees.
66
    
67
    source and target are iterators over tuples with: 
68
        (filename, sha, mode)
69
    """
70
    ret = delta.TreeDelta()
71
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
72
        if oldpath is None:
73
            ret.added.append((newpath, mapping.generate_file_id(newpath), mode_kind(newmode)))
74
        elif newpath is None:
75
            ret.removed.append((oldpath, mapping.generate_file_id(oldpath), mode_kind(oldmode)))
76
        elif oldpath != newpath:
77
            ret.renamed.append((oldpath, newpath, mapping.generate_file_id(oldpath), mode_kind(newmode), (oldsha != newsha), (oldmode != newmode)))
78
        elif mode_kind(oldmode) != mode_kind(newmode):
79
            ret.kind_changed.append((newpath, mapping.generate_file_id(newpath), mode_kind(oldmode), mode_kind(newmode)))
80
        elif oldsha != newsha or oldmode != newmode:
81
            ret.modified.append((newpath, mapping.generate_file_id(newpath), mode_kind(newmode), (oldsha != newsha), (oldmode != newmode)))
82
        else:
83
            ret.unchanged.append((newpath, mapping.generate_file_id(newpath), mode_kind(newmode)))
84
    return ret
85
86
87
class InterGitRevisionTrees(tree.InterTree):
88
    """InterTree that works between two git revision trees."""
89
90
    @classmethod
91
    def is_compatible(cls, source, target):
92
        return (isinstance(source, GitRevisionTree) and 
93
                isinstance(target, GitRevisionTree))
94
95
    def compare(self, want_unchanged=False, specific_files=None,
96
                extra_trees=None, require_versioned=False, include_root=False,
97
                want_unversioned=False):
98
        if self.source._repository._git.object_store != self.target._repository._git.object_store:
99
            raise AssertionError
100
        changes = self.source._repository._git.object_store.tree_changes(
101
            self.source.tree, self.target.tree, want_unchanged=want_unchanged)
102
        return tree_delta_from_git_changes(changes, self.target.mapping, 
103
            specific_file=specific_files)
104
105
106
tree.InterTree.register_optimiser(InterGitRevisionTrees)