~bzr/ubuntu/jaunty/bzr-git/bzr-ppa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>

# Based on the original from bzr-svn:
# Copyright (C) 2009 Lukas Lalinsky <lalinsky@gmail.com>
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Support in "bzr send" for git-am style patches."""

import time
import bzrlib
from bzrlib import (
    branch as _mod_branch,
    diff as _mod_diff,
    errors,
    osutils,
    revision as _mod_revision,
    )

try:
    from bzrlib.merge_directive import BaseMergeDirective
except ImportError:
    from bzrlib.merge_directive import (
        _BaseMergeDirective as BaseMergeDirective,
        )

from bzrlib.plugins.git import (
    version_info as bzr_git_version_info,
    )
from bzrlib.plugins.git.mapping import (
    object_mode,
    )
from bzrlib.plugins.git.object_store import (
    get_object_store,
    )

from cStringIO import StringIO
from dulwich import (
    __version__ as dulwich_version,
    )
from dulwich.objects import (
    Blob,
    )


version_tail = "bzr %s, bzr-git %d.%d.%d, dulwich %d.%d.%d" % (
    (bzrlib.__version__, ) + bzr_git_version_info[:3] + dulwich_version[:3])


class GitDiffTree(_mod_diff.DiffTree):
    """Provides a text representation between two trees, formatted for svn."""

    def _show_diff(self, specific_files, extra_trees):
        from dulwich.patch import write_blob_diff
        iterator = self.new_tree.iter_changes(self.old_tree,
            specific_files=specific_files, extra_trees=extra_trees,
            require_versioned=True)
        has_changes = 0
        def get_encoded_path(path):
            if path is not None:
                return path.encode(self.path_encoding, "replace")
        def get_file_mode(tree, path, kind, executable):
            if path is None:
                return 0
            return object_mode(kind, executable)
        def get_blob(present, tree, file_id):
            if present:
                return Blob.from_string(tree.get_file(file_id).read())
            else:
                return None
        trees = (self.old_tree, self.new_tree)
        for (file_id, paths, changed_content, versioned, parent, name, kind,
             executable) in iterator:
            # The root does not get diffed, and items with no known kind (that
            # is, missing) in both trees are skipped as well.
            if parent == (None, None) or kind == (None, None):
                continue
            path_encoded = (get_encoded_path(paths[0]),
                            get_encoded_path(paths[1]))
            present = ((kind[0] not in (None, 'directory')),
                       (kind[1] not in (None, 'directory')))
            if not present[0] and not present[1]:
                continue
            contents = (get_blob(present[0], trees[0], file_id),
                        get_blob(present[1], trees[1], file_id))
            renamed = (parent[0], name[0]) != (parent[1], name[1])
            mode = (get_file_mode(trees[0], path_encoded[0],
                                  kind[0], executable[0]),
                    get_file_mode(trees[1], path_encoded[1],
                                  kind[1], executable[1]))
            write_blob_diff(self.to_file,
                (path_encoded[0], mode[0], contents[0]),
                (path_encoded[1], mode[1], contents[1]))
            has_changes |= (changed_content or renamed)
        return has_changes


class GitMergeDirective(BaseMergeDirective):

    multiple_output_files = True

    def __init__(self, revision_id, testament_sha1, time, timezone,
                 target_branch, source_branch=None, message=None,
                 patches=None):
        super(GitMergeDirective, self).__init__(revision_id=revision_id,
            testament_sha1=testament_sha1, time=time, timezone=timezone,
            target_branch=target_branch, patch=None,
            source_branch=source_branch, message=message, bundle=None)
        self.patches = patches

    def to_lines(self):
        return self.patch.splitlines(True)

    def to_files(self):
        return self.patches

    @classmethod
    def _generate_commit(cls, repository, revision_id, num, total):
        s = StringIO()
        store = get_object_store(repository)
        commit = store[store._lookup_revision_sha1(revision_id)]
        from dulwich.patch import write_commit_patch, get_summary
        try:
            lhs_parent = repository.get_revision(revision_id).parent_ids[0]
        except IndexError:
            lhs_parent = _mod_revision.NULL_REVISION
        tree_1 = repository.revision_tree(lhs_parent)
        tree_2 = repository.revision_tree(revision_id)
        contents = StringIO()
        differ = GitDiffTree.from_trees_options(tree_1, tree_2,
                contents, 'utf8', None, 'a/', 'b/', None)
        differ.show_diff(None, None)
        write_commit_patch(s, commit, contents.getvalue(), (num, total),
                           version_tail)
        summary = "%04d-%s.patch" % (num, get_summary(commit).rstrip("."))
        return summary, s.getvalue()

    @classmethod
    def from_objects(cls, repository, revision_id, time, timezone,
                     target_branch, local_target_branch=None,
                     public_branch=None, message=None):
        patches = []
        submit_branch = _mod_branch.Branch.open(target_branch)
        submit_branch.lock_read()
        try:
            submit_revision_id = submit_branch.last_revision()
            repository.fetch(submit_branch.repository, submit_revision_id)
            graph = repository.get_graph()
            todo = graph.find_difference(submit_revision_id, revision_id)[1]
            total = len(todo)
            for i, revid in enumerate(graph.iter_topo_order(todo)):
                patches.append(cls._generate_commit(repository, revid, i+1,
                    total))
        finally:
            submit_branch.unlock()
        return cls(revision_id, None, time, timezone,
            target_branch=target_branch, source_branch=public_branch,
            message=message, patches=patches)


def send_git(branch, revision_id, submit_branch, public_branch, no_patch,
             no_bundle, message, base_revision_id):
    if no_patch:
        raise errors.BzrCommandError("no patch not supported for git-am style patches")
    if no_bundle:
        raise errors.BzrCommandError("no bundle not supported for git-am style patches")
    return GitMergeDirective.from_objects(
        branch.repository, revision_id, time.time(),
        osutils.local_time_offset(), submit_branch,
        public_branch=public_branch, message=message)