/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 determining revisions to fetch when fetching to git repo.

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