4
# Copyright (C) 2011 Jelmer Vernooij <jelmer@apache.org>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 (
54
from ..fastimport import exporter as fastexporter
58
CAPABILITIES.append("import")
60
def open_remote_dir(url):
62
return ControlDir.open(url)
63
except NotBranchError:
64
return ControlDir.create(url)
67
def fetch(outf, wants, shortname, remote_dir, local_dir):
68
remote_repo = remote_dir.find_repository()
69
local_repo = local_dir.find_repository()
70
inter = InterRepository.get(remote_repo, local_repo)
72
for (sha1, ref) in wants:
73
revs.append((sha1, None))
74
if (isinstance(remote_repo, GitRepository) and
75
isinstance(local_repo, GitRepository)):
79
inter.fetch_objects(revs, lossy=lossy)
83
def push(outf, wants, shortname, remote_dir, local_dir):
84
for (src_ref, dest_ref) in wants:
85
local_branch = local_dir.open_branch(ref=src_ref)
86
dest_branch_name = ref_to_branch_name(dest_ref)
87
if dest_branch_name == "master":
88
dest_branch_name = None
90
remote_branch = remote_dir.open_branch(name=dest_branch_name)
91
except NotBranchError:
92
remote_branch = remote_dir.create_branch(name=dest_branch_name)
93
local_branch.push(remote_branch)
94
outf.write("ok %s\n" % dest_ref)
98
class RemoteHelper(object):
99
"""Git remote helper."""
101
def __init__(self, local_dir, shortname, remote_dir):
102
self.local_dir = local_dir
103
self.shortname = shortname
104
self.remote_dir = remote_dir
108
def cmd_capabilities(self, outf, argv):
109
outf.write("\n".join(CAPABILITIES)+"\n\n")
111
def cmd_list(self, outf, argv):
113
repo = self.remote_dir.find_repository()
114
except NoRepositoryPresent:
115
repo = self.remote_dir.create_repository()
116
object_store = get_object_store(repo)
117
object_store.lock_read()
119
refs = get_refs_container(self.remote_dir, object_store)
120
for ref, git_sha1 in refs.as_dict().iteritems():
121
ref = ref.replace("~", "_")
122
outf.write("%s %s\n" % (git_sha1, ref))
125
object_store.unlock()
127
def cmd_option(self, outf, argv):
128
outf.write("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 fastexporter is None:
144
raise Exception("install bzr-fastimport for 'import' command support")
145
dest_branch_name = ref_to_branch_name(argv[1])
146
if dest_branch_name == "master":
147
dest_branch_name = None
148
remote_branch = self.remote_dir.open_branch(name=dest_branch_name)
149
exporter = fastexporter.BzrFastExporter(remote_branch,
150
outf=outf, ref=argv[1],
151
checkpoint=None, import_marks_file=None,
152
export_marks_file=None, revision=None,
153
verbose=None, plain_format=True,
158
"capabilities": cmd_capabilities,
160
"option": cmd_option,
163
"import": cmd_import,
166
def process(self, inf, outf):
171
self.process_line(l, 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, self.remote_dir, self.local_dir)
178
elif self.batchcmd == "push":
179
push(outf, self.wants, self.shortname, self.remote_dir, self.local_dir)
180
elif self.batchcmd is None:
183
raise AssertionError("invalid batch %r" % self.batchcmd)
187
self.commands[argv[0]](self, outf, argv)
189
raise Exception("Unknown remote command %r" % argv)
193
def open_local_dir():
195
git_path = os.environ["GIT_DIR"]
197
git_transport = get_transport_from_path(".")
198
git_format = LocalGitProber().probe_transport(git_transport)
200
if git_path.endswith("/.git"):
201
git_format = LocalGitControlDirFormat()
202
git_path = git_path[:-4]
204
git_format = BareLocalGitControlDirFormat()
205
git_transport = get_transport_from_path(git_path)
207
return git_format.open(git_transport)