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
revision as _mod_revision,
32
from ...controldir import RepositoryAcquisitionPolicy
33
from ...transport import (
34
do_catching_redirections,
35
get_transport_from_path,
38
from ...controldir import (
42
RepositoryAcquisitionPolicy,
44
from .object_store import (
48
from .transportgit import (
54
class GitDirConfig(object):
56
def get_default_stack_on(self):
59
def set_default_stack_on(self, value):
60
raise bzr_errors.BzrError("Cannot set configuration")
63
class GitControlDirFormat(ControlDirFormat):
65
colocated_branches = True
66
fixed_components = True
68
def __eq__(self, other):
69
return type(self) == type(other)
71
def is_supported(self):
74
def network_name(self):
78
class UseExistingRepository(RepositoryAcquisitionPolicy):
79
"""A policy of reusing an existing repository"""
81
def __init__(self, repository, stack_on=None, stack_on_pwd=None,
82
require_stacking=False):
85
:param repository: The repository to use.
86
:param stack_on: A location to stack on
87
:param stack_on_pwd: If stack_on is relative, the location it is
90
super(UseExistingRepository, self).__init__(
91
stack_on, stack_on_pwd, require_stacking)
92
self._repository = repository
94
def acquire_repository(self, make_working_trees=None, shared=False,
95
possible_transports=None):
96
"""Implementation of RepositoryAcquisitionPolicy.acquire_repository
98
Returns an existing repository to use.
100
return self._repository, False
103
class GitDir(ControlDir):
104
"""An adapter to the '.git' dir used by git."""
106
def is_supported(self):
109
def can_convert_format(self):
112
def break_lock(self):
113
# There are no global locks, so nothing to break.
114
raise NotImplementedError(self.break_lock)
116
def cloning_metadir(self, stacked=False):
117
return format_registry.make_controldir("git")
119
def checkout_metadir(self, stacked=False):
120
return format_registry.make_controldir("git")
122
def _get_selected_ref(self, branch, ref=None):
123
if ref is not None and branch is not None:
124
raise bzr_errors.BzrError("can't specify both ref and branch")
127
if branch is not None:
128
from .refs import branch_name_to_ref
129
return branch_name_to_ref(branch)
130
segment_parameters = getattr(
131
self.user_transport, "get_segment_parameters", lambda: {})()
132
ref = segment_parameters.get("ref")
134
return urlutils.unescape(ref)
135
if branch is None and getattr(self, "_get_selected_branch", False):
136
branch = self._get_selected_branch()
137
if branch is not None:
138
from .refs import branch_name_to_ref
139
return branch_name_to_ref(branch)
142
def get_config(self):
143
return GitDirConfig()
145
def _available_backup_name(self, base):
146
return osutils.available_backup_name(base, self.root_transport.has)
148
def sprout(self, url, revision_id=None, force_new_repo=False,
149
recurse='down', possible_transports=None,
150
accelerator_tree=None, hardlink=False, stacked=False,
151
source_branch=None, create_tree_if_local=True):
152
from ...repository import InterRepository
153
from ...transport.local import LocalTransport
154
from ...transport import get_transport
155
target_transport = get_transport(url, possible_transports)
156
target_transport.ensure_base()
157
cloning_format = self.cloning_metadir()
158
# Create/update the result branch
160
result = ControlDir.open_from_transport(target_transport)
161
except bzr_errors.NotBranchError:
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 _mod_branch.UnstackableBranchFormat(self._format, self.user_url)
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
def clone_on_transport(self, transport, revision_id=None,
193
force_new_repo=False, preserve_stacking=False, stacked_on=None,
194
create_prefix=False, use_existing_dir=True, no_tree=False):
195
"""See ControlDir.clone_on_transport."""
196
from ...repository import InterRepository
197
from .mapping import default_mapping
198
if stacked_on is not None:
199
raise _mod_branch.UnstackableBranchFormat(self._format, self.user_url)
201
format = BareLocalGitControlDirFormat()
203
format = LocalGitControlDirFormat()
204
(target_repo, target_controldir, stacking,
205
repo_policy) = format.initialize_on_transport_ex(
206
transport, use_existing_dir=use_existing_dir,
207
create_prefix=create_prefix,
208
force_new_repo=force_new_repo)
209
target_repo = target_controldir.find_repository()
210
target_git_repo = target_repo._git
211
source_repo = self.find_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
result_dir = self.__class__(transport, target_git_repo, format)
223
if revision_id is not None:
224
result_dir.open_branch().set_last_revision(revision_id)
227
def _find_commondir(self):
229
commondir = self.control_transport.get_bytes('commondir')
230
except bzr_errors.NoSuchFile:
233
commondir = commondir.rstrip('/.git/')
234
return ControlDir.open_from_transport(get_transport_from_path(commondir))
236
def find_repository(self):
237
"""Find the repository that should be used.
239
This does not require a branch as we use it to find the repo for
240
new branches as well as to hook existing branches up to their
243
return self._gitrepository_class(self._find_commondir())
245
def get_refs_container(self):
246
"""Retrieve the refs container.
248
raise NotImplementedError(self.get_refs_container)
250
def determine_repository_policy(self, force_new_repo=False, stack_on=None,
251
stack_on_pwd=None, require_stacking=False):
252
"""Return an object representing a policy to use.
254
This controls whether a new repository is created, and the format of
255
that repository, or some existing shared repository used instead.
257
If stack_on is supplied, will not seek a containing shared repo.
259
:param force_new_repo: If True, require a new repository to be created.
260
:param stack_on: If supplied, the location to stack on. If not
261
supplied, a default_stack_on location may be used.
262
:param stack_on_pwd: If stack_on is relative, the location it is
265
return UseExistingRepository(self.find_repository())
267
def get_branches(self):
268
from .refs import ref_to_branch_name
270
for ref in self.get_refs_container().keys():
272
branch_name = ref_to_branch_name(ref)
275
except UnicodeDecodeError:
276
trace.warning("Ignoring branch %r with unicode error ref", ref)
278
ret[branch_name] = self.open_branch(ref=ref)
281
def list_branches(self):
282
return self.get_branches().values()
285
class LocalGitControlDirFormat(GitControlDirFormat):
286
"""The .git directory control format."""
291
def _known_formats(self):
292
return set([LocalGitControlDirFormat()])
295
def repository_format(self):
296
from .repository import GitRepositoryFormat
297
return GitRepositoryFormat()
300
def workingtree_format(self):
301
from .workingtree import GitWorkingTreeFormat
302
return GitWorkingTreeFormat()
304
def get_branch_format(self):
305
from .branch import LocalGitBranchFormat
306
return LocalGitBranchFormat()
308
def open(self, transport, _found=None):
309
"""Open this directory.
312
from .transportgit import TransportRepo
313
gitrepo = TransportRepo(transport, self.bare,
314
refs_text=getattr(self, "_refs_text", None))
315
if not gitrepo._controltransport.has('HEAD'):
316
raise bzr_errors.NotBranchError(path=transport.base)
317
return LocalGitDir(transport, gitrepo, self)
319
def get_format_description(self):
320
return "Local Git Repository"
322
def initialize_on_transport(self, transport):
323
from .transportgit import TransportRepo
324
repo = TransportRepo.init(transport, bare=self.bare)
325
return self.open(transport)
327
def initialize_on_transport_ex(self, transport, use_existing_dir=False,
328
create_prefix=False, force_new_repo=False, stacked_on=None,
329
stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
330
shared_repo=False, vfs_only=False):
331
def make_directory(transport):
334
def redirected(transport, e, redirection_notice):
335
trace.note(redirection_notice)
336
return transport._redirected_to(e.source, e.target)
338
transport = do_catching_redirections(make_directory, transport,
340
except bzr_errors.FileExists:
341
if not use_existing_dir:
343
except bzr_errors.NoSuchFile:
344
if not create_prefix:
346
transport.create_prefix()
347
controldir = self.initialize_on_transport(transport)
349
result_repo = controldir.find_repository()
350
repository_policy = UseExistingRepository(result_repo)
351
result_repo.lock_write()
354
repository_policy = None
355
return (result_repo, controldir, False,
358
def is_supported(self):
361
def supports_transport(self, transport):
363
external_url = transport.external_url()
364
except bzr_errors.InProcessTransport:
365
raise bzr_errors.NotBranchError(path=transport.base)
366
return external_url.startswith("file:")
369
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
372
supports_workingtrees = False
374
def get_format_description(self):
375
return "Local Git Repository (bare)"
378
class LocalGitDir(GitDir):
379
"""An adapter to the '.git' dir used by git."""
381
def _get_gitrepository_class(self):
382
from .repository import LocalGitRepository
383
return LocalGitRepository
386
return "<%s at %r>" % (
387
self.__class__.__name__, self.root_transport.base)
389
_gitrepository_class = property(_get_gitrepository_class)
392
def user_transport(self):
393
return self.root_transport
396
def control_transport(self):
397
return self._git._controltransport
399
def __init__(self, transport, gitrepo, format):
400
self._format = format
401
self.root_transport = transport
402
self._mode_check_done = False
405
self.transport = transport
407
self.transport = transport.clone('.git')
408
self._mode_check_done = None
410
def is_control_filename(self, filename):
411
return (filename == '.git' or
412
filename.startswith('.git/') or
413
filename.startswith('.git\\'))
415
def _get_symref(self, ref):
416
from dulwich.repo import SYMREF
417
ref_chain, unused_sha = self._git.refs.follow(ref)
418
if len(ref_chain) == 1:
422
def set_branch_reference(self, target_branch, name=None):
423
ref = self._get_selected_ref(name)
424
if self.control_transport.base == target_branch.controldir.control_transport.base:
425
self._git.refs.set_symbolic_ref(ref, target_branch.ref)
428
target_path = target_branch.controldir.control_transport.local_abspath('.')
429
except bzr_errors.NotLocalUrl:
430
raise bzr_errors.IncompatibleFormat(target_branch._format, self._format)
431
# TODO(jelmer): Do some consistency checking across branches..
432
self.control_transport.put_bytes('commondir', target_path.encode('utf-8'))
433
# TODO(jelmer): Urgh, avoid mucking about with internals.
434
self._git._commontransport = target_branch.repository._git._commontransport.clone()
435
self._git.object_store = TransportObjectStore(self._git._commontransport.clone(OBJECTDIR))
436
self._git.refs.transport = self._git._commontransport
437
target_ref_chain, unused_sha = target_branch.controldir._git.refs.follow(target_branch.ref)
438
for target_ref in target_ref_chain:
439
if target_ref == b'HEAD':
443
# Can't create a reference to something that is not a in a repository.
444
raise bzr_errors.IncompatibleFormat(self.set_branch_reference, self)
445
self._git.refs.set_symbolic_ref(ref, target_ref)
447
def get_branch_reference(self, name=None):
448
ref = self._get_selected_ref(name)
449
target_ref = self._get_symref(ref)
450
if target_ref is not None:
451
from .refs import ref_to_branch_name
453
branch_name = ref_to_branch_name(target_ref)
455
params = {'ref': urllib.quote(target_ref, '')}
457
if branch_name != b'':
458
params = {'branch': urllib.quote(branch_name.encode('utf-8'), '')}
462
base_url = urlutils.local_path_to_url(self.control_transport.get_bytes('commondir')).rstrip('/.git/')+'/'
463
except bzr_errors.NoSuchFile:
464
base_url = self.user_url.rstrip('/')
465
return urlutils.join_segment_parameters(base_url, params)
468
def find_branch_format(self, name=None):
469
from .branch import (
470
LocalGitBranchFormat,
472
ref = self._get_selected_ref(name)
473
return LocalGitBranchFormat()
475
def get_branch_transport(self, branch_format, name=None):
476
if branch_format is None:
477
return self.transport
478
if isinstance(branch_format, LocalGitControlDirFormat):
479
return self.transport
480
raise bzr_errors.IncompatibleFormat(branch_format, self._format)
482
def get_repository_transport(self, format):
484
return self.transport
485
if isinstance(format, LocalGitControlDirFormat):
486
return self.transport
487
raise bzr_errors.IncompatibleFormat(format, self._format)
489
def get_workingtree_transport(self, format):
491
return self.transport
492
if isinstance(format, LocalGitControlDirFormat):
493
return self.transport
494
raise bzr_errors.IncompatibleFormat(format, self._format)
496
def open_branch(self, name=None, unsupported=False, ignore_fallbacks=None,
497
ref=None, possible_transports=None, nascent_ok=False):
498
"""'create' a branch for this dir."""
499
repo = self.find_repository()
500
from .branch import LocalGitBranch
501
ref = self._get_selected_ref(name, ref)
502
if not nascent_ok and ref not in self._git.refs:
503
raise bzr_errors.NotBranchError(self.root_transport.base,
505
ref_chain, unused_sha = self._git.refs.follow(ref)
506
if ref_chain[-1] == b'HEAD':
509
controldir = self._find_commondir()
510
return LocalGitBranch(controldir, repo, ref_chain[-1])
512
def destroy_branch(self, name=None):
513
refname = self._get_selected_ref(name)
514
if refname == b'HEAD':
515
# HEAD can't be removed
516
raise bzr_errors.UnsupportedOperation(
517
self.destroy_branch, self)
519
del self._git.refs[refname]
521
raise bzr_errors.NotBranchError(self.root_transport.base,
524
def destroy_repository(self):
525
raise bzr_errors.UnsupportedOperation(self.destroy_repository, self)
527
def destroy_workingtree(self):
528
raise bzr_errors.UnsupportedOperation(self.destroy_workingtree, self)
530
def destroy_workingtree_metadata(self):
531
raise bzr_errors.UnsupportedOperation(self.destroy_workingtree_metadata, self)
533
def needs_format_conversion(self, format=None):
534
return not isinstance(self._format, format.__class__)
536
def open_repository(self):
537
"""'open' a repository for this dir."""
538
if self.control_transport.has('commondir'):
539
raise bzr_errors.NoRepositoryPresent(self)
540
return self._gitrepository_class(self)
542
def has_workingtree(self):
543
return not self._git.bare
545
def open_workingtree(self, recommend_upgrade=True, unsupported=False):
546
if not self._git.bare:
547
from dulwich.errors import NoIndexPresent
548
repo = self.find_repository()
550
index = repo._git.open_index()
551
except NoIndexPresent:
554
from .workingtree import GitWorkingTree
555
branch = self.open_branch(ref=b'HEAD', nascent_ok=True)
556
return GitWorkingTree(self, repo, branch, index)
557
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
558
raise bzr_errors.NoWorkingTree(loc)
560
def create_repository(self, shared=False):
561
from .repository import GitRepositoryFormat
563
raise bzr_errors.IncompatibleFormat(GitRepositoryFormat(), self._format)
564
return self.find_repository()
566
def create_branch(self, name=None, repository=None,
567
append_revisions_only=None, ref=None):
568
refname = self._get_selected_ref(name, ref)
569
if refname != b'HEAD' and refname in self._git.refs:
570
raise bzr_errors.AlreadyBranchError(self.user_url)
571
repo = self.open_repository()
572
if refname in self._git.refs:
573
ref_chain, unused_sha = self._git.refs.follow(self._get_selected_ref(None))
574
if ref_chain[0] == b'HEAD':
575
refname = ref_chain[1]
576
from .branch import LocalGitBranch
577
branch = LocalGitBranch(self, repo, refname)
578
if append_revisions_only:
579
branch.set_append_revisions_only(append_revisions_only)
582
def backup_bzrdir(self):
583
if not self._git.bare:
584
self.root_transport.copy_tree(".git", ".git.backup")
585
return (self.root_transport.abspath(".git"),
586
self.root_transport.abspath(".git.backup"))
588
basename = urlutils.basename(self.root_transport.base)
589
parent = self.root_transport.clone('..')
590
parent.copy_tree(basename, basename + ".backup")
592
def create_workingtree(self, revision_id=None, from_branch=None,
593
accelerator_tree=None, hardlink=False):
595
raise bzr_errors.UnsupportedOperation(self.create_workingtree, self)
596
from dulwich.index import build_index_from_tree
597
if from_branch is None:
598
from_branch = self.open_branch(nascent_ok=True)
599
if revision_id is None:
600
revision_id = from_branch.last_revision()
601
repo = self.find_repository()
602
from .workingtree import GitWorkingTree
603
index = self._git.open_index()
604
wt = GitWorkingTree(self, repo, from_branch, index)
605
wt.set_last_revision(revision_id)
606
wt._build_checkout_with_index()
609
def _find_or_create_repository(self, force_new_repo=None):
610
return self.create_repository(shared=False)
612
def _find_creation_modes(self):
613
"""Determine the appropriate modes for files and directories.
615
They're always set to be consistent with the base directory,
616
assuming that this transport allows setting modes.
618
# TODO: Do we need or want an option (maybe a config setting) to turn
619
# this off or override it for particular locations? -- mbp 20080512
620
if self._mode_check_done:
622
self._mode_check_done = True
624
st = self.transport.stat('.')
625
except bzr_errors.TransportNotPossible:
626
self._dir_mode = None
627
self._file_mode = None
629
# Check the directory mode, but also make sure the created
630
# directories and files are read-write for this user. This is
631
# mostly a workaround for filesystems which lie about being able to
632
# write to a directory (cygwin & win32)
633
if (st.st_mode & 07777 == 00000):
634
# FTP allows stat but does not return dir/file modes
635
self._dir_mode = None
636
self._file_mode = None
638
self._dir_mode = (st.st_mode & 07777) | 00700
639
# Remove the sticky and execute bits for files
640
self._file_mode = self._dir_mode & ~07111
642
def _get_file_mode(self):
643
"""Return Unix mode for newly created files, or None.
645
if not self._mode_check_done:
646
self._find_creation_modes()
647
return self._file_mode
649
def _get_dir_mode(self):
650
"""Return Unix mode for newly created directories, or None.
652
if not self._mode_check_done:
653
self._find_creation_modes()
654
return self._dir_mode
656
def get_refs_container(self):
657
return self._git.refs
659
def get_peeled(self, ref):
660
return self._git.get_peeled(ref)