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

Include dulwich version in version tail.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
from bzrlib import (
26
26
    branch as _mod_branch,
27
27
    diff as _mod_diff,
28
 
    errors,
 
28
    merge_directive,
29
29
    osutils,
30
30
    revision as _mod_revision,
31
31
    )
32
32
 
33
 
try:
34
 
    from bzrlib.merge_directive import BaseMergeDirective
35
 
except ImportError:
36
 
    from bzrlib.merge_directive import (
37
 
        _BaseMergeDirective as BaseMergeDirective,
38
 
        )
39
 
 
40
33
from bzrlib.plugins.git import (
41
34
    version_info as bzr_git_version_info,
42
35
    )
63
56
class GitDiffTree(_mod_diff.DiffTree):
64
57
    """Provides a text representation between two trees, formatted for svn."""
65
58
 
 
59
    def _get_file_mode(self, tree, path, kind, executable):
 
60
        if path is None:
 
61
            return None
 
62
        return object_mode(kind, executable)
 
63
 
66
64
    def _show_diff(self, specific_files, extra_trees):
67
65
        from dulwich.patch import write_blob_diff
68
66
        iterator = self.new_tree.iter_changes(self.old_tree,
72
70
        def get_encoded_path(path):
73
71
            if path is not None:
74
72
                return path.encode(self.path_encoding, "replace")
75
 
        def get_file_mode(tree, path, kind, executable):
76
 
            if path is None:
77
 
                return 0
78
 
            return object_mode(kind, executable)
79
 
        def get_blob(present, tree, file_id):
80
 
            if present:
81
 
                return Blob.from_string(tree.get_file(file_id).read())
82
 
            else:
83
 
                return None
84
 
        trees = (self.old_tree, self.new_tree)
85
73
        for (file_id, paths, changed_content, versioned, parent, name, kind,
86
74
             executable) in iterator:
87
75
            # The root does not get diffed, and items with no known kind (that
88
76
            # is, missing) in both trees are skipped as well.
89
77
            if parent == (None, None) or kind == (None, None):
90
78
                continue
91
 
            path_encoded = (get_encoded_path(paths[0]),
92
 
                            get_encoded_path(paths[1]))
93
 
            present = ((kind[0] not in (None, 'directory')),
94
 
                       (kind[1] not in (None, 'directory')))
95
 
            if not present[0] and not present[1]:
96
 
                continue
97
 
            contents = (get_blob(present[0], trees[0], file_id),
98
 
                        get_blob(present[1], trees[1], file_id))
 
79
            oldpath, newpath = paths
 
80
            oldpath_encoded = get_encoded_path(paths[0])
 
81
            newpath_encoded = get_encoded_path(paths[1])
 
82
            old_present = (kind[0] is not None and versioned[0])
 
83
            if old_present is not None:
 
84
                old_contents = Blob.from_string(self.old_tree.get_file(file_id).read())
 
85
            else:
 
86
                old_contents = None
 
87
            new_present = (kind[1] is not None and versioned[1])
 
88
            if new_present is not None:
 
89
                new_contents = Blob.from_string(self.new_tree.get_file(file_id).read())
 
90
            else:
 
91
                new_contents = None
99
92
            renamed = (parent[0], name[0]) != (parent[1], name[1])
100
 
            mode = (get_file_mode(trees[0], path_encoded[0],
101
 
                                  kind[0], executable[0]),
102
 
                    get_file_mode(trees[1], path_encoded[1],
103
 
                                  kind[1], executable[1]))
104
 
            write_blob_diff(self.to_file,
105
 
                (path_encoded[0], mode[0], contents[0]),
106
 
                (path_encoded[1], mode[1], contents[1]))
107
 
            has_changes |= (changed_content or renamed)
 
93
            old_mode = self._get_file_mode(self.old_tree, oldpath_encoded, 
 
94
                                           kind[0], executable[0])
 
95
            new_mode = self._get_file_mode(self.new_tree, newpath_encoded,
 
96
                                           kind[1], executable[1])
 
97
            write_blob_diff(self.to_file, 
 
98
                (oldpath_encoded, old_mode, old_contents), 
 
99
                (newpath_encoded, new_mode, new_contents))
 
100
            has_changes = (changed_content or renamed)
108
101
        return has_changes
109
102
 
110
103
 
111
 
class GitMergeDirective(BaseMergeDirective):
112
 
 
113
 
    multiple_output_files = True
114
 
 
115
 
    def __init__(self, revision_id, testament_sha1, time, timezone,
116
 
                 target_branch, source_branch=None, message=None,
117
 
                 patches=None):
118
 
        super(GitMergeDirective, self).__init__(revision_id=revision_id,
119
 
            testament_sha1=testament_sha1, time=time, timezone=timezone,
120
 
            target_branch=target_branch, patch=None,
121
 
            source_branch=source_branch, message=message, bundle=None)
122
 
        self.patches = patches
 
104
class GitMergeDirective(merge_directive._BaseMergeDirective):
123
105
 
124
106
    def to_lines(self):
125
107
        return self.patch.splitlines(True)
126
108
 
127
 
    def to_files(self):
128
 
        return self.patches
129
 
 
130
109
    @classmethod
131
110
    def _generate_commit(cls, repository, revision_id, num, total):
132
111
        s = StringIO()
140
119
        tree_1 = repository.revision_tree(lhs_parent)
141
120
        tree_2 = repository.revision_tree(revision_id)
142
121
        contents = StringIO()
143
 
        differ = GitDiffTree.from_trees_options(tree_1, tree_2,
 
122
        differ = GitDiffTree.from_trees_options(tree_1, tree_2, 
144
123
                contents, 'utf8', None, 'a/', 'b/', None)
145
124
        differ.show_diff(None, None)
146
 
        write_commit_patch(s, commit, contents.getvalue(), (num, total),
 
125
        write_commit_patch(s, commit, contents.getvalue(), (num, total), 
147
126
                           version_tail)
148
 
        summary = "%04d-%s.patch" % (num, get_summary(commit).rstrip("."))
 
127
        summary = "%04d-%s" % (num, get_summary(commit))
149
128
        return summary, s.getvalue()
150
129
 
151
130
    @classmethod
162
141
            todo = graph.find_difference(submit_revision_id, revision_id)[1]
163
142
            total = len(todo)
164
143
            for i, revid in enumerate(graph.iter_topo_order(todo)):
165
 
                patches.append(cls._generate_commit(repository, revid, i+1,
166
 
                    total))
 
144
                patches.append(cls._generate_commit(repository, revid, i+1, 
 
145
                                                    total))
167
146
        finally:
168
147
            submit_branch.unlock()
169
 
        return cls(revision_id, None, time, timezone,
170
 
            target_branch=target_branch, source_branch=public_branch,
171
 
            message=message, patches=patches)
172
 
 
173
 
 
174
 
def send_git(branch, revision_id, submit_branch, public_branch, no_patch,
175
 
             no_bundle, message, base_revision_id):
176
 
    if no_patch:
177
 
        raise errors.BzrCommandError("no patch not supported for git-am style patches")
178
 
    if no_bundle:
179
 
        raise errors.BzrCommandError("no bundle not supported for git-am style patches")
 
148
        return cls(revision_id, None, time, timezone, target_branch, 
 
149
            "".join([patch for (summary, patch) in patches]), 
 
150
            None, public_branch, message)
 
151
 
 
152
 
 
153
def send_git(branch, revision_id, submit_branch, public_branch,
 
154
              no_patch, no_bundle, message, base_revision_id):
180
155
    return GitMergeDirective.from_objects(
181
156
        branch.repository, revision_id, time.time(),
182
157
        osutils.local_time_offset(), submit_branch,