105
74
def cloning_metadir(self, stacked=False):
106
75
return format_registry.make_bzrdir("default")
108
def _branch_name_to_ref(self, name):
109
raise NotImplementedError(self._branch_name_to_ref)
77
def checkout_metadir(self, stacked=False):
78
return format_registry.make_bzrdir("default")
111
if bzrlib_version >= (2, 2):
112
def open_branch(self, name=None, unsupported=False,
113
ignore_fallbacks=None):
114
return self._open_branch(name=name,
115
ignore_fallbacks=ignore_fallbacks, unsupported=unsupported)
117
def open_branch(self, ignore_fallbacks=None, unsupported=False):
118
return self._open_branch(name=None,
119
ignore_fallbacks=ignore_fallbacks, unsupported=unsupported)
80
def _get_selected_ref(self, branch, ref=None):
81
if ref is not None and branch is not None:
82
raise bzr_errors.BzrError("can't specify both ref and branch")
85
if branch is None and getattr(self, "_get_selected_branch", False):
86
branch = self._get_selected_branch()
87
if branch is not None:
88
from bzrlib.plugins.git.refs import branch_name_to_ref
89
return branch_name_to_ref(branch, None)
90
segment_parameters = getattr(
91
self.user_transport, "get_segment_parameters", lambda: {})()
92
ref = segment_parameters.get("ref")
94
ref = urlutils.unescape(ref)
121
97
def get_config(self):
122
98
return GitDirConfig()
100
def _available_backup_name(self, base):
101
return osutils.available_backup_name(base, self.root_transport.has)
103
def sprout(self, url, revision_id=None, force_new_repo=False,
104
recurse='down', possible_transports=None,
105
accelerator_tree=None, hardlink=False, stacked=False,
106
source_branch=None, create_tree_if_local=True):
107
from bzrlib.repository import InterRepository
108
from bzrlib.transport.local import LocalTransport
109
from bzrlib.transport import get_transport
110
target_transport = get_transport(url, possible_transports)
111
target_transport.ensure_base()
112
cloning_format = self.cloning_metadir()
113
# Create/update the result branch
114
result = cloning_format.initialize_on_transport(target_transport)
115
source_branch = self.open_branch()
116
source_repository = self.find_repository()
118
result_repo = result.find_repository()
119
except bzr_errors.NoRepositoryPresent:
120
result_repo = result.create_repository()
121
target_is_empty = True
123
target_is_empty = None # Unknown
125
raise bzr_errors.IncompatibleRepositories(source_repository, result_repo)
126
interrepo = InterRepository.get(source_repository, result_repo)
128
if revision_id is not None:
129
determine_wants = interrepo.get_determine_wants_revids(
130
[revision_id], include_tags=False)
132
determine_wants = interrepo.determine_wants_all
133
interrepo.fetch_objects(determine_wants=determine_wants,
134
mapping=source_branch.mapping)
135
result_branch = source_branch.sprout(result,
136
revision_id=revision_id, repository=result_repo)
137
if (create_tree_if_local
138
and isinstance(target_transport, LocalTransport)
139
and (result_repo is None or result_repo.make_working_trees())):
140
wt = result.create_workingtree(accelerator_tree=accelerator_tree,
141
hardlink=hardlink, from_branch=result_branch)
144
if wt.path2id('') is None:
146
wt.set_root_id(self.open_workingtree.get_root_id())
147
except bzr_errors.NoWorkingTree:
153
def clone_on_transport(self, transport, revision_id=None,
154
force_new_repo=False, preserve_stacking=False, stacked_on=None,
155
create_prefix=False, use_existing_dir=True, no_tree=False):
156
"""See ControlDir.clone_on_transport."""
157
from bzrlib.repository import InterRepository
158
from bzrlib.plugins.git.mapping import default_mapping
160
format = BareLocalGitControlDirFormat()
162
format = LocalGitControlDirFormat()
163
(target_repo, target_controldir, stacking, repo_policy) = format.initialize_on_transport_ex(transport, use_existing_dir=use_existing_dir, create_prefix=create_prefix, force_new_repo=force_new_repo)
164
target_git_repo = target_repo._git
165
source_repo = self.open_repository()
166
source_git_repo = source_repo._git
167
interrepo = InterRepository.get(source_repo, target_repo)
168
if revision_id is not None:
169
determine_wants = interrepo.get_determine_wants_revids([revision_id], include_tags=True)
171
determine_wants = interrepo.determine_wants_all
172
(pack_hint, _, refs) = interrepo.fetch_objects(determine_wants,
173
mapping=default_mapping)
174
for name, val in refs.iteritems():
175
target_git_repo.refs[name] = val
176
return self.__class__(transport, target_git_repo, format)
178
def find_repository(self):
179
"""Find the repository that should be used.
181
This does not require a branch as we use it to find the repo for
182
new branches as well as to hook existing branches up to their
185
return self.open_repository()
187
def get_refs_container(self):
188
"""Retrieve the refs container.
190
raise NotImplementedError(self.get_refs_container)
193
class LocalGitControlDirFormat(GitControlDirFormat):
194
"""The .git directory control format."""
199
def _known_formats(self):
200
return set([LocalGitControlDirFormat()])
203
def repository_format(self):
204
from bzrlib.plugins.git.repository import GitRepositoryFormat
205
return GitRepositoryFormat()
207
def get_branch_format(self):
208
from bzrlib.plugins.git.branch import GitBranchFormat
209
return GitBranchFormat()
211
def open(self, transport, _found=None):
212
"""Open this directory.
215
from bzrlib.plugins.git.transportgit import TransportRepo
216
gitrepo = TransportRepo(transport, self.bare,
217
refs_text=getattr(self, "_refs_text", None))
218
return LocalGitDir(transport, gitrepo, self)
220
def get_format_description(self):
221
return "Local Git Repository"
223
def initialize_on_transport(self, transport):
224
from bzrlib.plugins.git.transportgit import TransportRepo
225
TransportRepo.init(transport, bare=self.bare)
226
return self.open(transport)
228
def initialize_on_transport_ex(self, transport, use_existing_dir=False,
229
create_prefix=False, force_new_repo=False, stacked_on=None,
230
stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
231
shared_repo=False, vfs_only=False):
232
def make_directory(transport):
235
def redirected(transport, e, redirection_notice):
236
trace.note(redirection_notice)
237
return transport._redirected_to(e.source, e.target)
239
transport = do_catching_redirections(make_directory, transport,
241
except bzr_errors.FileExists:
242
if not use_existing_dir:
244
except bzr_errors.NoSuchFile:
245
if not create_prefix:
247
transport.create_prefix()
248
controldir = self.initialize_on_transport(transport)
249
repository = controldir.open_repository()
250
repository.lock_write()
251
return (repository, controldir, False, CreateRepository(controldir))
253
def is_supported(self):
256
def supports_transport(self, transport):
258
external_url = transport.external_url()
259
except bzr_errors.InProcessTransport:
260
raise bzr_errors.NotBranchError(path=transport.base)
261
return (external_url.startswith("http:") or
262
external_url.startswith("https:") or
263
external_url.startswith("file:"))
266
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
269
supports_workingtrees = False
271
def get_format_description(self):
272
return "Local Git Repository (bare)"
125
275
class LocalGitDir(GitDir):
126
276
"""An adapter to the '.git' dir used by git."""
148
302
self.transport = transport
150
304
self.transport = transport.clone('.git')
151
self._lockfiles = lockfiles
152
305
self._mode_check_done = None
154
def _branch_name_to_ref(self, name):
155
from bzrlib.plugins.git.refs import branch_name_to_ref
156
ref = branch_name_to_ref(name, None)
158
from dulwich.repo import SYMREF
159
refcontents = self._git.refs.read_ref(ref)
160
if refcontents.startswith(SYMREF):
161
ref = refcontents[len(SYMREF):]
164
307
def is_control_filename(self, filename):
165
return filename == '.git' or filename.startswith('.git/')
308
return (filename == '.git' or filename.startswith('.git/'))
310
def _get_symref(self, ref):
311
from dulwich.repo import SYMREF
312
refcontents = self._git.refs.read_ref(ref)
313
if refcontents is None: # no such ref
315
if refcontents.startswith(SYMREF):
316
return refcontents[len(SYMREF):].rstrip("\n")
319
def set_branch_reference(self, name, target):
320
ref = self._get_selected_ref(name)
323
if not getattr(target, "ref", None):
324
raise bzr_errors.BzrError("Can only set symrefs to Git refs")
325
self._git.refs.set_symbolic_ref(ref, target.ref)
327
def get_branch_reference(self, name=None):
328
ref = self._get_selected_ref(name)
331
target_ref = self._get_symref(ref)
332
if target_ref is not None:
333
return urlutils.join_segment_parameters(
334
self.user_url.rstrip("/"), {"ref": urllib.quote(target_ref, '')})
337
def find_branch_format(self, name=None):
338
from bzrlib.plugins.git.branch import (
340
GitSymrefBranchFormat,
342
ref = self._get_selected_ref(name)
345
if self._get_symref(ref) is not None:
346
return GitSymrefBranchFormat()
348
return GitBranchFormat()
167
350
def get_branch_transport(self, branch_format, name=None):
168
351
if branch_format is None:
239
442
raise bzr_errors.NoWorkingTree(loc)
241
444
def create_repository(self, shared=False):
445
from bzrlib.plugins.git.repository import GitRepositoryFormat
447
raise bzr_errors.IncompatibleFormat(GitRepositoryFormat(), self._format)
242
448
return self.open_repository()
244
def create_branch(self, name=None):
245
refname = self._branch_name_to_ref(name)
450
def create_branch(self, name=None, repository=None,
451
append_revisions_only=None, ref=None):
452
refname = self._get_selected_ref(name, ref)
246
453
from dulwich.protocol import ZERO_SHA
247
self._git.refs[refname or "HEAD"] = ZERO_SHA
248
return self.open_branch(name)
454
# FIXME: This is a bit awkward. Perhaps we should have a
455
# a separate method for changing the default branch?
457
refname = "refs/heads/master"
462
if refname in self._git.refs:
463
raise bzr_errors.AlreadyBranchError(self.base)
464
self._git.refs[refname] = ZERO_SHA
466
self._git.refs.set_symbolic_ref("HEAD", refname)
467
branch = self.open_branch(name)
468
if append_revisions_only:
469
branch.set_append_revisions_only(append_revisions_only)
250
472
def backup_bzrdir(self):
473
if not self._git.bare:
252
474
self.root_transport.copy_tree(".git", ".git.backup")
253
475
return (self.root_transport.abspath(".git"),
254
476
self.root_transport.abspath(".git.backup"))
256
raise bzr_errors.BzrError("Unable to backup bare repositories")
478
basename = urlutils.basename(self.root_transport.base)
479
parent = self.root_transport.clone('..')
480
parent.copy_tree(basename, basename + ".backup")
258
482
def create_workingtree(self, revision_id=None, from_branch=None,
259
483
accelerator_tree=None, hardlink=False):
260
484
if self._git.bare:
261
raise bzr_errors.BzrError("Can't create working tree in a bare repo")
485
raise bzr_errors.UnsupportedOperation(self.create_workingtree, self)
262
486
from dulwich.index import write_index
263
487
from dulwich.pack import SHA1Writer
264
488
f = open(self.transport.local_abspath("index"), 'w+')