102
111
def break_lock(self):
112
# There are no global locks, so nothing to break.
113
raise NotImplementedError(self.break_lock)
105
115
def cloning_metadir(self, stacked=False):
106
return format_registry.make_bzrdir("default")
108
def _branch_name_to_ref(self, name):
109
raise NotImplementedError(self._branch_name_to_ref)
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)
116
return format_registry.make_controldir("git")
118
def checkout_metadir(self, stacked=False):
119
return format_registry.make_controldir("git")
121
def _get_selected_ref(self, branch, ref=None):
122
if ref is not None and branch is not None:
123
raise brz_errors.BzrError("can't specify both ref and branch")
126
if branch is not None:
127
from .refs import branch_name_to_ref
128
return branch_name_to_ref(branch)
129
segment_parameters = getattr(
130
self.user_transport, "get_segment_parameters", lambda: {})()
131
ref = segment_parameters.get("ref")
133
return urlutils.unquote_to_bytes(ref)
134
if branch is None and getattr(self, "_get_selected_branch", False):
135
branch = self._get_selected_branch()
136
if branch is not None:
137
from .refs import branch_name_to_ref
138
return branch_name_to_ref(branch)
121
141
def get_config(self):
122
142
return GitDirConfig()
144
def _available_backup_name(self, base):
145
return osutils.available_backup_name(base, self.root_transport.has)
147
def sprout(self, url, revision_id=None, force_new_repo=False,
148
recurse='down', possible_transports=None,
149
accelerator_tree=None, hardlink=False, stacked=False,
150
source_branch=None, create_tree_if_local=True):
151
from ..repository import InterRepository
152
from ..transport.local import LocalTransport
153
from ..transport import get_transport
154
target_transport = get_transport(url, possible_transports)
155
target_transport.ensure_base()
156
cloning_format = self.cloning_metadir()
157
# Create/update the result branch
159
result = ControlDir.open_from_transport(target_transport)
160
except brz_errors.NotBranchError:
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 brz_errors.NoRepositoryPresent:
167
result_repo = result.create_repository()
169
raise _mod_branch.UnstackableBranchFormat(
170
self._format, self.user_url)
171
interrepo = InterRepository.get(source_repository, result_repo)
173
if revision_id is not None:
174
determine_wants = interrepo.get_determine_wants_revids(
175
[revision_id], include_tags=True)
177
determine_wants = interrepo.determine_wants_all
178
interrepo.fetch_objects(determine_wants=determine_wants,
179
mapping=source_branch.mapping)
180
result_branch = source_branch.sprout(
181
result, revision_id=revision_id, repository=result_repo)
182
if (create_tree_if_local and
183
result.open_branch(name="").name == result_branch.name and
184
isinstance(target_transport, LocalTransport) and
185
(result_repo is None or result_repo.make_working_trees())):
186
wt = result.create_workingtree(
187
accelerator_tree=accelerator_tree,
188
hardlink=hardlink, from_branch=result_branch)
191
if recurse == 'down':
192
with contextlib.ExitStack() as stack:
195
basis = wt.basis_tree()
196
elif result_branch is not None:
197
basis = result_branch.basis_tree()
198
elif source_branch is not None:
199
basis = source_branch.basis_tree()
200
if basis is not None:
201
stack.enter_context(basis.lock_read())
202
subtrees = basis.iter_references()
205
for path in subtrees:
206
target = urlutils.join(url, urlutils.escape(path))
207
sublocation = wt.reference_parent(
208
path, possible_transports=possible_transports)
209
if sublocation is None:
211
'Ignoring nested tree %s, parent location unknown.',
214
sublocation.controldir.sprout(
215
target, basis.get_reference_revision(path),
216
force_new_repo=force_new_repo, recurse=recurse,
220
def clone_on_transport(self, transport, revision_id=None,
221
force_new_repo=False, preserve_stacking=False,
222
stacked_on=None, create_prefix=False,
223
use_existing_dir=True, no_tree=False):
224
"""See ControlDir.clone_on_transport."""
225
from ..repository import InterRepository
226
from .mapping import default_mapping
227
if stacked_on is not None:
228
raise _mod_branch.UnstackableBranchFormat(
229
self._format, self.user_url)
231
format = BareLocalGitControlDirFormat()
233
format = LocalGitControlDirFormat()
234
(target_repo, target_controldir, stacking,
235
repo_policy) = format.initialize_on_transport_ex(
236
transport, use_existing_dir=use_existing_dir,
237
create_prefix=create_prefix,
238
force_new_repo=force_new_repo)
239
target_repo = target_controldir.find_repository()
240
target_git_repo = target_repo._git
241
source_repo = self.find_repository()
242
interrepo = InterRepository.get(source_repo, target_repo)
243
if revision_id is not None:
244
determine_wants = interrepo.get_determine_wants_revids(
245
[revision_id], include_tags=True)
247
determine_wants = interrepo.determine_wants_all
248
(pack_hint, _, refs) = interrepo.fetch_objects(determine_wants,
249
mapping=default_mapping)
250
for name, val in refs.items():
251
target_git_repo.refs[name] = val
252
result_dir = self.__class__(transport, target_git_repo, format)
253
if revision_id is not None:
254
result_dir.open_branch().set_last_revision(revision_id)
256
# Cheaper to check if the target is not local, than to try making
258
result_dir.root_transport.local_abspath('.')
259
if result_dir.open_repository().make_working_trees():
260
self.open_workingtree().clone(
261
result_dir, revision_id=revision_id)
262
except (brz_errors.NoWorkingTree, brz_errors.NotLocalUrl):
267
def find_repository(self):
268
"""Find the repository that should be used.
270
This does not require a branch as we use it to find the repo for
271
new branches as well as to hook existing branches up to their
274
return self._gitrepository_class(self._find_commondir())
276
def get_refs_container(self):
277
"""Retrieve the refs container.
279
raise NotImplementedError(self.get_refs_container)
281
def determine_repository_policy(self, force_new_repo=False, stack_on=None,
282
stack_on_pwd=None, require_stacking=False):
283
"""Return an object representing a policy to use.
285
This controls whether a new repository is created, and the format of
286
that repository, or some existing shared repository used instead.
288
If stack_on is supplied, will not seek a containing shared repo.
290
:param force_new_repo: If True, require a new repository to be created.
291
:param stack_on: If supplied, the location to stack on. If not
292
supplied, a default_stack_on location may be used.
293
:param stack_on_pwd: If stack_on is relative, the location it is
296
return UseExistingRepository(self.find_repository())
298
def get_branches(self):
299
from .refs import ref_to_branch_name
301
for ref in self.get_refs_container().keys():
303
branch_name = ref_to_branch_name(ref)
304
except UnicodeDecodeError:
305
trace.warning("Ignoring branch %r with unicode error ref", ref)
309
ret[branch_name] = self.open_branch(ref=ref)
312
def list_branches(self):
313
return list(self.get_branches().values())
315
def push_branch(self, source, revision_id=None, overwrite=False,
316
remember=False, create_prefix=False, lossy=False,
318
"""Push the source branch into this ControlDir."""
319
push_result = GitPushResult()
320
push_result.workingtree_updated = None
321
push_result.master_branch = None
322
push_result.source_branch = source
323
push_result.stacked_on = None
324
from .branch import GitBranch
325
if isinstance(source, GitBranch) and lossy:
326
raise brz_errors.LossyPushToSameVCS(source.controldir, self)
327
target = self.open_branch(name, nascent_ok=True)
328
push_result.branch_push_result = source.push(
329
target, overwrite=overwrite, stop_revision=revision_id,
331
push_result.new_revid = push_result.branch_push_result.new_revid
332
push_result.old_revid = push_result.branch_push_result.old_revid
334
wt = self.open_workingtree()
335
except brz_errors.NoWorkingTree:
336
push_result.workingtree_updated = None
338
if self.open_branch(name="").name == target.name:
340
old_revision=push_result.old_revid,
341
new_revision=push_result.new_revid)
342
push_result.workingtree_updated = True
344
push_result.workingtree_updated = False
345
push_result.target_branch = target
346
if source.get_push_location() is None or remember:
347
source.set_push_location(push_result.target_branch.base)
351
class LocalGitControlDirFormat(GitControlDirFormat):
352
"""The .git directory control format."""
357
def _known_formats(self):
358
return set([LocalGitControlDirFormat()])
361
def repository_format(self):
362
from .repository import GitRepositoryFormat
363
return GitRepositoryFormat()
366
def workingtree_format(self):
367
from .workingtree import GitWorkingTreeFormat
368
return GitWorkingTreeFormat()
370
def get_branch_format(self):
371
from .branch import LocalGitBranchFormat
372
return LocalGitBranchFormat()
374
def open(self, transport, _found=None):
375
"""Open this directory.
378
from .transportgit import TransportRepo
380
def _open(transport):
382
return TransportRepo(transport, self.bare,
383
refs_text=getattr(self, "_refs_text", None))
384
except ValueError as e:
385
if e.args == ('Expected file to start with \'gitdir: \'', ):
386
raise brz_errors.NotBranchError(path=transport.base)
389
def redirected(transport, e, redirection_notice):
390
trace.note(redirection_notice)
391
return transport._redirected_to(e.source, e.target)
392
gitrepo = do_catching_redirections(_open, transport, redirected)
393
if not _found and not gitrepo._controltransport.has('objects'):
394
raise brz_errors.NotBranchError(path=transport.base)
395
return LocalGitDir(transport, gitrepo, self)
397
def get_format_description(self):
398
return "Local Git Repository"
400
def initialize_on_transport(self, transport):
401
from .transportgit import TransportRepo
402
git_repo = TransportRepo.init(transport, bare=self.bare)
403
return LocalGitDir(transport, git_repo, self)
405
def initialize_on_transport_ex(self, transport, use_existing_dir=False,
406
create_prefix=False, force_new_repo=False,
408
stack_on_pwd=None, repo_format_name=None,
409
make_working_trees=None,
410
shared_repo=False, vfs_only=False):
412
raise brz_errors.SharedRepositoriesUnsupported(self)
414
def make_directory(transport):
418
def redirected(transport, e, redirection_notice):
419
trace.note(redirection_notice)
420
return transport._redirected_to(e.source, e.target)
422
transport = do_catching_redirections(
423
make_directory, transport, redirected)
424
except brz_errors.FileExists:
425
if not use_existing_dir:
427
except brz_errors.NoSuchFile:
428
if not create_prefix:
430
transport.create_prefix()
431
controldir = self.initialize_on_transport(transport)
433
result_repo = controldir.find_repository()
434
repository_policy = UseExistingRepository(result_repo)
435
result_repo.lock_write()
438
repository_policy = None
439
return (result_repo, controldir, False,
442
def is_supported(self):
445
def supports_transport(self, transport):
447
external_url = transport.external_url()
448
except brz_errors.InProcessTransport:
449
raise brz_errors.NotBranchError(path=transport.base)
450
return external_url.startswith("file:")
452
def is_control_filename(self, filename):
453
return (filename == '.git'
454
or filename.startswith('.git/')
455
or filename.startswith('.git\\'))
458
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
461
supports_workingtrees = False
463
def get_format_description(self):
464
return "Local Git Repository (bare)"
466
def is_control_filename(self, filename):
125
470
class LocalGitDir(GitDir):
126
471
"""An adapter to the '.git' dir used by git."""
128
473
def _get_gitrepository_class(self):
129
from bzrlib.plugins.git.repository import LocalGitRepository
474
from .repository import LocalGitRepository
130
475
return LocalGitRepository
478
return "<%s at %r>" % (
479
self.__class__.__name__, self.root_transport.base)
132
481
_gitrepository_class = property(_get_gitrepository_class)
148
497
self.transport = transport
150
499
self.transport = transport.clone('.git')
151
self._lockfiles = lockfiles
152
500
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
def is_control_filename(self, filename):
165
return filename == '.git' or filename.startswith('.git/')
502
def _get_symref(self, ref):
503
ref_chain, unused_sha = self._git.refs.follow(ref)
504
if len(ref_chain) == 1:
508
def set_branch_reference(self, target_branch, name=None):
509
ref = self._get_selected_ref(name)
510
target_transport = target_branch.controldir.control_transport
511
if self.control_transport.base == target_transport.base:
512
if ref == target_branch.ref:
513
raise BranchReferenceLoop(target_branch)
514
self._git.refs.set_symbolic_ref(ref, target_branch.ref)
518
target_branch.controldir.control_transport.local_abspath(
520
except brz_errors.NotLocalUrl:
521
raise brz_errors.IncompatibleFormat(
522
target_branch._format, self._format)
523
# TODO(jelmer): Do some consistency checking across branches..
524
self.control_transport.put_bytes(
525
'commondir', target_path.encode('utf-8'))
526
# TODO(jelmer): Urgh, avoid mucking about with internals.
527
self._git._commontransport = (
528
target_branch.repository._git._commontransport.clone())
529
self._git.object_store = TransportObjectStore(
530
self._git._commontransport.clone(OBJECTDIR))
531
self._git.refs.transport = self._git._commontransport
532
target_ref_chain, unused_sha = (
533
target_branch.controldir._git.refs.follow(target_branch.ref))
534
for target_ref in target_ref_chain:
535
if target_ref == b'HEAD':
539
# Can't create a reference to something that is not a in a repository.
540
raise brz_errors.IncompatibleFormat(
541
self.set_branch_reference, self)
542
self._git.refs.set_symbolic_ref(ref, target_ref)
544
def get_branch_reference(self, name=None):
545
ref = self._get_selected_ref(name)
546
target_ref = self._get_symref(ref)
547
if target_ref is not None:
548
from .refs import ref_to_branch_name
550
branch_name = ref_to_branch_name(target_ref)
552
params = {'ref': urlutils.quote(
553
target_ref.decode('utf-8'), '')}
555
if branch_name != '':
556
params = {'branch': urlutils.quote(branch_name, '')}
560
commondir = self.control_transport.get_bytes('commondir')
561
except brz_errors.NoSuchFile:
562
base_url = self.user_url.rstrip('/')
564
base_url = urlutils.local_path_to_url(
565
commondir.decode(osutils._fs_enc)).rstrip('/.git/') + '/'
566
return urlutils.join_segment_parameters(base_url, params)
569
def find_branch_format(self, name=None):
570
from .branch import (
571
LocalGitBranchFormat,
573
return LocalGitBranchFormat()
167
575
def get_branch_transport(self, branch_format, name=None):
168
576
if branch_format is None:
169
577
return self.transport
170
578
if isinstance(branch_format, LocalGitControlDirFormat):
171
579
return self.transport
172
raise bzr_errors.IncompatibleFormat(branch_format, self._format)
580
raise brz_errors.IncompatibleFormat(branch_format, self._format)
174
582
def get_repository_transport(self, format):
175
583
if format is None:
176
584
return self.transport
177
585
if isinstance(format, LocalGitControlDirFormat):
178
586
return self.transport
179
raise bzr_errors.IncompatibleFormat(format, self._format)
587
raise brz_errors.IncompatibleFormat(format, self._format)
181
589
def get_workingtree_transport(self, format):
182
590
if format is None:
183
591
return self.transport
184
592
if isinstance(format, LocalGitControlDirFormat):
185
593
return self.transport
186
raise bzr_errors.IncompatibleFormat(format, self._format)
594
raise brz_errors.IncompatibleFormat(format, self._format)
188
def _open_branch(self, name=None, ignore_fallbacks=None, unsupported=False):
596
def open_branch(self, name=None, unsupported=False, ignore_fallbacks=None,
597
ref=None, possible_transports=None, nascent_ok=False):
189
598
"""'create' a branch for this dir."""
190
repo = self.open_repository()
191
from bzrlib.plugins.git.branch import LocalGitBranch
192
return LocalGitBranch(self, repo, self._branch_name_to_ref(name),
599
repo = self.find_repository()
600
from .branch import LocalGitBranch
601
ref = self._get_selected_ref(name, ref)
602
if not nascent_ok and ref not in self._git.refs:
603
raise brz_errors.NotBranchError(
604
self.root_transport.base, controldir=self)
605
ref_chain, unused_sha = self._git.refs.follow(ref)
606
if ref_chain[-1] == b'HEAD':
609
controldir = self._find_commondir()
610
return LocalGitBranch(controldir, repo, ref_chain[-1])
195
612
def destroy_branch(self, name=None):
196
refname = self._branch_name_to_ref(name)
197
if not refname in self._git.refs:
198
raise bzr_errors.NotBranchError(self.root_transport.base,
200
del self._git.refs[refname]
613
refname = self._get_selected_ref(name)
614
if refname == b'HEAD':
615
# HEAD can't be removed
616
raise brz_errors.UnsupportedOperation(
617
self.destroy_branch, self)
619
del self._git.refs[refname]
621
raise brz_errors.NotBranchError(
622
self.root_transport.base, controldir=self)
202
624
def destroy_repository(self):
203
raise bzr_errors.UnsupportedOperation(self.destroy_repository, self)
625
raise brz_errors.UnsupportedOperation(self.destroy_repository, self)
205
627
def destroy_workingtree(self):
206
raise bzr_errors.UnsupportedOperation(self.destroy_workingtree, self)
628
raise brz_errors.UnsupportedOperation(self.destroy_workingtree, self)
630
def destroy_workingtree_metadata(self):
631
raise brz_errors.UnsupportedOperation(
632
self.destroy_workingtree_metadata, self)
208
634
def needs_format_conversion(self, format=None):
209
635
return not isinstance(self._format, format.__class__)
211
def list_branches(self):
213
for name in self._git.get_refs():
214
if name.startswith("refs/heads/"):
215
ret.append(self.open_branch(name=name))
218
def open_repository(self, shared=False):
637
def open_repository(self):
219
638
"""'open' a repository for this dir."""
220
return self._gitrepository_class(self, self._lockfiles)
222
def open_workingtree(self, recommend_upgrade=True):
639
if self.control_transport.has('commondir'):
640
raise brz_errors.NoRepositoryPresent(self)
641
return self._gitrepository_class(self)
643
def has_workingtree(self):
644
return not self._git.bare
646
def open_workingtree(self, recommend_upgrade=True, unsupported=False):
223
647
if not self._git.bare:
224
from dulwich.errors import NoIndexPresent
225
repo = self.open_repository()
227
index = repo._git.open_index()
228
except NoIndexPresent:
231
from bzrlib.plugins.git.workingtree import GitWorkingTree
233
branch = self.open_branch()
234
except bzr_errors.NotBranchError:
237
return GitWorkingTree(self, repo, branch, index)
648
repo = self.find_repository()
649
from .workingtree import GitWorkingTree
650
branch = self.open_branch(ref=b'HEAD', nascent_ok=True)
651
return GitWorkingTree(self, repo, branch)
238
652
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
239
raise bzr_errors.NoWorkingTree(loc)
653
raise brz_errors.NoWorkingTree(loc)
241
655
def create_repository(self, shared=False):
242
return self.open_repository()
656
from .repository import GitRepositoryFormat
658
raise brz_errors.IncompatibleFormat(
659
GitRepositoryFormat(), self._format)
660
return self.find_repository()
244
def create_branch(self, name=None):
245
refname = self._branch_name_to_ref(name)
246
from dulwich.protocol import ZERO_SHA
247
self._git.refs[refname or "HEAD"] = ZERO_SHA
248
return self.open_branch(name)
662
def create_branch(self, name=None, repository=None,
663
append_revisions_only=None, ref=None):
664
refname = self._get_selected_ref(name, ref)
665
if refname != b'HEAD' and refname in self._git.refs:
666
raise brz_errors.AlreadyBranchError(self.user_url)
667
repo = self.open_repository()
668
if refname in self._git.refs:
669
ref_chain, unused_sha = self._git.refs.follow(
670
self._get_selected_ref(None))
671
if ref_chain[0] == b'HEAD':
672
refname = ref_chain[1]
673
from .branch import LocalGitBranch
674
branch = LocalGitBranch(self, repo, refname)
675
if append_revisions_only:
676
branch.set_append_revisions_only(append_revisions_only)
250
679
def backup_bzrdir(self):
680
if not self._git.bare:
252
681
self.root_transport.copy_tree(".git", ".git.backup")
253
682
return (self.root_transport.abspath(".git"),
254
683
self.root_transport.abspath(".git.backup"))
256
raise bzr_errors.BzrError("Unable to backup bare repositories")
685
basename = urlutils.basename(self.root_transport.base)
686
parent = self.root_transport.clone('..')
687
parent.copy_tree(basename, basename + ".backup")
258
689
def create_workingtree(self, revision_id=None, from_branch=None,
259
accelerator_tree=None, hardlink=False):
690
accelerator_tree=None, hardlink=False):
260
691
if self._git.bare:
261
raise bzr_errors.BzrError("Can't create working tree in a bare repo")
262
from dulwich.index import write_index
263
from dulwich.pack import SHA1Writer
264
f = open(self.transport.local_abspath("index"), 'w+')
270
return self.open_workingtree()
272
def find_repository(self):
273
"""Find the repository that should be used.
275
This does not require a branch as we use it to find the repo for
276
new branches as well as to hook existing branches up to their
279
return self.open_repository()
692
raise brz_errors.UnsupportedOperation(
693
self.create_workingtree, self)
694
if from_branch is None:
695
from_branch = self.open_branch(nascent_ok=True)
696
if revision_id is None:
697
revision_id = from_branch.last_revision()
698
repo = self.find_repository()
699
from .workingtree import GitWorkingTree
700
wt = GitWorkingTree(self, repo, from_branch)
701
wt.set_last_revision(revision_id)
702
wt._build_checkout_with_index()
705
def _find_or_create_repository(self, force_new_repo=None):
706
return self.create_repository(shared=False)
281
708
def _find_creation_modes(self):
282
709
"""Determine the appropriate modes for files and directories.