/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 tree.py

More work on roundtrip push support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
    GitInventory,
29
29
    )
30
30
from bzrlib.plugins.git.mapping import (
31
 
    GitFileIdMap,
32
31
    mode_is_executable,
33
32
    mode_kind,
34
33
    )
35
34
 
36
35
 
37
36
class GitRevisionTree(revisiontree.RevisionTree):
 
37
    """Revision tree implementation based on Git objects."""
38
38
 
39
39
    def __init__(self, repository, revision_id):
40
40
        self._revision_id = revision_id
47
47
        except KeyError, r:
48
48
            raise errors.NoSuchRevision(repository, revision_id)
49
49
        self.tree = commit.tree
50
 
        try:
51
 
            file_id_map_sha = store[self.tree][self.mapping.BZR_FILE_IDS_FILE][1]
52
 
        except KeyError:
53
 
            file_ids = {}
54
 
        else:
55
 
            file_ids = self.mapping.import_fileid_map(store[file_id_map_sha])
56
 
        fileid_map = GitFileIdMap(file_ids, self.mapping)
57
 
        self._inventory = GitInventory(self.tree, self.mapping, fileid_map, store, 
58
 
                                       revision_id)
 
50
        fileid_map = self.mapping.get_fileid_map(store.__getitem__, self.tree)
 
51
        self._inventory = GitInventory(self.tree, self.mapping, fileid_map,
 
52
            store, revision_id)
59
53
 
60
54
    def get_revision_id(self):
 
55
        """See RevisionTree.get_revision_id."""
61
56
        return self._revision_id
62
57
 
63
58
    def get_file_text(self, file_id, path=None):
 
59
        """See RevisionTree.get_file_text."""
64
60
        if path is not None:
65
61
            entry = self._inventory._get_ie(path)
66
62
        else:
70
66
        return entry.object.data
71
67
 
72
68
 
73
 
def tree_delta_from_git_changes(changes, mapping, specific_file=None, 
74
 
                                require_versioned=False):
 
69
def tree_delta_from_git_changes(changes, mapping,
 
70
        (old_fileid_map, new_fileid_map), specific_file=None,
 
71
        require_versioned=False):
75
72
    """Create a TreeDelta from two git trees.
76
 
    
77
 
    source and target are iterators over tuples with: 
 
73
 
 
74
    source and target are iterators over tuples with:
78
75
        (filename, sha, mode)
79
76
    """
80
77
    ret = delta.TreeDelta()
81
78
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
 
79
        if mapping.is_control_file(oldpath):
 
80
            oldpath = None
 
81
        if mapping.is_control_file(newpath):
 
82
            newpath = None
 
83
        if oldpath is None and newpath is None:
 
84
            continue
82
85
        if oldpath is None:
83
 
            ret.added.append((newpath, mapping.generate_file_id(newpath), mode_kind(newmode)))
 
86
            ret.added.append((newpath, new_fileid_map.lookup_file_id(newpath.encode("utf-8")), mode_kind(newmode)))
84
87
        elif newpath is None:
85
 
            ret.removed.append((oldpath, mapping.generate_file_id(oldpath), mode_kind(oldmode)))
 
88
            ret.removed.append((oldpath, old_fileid_map.lookup_file_id(oldpath.encode("utf-8")), mode_kind(oldmode)))
86
89
        elif oldpath != newpath:
87
 
            ret.renamed.append((oldpath, newpath, mapping.generate_file_id(oldpath), mode_kind(newmode), (oldsha != newsha), (oldmode != newmode)))
 
90
            ret.renamed.append((oldpath, newpath, old_fileid_map.lookup_file_id(oldpath.encode("utf-8")), mode_kind(newmode), (oldsha != newsha), (oldmode != newmode)))
88
91
        elif mode_kind(oldmode) != mode_kind(newmode):
89
 
            ret.kind_changed.append((newpath, mapping.generate_file_id(newpath), mode_kind(oldmode), mode_kind(newmode)))
 
92
            ret.kind_changed.append((newpath, new_fileid_map.lookup_file_id(newpath.encode("utf-8")), mode_kind(oldmode), mode_kind(newmode)))
90
93
        elif oldsha != newsha or oldmode != newmode:
91
 
            ret.modified.append((newpath, mapping.generate_file_id(newpath), mode_kind(newmode), (oldsha != newsha), (oldmode != newmode)))
 
94
            ret.modified.append((newpath, new_fileid_map.lookup_file_id(newpath.encode("utf-8")), mode_kind(newmode), (oldsha != newsha), (oldmode != newmode)))
92
95
        else:
93
 
            ret.unchanged.append((newpath, mapping.generate_file_id(newpath), mode_kind(newmode)))
 
96
            ret.unchanged.append((newpath, new_fileid_map.lookup_file_id(newpath.encode("utf-8")), mode_kind(newmode)))
94
97
    return ret
95
98
 
96
99
 
97
 
def changes_from_git_changes(changes, mapping, specific_file=None, 
 
100
def changes_from_git_changes(changes, mapping, specific_file=None,
98
101
                                require_versioned=False):
99
102
    """Create a iter_changes-like generator from a git stream.
100
 
    
101
 
    source and target are iterators over tuples with: 
 
103
 
 
104
    source and target are iterators over tuples with:
102
105
        (filename, sha, mode)
103
106
    """
104
107
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
135
138
                newname = newpath
136
139
            else:
137
140
                newparent = mapping.generate_file_id(newparentpath)
138
 
        yield fileid, (oldpath, newpath), (oldsha != newsha), (oldpath is not None, newpath is not None), (oldparent, newparent), (oldname, newname), (oldkind, newkind), (oldexe, newexe)
 
141
        yield (fileid, (oldpath, newpath), (oldsha != newsha),
 
142
             (oldpath is not None, newpath is not None),
 
143
             (oldparent, newparent), (oldname, newname),
 
144
             (oldkind, newkind), (oldexe, newexe))
139
145
 
140
146
 
141
147
class InterGitRevisionTrees(tree.InterTree):
147
153
 
148
154
    @classmethod
149
155
    def is_compatible(cls, source, target):
150
 
        return (isinstance(source, GitRevisionTree) and 
 
156
        return (isinstance(source, GitRevisionTree) and
151
157
                isinstance(target, GitRevisionTree))
152
158
 
153
159
    def compare(self, want_unchanged=False, specific_files=None,
157
163
            raise AssertionError
158
164
        changes = self.source._repository._git.object_store.tree_changes(
159
165
            self.source.tree, self.target.tree, want_unchanged=want_unchanged)
160
 
        return tree_delta_from_git_changes(changes, self.target.mapping, 
 
166
        source_fileid_map = self.source.mapping.get_fileid_map(
 
167
            self.source._repository._git.object_store.__getitem__,
 
168
            self.source.tree)
 
169
        target_fileid_map = self.target.mapping.get_fileid_map(
 
170
            self.target._repository._git.object_store.__getitem__,
 
171
            self.target.tree)
 
172
        return tree_delta_from_git_changes(changes, self.target.mapping,
 
173
            (source_fileid_map, target_fileid_map),
161
174
            specific_file=specific_files)
162
175
 
163
176
    def iter_changes(self, include_unchanged=False, specific_files=None,
164
 
        pb=None, extra_trees=[], require_versioned=True, want_unversioned=False):
 
177
        pb=None, extra_trees=[], require_versioned=True,
 
178
        want_unversioned=False):
165
179
        if self.source._repository._git.object_store != self.target._repository._git.object_store:
166
180
            raise AssertionError
167
181
        changes = self.source._repository._git.object_store.tree_changes(
168
 
            self.source.tree, self.target.tree, 
 
182
            self.source.tree, self.target.tree,
169
183
            want_unchanged=include_unchanged)
170
 
        return changes_from_git_changes(changes, self.target.mapping, 
 
184
        return changes_from_git_changes(changes, self.target.mapping,
171
185
            specific_file=specific_files)
172
186
 
173
187