/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.238.1 by Lukas Lalinsky, Jelmer Vernooij
Import initial work on 'bzr send --format=git' based on luks' patch for bzr-svn.
1
# Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
2
3
# Based on a similar file written for bzr-svn:
4
# Copyright (C) 2009 Lukas Lalinsky <lalinsky@gmail.com>
5
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
import time
21
from bzrlib import (
22
    branch as _mod_branch,
23
    errors,
24
    merge_directive,
25
    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_svn_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_svn_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_svn_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)
72
73
74
class GitMergeDirective(merge_directive._BaseMergeDirective):
75
76
    def to_lines(self):
77
        return self.patch.splitlines(True)
78
79
    @classmethod
80
    def _generate_diff(cls, repository, svn_repository, revision_id, ancestor_id):
81
        from bzrlib.diff import DiffText
82
        writer = GitDiffWriter(svn_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
91
92
    @classmethod
93
    def from_objects(cls, repository, revision_id, time, timezone,
94
                     target_branch, local_target_branch=None,
95
                     public_branch=None, message=None):
96
        from bzrlib.plugins.git.repository import GitRepository
97
        submit_branch = _mod_branch.Branch.open(target_branch)
98
        if submit_branch.get_parent() is not None:
99
            submit_branch = _mod_branch.Branch.open(submit_branch.get_parent())
100
        if not isinstance(submit_branch.repository, GitRepository):
101
            raise errors.BzrError("Not a Subversion repository")
102
103
        submit_branch.lock_read()
104
        try:
105
            submit_revision_id = submit_branch.last_revision()
106
            submit_revision_id = _mod_revision.ensure_null(submit_revision_id)
107
            repository.fetch(submit_branch.repository, submit_revision_id)
108
            graph = repository.get_graph()
109
            ancestor_id = graph.find_unique_lca(revision_id,
110
                                                submit_revision_id)
111
            patch = cls._generate_diff(repository, submit_branch.repository,
112
                                    revision_id, ancestor_id)
113
        finally:
114
            submit_branch.unlock()
115
        return cls(revision_id, None, time, timezone, target_branch,
116
            patch, None, public_branch, message)
117
118
119
def send_svn(branch, revision_id, submit_branch, public_branch,
120
              no_patch, no_bundle, message, base_revision_id):
121
    return GitMergeDirective.from_objects(
122
        branch.repository, revision_id, time.time(),
123
        osutils.local_time_offset(), submit_branch,
124
        public_branch=public_branch, message=message)