105
126
def cloning_metadir(self, stacked=False):
106
127
return format_registry.make_bzrdir("default")
108
def _branch_name_to_ref(self, name):
109
raise NotImplementedError(self._branch_name_to_ref)
129
def checkout_metadir(self, stacked=False):
130
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)
132
def _get_selected_ref(self, branch):
133
if branch is None and getattr(self, "_get_selected_branch", False):
134
branch = self._get_selected_branch()
135
if branch is not None:
136
from bzrlib.plugins.git.refs import branch_name_to_ref
137
return branch_name_to_ref(branch, None)
138
segment_parameters = getattr(
139
self.user_transport, "get_segment_parameters", lambda: {})()
140
ref = segment_parameters.get("ref")
142
ref = urlutils.unescape(ref)
121
145
def get_config(self):
122
146
return GitDirConfig()
148
def _available_backup_name(self, base):
149
return osutils.available_backup_name(base, self.root_transport.has)
151
def sprout(self, url, revision_id=None, force_new_repo=False,
152
recurse='down', possible_transports=None,
153
accelerator_tree=None, hardlink=False, stacked=False,
154
source_branch=None, create_tree_if_local=True):
155
from bzrlib.repository import InterRepository
156
from bzrlib.transport.local import LocalTransport
157
from bzrlib.transport import get_transport
158
target_transport = get_transport(url, possible_transports)
159
target_transport.ensure_base()
160
cloning_format = self.cloning_metadir()
161
# Create/update the result branch
162
result = cloning_format.initialize_on_transport(target_transport)
163
source_branch = self.open_branch()
164
source_repository = self.find_repository()
166
result_repo = result.find_repository()
167
except bzr_errors.NoRepositoryPresent:
168
result_repo = result.create_repository()
169
target_is_empty = True
171
target_is_empty = None # Unknown
173
raise bzr_errors.IncompatibleRepositories(source_repository, result_repo)
174
interrepo = InterRepository.get(source_repository, result_repo)
176
if revision_id is not None:
177
determine_wants = interrepo.get_determine_wants_revids(
178
[revision_id], include_tags=True)
180
determine_wants = interrepo.determine_wants_all
181
interrepo.fetch_objects(determine_wants=determine_wants,
182
mapping=source_branch.mapping)
183
result_branch = source_branch.sprout(result,
184
revision_id=revision_id, repository=result_repo)
185
if (create_tree_if_local
186
and isinstance(target_transport, LocalTransport)
187
and (result_repo is None or result_repo.make_working_trees())):
188
wt = result.create_workingtree(accelerator_tree=accelerator_tree,
189
hardlink=hardlink, from_branch=result_branch)
192
if wt.path2id('') is None:
194
wt.set_root_id(self.open_workingtree.get_root_id())
195
except bzr_errors.NoWorkingTree:
201
def clone_on_transport(self, transport, revision_id=None,
202
force_new_repo=False, preserve_stacking=False, stacked_on=None,
203
create_prefix=False, use_existing_dir=True, no_tree=False):
204
"""See ControlDir.clone_on_transport."""
205
from bzrlib.repository import InterRepository
206
from bzrlib.plugins.git.mapping import default_mapping
208
format = BareLocalGitControlDirFormat()
210
format = LocalGitControlDirFormat()
211
(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)
212
target_git_repo = target_repo._git
213
source_repo = self.open_repository()
214
source_git_repo = source_repo._git
215
interrepo = InterRepository.get(source_repo, target_repo)
216
if revision_id is not None:
217
determine_wants = interrepo.get_determine_wants_revids([revision_id], include_tags=True)
219
determine_wants = interrepo.determine_wants_all
220
(pack_hint, _, refs) = interrepo.fetch_objects(determine_wants,
221
mapping=default_mapping)
222
for name, val in refs.iteritems():
223
target_git_repo.refs[name] = val
224
lockfiles = GitLockableFiles(transport, GitLock())
225
return self.__class__(transport, lockfiles, target_git_repo, format)
227
def find_repository(self):
228
"""Find the repository that should be used.
230
This does not require a branch as we use it to find the repo for
231
new branches as well as to hook existing branches up to their
234
return self.open_repository()
237
class LocalGitControlDirFormat(GitControlDirFormat):
238
"""The .git directory control format."""
243
def _known_formats(self):
244
return set([LocalGitControlDirFormat()])
247
def repository_format(self):
248
from bzrlib.plugins.git.repository import GitRepositoryFormat
249
return GitRepositoryFormat()
251
def get_branch_format(self):
252
from bzrlib.plugins.git.branch import GitBranchFormat
253
return GitBranchFormat()
255
def open(self, transport, _found=None):
256
"""Open this directory.
259
from bzrlib.plugins.git.transportgit import TransportRepo
260
gitrepo = TransportRepo(transport, self.bare)
261
lockfiles = GitLockableFiles(transport, GitLock())
262
return LocalGitDir(transport, lockfiles, gitrepo, self)
264
def get_format_description(self):
265
return "Local Git Repository"
267
def initialize_on_transport(self, transport):
268
from bzrlib.plugins.git.transportgit import TransportRepo
269
TransportRepo.init(transport, bare=self.bare)
270
return self.open(transport)
272
def initialize_on_transport_ex(self, transport, use_existing_dir=False,
273
create_prefix=False, force_new_repo=False, stacked_on=None,
274
stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
275
shared_repo=False, vfs_only=False):
276
def make_directory(transport):
279
def redirected(transport, e, redirection_notice):
280
trace.note(redirection_notice)
281
return transport._redirected_to(e.source, e.target)
283
transport = do_catching_redirections(make_directory, transport,
285
except bzr_errors.FileExists:
286
if not use_existing_dir:
288
except bzr_errors.NoSuchFile:
289
if not create_prefix:
291
transport.create_prefix()
292
controldir = self.initialize_on_transport(transport)
293
repository = controldir.open_repository()
294
repository.lock_write()
295
return (repository, controldir, False, CreateRepository(controldir))
297
def is_supported(self):
301
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
304
supports_workingtrees = False
306
def get_format_description(self):
307
return "Local Git Repository (bare)"
125
310
class LocalGitDir(GitDir):
126
311
"""An adapter to the '.git' dir used by git."""
151
340
self._lockfiles = lockfiles
152
341
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
343
def is_control_filename(self, filename):
165
return filename == '.git' or filename.startswith('.git/')
344
return (filename == '.git' or filename.startswith('.git/'))
346
def _get_symref(self, ref):
347
from dulwich.repo import SYMREF
348
refcontents = self._git.refs.read_ref(ref)
349
if refcontents is None: # no such ref
351
if refcontents.startswith(SYMREF):
352
return refcontents[len(SYMREF):].rstrip("\n")
355
def set_branch_reference(self, name, target):
356
ref = self._get_selected_ref(name)
359
if not getattr(target, "ref", None):
360
raise bzr_errors.BzrError("Can only set symrefs to Git refs")
361
self._git.refs.set_symbolic_ref(ref, target.ref)
363
def get_branch_reference(self, name=None):
364
ref = self._get_selected_ref(name)
367
target_ref = self._get_symref(ref)
368
if target_ref is not None:
369
return urlutils.join_segment_parameters(
370
self.user_url.rstrip("/"), {"ref": urllib.quote(target_ref, '')})
373
def find_branch_format(self, name=None):
374
from bzrlib.plugins.git.branch import (
376
GitSymrefBranchFormat,
378
ref = self._get_selected_ref(name)
381
if self._get_symref(ref) is not None:
382
return GitSymrefBranchFormat()
384
return GitBranchFormat()
167
386
def get_branch_transport(self, branch_format, name=None):
168
387
if branch_format is None: