/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 git-remote-bzr

fix 2.3 and 2.4 compatibility.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# vim: expandtab
 
3
 
 
4
# Copyright (C) 2011 Jelmer Vernooij <jelmer@apache.org>
 
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
 
 
21
"""Remote helper for git for accessing bzr repositories."""
 
22
 
 
23
import signal
 
24
import sys
 
25
 
 
26
def handle_sigint(signal, frame):
 
27
    sys.exit(0)
 
28
 
 
29
signal.signal(signal.SIGINT, handle_sigint)
 
30
 
 
31
CAPABILITIES = ["fetch", "option", "push"]
 
32
 
 
33
import optparse
 
34
import os
 
35
import bzrlib
 
36
bzrlib.initialize()
 
37
 
 
38
from bzrlib.plugin import load_plugins
 
39
load_plugins()
 
40
 
 
41
from bzrlib.controldir import ControlDir
 
42
from bzrlib.errors import NotBranchError, NoRepositoryPresent
 
43
from bzrlib.repository import InterRepository
 
44
from bzrlib.transport import get_transport_from_path
 
45
 
 
46
from bzrlib.plugins.git import (
 
47
    LocalGitProber,
 
48
    )
 
49
from bzrlib.plugins.git.dir import (
 
50
    BareLocalGitControlDirFormat,
 
51
    LocalGitControlDirFormat,
 
52
    )
 
53
 
 
54
from bzrlib.plugins.git.object_store import (
 
55
    get_object_store,
 
56
    )
 
57
from bzrlib.plugins.git.refs import (
 
58
    BazaarRefsContainer,
 
59
    ref_to_branch_name,
 
60
    )
 
61
 
 
62
try:
 
63
    from bzrlib.plugins.fastimport import exporter as fastexporter
 
64
except ImportError:
 
65
    pass
 
66
else:
 
67
    CAPABILITIES.append("import")
 
68
 
 
69
options = {}
 
70
transports = []
 
71
 
 
72
def cmd_capabilities(argv, name, remote_dir):
 
73
    sys.stdout.write("\n".join(CAPABILITIES)+"\n\n")
 
74
    sys.stdout.flush()
 
75
 
 
76
 
 
77
def cmd_list(argv, name, remote_dir):
 
78
    try:
 
79
        repo = remote_dir.find_repository()
 
80
    except NoRepositoryPresent:
 
81
        repo = remote_dir.create_repository()
 
82
    object_store = get_object_store(repo)
 
83
    object_store.lock_read()
 
84
    try:
 
85
        refs = BazaarRefsContainer(remote_dir, object_store)
 
86
        for ref, git_sha1 in refs.as_dict().iteritems():
 
87
            sys.stdout.write("%s %s\n" % (git_sha1, ref))
 
88
        sys.stdout.write("\n")
 
89
    finally:
 
90
        object_store.unlock()
 
91
    sys.stdout.flush()
 
92
 
 
93
 
 
94
def cmd_option(argv, name, remote_dir):
 
95
    sys.stdout.write("unsupported\n")
 
96
    sys.stdout.flush()
 
97
 
 
98
 
 
99
batchcmd = None
 
100
wants = []
 
101
def cmd_fetch(argv, name, remote_dir):
 
102
    global batchcmd, wants
 
103
    if batchcmd not in (None, "fetch"):
 
104
        raise Exception("fetch command inside other batch command")
 
105
    wants.append(tuple(argv[1:]))
 
106
    batchcmd = "fetch"
 
107
 
 
108
 
 
109
def cmd_push(argv, name, remote_dir):
 
110
    global batchcmd, wants
 
111
    if batchcmd not in (None, "push"):
 
112
        raise Exception("push command inside other batch command")
 
113
    wants.append(tuple(argv[1].split(":", 1)))
 
114
    batchcmd = "push"
 
115
 
 
116
 
 
117
def cmd_import(argv, name, remote_dir):
 
118
    dest_branch_name = ref_to_branch_name(argv[1])
 
119
    if dest_branch_name == "master":
 
120
        dest_branch_name = None
 
121
    remote_branch = remote_dir.open_branch(name=dest_branch_name)
 
122
    exporter = fastexporter.BzrFastExporter(remote_branch,
 
123
        outf=sys.stdout, git_branch=argv[1],
 
124
        checkpoint=None, import_marks_file=None,
 
125
        export_marks_file=None, revision=None,
 
126
        verbose=None, plain_format=True,
 
127
        rewrite_tags=False)
 
128
    exporter.run()
 
129
    sys.stdout.flush()
 
130
 
 
131
 
 
132
def fetch(wants, shortname, remote_dir, local_dir):
 
133
    remote_repo = remote_dir.find_repository()
 
134
    local_repo = local_dir.find_repository()
 
135
    inter = InterRepository.get(remote_repo, local_repo)
 
136
    def update_refs(heads):
 
137
        ret = {}
 
138
        for (sha1, ref) in wants:
 
139
            ret[ref] = (sha1, None)
 
140
        return ret
 
141
    inter.fetch_refs(update_refs, lossy=True)
 
142
    sys.stdout.write("\n")
 
143
    sys.stdout.flush()
 
144
 
 
145
 
 
146
def push(wants, shortname, remote_dir, local_dir):
 
147
    for (src_ref, dest_ref) in wants:
 
148
        local_branch = local_dir.open_branch(ref=src_ref)
 
149
        dest_branch_name = ref_to_branch_name(dest_ref)
 
150
        if dest_branch_name == "master":
 
151
            dest_branch_name = None
 
152
        try:
 
153
            remote_branch = remote_dir.open_branch(name=dest_branch_name)
 
154
        except NotBranchError:
 
155
            remote_branch = remote_dir.create_branch(name=dest_branch_name)
 
156
        local_branch.push(remote_branch)
 
157
        sys.stdout.write("ok %s\n" % dest_ref)
 
158
    sys.stdout.write("\n")
 
159
    sys.stdout.flush()
 
160
 
 
161
 
 
162
commands = {
 
163
    "capabilities": cmd_capabilities,
 
164
    "list": cmd_list,
 
165
    "option": cmd_option,
 
166
    "fetch": cmd_fetch,
 
167
    "push": cmd_push,
 
168
    "import": cmd_import,
 
169
    }
 
170
 
 
171
parser = optparse.OptionParser()
 
172
(opts, args) = parser.parse_args()
 
173
(shortname, url) = args
 
174
 
 
175
try:
 
176
    remote_dir = ControlDir.open(url)
 
177
except NotBranchError:
 
178
    remote_dir = ControlDir.create(url)
 
179
 
 
180
try:
 
181
    git_path = os.environ["GIT_DIR"]
 
182
except KeyError:
 
183
    git_transport = get_transport_from_path(".")
 
184
    git_format = LocalGitProber().probe_transport(git_transport)
 
185
else:
 
186
    if git_path.endswith("/.git"):
 
187
        git_format = LocalGitControlDirFormat()
 
188
        git_path = git_path[:-4]
 
189
    else:
 
190
        git_format = BareLocalGitControlDirFormat()
 
191
    git_transport = get_transport_from_path(git_path)
 
192
 
 
193
local_dir = git_format.open(git_transport)
 
194
 
 
195
while True:
 
196
    l = sys.stdin.readline()
 
197
    if not l:
 
198
        break
 
199
    argv = l.strip().split()
 
200
    if argv == []:
 
201
        if batchcmd == "fetch":
 
202
            fetch(wants, shortname, remote_dir, local_dir)
 
203
        elif batchcmd == "push":
 
204
            push(wants, shortname, remote_dir, local_dir)
 
205
        elif batchcmd is None:
 
206
            break
 
207
        else:
 
208
            raise AssertionError("invalid batch %r" % batchcmd)
 
209
        batchcmd = None
 
210
    else:
 
211
        try:
 
212
           commands[argv[0]](argv, shortname, remote_dir)
 
213
        except KeyError:
 
214
           raise Exception("Unknown remote command %r" % argv)