/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:
34
34
 
35
35
 
36
36
class GitRevisionTree(revisiontree.RevisionTree):
 
37
    """Revision tree implementation based on Git objects."""
37
38
 
38
39
    def __init__(self, repository, revision_id):
39
40
        self._revision_id = revision_id
40
41
        self._repository = repository
41
42
        store = repository._git.object_store
42
43
        assert isinstance(revision_id, str)
43
 
        git_id, self.mapping = repository.lookup_git_revid(revision_id)
 
44
        git_id, self.mapping = repository.lookup_bzr_revision_id(revision_id)
44
45
        try:
45
46
            commit = store[git_id]
46
47
        except KeyError, r:
47
48
            raise errors.NoSuchRevision(repository, revision_id)
48
49
        self.tree = commit.tree
49
 
        self._inventory = GitInventory(self.tree, self.mapping, store, 
50
 
                                       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)
51
53
 
52
54
    def get_revision_id(self):
 
55
        """See RevisionTree.get_revision_id."""
53
56
        return self._revision_id
54
57
 
55
58
    def get_file_text(self, file_id, path=None):
 
59
        """See RevisionTree.get_file_text."""
56
60
        if path is not None:
57
61
            entry = self._inventory._get_ie(path)
58
62
        else:
59
63
            entry = self._inventory[file_id]
60
 
        if entry.kind == 'directory': return ""
 
64
        if entry.kind in ('directory', 'tree-reference'):
 
65
            return ""
61
66
        return entry.object.data
62
67
 
63
68
 
64
 
def tree_delta_from_git_changes(changes, mapping, specific_file=None, 
65
 
                                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):
66
72
    """Create a TreeDelta from two git trees.
67
 
    
68
 
    source and target are iterators over tuples with: 
 
73
 
 
74
    source and target are iterators over tuples with:
69
75
        (filename, sha, mode)
70
76
    """
71
77
    ret = delta.TreeDelta()
72
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
73
85
        if oldpath is None:
74
 
            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)))
75
87
        elif newpath is None:
76
 
            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)))
77
89
        elif oldpath != newpath:
78
 
            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)))
79
91
        elif mode_kind(oldmode) != mode_kind(newmode):
80
 
            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)))
81
93
        elif oldsha != newsha or oldmode != newmode:
82
 
            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)))
83
95
        else:
84
 
            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)))
85
97
    return ret
86
98
 
87
99
 
88
 
def changes_from_git_changes(changes, mapping, specific_file=None, 
 
100
def changes_from_git_changes(changes, mapping, specific_file=None,
89
101
                                require_versioned=False):
90
102
    """Create a iter_changes-like generator from a git stream.
91
 
    
92
 
    source and target are iterators over tuples with: 
 
103
 
 
104
    source and target are iterators over tuples with:
93
105
        (filename, sha, mode)
94
106
    """
95
107
    for (oldpath, newpath), (oldmode, newmode), (oldsha, newsha) in changes:
126
138
                newname = newpath
127
139
            else:
128
140
                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)
 
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))
130
145
 
131
146
 
132
147
class InterGitRevisionTrees(tree.InterTree):
133
148
    """InterTree that works between two git revision trees."""
134
149
 
 
150
    _matching_from_tree_format = None
 
151
    _matching_to_tree_format = None
 
152
    _test_mutable_trees_to_test_trees = None
 
153
 
135
154
    @classmethod
136
155
    def is_compatible(cls, source, target):
137
 
        return (isinstance(source, GitRevisionTree) and 
 
156
        return (isinstance(source, GitRevisionTree) and
138
157
                isinstance(target, GitRevisionTree))
139
158
 
140
159
    def compare(self, want_unchanged=False, specific_files=None,
144
163
            raise AssertionError
145
164
        changes = self.source._repository._git.object_store.tree_changes(
146
165
            self.source.tree, self.target.tree, want_unchanged=want_unchanged)
147
 
        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),
148
174
            specific_file=specific_files)
149
175
 
150
176
    def iter_changes(self, include_unchanged=False, specific_files=None,
151
 
        pb=None, extra_trees=[], require_versioned=True, want_unversioned=False):
 
177
        pb=None, extra_trees=[], require_versioned=True,
 
178
        want_unversioned=False):
152
179
        if self.source._repository._git.object_store != self.target._repository._git.object_store:
153
180
            raise AssertionError
154
181
        changes = self.source._repository._git.object_store.tree_changes(
155
 
            self.source.tree, self.target.tree, 
 
182
            self.source.tree, self.target.tree,
156
183
            want_unchanged=include_unchanged)
157
 
        return changes_from_git_changes(changes, self.target.mapping, 
 
184
        return changes_from_git_changes(changes, self.target.mapping,
158
185
            specific_file=specific_files)
159
186
 
160
187