1
# Copyright (C) 2007 Canonical Ltd
2
# Copyright (C) 2010-2018 Jelmer Vernooij
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
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
repository as _mod_repository,
30
revision as _mod_revision,
33
from ...transport import (
34
do_catching_redirections,
35
get_transport_from_path,
38
from ...controldir import (
43
RepositoryAcquisitionPolicy,
45
from .object_store import (
52
from .transportgit import (
58
class GitDirConfig(object):
60
def get_default_stack_on(self):
63
def set_default_stack_on(self, value):
64
raise bzr_errors.BzrError("Cannot set configuration")
67
class GitControlDirFormat(ControlDirFormat):
69
colocated_branches = True
70
fixed_components = True
72
def __eq__(self, other):
73
return type(self) == type(other)
75
def is_supported(self):
78
def network_name(self):
82
class UseExistingRepository(RepositoryAcquisitionPolicy):
83
"""A policy of reusing an existing repository"""
85
def __init__(self, repository, stack_on=None, stack_on_pwd=None,
86
require_stacking=False):
89
:param repository: The repository to use.
90
:param stack_on: A location to stack on
91
:param stack_on_pwd: If stack_on is relative, the location it is
94
super(UseExistingRepository, self).__init__(
95
stack_on, stack_on_pwd, require_stacking)
96
self._repository = repository
98
def acquire_repository(self, make_working_trees=None, shared=False,
99
possible_transports=None):
100
"""Implementation of RepositoryAcquisitionPolicy.acquire_repository
102
Returns an existing repository to use.
104
return self._repository, False
107
class GitDir(ControlDir):
108
"""An adapter to the '.git' dir used by git."""
110
def is_supported(self):
113
def can_convert_format(self):
116
def break_lock(self):
117
# There are no global locks, so nothing to break.
118
raise NotImplementedError(self.break_lock)
120
def cloning_metadir(self, stacked=False):
121
return format_registry.make_controldir("git")
123
def checkout_metadir(self, stacked=False):
124
return format_registry.make_controldir("git")
126
def _get_selected_ref(self, branch, ref=None):
127
if ref is not None and branch is not None:
128
raise bzr_errors.BzrError("can't specify both ref and branch")
131
if branch is not None:
132
from .refs import branch_name_to_ref
133
return branch_name_to_ref(branch)
134
segment_parameters = getattr(
135
self.user_transport, "get_segment_parameters", lambda: {})()
136
ref = segment_parameters.get("ref")
138
return urlutils.unescape(ref)
139
if branch is None and getattr(self, "_get_selected_branch", False):
140
branch = self._get_selected_branch()
141
if branch is not None:
142
from .refs import branch_name_to_ref
143
return branch_name_to_ref(branch)
146
def get_config(self):
147
return GitDirConfig()
149
def _available_backup_name(self, base):
150
return osutils.available_backup_name(base, self.root_transport.has)
152
def sprout(self, url, revision_id=None, force_new_repo=False,
153
recurse='down', possible_transports=None,
154
accelerator_tree=None, hardlink=False, stacked=False,
155
source_branch=None, create_tree_if_local=True):
156
from ...repository import InterRepository
157
from ...transport.local import LocalTransport
158
from ...transport import get_transport
159
target_transport = get_transport(url, possible_transports)
160
target_transport.ensure_base()
161
cloning_format = self.cloning_metadir()
162
# Create/update the result branch
164
result = ControlDir.open_from_transport(target_transport)
165
except bzr_errors.NotBranchError:
166
result = cloning_format.initialize_on_transport(target_transport)
167
source_branch = self.open_branch()
168
source_repository = self.find_repository()
170
result_repo = result.find_repository()
171
except bzr_errors.NoRepositoryPresent:
172
result_repo = result.create_repository()
173
target_is_empty = True
175
target_is_empty = None # Unknown
177
raise _mod_branch.UnstackableBranchFormat(self._format, self.user_url)
178
interrepo = InterRepository.get(source_repository, result_repo)
180
if revision_id is not None:
181
determine_wants = interrepo.get_determine_wants_revids(
182
[revision_id], include_tags=True)
184
determine_wants = interrepo.determine_wants_all
185
interrepo.fetch_objects(determine_wants=determine_wants,
186
mapping=source_branch.mapping)
187
result_branch = source_branch.sprout(result,
188
revision_id=revision_id, repository=result_repo)
189
if (create_tree_if_local
190
and isinstance(target_transport, LocalTransport)
191
and (result_repo is None or result_repo.make_working_trees())):
192
wt = result.create_workingtree(accelerator_tree=accelerator_tree,
193
hardlink=hardlink, from_branch=result_branch)
196
def clone_on_transport(self, transport, revision_id=None,
197
force_new_repo=False, preserve_stacking=False, stacked_on=None,
198
create_prefix=False, use_existing_dir=True, no_tree=False):
199
"""See ControlDir.clone_on_transport."""
200
from ...repository import InterRepository
201
from .mapping import default_mapping
202
if stacked_on is not None:
203
raise _mod_branch.UnstackableBranchFormat(self._format, self.user_url)
205
format = BareLocalGitControlDirFormat()
207
format = LocalGitControlDirFormat()
208
(target_repo, target_controldir, stacking,
209
repo_policy) = format.initialize_on_transport_ex(
210
transport, use_existing_dir=use_existing_dir,
211
create_prefix=create_prefix,
212
force_new_repo=force_new_repo)
213
target_repo = target_controldir.find_repository()
214
target_git_repo = target_repo._git
215
source_repo = self.find_repository()
216
source_git_repo = source_repo._git
217
interrepo = InterRepository.get(source_repo, target_repo)
218
if revision_id is not None:
219
determine_wants = interrepo.get_determine_wants_revids([revision_id], include_tags=True)
221
determine_wants = interrepo.determine_wants_all
222
(pack_hint, _, refs) = interrepo.fetch_objects(determine_wants,
223
mapping=default_mapping)
224
for name, val in refs.iteritems():
225
target_git_repo.refs[name] = val
226
result_dir = self.__class__(transport, target_git_repo, format)
227
if revision_id is not None:
228
result_dir.open_branch().set_last_revision(revision_id)
230
# Cheaper to check if the target is not local, than to try making
232
result_dir.root_transport.local_abspath('.')
233
if result_dir.open_repository().make_working_trees():
234
self.open_workingtree().clone(result_dir, revision_id=revision_id)
235
except (bzr_errors.NoWorkingTree, bzr_errors.NotLocalUrl):
240
def find_repository(self):
241
"""Find the repository that should be used.
243
This does not require a branch as we use it to find the repo for
244
new branches as well as to hook existing branches up to their
247
return self._gitrepository_class(self._find_commondir())
249
def get_refs_container(self):
250
"""Retrieve the refs container.
252
raise NotImplementedError(self.get_refs_container)
254
def determine_repository_policy(self, force_new_repo=False, stack_on=None,
255
stack_on_pwd=None, require_stacking=False):
256
"""Return an object representing a policy to use.
258
This controls whether a new repository is created, and the format of
259
that repository, or some existing shared repository used instead.
261
If stack_on is supplied, will not seek a containing shared repo.
263
:param force_new_repo: If True, require a new repository to be created.
264
:param stack_on: If supplied, the location to stack on. If not
265
supplied, a default_stack_on location may be used.
266
:param stack_on_pwd: If stack_on is relative, the location it is
269
return UseExistingRepository(self.find_repository())
271
def get_branches(self):
272
from .refs import ref_to_branch_name
274
for ref in self.get_refs_container().keys():
276
branch_name = ref_to_branch_name(ref)
279
except UnicodeDecodeError:
280
trace.warning("Ignoring branch %r with unicode error ref", ref)
282
ret[branch_name] = self.open_branch(ref=ref)
285
def list_branches(self):
286
return self.get_branches().values()
288
def push_branch(self, source, revision_id=None, overwrite=False,
289
remember=False, create_prefix=False, lossy=False,
291
"""Push the source branch into this ControlDir."""
292
push_result = GitPushResult()
293
push_result.workingtree_updated = None
294
push_result.master_branch = None
295
push_result.source_branch = source
296
push_result.stacked_on = None
297
repo = self.find_repository()
298
refname = self._get_selected_ref(name)
299
from .branch import GitBranch
300
if isinstance(source, GitBranch) and lossy:
301
raise bzr_errors.LossyPushToSameVCS(source.controldir, self)
302
target = self.open_branch(name, nascent_ok=True)
303
push_result.branch_push_result = source.push(
304
target, overwrite=overwrite, stop_revision=revision_id,
306
push_result.new_revid = push_result.branch_push_result.new_revid
307
push_result.old_revid = push_result.branch_push_result.old_revid
308
push_result.target_branch = self.open_branch(name)
309
if source.get_push_location() is None or remember:
310
source.set_push_location(push_result.target_branch.base)
314
class LocalGitControlDirFormat(GitControlDirFormat):
315
"""The .git directory control format."""
320
def _known_formats(self):
321
return set([LocalGitControlDirFormat()])
324
def repository_format(self):
325
from .repository import GitRepositoryFormat
326
return GitRepositoryFormat()
329
def workingtree_format(self):
330
from .workingtree import GitWorkingTreeFormat
331
return GitWorkingTreeFormat()
333
def get_branch_format(self):
334
from .branch import LocalGitBranchFormat
335
return LocalGitBranchFormat()
337
def open(self, transport, _found=None):
338
"""Open this directory.
341
from .transportgit import TransportRepo
342
gitrepo = TransportRepo(transport, self.bare,
343
refs_text=getattr(self, "_refs_text", None))
344
if not gitrepo._controltransport.has('HEAD'):
345
raise bzr_errors.NotBranchError(path=transport.base)
346
return LocalGitDir(transport, gitrepo, self)
348
def get_format_description(self):
349
return "Local Git Repository"
351
def initialize_on_transport(self, transport):
352
from .transportgit import TransportRepo
353
repo = TransportRepo.init(transport, bare=self.bare)
354
return self.open(transport)
356
def initialize_on_transport_ex(self, transport, use_existing_dir=False,
357
create_prefix=False, force_new_repo=False, stacked_on=None,
358
stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
359
shared_repo=False, vfs_only=False):
360
def make_directory(transport):
363
def redirected(transport, e, redirection_notice):
364
trace.note(redirection_notice)
365
return transport._redirected_to(e.source, e.target)
367
transport = do_catching_redirections(make_directory, transport,
369
except bzr_errors.FileExists:
370
if not use_existing_dir:
372
except bzr_errors.NoSuchFile:
373
if not create_prefix:
375
transport.create_prefix()
376
controldir = self.initialize_on_transport(transport)
378
result_repo = controldir.find_repository()
379
repository_policy = UseExistingRepository(result_repo)
380
result_repo.lock_write()
383
repository_policy = None
384
return (result_repo, controldir, False,
387
def is_supported(self):
390
def supports_transport(self, transport):
392
external_url = transport.external_url()
393
except bzr_errors.InProcessTransport:
394
raise bzr_errors.NotBranchError(path=transport.base)
395
return external_url.startswith("file:")
398
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
401
supports_workingtrees = False
403
def get_format_description(self):
404
return "Local Git Repository (bare)"
407
class LocalGitDir(GitDir):
408
"""An adapter to the '.git' dir used by git."""
410
def _get_gitrepository_class(self):
411
from .repository import LocalGitRepository
412
return LocalGitRepository
415
return "<%s at %r>" % (
416
self.__class__.__name__, self.root_transport.base)
418
_gitrepository_class = property(_get_gitrepository_class)
421
def user_transport(self):
422
return self.root_transport
425
def control_transport(self):
426
return self._git._controltransport
428
def __init__(self, transport, gitrepo, format):
429
self._format = format
430
self.root_transport = transport
431
self._mode_check_done = False
434
self.transport = transport
436
self.transport = transport.clone('.git')
437
self._mode_check_done = None
439
def is_control_filename(self, filename):
440
return (filename == '.git' or
441
filename.startswith('.git/') or
442
filename.startswith('.git\\'))
444
def _get_symref(self, ref):
445
from dulwich.repo import SYMREF
446
ref_chain, unused_sha = self._git.refs.follow(ref)
447
if len(ref_chain) == 1:
451
def set_branch_reference(self, target_branch, name=None):
452
ref = self._get_selected_ref(name)
453
if self.control_transport.base == target_branch.controldir.control_transport.base:
454
if ref == target_branch.ref:
455
raise BranchReferenceLoop(target_branch)
456
self._git.refs.set_symbolic_ref(ref, target_branch.ref)
459
target_path = target_branch.controldir.control_transport.local_abspath('.')
460
except bzr_errors.NotLocalUrl:
461
raise bzr_errors.IncompatibleFormat(target_branch._format, self._format)
462
# TODO(jelmer): Do some consistency checking across branches..
463
self.control_transport.put_bytes('commondir', target_path.encode('utf-8'))
464
# TODO(jelmer): Urgh, avoid mucking about with internals.
465
self._git._commontransport = target_branch.repository._git._commontransport.clone()
466
self._git.object_store = TransportObjectStore(self._git._commontransport.clone(OBJECTDIR))
467
self._git.refs.transport = self._git._commontransport
468
target_ref_chain, unused_sha = target_branch.controldir._git.refs.follow(target_branch.ref)
469
for target_ref in target_ref_chain:
470
if target_ref == b'HEAD':
474
# Can't create a reference to something that is not a in a repository.
475
raise bzr_errors.IncompatibleFormat(self.set_branch_reference, self)
476
self._git.refs.set_symbolic_ref(ref, target_ref)
478
def get_branch_reference(self, name=None):
479
ref = self._get_selected_ref(name)
480
target_ref = self._get_symref(ref)
481
if target_ref is not None:
482
from .refs import ref_to_branch_name
484
branch_name = ref_to_branch_name(target_ref)
486
params = {'ref': urllib.quote(target_ref, '')}
488
if branch_name != b'':
489
params = {'branch': urllib.quote(branch_name.encode('utf-8'), '')}
493
base_url = urlutils.local_path_to_url(self.control_transport.get_bytes('commondir')).rstrip('/.git/')+'/'
494
except bzr_errors.NoSuchFile:
495
base_url = self.user_url.rstrip('/')
496
return urlutils.join_segment_parameters(base_url, params)
499
def find_branch_format(self, name=None):
500
from .branch import (
501
LocalGitBranchFormat,
503
ref = self._get_selected_ref(name)
504
return LocalGitBranchFormat()
506
def get_branch_transport(self, branch_format, name=None):
507
if branch_format is None:
508
return self.transport
509
if isinstance(branch_format, LocalGitControlDirFormat):
510
return self.transport
511
raise bzr_errors.IncompatibleFormat(branch_format, self._format)
513
def get_repository_transport(self, format):
515
return self.transport
516
if isinstance(format, LocalGitControlDirFormat):
517
return self.transport
518
raise bzr_errors.IncompatibleFormat(format, self._format)
520
def get_workingtree_transport(self, format):
522
return self.transport
523
if isinstance(format, LocalGitControlDirFormat):
524
return self.transport
525
raise bzr_errors.IncompatibleFormat(format, self._format)
527
def open_branch(self, name=None, unsupported=False, ignore_fallbacks=None,
528
ref=None, possible_transports=None, nascent_ok=False):
529
"""'create' a branch for this dir."""
530
repo = self.find_repository()
531
from .branch import LocalGitBranch
532
ref = self._get_selected_ref(name, ref)
533
if not nascent_ok and ref not in self._git.refs:
534
raise bzr_errors.NotBranchError(self.root_transport.base,
536
ref_chain, unused_sha = self._git.refs.follow(ref)
537
if ref_chain[-1] == b'HEAD':
540
controldir = self._find_commondir()
541
return LocalGitBranch(controldir, repo, ref_chain[-1])
543
def destroy_branch(self, name=None):
544
refname = self._get_selected_ref(name)
545
if refname == b'HEAD':
546
# HEAD can't be removed
547
raise bzr_errors.UnsupportedOperation(
548
self.destroy_branch, self)
550
del self._git.refs[refname]
552
raise bzr_errors.NotBranchError(self.root_transport.base,
555
def destroy_repository(self):
556
raise bzr_errors.UnsupportedOperation(self.destroy_repository, self)
558
def destroy_workingtree(self):
559
raise bzr_errors.UnsupportedOperation(self.destroy_workingtree, self)
561
def destroy_workingtree_metadata(self):
562
raise bzr_errors.UnsupportedOperation(self.destroy_workingtree_metadata, self)
564
def needs_format_conversion(self, format=None):
565
return not isinstance(self._format, format.__class__)
567
def open_repository(self):
568
"""'open' a repository for this dir."""
569
if self.control_transport.has('commondir'):
570
raise bzr_errors.NoRepositoryPresent(self)
571
return self._gitrepository_class(self)
573
def has_workingtree(self):
574
return not self._git.bare
576
def open_workingtree(self, recommend_upgrade=True, unsupported=False):
577
if not self._git.bare:
578
from dulwich.errors import NoIndexPresent
579
repo = self.find_repository()
580
from .workingtree import GitWorkingTree
581
branch = self.open_branch(ref=b'HEAD', nascent_ok=True)
582
return GitWorkingTree(self, repo, branch)
583
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
584
raise bzr_errors.NoWorkingTree(loc)
586
def create_repository(self, shared=False):
587
from .repository import GitRepositoryFormat
589
raise bzr_errors.IncompatibleFormat(GitRepositoryFormat(), self._format)
590
return self.find_repository()
592
def create_branch(self, name=None, repository=None,
593
append_revisions_only=None, ref=None):
594
refname = self._get_selected_ref(name, ref)
595
if refname != b'HEAD' and refname in self._git.refs:
596
raise bzr_errors.AlreadyBranchError(self.user_url)
597
repo = self.open_repository()
598
if refname in self._git.refs:
599
ref_chain, unused_sha = self._git.refs.follow(self._get_selected_ref(None))
600
if ref_chain[0] == b'HEAD':
601
refname = ref_chain[1]
602
from .branch import LocalGitBranch
603
branch = LocalGitBranch(self, repo, refname)
604
if append_revisions_only:
605
branch.set_append_revisions_only(append_revisions_only)
608
def backup_bzrdir(self):
609
if not self._git.bare:
610
self.root_transport.copy_tree(".git", ".git.backup")
611
return (self.root_transport.abspath(".git"),
612
self.root_transport.abspath(".git.backup"))
614
basename = urlutils.basename(self.root_transport.base)
615
parent = self.root_transport.clone('..')
616
parent.copy_tree(basename, basename + ".backup")
618
def create_workingtree(self, revision_id=None, from_branch=None,
619
accelerator_tree=None, hardlink=False):
621
raise bzr_errors.UnsupportedOperation(self.create_workingtree, self)
622
if from_branch is None:
623
from_branch = self.open_branch(nascent_ok=True)
624
if revision_id is None:
625
revision_id = from_branch.last_revision()
626
repo = self.find_repository()
627
from .workingtree import GitWorkingTree
628
wt = GitWorkingTree(self, repo, from_branch)
629
wt.set_last_revision(revision_id)
630
wt._build_checkout_with_index()
633
def _find_or_create_repository(self, force_new_repo=None):
634
return self.create_repository(shared=False)
636
def _find_creation_modes(self):
637
"""Determine the appropriate modes for files and directories.
639
They're always set to be consistent with the base directory,
640
assuming that this transport allows setting modes.
642
# TODO: Do we need or want an option (maybe a config setting) to turn
643
# this off or override it for particular locations? -- mbp 20080512
644
if self._mode_check_done:
646
self._mode_check_done = True
648
st = self.transport.stat('.')
649
except bzr_errors.TransportNotPossible:
650
self._dir_mode = None
651
self._file_mode = None
653
# Check the directory mode, but also make sure the created
654
# directories and files are read-write for this user. This is
655
# mostly a workaround for filesystems which lie about being able to
656
# write to a directory (cygwin & win32)
657
if (st.st_mode & 07777 == 00000):
658
# FTP allows stat but does not return dir/file modes
659
self._dir_mode = None
660
self._file_mode = None
662
self._dir_mode = (st.st_mode & 07777) | 00700
663
# Remove the sticky and execute bits for files
664
self._file_mode = self._dir_mode & ~07111
666
def _get_file_mode(self):
667
"""Return Unix mode for newly created files, or None.
669
if not self._mode_check_done:
670
self._find_creation_modes()
671
return self._file_mode
673
def _get_dir_mode(self):
674
"""Return Unix mode for newly created directories, or None.
676
if not self._mode_check_done:
677
self._find_creation_modes()
678
return self._dir_mode
680
def get_refs_container(self):
681
return self._git.refs
683
def get_peeled(self, ref):
684
return self._git.get_peeled(ref)
686
def _find_commondir(self):
688
commondir = self.control_transport.get_bytes('commondir')
689
except bzr_errors.NoSuchFile:
692
commondir = commondir.rstrip('/.git/')
693
return ControlDir.open_from_transport(get_transport_from_path(commondir))