13
14
# You should have received a copy of the GNU General Public License
14
15
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""An adapter between a Git control dir and a Bazaar BzrDir"""
22
from bzrlib.lazy_import import lazy_import
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""An adapter between a Git control dir and a Bazaar ControlDir."""
20
from __future__ import absolute_import
25
branch as _mod_branch,
29
lazy_import(globals(), """
30
from bzrlib.lockable_files import TransportLock
31
from bzrlib.plugins.git import (
39
from bzrlib.plugins.git import LocalGitBzrDirFormat
43
class GitLock(object):
44
"""A lock that thunks through to Git."""
46
def lock_write(self, token=None):
58
def validate_token(self, token):
62
class GitLockableFiles(lockable_files.LockableFiles):
63
"""Git specific lockable files abstraction."""
65
def __init__(self, transport, lock):
67
self._transaction = None
68
self._lock_mode = None
70
self._transport = transport
73
class GitDir(bzrdir.BzrDir):
31
from ..transport import (
32
do_catching_redirections,
33
get_transport_from_path,
36
from ..controldir import (
41
RepositoryAcquisitionPolicy,
47
from .transportgit import (
53
class GitDirConfig(object):
55
def get_default_stack_on(self):
58
def set_default_stack_on(self, value):
59
raise brz_errors.BzrError("Cannot set configuration")
62
class GitControlDirFormat(ControlDirFormat):
64
colocated_branches = True
65
fixed_components = True
67
def __eq__(self, other):
68
return type(self) == type(other)
70
def is_supported(self):
73
def network_name(self):
77
class UseExistingRepository(RepositoryAcquisitionPolicy):
78
"""A policy of reusing an existing repository"""
80
def __init__(self, repository, stack_on=None, stack_on_pwd=None,
81
require_stacking=False):
84
:param repository: The repository to use.
85
:param stack_on: A location to stack on
86
:param stack_on_pwd: If stack_on is relative, the location it is
89
super(UseExistingRepository, self).__init__(
90
stack_on, stack_on_pwd, require_stacking)
91
self._repository = repository
93
def acquire_repository(self, make_working_trees=None, shared=False,
94
possible_transports=None):
95
"""Implementation of RepositoryAcquisitionPolicy.acquire_repository
97
Returns an existing repository to use.
99
return self._repository, False
102
class GitDir(ControlDir):
74
103
"""An adapter to the '.git' dir used by git."""
76
105
def is_supported(self):
108
def can_convert_format(self):
111
def break_lock(self):
112
# There are no global locks, so nothing to break.
113
raise NotImplementedError(self.break_lock)
79
115
def cloning_metadir(self, stacked=False):
80
return bzrlib.bzrdir.format_registry.make_bzrdir("1.9-rich-root")
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)
141
def get_config(self):
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):
83
470
class LocalGitDir(GitDir):
84
471
"""An adapter to the '.git' dir used by git."""
86
_gitrepository_class = repository.LocalGitRepository
88
def __init__(self, transport, lockfiles, gitrepo, format):
473
def _get_gitrepository_class(self):
474
from .repository import LocalGitRepository
475
return LocalGitRepository
478
return "<%s at %r>" % (
479
self.__class__.__name__, self.root_transport.base)
481
_gitrepository_class = property(_get_gitrepository_class)
484
def user_transport(self):
485
return self.root_transport
488
def control_transport(self):
489
return self._git._controltransport
491
def __init__(self, transport, gitrepo, format):
89
492
self._format = format
90
493
self.root_transport = transport
494
self._mode_check_done = False
91
495
self._git = gitrepo
93
497
self.transport = transport
95
499
self.transport = transport.clone('.git')
96
self._lockfiles = lockfiles
98
def get_branch_transport(self, branch_format):
500
self._mode_check_done = None
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()
575
def get_branch_transport(self, branch_format, name=None):
99
576
if branch_format is None:
100
577
return self.transport
101
if isinstance(branch_format, LocalGitBzrDirFormat):
102
return self.transport
103
raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
105
get_repository_transport = get_branch_transport
106
get_workingtree_transport = get_branch_transport
108
def open_branch(self, ignored=None):
578
if isinstance(branch_format, LocalGitControlDirFormat):
579
return self.transport
580
raise brz_errors.IncompatibleFormat(branch_format, self._format)
582
def get_repository_transport(self, format):
584
return self.transport
585
if isinstance(format, LocalGitControlDirFormat):
586
return self.transport
587
raise brz_errors.IncompatibleFormat(format, self._format)
589
def get_workingtree_transport(self, format):
591
return self.transport
592
if isinstance(format, LocalGitControlDirFormat):
593
return self.transport
594
raise brz_errors.IncompatibleFormat(format, self._format)
596
def open_branch(self, name=None, unsupported=False, ignore_fallbacks=None,
597
ref=None, possible_transports=None, nascent_ok=False):
109
598
"""'create' a branch for this dir."""
110
repo = self.open_repository()
111
return branch.LocalGitBranch(self, repo, "HEAD", repo._git.head(), self._lockfiles)
113
def open_repository(self, shared=False):
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])
612
def destroy_branch(self, name=None):
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)
624
def destroy_repository(self):
625
raise brz_errors.UnsupportedOperation(self.destroy_repository, self)
627
def 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)
634
def needs_format_conversion(self, format=None):
635
return not isinstance(self._format, format.__class__)
637
def open_repository(self):
114
638
"""'open' a repository for this dir."""
115
return self._gitrepository_class(self, self._lockfiles)
117
def open_workingtree(self, recommend_upgrade=True):
118
if (not self._git.bare and
119
os.path.exists(os.path.join(self._git.controldir(), "index"))):
120
return workingtree.GitWorkingTree(self, self.open_repository(),
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):
647
if not self._git.bare:
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)
122
652
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
123
raise errors.bzr_errors.NoWorkingTree(loc)
653
raise brz_errors.NoWorkingTree(loc)
125
655
def create_repository(self, shared=False):
126
return self.open_repository()
656
from .repository import GitRepositoryFormat
658
raise brz_errors.IncompatibleFormat(
659
GitRepositoryFormat(), self._format)
660
return self.find_repository()
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)
679
def backup_bzrdir(self):
680
if not self._git.bare:
681
self.root_transport.copy_tree(".git", ".git.backup")
682
return (self.root_transport.abspath(".git"),
683
self.root_transport.abspath(".git.backup"))
685
basename = urlutils.basename(self.root_transport.base)
686
parent = self.root_transport.clone('..')
687
parent.copy_tree(basename, basename + ".backup")
689
def create_workingtree(self, revision_id=None, from_branch=None,
690
accelerator_tree=None, hardlink=False):
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)
708
def _find_creation_modes(self):
709
"""Determine the appropriate modes for files and directories.
711
They're always set to be consistent with the base directory,
712
assuming that this transport allows setting modes.
714
# TODO: Do we need or want an option (maybe a config setting) to turn
715
# this off or override it for particular locations? -- mbp 20080512
716
if self._mode_check_done:
718
self._mode_check_done = True
720
st = self.transport.stat('.')
721
except brz_errors.TransportNotPossible:
722
self._dir_mode = None
723
self._file_mode = None
725
# Check the directory mode, but also make sure the created
726
# directories and files are read-write for this user. This is
727
# mostly a workaround for filesystems which lie about being able to
728
# write to a directory (cygwin & win32)
729
if (st.st_mode & 0o7777 == 0o0000):
730
# FTP allows stat but does not return dir/file modes
731
self._dir_mode = None
732
self._file_mode = None
734
self._dir_mode = (st.st_mode & 0o7777) | 0o0700
735
# Remove the sticky and execute bits for files
736
self._file_mode = self._dir_mode & ~0o7111
738
def _get_file_mode(self):
739
"""Return Unix mode for newly created files, or None.
741
if not self._mode_check_done:
742
self._find_creation_modes()
743
return self._file_mode
745
def _get_dir_mode(self):
746
"""Return Unix mode for newly created directories, or None.
748
if not self._mode_check_done:
749
self._find_creation_modes()
750
return self._dir_mode
752
def get_refs_container(self):
753
return self._git.refs
755
def get_peeled(self, ref):
756
return self._git.get_peeled(ref)
758
def _find_commondir(self):
760
commondir = self.control_transport.get_bytes('commondir')
761
except brz_errors.NoSuchFile:
764
commondir = commondir.rstrip(b'/.git/').decode(osutils._fs_enc)
765
return ControlDir.open_from_transport(
766
get_transport_from_path(commondir))