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 ...transport import get_transport_from_path
38
BareLocalGitControlDirFormat,
39
LocalGitControlDirFormat,
42
from .object_store import (
49
from .repository import (
53
from ..fastimport import exporter as fastexporter
60
CAPABILITIES.append("import")
62
def open_remote_dir(url):
64
return ControlDir.open(url)
65
except NotBranchError:
66
return ControlDir.create(url)
69
def fetch(outf, wants, shortname, remote_dir, local_dir):
70
remote_repo = remote_dir.find_repository()
71
local_repo = local_dir.find_repository()
72
inter = InterRepository.get(remote_repo, local_repo)
74
for (sha1, ref) in wants:
75
revs.append((sha1, None))
76
if (isinstance(remote_repo, GitRepository) and
77
isinstance(local_repo, GitRepository)):
81
inter.fetch_objects(revs, lossy=lossy)
85
def push(outf, wants, shortname, remote_dir, local_dir):
86
for (src_ref, dest_ref) in wants:
87
local_branch = local_dir.open_branch(ref=src_ref)
88
dest_branch_name = ref_to_branch_name(dest_ref)
89
if dest_branch_name == "master":
90
dest_branch_name = None
92
remote_branch = remote_dir.open_branch(name=dest_branch_name)
93
except NotBranchError:
94
remote_branch = remote_dir.create_branch(name=dest_branch_name)
95
local_branch.push(remote_branch)
96
outf.write("ok %s\n" % dest_ref)
100
class RemoteHelper(object):
101
"""Git remote helper."""
103
def __init__(self, local_dir, shortname, remote_dir):
104
self.local_dir = local_dir
105
self.shortname = shortname
106
self.remote_dir = remote_dir
110
def cmd_capabilities(self, outf, argv):
111
outf.write("\n".join(CAPABILITIES)+"\n\n")
113
def cmd_list(self, outf, argv):
115
repo = self.remote_dir.find_repository()
116
except NoRepositoryPresent:
117
repo = self.remote_dir.create_repository()
118
object_store = get_object_store(repo)
119
with object_store.lock_read():
120
refs = get_refs_container(self.remote_dir, object_store)
121
for ref, git_sha1 in refs.as_dict().iteritems():
122
ref = ref.replace("~", "_")
123
outf.write("%s %s\n" % (git_sha1, ref))
126
def cmd_option(self, outf, argv):
127
outf.write("unsupported\n")
129
def cmd_fetch(self, outf, argv):
130
if self.batchcmd not in (None, "fetch"):
131
raise Exception("fetch command inside other batch command")
132
self.wants.append(tuple(argv[1:]))
133
self.batchcmd = "fetch"
135
def cmd_push(self, outf, argv):
136
if self.batchcmd not in (None, "push"):
137
raise Exception("push command inside other batch command")
138
self.wants.append(tuple(argv[1].split(":", 1)))
139
self.batchcmd = "push"
141
def cmd_import(self, outf, argv):
142
if "fastimport" in CAPABILITIES:
143
raise Exception("install fastimport for 'import' command support")
144
dest_branch_name = ref_to_branch_name(argv[1])
145
if dest_branch_name == "master":
146
dest_branch_name = None
147
remote_branch = self.remote_dir.open_branch(name=dest_branch_name)
148
exporter = fastexporter.BzrFastExporter(remote_branch,
149
outf=outf, ref=argv[1],
150
checkpoint=None, import_marks_file=None,
151
export_marks_file=None, revision=None,
152
verbose=None, plain_format=True,
157
"capabilities": cmd_capabilities,
159
"option": cmd_option,
162
"import": cmd_import,
165
def process(self, inf, outf):
170
self.process_line(l, outf)
172
def process_line(self, l, outf):
173
argv = l.strip().split()
175
if self.batchcmd == "fetch":
176
fetch(outf, self.wants, self.shortname, self.remote_dir, self.local_dir)
177
elif self.batchcmd == "push":
178
push(outf, self.wants, self.shortname, self.remote_dir, self.local_dir)
179
elif self.batchcmd is None:
182
raise AssertionError("invalid batch %r" % self.batchcmd)
186
self.commands[argv[0]](self, outf, argv)
188
raise Exception("Unknown remote command %r" % argv)
192
def open_local_dir():
194
git_path = os.environ["GIT_DIR"]
196
git_transport = get_transport_from_path(".")
197
git_format = LocalGitProber().probe_transport(git_transport)
199
if git_path.endswith("/.git"):
200
git_format = LocalGitControlDirFormat()
201
git_path = git_path[:-4]
203
git_format = BareLocalGitControlDirFormat()
204
git_transport = get_transport_from_path(git_path)
206
return git_format.open(git_transport)