1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# vim: expandtab
# Copyright (C) 2011-2018 Jelmer Vernooij <jelmer@jelmer.uk>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Remote helper for git for accessing bzr repositories."""
CAPABILITIES = ["fetch", "option", "push"]
import os
from ..controldir import ControlDir
from ..errors import NotBranchError, NoRepositoryPresent
from ..repository import InterRepository
from ..transport import get_transport_from_path
from . import (
LocalGitProber,
)
from .dir import (
BareLocalGitControlDirFormat,
LocalGitControlDirFormat,
)
from .object_store import (
get_object_store,
)
from .refs import (
get_refs_container,
ref_to_branch_name,
)
from .repository import (
GitRepository,
)
from ..plugins.fastimport import exporter as fastexporter
try:
import fastimport # noqa: F401
except ImportError:
pass
else:
CAPABILITIES.append("import")
CAPABILITIES.append("refspec *:*")
def open_remote_dir(url):
try:
return ControlDir.open(url)
except NotBranchError:
return ControlDir.create(url)
def fetch(outf, wants, shortname, remote_dir, local_dir):
remote_repo = remote_dir.find_repository()
local_repo = local_dir.find_repository()
inter = InterRepository.get(remote_repo, local_repo)
revs = []
for (sha1, ref) in wants:
revs.append((sha1, None))
if (isinstance(remote_repo, GitRepository) and
isinstance(local_repo, GitRepository)):
lossy = False
else:
lossy = True
inter.fetch_revs(revs, lossy=lossy)
outf.write(b"\n")
def push(outf, wants, shortname, remote_dir, local_dir):
for (src_ref, dest_ref) in wants:
local_branch = local_dir.open_branch(ref=src_ref)
dest_branch_name = ref_to_branch_name(dest_ref)
if dest_branch_name == "master":
dest_branch_name = None
try:
remote_branch = remote_dir.open_branch(name=dest_branch_name)
except NotBranchError:
remote_branch = remote_dir.create_branch(name=dest_branch_name)
local_branch.push(remote_branch)
outf.write(b"ok %s\n" % dest_ref)
outf.write(b"\n")
class RemoteHelper(object):
"""Git remote helper."""
def __init__(self, local_dir, shortname, remote_dir):
self.local_dir = local_dir
self.shortname = shortname
self.remote_dir = remote_dir
self.batchcmd = None
self.wants = []
def cmd_capabilities(self, outf, argv):
outf.write(b"\n".join([c.encode() for c in CAPABILITIES]) + b"\n\n")
def cmd_list(self, outf, argv):
try:
repo = self.remote_dir.find_repository()
except NoRepositoryPresent:
repo = self.remote_dir.create_repository()
object_store = get_object_store(repo)
with object_store.lock_read():
refs = get_refs_container(self.remote_dir, object_store)
for ref, git_sha1 in refs.as_dict().items():
ref = ref.replace(b"~", b"_")
outf.write(b"%s %s\n" % (git_sha1, ref))
outf.write(b"\n")
def cmd_option(self, outf, argv):
outf.write(b"unsupported\n")
def cmd_fetch(self, outf, argv):
if self.batchcmd not in (None, "fetch"):
raise Exception("fetch command inside other batch command")
self.wants.append(tuple(argv[1:]))
self.batchcmd = "fetch"
def cmd_push(self, outf, argv):
if self.batchcmd not in (None, "push"):
raise Exception("push command inside other batch command")
self.wants.append(tuple(argv[1].split(":", 1)))
self.batchcmd = "push"
def cmd_import(self, outf, argv):
if "fastimport" in CAPABILITIES:
raise Exception("install fastimport for 'import' command support")
ref = argv[1].encode('utf-8')
dest_branch_name = ref_to_branch_name(ref)
if dest_branch_name == "master":
dest_branch_name = None
remote_branch = self.remote_dir.open_branch(name=dest_branch_name)
exporter = fastexporter.BzrFastExporter(
remote_branch, outf=outf, ref=ref, checkpoint=None,
import_marks_file=None, export_marks_file=None, revision=None,
verbose=None, plain_format=True, rewrite_tags=False)
exporter.run()
commands = {
"capabilities": cmd_capabilities,
"list": cmd_list,
"option": cmd_option,
"fetch": cmd_fetch,
"push": cmd_push,
"import": cmd_import,
}
def process(self, inf, outf):
while True:
line = inf.readline()
if not line:
break
self.process_line(line, outf)
def process_line(self, l, outf):
argv = l.strip().split()
if argv == []:
if self.batchcmd == "fetch":
fetch(outf, self.wants, self.shortname,
self.remote_dir, self.local_dir)
elif self.batchcmd == "push":
push(outf, self.wants, self.shortname,
self.remote_dir, self.local_dir)
elif self.batchcmd is None:
return
else:
raise AssertionError("invalid batch %r" % self.batchcmd)
self.batchcmd = None
else:
try:
self.commands[argv[0].decode()](self, outf, argv)
except KeyError:
raise Exception("Unknown remote command %r" % argv)
outf.flush()
def open_local_dir():
try:
git_path = os.environ["GIT_DIR"]
except KeyError:
git_transport = get_transport_from_path(".")
git_format = LocalGitProber().probe_transport(git_transport)
else:
if git_path.endswith("/.git"):
git_format = LocalGitControlDirFormat()
git_path = git_path[:-4]
else:
git_format = BareLocalGitControlDirFormat()
git_transport = get_transport_from_path(git_path)
return git_format.open(git_transport)
|