105
125
def cloning_metadir(self, stacked=False):
106
126
return format_registry.make_bzrdir("default")
108
def _branch_name_to_ref(self, name):
109
raise NotImplementedError(self._branch_name_to_ref)
128
def checkout_metadir(self, stacked=False):
129
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)
131
def _get_selected_ref(self, branch):
132
if branch is None and getattr(self, "_get_selected_branch", False):
133
branch = self._get_selected_branch()
134
if branch is not None:
135
from bzrlib.plugins.git.refs import branch_name_to_ref
136
return branch_name_to_ref(branch, None)
137
segment_parameters = getattr(
138
self.user_transport, "get_segment_parameters", lambda: {})()
139
ref = segment_parameters.get("ref")
141
ref = urlutils.unescape(ref)
121
144
def get_config(self):
122
145
return GitDirConfig()
147
def _available_backup_name(self, base):
148
return osutils.available_backup_name(base, self.root_transport.has)
150
def sprout(self, url, revision_id=None, force_new_repo=False,
151
recurse='down', possible_transports=None,
152
accelerator_tree=None, hardlink=False, stacked=False,
153
source_branch=None, create_tree_if_local=True):
154
from bzrlib.repository import InterRepository
155
from bzrlib.transport.local import LocalTransport
156
from bzrlib.transport import get_transport
157
target_transport = get_transport(url, possible_transports)
158
target_transport.ensure_base()
159
cloning_format = self.cloning_metadir()
160
# Create/update the result branch
161
result = cloning_format.initialize_on_transport(target_transport)
162
source_branch = self.open_branch()
163
source_repository = self.find_repository()
165
result_repo = result.find_repository()
166
except bzr_errors.NoRepositoryPresent:
167
result_repo = result.create_repository()
168
target_is_empty = True
170
target_is_empty = None # Unknown
172
raise bzr_errors.IncompatibleRepositories(source_repository, result_repo)
173
interrepo = InterRepository.get(source_repository, result_repo)
175
if revision_id is not None:
176
determine_wants = interrepo.get_determine_wants_revids(
177
[revision_id], include_tags=True)
179
determine_wants = interrepo.determine_wants_all
180
interrepo.fetch_objects(determine_wants=determine_wants,
181
mapping=source_branch.mapping)
182
result_branch = source_branch.sprout(result,
183
revision_id=revision_id, repository=result_repo)
184
if (create_tree_if_local and isinstance(target_transport, LocalTransport)
185
and (result_repo is None or result_repo.make_working_trees())):
186
wt = result.create_workingtree(accelerator_tree=accelerator_tree,
187
hardlink=hardlink, from_branch=result_branch)
190
if wt.path2id('') is None:
192
wt.set_root_id(self.open_workingtree.get_root_id())
193
except bzr_errors.NoWorkingTree:
199
def clone_on_transport(self, transport, revision_id=None,
200
force_new_repo=False, preserve_stacking=False, stacked_on=None,
201
create_prefix=False, use_existing_dir=True, no_tree=False):
202
"""See ControlDir.clone_on_transport."""
203
from bzrlib.repository import InterRepository
204
from bzrlib.plugins.git.mapping import default_mapping
206
format = BareLocalGitControlDirFormat()
208
format = LocalGitControlDirFormat()
209
(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)
210
target_git_repo = target_repo._git
211
source_repo = self.open_repository()
212
source_git_repo = source_repo._git
213
interrepo = InterRepository.get(source_repo, target_repo)
214
if revision_id is not None:
215
determine_wants = interrepo.get_determine_wants_revids([revision_id], include_tags=True)
217
determine_wants = interrepo.determine_wants_all
218
(pack_hint, _, refs) = interrepo.fetch_objects(determine_wants,
219
mapping=default_mapping)
220
for name, val in refs.iteritems():
221
target_git_repo.refs[name] = val
222
lockfiles = GitLockableFiles(transport, GitLock())
223
return self.__class__(transport, lockfiles, target_git_repo, format)
225
def find_repository(self):
226
"""Find the repository that should be used.
228
This does not require a branch as we use it to find the repo for
229
new branches as well as to hook existing branches up to their
232
return self.open_repository()
235
class LocalGitControlDirFormat(GitControlDirFormat):
236
"""The .git directory control format."""
241
def _known_formats(self):
242
return set([LocalGitControlDirFormat()])
245
def repository_format(self):
246
from bzrlib.plugins.git.repository import GitRepositoryFormat
247
return GitRepositoryFormat()
249
def get_branch_format(self):
250
from bzrlib.plugins.git.branch import GitBranchFormat
251
return GitBranchFormat()
253
def open(self, transport, _found=None):
254
"""Open this directory.
257
from bzrlib.plugins.git.transportgit import TransportRepo
258
gitrepo = TransportRepo(transport)
259
lockfiles = GitLockableFiles(transport, GitLock())
260
return LocalGitDir(transport, lockfiles, gitrepo, self)
262
def get_format_description(self):
263
return "Local Git Repository"
265
def initialize_on_transport(self, transport):
266
from bzrlib.plugins.git.transportgit import TransportRepo
267
TransportRepo.init(transport, bare=self.bare)
268
return self.open(transport)
270
def initialize_on_transport_ex(self, transport, use_existing_dir=False,
271
create_prefix=False, force_new_repo=False, stacked_on=None,
272
stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
273
shared_repo=False, vfs_only=False):
274
def make_directory(transport):
277
def redirected(transport, e, redirection_notice):
278
trace.note(redirection_notice)
279
return transport._redirected_to(e.source, e.target)
281
transport = do_catching_redirections(make_directory, transport,
283
except bzr_errors.FileExists:
284
if not use_existing_dir:
286
except bzr_errors.NoSuchFile:
287
if not create_prefix:
289
transport.create_prefix()
290
controldir = self.initialize_on_transport(transport)
291
repository = controldir.open_repository()
292
repository.lock_write()
293
return (repository, controldir, False, CreateRepository(controldir))
295
def is_supported(self):
299
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
302
supports_workingtrees = False
304
def get_format_description(self):
305
return "Local Git Repository (bare)"
125
308
class LocalGitDir(GitDir):
126
309
"""An adapter to the '.git' dir used by git."""
151
338
self._lockfiles = lockfiles
152
339
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
341
def is_control_filename(self, filename):
165
return filename == '.git' or filename.startswith('.git/')
342
return (filename == '.git' or filename.startswith('.git/'))
344
def _get_symref(self, ref):
345
from dulwich.repo import SYMREF
346
refcontents = self._git.refs.read_ref(ref)
347
if refcontents is None: # no such ref
349
if refcontents.startswith(SYMREF):
350
return refcontents[len(SYMREF):].rstrip("\n")
353
def set_branch_reference(self, name, target):
354
ref = self._get_selected_ref(name)
357
if not getattr(target, "ref", None):
358
raise bzr_errors.BzrError("Can only set symrefs to Git refs")
359
self._git.refs.set_symbolic_ref(ref, target.ref)
361
def get_branch_reference(self, name=None):
362
ref = self._get_selected_ref(name)
365
target_ref = self._get_symref(ref)
366
if target_ref is not None:
367
return ",ref=%s" % urllib.quote(target_ref)
370
def find_branch_format(self, name=None):
371
from bzrlib.plugins.git.branch import (
373
GitSymrefBranchFormat,
375
ref = self._get_selected_ref(name)
378
if self._get_symref(ref) is not None:
379
return GitSymrefBranchFormat()
381
return GitBranchFormat()
167
383
def get_branch_transport(self, branch_format, name=None):
168
384
if branch_format is None: