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
61
CAPABILITIES.append("import")
63
def open_remote_dir(url):
65
return ControlDir.open(url)
66
except NotBranchError:
67
return ControlDir.create(url)
70
def fetch(outf, wants, shortname, remote_dir, local_dir):
71
remote_repo = remote_dir.find_repository()
72
local_repo = local_dir.find_repository()
73
inter = InterRepository.get(remote_repo, local_repo)
75
for (sha1, ref) in wants:
76
revs.append((sha1, None))
77
if (isinstance(remote_repo, GitRepository) and
78
isinstance(local_repo, GitRepository)):
82
inter.fetch_objects(revs, lossy=lossy)
86
def push(outf, wants, shortname, remote_dir, local_dir):
87
for (src_ref, dest_ref) in wants:
88
local_branch = local_dir.open_branch(ref=src_ref)
89
dest_branch_name = ref_to_branch_name(dest_ref)
90
if dest_branch_name == "master":
91
dest_branch_name = None
93
remote_branch = remote_dir.open_branch(name=dest_branch_name)
94
except NotBranchError:
95
remote_branch = remote_dir.create_branch(name=dest_branch_name)
96
local_branch.push(remote_branch)
97
outf.write(b"ok %s\n" % dest_ref)
101
class RemoteHelper(object):
102
"""Git remote helper."""
104
def __init__(self, local_dir, shortname, remote_dir):
105
self.local_dir = local_dir
106
self.shortname = shortname
107
self.remote_dir = remote_dir
111
def cmd_capabilities(self, outf, argv):
112
outf.write(b"\n".join([c.encode() for c in CAPABILITIES])+b"\n\n")
114
def cmd_list(self, outf, argv):
116
repo = self.remote_dir.find_repository()
117
except NoRepositoryPresent:
118
repo = self.remote_dir.create_repository()
119
object_store = get_object_store(repo)
120
with object_store.lock_read():
121
refs = get_refs_container(self.remote_dir, object_store)
122
for ref, git_sha1 in viewitems(refs.as_dict()):
123
ref = ref.replace(b"~", b"_")
124
outf.write(b"%s %s\n" % (git_sha1, ref))
127
def cmd_option(self, outf, argv):
128
outf.write(b"unsupported\n")
130
def cmd_fetch(self, outf, argv):
131
if self.batchcmd not in (None, "fetch"):
132
raise Exception("fetch command inside other batch command")
133
self.wants.append(tuple(argv[1:]))
134
self.batchcmd = "fetch"
136
def cmd_push(self, outf, argv):
137
if self.batchcmd not in (None, "push"):
138
raise Exception("push command inside other batch command")
139
self.wants.append(tuple(argv[1].split(":", 1)))
140
self.batchcmd = "push"
142
def cmd_import(self, outf, argv):
143
if "fastimport" in CAPABILITIES:
144
raise Exception("install fastimport for 'import' command support")
145
ref = argv[1].encode('utf-8')
146
dest_branch_name = ref_to_branch_name(ref)
147
if dest_branch_name == "master":
148
dest_branch_name = None
149
remote_branch = self.remote_dir.open_branch(name=dest_branch_name)
150
exporter = fastexporter.BzrFastExporter(remote_branch,
152
checkpoint=None, import_marks_file=None,
153
export_marks_file=None, revision=None,
154
verbose=None, plain_format=True,
159
"capabilities": cmd_capabilities,
161
"option": cmd_option,
164
"import": cmd_import,
167
def process(self, inf, outf):
172
self.process_line(l, outf)
174
def process_line(self, l, outf):
175
argv = l.strip().split()
177
if self.batchcmd == "fetch":
178
fetch(outf, self.wants, self.shortname, self.remote_dir, self.local_dir)
179
elif self.batchcmd == "push":
180
push(outf, self.wants, self.shortname, self.remote_dir, self.local_dir)
181
elif self.batchcmd is None:
184
raise AssertionError("invalid batch %r" % self.batchcmd)
188
self.commands[argv[0]](self, outf, argv)
190
raise Exception("Unknown remote command %r" % argv)
194
def open_local_dir():
196
git_path = os.environ["GIT_DIR"]
198
git_transport = get_transport_from_path(".")
199
git_format = LocalGitProber().probe_transport(git_transport)
201
if git_path.endswith("/.git"):
202
git_format = LocalGitControlDirFormat()
203
git_path = git_path[:-4]
205
git_format = BareLocalGitControlDirFormat()
206
git_transport = get_transport_from_path(git_path)
208
return git_format.open(git_transport)