4
# Copyright (C) 2011-2018 Jelmer Vernooij <jelmer@jelmer.uk>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
"""Remote helper for git for accessing bzr repositories."""
23
from __future__ import absolute_import
25
CAPABILITIES = ["fetch", "option", "push"]
29
from ..controldir import ControlDir
30
from ..errors import NotBranchError, NoRepositoryPresent
31
from ..repository import InterRepository
32
from ..sixish import viewitems
33
from ..transport import get_transport_from_path
39
BareLocalGitControlDirFormat,
40
LocalGitControlDirFormat,
43
from .object_store import (
50
from .repository import (
54
from ..plugins.fastimport import exporter as fastexporter
57
import fastimport # noqa: F401
61
CAPABILITIES.append("import")
64
def open_remote_dir(url):
66
return ControlDir.open(url)
67
except NotBranchError:
68
return ControlDir.create(url)
71
def fetch(outf, wants, shortname, remote_dir, local_dir):
72
remote_repo = remote_dir.find_repository()
73
local_repo = local_dir.find_repository()
74
inter = InterRepository.get(remote_repo, local_repo)
76
for (sha1, ref) in wants:
77
revs.append((sha1, None))
78
if (isinstance(remote_repo, GitRepository) and
79
isinstance(local_repo, GitRepository)):
83
inter.fetch_objects(revs, lossy=lossy)
87
def push(outf, wants, shortname, remote_dir, local_dir):
88
for (src_ref, dest_ref) in wants:
89
local_branch = local_dir.open_branch(ref=src_ref)
90
dest_branch_name = ref_to_branch_name(dest_ref)
91
if dest_branch_name == "master":
92
dest_branch_name = None
94
remote_branch = remote_dir.open_branch(name=dest_branch_name)
95
except NotBranchError:
96
remote_branch = remote_dir.create_branch(name=dest_branch_name)
97
local_branch.push(remote_branch)
98
outf.write(b"ok %s\n" % dest_ref)
102
class RemoteHelper(object):
103
"""Git remote helper."""
105
def __init__(self, local_dir, shortname, remote_dir):
106
self.local_dir = local_dir
107
self.shortname = shortname
108
self.remote_dir = remote_dir
112
def cmd_capabilities(self, outf, argv):
113
outf.write(b"\n".join([c.encode() for c in CAPABILITIES]) + b"\n\n")
115
def cmd_list(self, outf, argv):
117
repo = self.remote_dir.find_repository()
118
except NoRepositoryPresent:
119
repo = self.remote_dir.create_repository()
120
object_store = get_object_store(repo)
121
with object_store.lock_read():
122
refs = get_refs_container(self.remote_dir, object_store)
123
for ref, git_sha1 in viewitems(refs.as_dict()):
124
ref = ref.replace(b"~", b"_")
125
outf.write(b"%s %s\n" % (git_sha1, ref))
128
def cmd_option(self, outf, argv):
129
outf.write(b"unsupported\n")
131
def cmd_fetch(self, outf, argv):
132
if self.batchcmd not in (None, "fetch"):
133
raise Exception("fetch command inside other batch command")
134
self.wants.append(tuple(argv[1:]))
135
self.batchcmd = "fetch"
137
def cmd_push(self, outf, argv):
138
if self.batchcmd not in (None, "push"):
139
raise Exception("push command inside other batch command")
140
self.wants.append(tuple(argv[1].split(":", 1)))
141
self.batchcmd = "push"
143
def cmd_import(self, outf, argv):
144
if "fastimport" in CAPABILITIES:
145
raise Exception("install fastimport for 'import' command support")
146
ref = argv[1].encode('utf-8')
147
dest_branch_name = ref_to_branch_name(ref)
148
if dest_branch_name == "master":
149
dest_branch_name = None
150
remote_branch = self.remote_dir.open_branch(name=dest_branch_name)
151
exporter = fastexporter.BzrFastExporter(
152
remote_branch, outf=outf, ref=ref, checkpoint=None,
153
import_marks_file=None, export_marks_file=None, revision=None,
154
verbose=None, plain_format=True, rewrite_tags=False)
158
"capabilities": cmd_capabilities,
160
"option": cmd_option,
163
"import": cmd_import,
166
def process(self, inf, outf):
168
line = inf.readline()
171
self.process_line(line, outf)
173
def process_line(self, l, outf):
174
argv = l.strip().split()
176
if self.batchcmd == "fetch":
177
fetch(outf, self.wants, self.shortname,
178
self.remote_dir, self.local_dir)
179
elif self.batchcmd == "push":
180
push(outf, self.wants, self.shortname,
181
self.remote_dir, self.local_dir)
182
elif self.batchcmd is None:
185
raise AssertionError("invalid batch %r" % self.batchcmd)
189
self.commands[argv[0]](self, outf, argv)
191
raise Exception("Unknown remote command %r" % argv)
195
def open_local_dir():
197
git_path = os.environ["GIT_DIR"]
199
git_transport = get_transport_from_path(".")
200
git_format = LocalGitProber().probe_transport(git_transport)
202
if git_path.endswith("/.git"):
203
git_format = LocalGitControlDirFormat()
204
git_path = git_path[:-4]
206
git_format = BareLocalGitControlDirFormat()
207
git_transport = get_transport_from_path(git_path)
209
return git_format.open(git_transport)