/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

  • Committer: Jelmer Vernooij
  • Date: 2009-07-25 14:44:25 UTC
  • mto: (0.200.597 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20090725144425-n42ho5hyytcav2xa
Support sending git am-style patches with "bzr send --format=git". 
Main thing missing at the moment is support for multiple revisions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
2
2
 
3
 
# Based on a similar file written for bzr-svn:
 
3
# Based on the original from bzr-svn:
4
4
# Copyright (C) 2009 Lukas Lalinsky <lalinsky@gmail.com>
 
5
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
5
6
 
6
7
# This program is free software; you can redistribute it and/or modify
7
8
# it under the terms of the GNU General Public License as published by
18
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
 
20
21
import time
 
22
import bzrlib
21
23
from bzrlib import (
22
24
    branch as _mod_branch,
23
 
    errors,
 
25
    diff as _mod_diff,
24
26
    merge_directive,
25
27
    osutils,
26
 
    revision as _mod_revision
27
 
    )
28
 
 
29
 
 
30
 
class GitDiffWriter(object):
31
 
 
32
 
    def __init__(self, repository, base_revision_id):
33
 
        self.repository = repository
34
 
        self.base_revision_id = base_revision_id
35
 
        self.tree_rev_info = {}
36
 
 
37
 
    def get_git_rev_info(self, tree):
38
 
        if tree in self.tree_rev_info:
39
 
            return self.tree_rev_info[tree]
40
 
        revision_id = tree.get_revision_id()
41
 
        if revision_id == self.base_revision_id:
42
 
            rev_info = '(working copy)'
43
 
        elif _mod_revision.is_null(revision_id):
44
 
            rev_info = '(revision 0)'
45
 
        else:
46
 
            info = self.repository.lookup_revision_id(revision_id)
47
 
            rev_info = '(revision %d)' % info[0][2]
48
 
        self.tree_rev_info[tree] = rev_info
49
 
        return rev_info
50
 
 
51
 
    def diff_text(self, difftext, file_id, old_path, new_path, old_kind, new_kind):
52
 
        if 'file' not in (old_kind, new_kind):
53
 
            return difftext.CANNOT_DIFF
54
 
        from_file_id = to_file_id = file_id
55
 
        if old_kind == 'file':
56
 
            old_date = self.get_git_rev_info(difftext.old_tree)
57
 
        elif old_kind is None:
58
 
            old_date = None
59
 
            from_file_id = None
60
 
        else:
61
 
            return difftext.CANNOT_DIFF
62
 
        if new_kind == 'file':
63
 
            new_date = self.get_git_rev_info(difftext.new_tree)
64
 
        elif new_kind is None:
65
 
            new_date = None
66
 
            to_file_id = None
67
 
        else:
68
 
            return difftext.CANNOT_DIFF
69
 
        from_label = '%s%s\t%s' % (difftext.old_label, old_path, old_date or '(revision 0)')
70
 
        to_label = '%s%s\t%s' % (difftext.new_label, new_path, new_date or '(revision 0)')
71
 
        return difftext.diff_text(from_file_id, to_file_id, from_label, to_label)
 
28
    revision as _mod_revision,
 
29
    )
 
30
 
 
31
from bzrlib.plugins.git import (
 
32
    version_info as bzr_git_version_info,
 
33
    )
 
34
from bzrlib.plugins.git.mapping import (
 
35
    object_mode,
 
36
    )
 
37
from bzrlib.plugins.git.object_store import (
 
38
    get_object_store,
 
39
    )
 
40
 
 
41
from cStringIO import StringIO
 
42
from dulwich.objects import (
 
43
    Blob,
 
44
    )
 
45
 
 
46
 
 
47
class GitDiffTree(_mod_diff.DiffTree):
 
48
    """Provides a text representation between two trees, formatted for svn."""
 
49
 
 
50
    def __init__(self, old_tree, new_tree, to_file, path_encoding='utf-8',
 
51
                 diff_text=None, extra_factories=None):
 
52
        super(GitDiffTree, self).__init__(old_tree, new_tree, to_file, path_encoding,
 
53
            diff_text, extra_factories)
 
54
 
 
55
    def _write_file_mode(self, old_mode, new_mode):
 
56
        if old_mode == new_mode:
 
57
            return
 
58
        if new_mode is not None:
 
59
            if old_mode is not None:
 
60
                self.to_file.write("old file mode %o\n" % old_mode)
 
61
            self.to_file.write("new file mode %o\n" % new_mode) 
 
62
        else:
 
63
            self.to_file.write("deleted file mode %o\n" % old_mode)
 
64
 
 
65
    def _get_rev(self, contents):
 
66
        if contents is None:
 
67
            return "0" * 7
 
68
        else:
 
69
            return Blob.from_string("".join(contents)).id[:7]
 
70
 
 
71
    def _write_contents_diff(self, old_path, old_mode, old_contents, new_path, new_mode, new_contents):
 
72
        if old_path is None:
 
73
            old_path = "/dev/null"
 
74
        else:
 
75
            old_path = "a/%s" % old_path
 
76
        if new_path is None:
 
77
            new_path = "/dev/null"
 
78
        else:
 
79
            new_path = "b/%s" % new_path
 
80
        self.to_file.write("diff --git %s %s\n" % (old_path, new_path))
 
81
        self._write_file_mode(old_mode, new_mode)
 
82
        old_rev = self._get_rev(old_contents)
 
83
        new_rev = self._get_rev(new_contents)
 
84
        self.to_file.write("index %s..%s %o\n" % (old_rev, new_rev, new_mode))
 
85
        _mod_diff.internal_diff(old_path, old_contents,
 
86
                                new_path, new_contents,
 
87
                                self.to_file)
 
88
 
 
89
    def _get_file_mode(self, tree, path, kind, executable):
 
90
        if path is None:
 
91
            return None
 
92
        return object_mode(kind, executable)
 
93
 
 
94
    def _show_diff(self, specific_files, extra_trees):
 
95
        iterator = self.new_tree.iter_changes(self.old_tree,
 
96
                                               specific_files=specific_files,
 
97
                                               extra_trees=extra_trees,
 
98
                                               require_versioned=True)
 
99
        has_changes = 0
 
100
        def get_encoded_path(path):
 
101
            if path is not None:
 
102
                return path.encode(self.path_encoding, "replace")
 
103
        for (file_id, paths, changed_content, versioned, parent, name, kind,
 
104
             executable) in iterator:
 
105
            # The root does not get diffed, and items with no known kind (that
 
106
            # is, missing) in both trees are skipped as well.
 
107
            if parent == (None, None) or kind == (None, None):
 
108
                continue
 
109
            oldpath, newpath = paths
 
110
            oldpath_encoded = get_encoded_path(paths[0])
 
111
            newpath_encoded = get_encoded_path(paths[1])
 
112
            old_present = (kind[0] is not None and versioned[0])
 
113
            if old_present is not None:
 
114
                old_contents = self.old_tree.get_file(file_id).readlines()
 
115
            else:
 
116
                old_contents = None
 
117
            new_present = (kind[1] is not None and versioned[1])
 
118
            if new_present is not None:
 
119
                new_contents = self.new_tree.get_file(file_id).readlines()
 
120
            else:
 
121
                new_contents = None
 
122
            renamed = (parent[0], name[0]) != (parent[1], name[1])
 
123
            old_mode = self._get_file_mode(self.old_tree, oldpath_encoded, kind[0], executable[0])
 
124
            new_mode = self._get_file_mode(self.new_tree, newpath_encoded, kind[1], executable[1])
 
125
 
 
126
            self._write_contents_diff(oldpath_encoded, old_mode, old_contents, 
 
127
                                      newpath_encoded, new_mode, new_contents)
 
128
 
 
129
            has_changes = (changed_content or renamed)
 
130
 
 
131
        return has_changes
72
132
 
73
133
 
74
134
class GitMergeDirective(merge_directive._BaseMergeDirective):
77
137
        return self.patch.splitlines(True)
78
138
 
79
139
    @classmethod
80
 
    def _generate_diff(cls, repository, target_repository, revision_id, ancestor_id):
81
 
        from bzrlib.diff import DiffText
82
 
        writer = GitDiffWriter(target_repository, revision_id)
83
 
        def DiffText_diff(self, file_id, old_path, new_path, old_kind, new_kind):
84
 
            return writer.diff_text(self, file_id, old_path, new_path, old_kind, new_kind)
85
 
        old_DiffText_diff = DiffText.diff
86
 
        DiffText.diff = DiffText_diff
87
 
        patch = merge_directive._BaseMergeDirective._generate_diff(
88
 
            repository, revision_id, ancestor_id)
89
 
        DiffText.diff = old_DiffText_diff
90
 
        return patch
 
140
    def _generate_commit(cls, repository, revision_id, num, total):
 
141
        s = StringIO()
 
142
        store = get_object_store(repository)
 
143
        commit = store[store._lookup_revision_sha1(revision_id)]
 
144
        s.write("From %s %s\n" % (commit.id, time.ctime(commit.commit_time)))
 
145
        s.write("From: %s\n" % commit.author)
 
146
        s.write("Date: %s\n" % time.strftime("%a, %d %b %Y %H:%M:%S %Z"))
 
147
        s.write("Subject: [PATCH %d/%d] %s\n" % (num, total, commit.message))
 
148
        s.write("\n")
 
149
        s.write("---\n")
 
150
        s.write("TODO: Print diffstat\n")
 
151
        s.write("\n")
 
152
        try:
 
153
            lhs_parent = repository.get_revision(revision_id).parent_ids[0]
 
154
        except IndexError:
 
155
            lhs_parent = _mod_revision.NULL_REVISION
 
156
        tree_1 = repository.revision_tree(lhs_parent)
 
157
        tree_2 = repository.revision_tree(revision_id)
 
158
        differ = GitDiffTree.from_trees_options(tree_1, tree_2, s, 'utf8', None,
 
159
            'a/', 'b/', None)
 
160
        differ.show_diff(None, None)
 
161
        s.write("-- \n")
 
162
        s.write("bzr %s, bzr-git %d.%d.%d\n" % ((bzrlib.__version__, ) + bzr_git_version_info[:3]))
 
163
        summary = "%04d-%s" % (num, commit.message.splitlines()[0].replace(" ", "-"))
 
164
        return summary, s.getvalue()
91
165
 
92
166
    @classmethod
93
167
    def from_objects(cls, repository, revision_id, time, timezone,
94
168
                     target_branch, local_target_branch=None,
95
169
                     public_branch=None, message=None):
96
170
        submit_branch = _mod_branch.Branch.open(target_branch)
97
 
        if submit_branch.get_parent() is not None:
98
 
            submit_branch = _mod_branch.Branch.open(submit_branch.get_parent())
99
 
 
100
171
        submit_branch.lock_read()
101
172
        try:
102
173
            submit_revision_id = submit_branch.last_revision()
103
 
            submit_revision_id = _mod_revision.ensure_null(submit_revision_id)
104
174
            repository.fetch(submit_branch.repository, submit_revision_id)
105
 
            graph = repository.get_graph()
106
 
            ancestor_id = graph.find_unique_lca(revision_id,
107
 
                                                submit_revision_id)
108
 
            patch = cls._generate_diff(repository, submit_branch.repository,
109
 
                                    revision_id, ancestor_id)
 
175
            summary, patch = cls._generate_commit(repository, revision_id, 1, 1)
110
176
        finally:
111
177
            submit_branch.unlock()
112
178
        return cls(revision_id, None, time, timezone, target_branch,