/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.269.1 by Jelmer Vernooij
add git remote helper
1
#!/usr/bin/env python
0.269.2 by Jelmer Vernooij
Implement 'option' and 'list' in git-remote-bzr
2
# vim: expandtab
0.269.1 by Jelmer Vernooij
add git remote helper
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
0.200.1425 by Jelmer Vernooij
Don't print back trace when git-remote-bzr is interrupted.
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
0.269.8 by Jelmer Vernooij
Support push in git-remote-bzr.
31
CAPABILITIES = ["fetch", "option", "push"]
0.269.2 by Jelmer Vernooij
Implement 'option' and 'list' in git-remote-bzr
32
0.269.1 by Jelmer Vernooij
add git remote helper
33
import optparse
0.269.3 by Jelmer Vernooij
Add stub fetch implementation.
34
import os
0.269.1 by Jelmer Vernooij
add git remote helper
35
import bzrlib
36
bzrlib.initialize()
37
38
from bzrlib.plugin import load_plugins
39
load_plugins()
40
0.269.2 by Jelmer Vernooij
Implement 'option' and 'list' in git-remote-bzr
41
from bzrlib.controldir import ControlDir
0.269.10 by Jelmer Vernooij
support pushing to new repo
42
from bzrlib.errors import NotBranchError, NoRepositoryPresent
0.269.4 by Jelmer Vernooij
Implement fetch in git-remote-bzr
43
from bzrlib.repository import InterRepository
44
from bzrlib.transport import get_transport_from_path
45
0.269.6 by Jelmer Vernooij
Fix situation where GIT_DIR isn't set.
46
from bzrlib.plugins.git import (
47
    LocalGitProber,
48
    )
0.269.4 by Jelmer Vernooij
Implement fetch in git-remote-bzr
49
from bzrlib.plugins.git.dir import (
50
    BareLocalGitControlDirFormat,
51
    LocalGitControlDirFormat,
52
    )
0.269.2 by Jelmer Vernooij
Implement 'option' and 'list' in git-remote-bzr
53
54
from bzrlib.plugins.git.object_store import (
55
    get_object_store,
56
    )
57
from bzrlib.plugins.git.refs import (
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
58
    get_refs,
0.269.8 by Jelmer Vernooij
Support push in git-remote-bzr.
59
    ref_to_branch_name,
0.269.2 by Jelmer Vernooij
Implement 'option' and 'list' in git-remote-bzr
60
    )
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
61
from bzrlib.plugins.git.repository import (
62
    GitRepository,
63
    )
0.269.2 by Jelmer Vernooij
Implement 'option' and 'list' in git-remote-bzr
64
0.269.9 by Jelmer Vernooij
Support 'import' command (untested).
65
try:
66
    from bzrlib.plugins.fastimport import exporter as fastexporter
67
except ImportError:
68
    pass
69
else:
70
    CAPABILITIES.append("import")
71
0.269.2 by Jelmer Vernooij
Implement 'option' and 'list' in git-remote-bzr
72
options = {}
73
transports = []
74
0.269.3 by Jelmer Vernooij
Add stub fetch implementation.
75
def cmd_capabilities(argv, name, remote_dir):
0.269.2 by Jelmer Vernooij
Implement 'option' and 'list' in git-remote-bzr
76
    sys.stdout.write("\n".join(CAPABILITIES)+"\n\n")
77
    sys.stdout.flush()
78
79
0.269.3 by Jelmer Vernooij
Add stub fetch implementation.
80
def cmd_list(argv, name, remote_dir):
0.269.10 by Jelmer Vernooij
support pushing to new repo
81
    try:
82
        repo = remote_dir.find_repository()
83
    except NoRepositoryPresent:
84
        repo = remote_dir.create_repository()
0.269.2 by Jelmer Vernooij
Implement 'option' and 'list' in git-remote-bzr
85
    object_store = get_object_store(repo)
86
    object_store.lock_read()
87
    try:
0.200.1434 by Jelmer Vernooij
Move refs access to control dir.
88
        refs = get_refs(remote_dir, object_store)
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
89
        for ref, git_sha1 in refs.iteritems():
0.269.5 by Jelmer Vernooij
Don't claim to support things that are not.
90
            sys.stdout.write("%s %s\n" % (git_sha1, ref))
0.269.2 by Jelmer Vernooij
Implement 'option' and 'list' in git-remote-bzr
91
        sys.stdout.write("\n")
92
    finally:
93
        object_store.unlock()
94
    sys.stdout.flush()
95
96
0.269.3 by Jelmer Vernooij
Add stub fetch implementation.
97
def cmd_option(argv, name, remote_dir):
0.269.2 by Jelmer Vernooij
Implement 'option' and 'list' in git-remote-bzr
98
    sys.stdout.write("unsupported\n")
99
    sys.stdout.flush()
100
0.269.1 by Jelmer Vernooij
add git remote helper
101
0.269.3 by Jelmer Vernooij
Add stub fetch implementation.
102
batchcmd = None
0.269.4 by Jelmer Vernooij
Implement fetch in git-remote-bzr
103
wants = []
0.269.3 by Jelmer Vernooij
Add stub fetch implementation.
104
def cmd_fetch(argv, name, remote_dir):
105
    global batchcmd, wants
106
    if batchcmd not in (None, "fetch"):
107
        raise Exception("fetch command inside other batch command")
0.269.4 by Jelmer Vernooij
Implement fetch in git-remote-bzr
108
    wants.append(tuple(argv[1:]))
0.269.3 by Jelmer Vernooij
Add stub fetch implementation.
109
    batchcmd = "fetch"
110
111
0.269.8 by Jelmer Vernooij
Support push in git-remote-bzr.
112
def cmd_push(argv, name, remote_dir):
113
    global batchcmd, wants
114
    if batchcmd not in (None, "push"):
115
        raise Exception("push command inside other batch command")
116
    wants.append(tuple(argv[1].split(":", 1)))
117
    batchcmd = "push"
118
119
0.269.9 by Jelmer Vernooij
Support 'import' command (untested).
120
def cmd_import(argv, name, remote_dir):
121
    dest_branch_name = ref_to_branch_name(argv[1])
122
    if dest_branch_name == "master":
123
        dest_branch_name = None
124
    remote_branch = remote_dir.open_branch(name=dest_branch_name)
125
    exporter = fastexporter.BzrFastExporter(remote_branch,
126
        outf=sys.stdout, git_branch=argv[1],
127
        checkpoint=None, import_marks_file=None,
128
        export_marks_file=None, revision=None,
129
        verbose=None, plain_format=True,
130
        rewrite_tags=False)
131
    exporter.run()
132
    sys.stdout.flush()
133
134
0.269.4 by Jelmer Vernooij
Implement fetch in git-remote-bzr
135
def fetch(wants, shortname, remote_dir, local_dir):
136
    remote_repo = remote_dir.find_repository()
137
    local_repo = local_dir.find_repository()
138
    inter = InterRepository.get(remote_repo, local_repo)
139
    def update_refs(heads):
140
        ret = {}
141
        for (sha1, ref) in wants:
142
            ret[ref] = (sha1, None)
143
        return ret
0.200.1433 by Jelmer Vernooij
Fix fetching between git repositories.
144
    if (isinstance(remote_repo, GitRepository) and
145
        isinstance(local_repo, GitRepository)):
146
        lossy = False
147
    else:
148
        lossy = True
149
    inter.fetch_refs(update_refs, lossy=lossy)
0.269.3 by Jelmer Vernooij
Add stub fetch implementation.
150
    sys.stdout.write("\n")
151
    sys.stdout.flush()
152
153
0.269.8 by Jelmer Vernooij
Support push in git-remote-bzr.
154
def push(wants, shortname, remote_dir, local_dir):
155
    for (src_ref, dest_ref) in wants:
156
        local_branch = local_dir.open_branch(ref=src_ref)
157
        dest_branch_name = ref_to_branch_name(dest_ref)
158
        if dest_branch_name == "master":
159
            dest_branch_name = None
160
        try:
161
            remote_branch = remote_dir.open_branch(name=dest_branch_name)
162
        except NotBranchError:
163
            remote_branch = remote_dir.create_branch(name=dest_branch_name)
164
        local_branch.push(remote_branch)
165
        sys.stdout.write("ok %s\n" % dest_ref)
166
    sys.stdout.write("\n")
167
    sys.stdout.flush()
168
169
0.269.1 by Jelmer Vernooij
add git remote helper
170
commands = {
171
    "capabilities": cmd_capabilities,
0.269.2 by Jelmer Vernooij
Implement 'option' and 'list' in git-remote-bzr
172
    "list": cmd_list,
173
    "option": cmd_option,
0.269.3 by Jelmer Vernooij
Add stub fetch implementation.
174
    "fetch": cmd_fetch,
0.269.8 by Jelmer Vernooij
Support push in git-remote-bzr.
175
    "push": cmd_push,
0.269.9 by Jelmer Vernooij
Support 'import' command (untested).
176
    "import": cmd_import,
0.269.1 by Jelmer Vernooij
add git remote helper
177
    }
178
179
parser = optparse.OptionParser()
180
(opts, args) = parser.parse_args()
181
(shortname, url) = args
182
0.269.10 by Jelmer Vernooij
support pushing to new repo
183
try:
184
    remote_dir = ControlDir.open(url)
185
except NotBranchError:
186
    remote_dir = ControlDir.create(url)
0.269.2 by Jelmer Vernooij
Implement 'option' and 'list' in git-remote-bzr
187
0.269.6 by Jelmer Vernooij
Fix situation where GIT_DIR isn't set.
188
try:
189
    git_path = os.environ["GIT_DIR"]
190
except KeyError:
191
    git_transport = get_transport_from_path(".")
192
    git_format = LocalGitProber().probe_transport(git_transport)
0.269.4 by Jelmer Vernooij
Implement fetch in git-remote-bzr
193
else:
0.269.6 by Jelmer Vernooij
Fix situation where GIT_DIR isn't set.
194
    if git_path.endswith("/.git"):
195
        git_format = LocalGitControlDirFormat()
196
        git_path = git_path[:-4]
197
    else:
198
        git_format = BareLocalGitControlDirFormat()
199
    git_transport = get_transport_from_path(git_path)
0.269.4 by Jelmer Vernooij
Implement fetch in git-remote-bzr
200
0.269.6 by Jelmer Vernooij
Fix situation where GIT_DIR isn't set.
201
local_dir = git_format.open(git_transport)
0.269.4 by Jelmer Vernooij
Implement fetch in git-remote-bzr
202
0.269.1 by Jelmer Vernooij
add git remote helper
203
while True:
204
    l = sys.stdin.readline()
0.269.3 by Jelmer Vernooij
Add stub fetch implementation.
205
    if not l:
206
        break
0.269.1 by Jelmer Vernooij
add git remote helper
207
    argv = l.strip().split()
0.269.3 by Jelmer Vernooij
Add stub fetch implementation.
208
    if argv == []:
209
        if batchcmd == "fetch":
0.269.4 by Jelmer Vernooij
Implement fetch in git-remote-bzr
210
            fetch(wants, shortname, remote_dir, local_dir)
0.269.8 by Jelmer Vernooij
Support push in git-remote-bzr.
211
        elif batchcmd == "push":
212
            push(wants, shortname, remote_dir, local_dir)
0.269.4 by Jelmer Vernooij
Implement fetch in git-remote-bzr
213
        elif batchcmd is None:
214
            break
0.269.3 by Jelmer Vernooij
Add stub fetch implementation.
215
        else:
216
            raise AssertionError("invalid batch %r" % batchcmd)
0.269.4 by Jelmer Vernooij
Implement fetch in git-remote-bzr
217
        batchcmd = None
0.269.3 by Jelmer Vernooij
Add stub fetch implementation.
218
    else:
219
        try:
220
           commands[argv[0]](argv, shortname, remote_dir)
221
        except KeyError:
222
           raise Exception("Unknown remote command %r" % argv)