65
77
self._transaction = None
66
78
self._lock_mode = None
68
79
self._transport = transport
71
class GitDir(bzrdir.BzrDir):
80
if LockWarner is None:
84
self._lock_warner = LockWarner(repr(self))
87
class GitDirConfig(object):
89
def get_default_stack_on(self):
92
def set_default_stack_on(self, value):
93
raise bzr_errors.BzrError("Cannot set configuration")
96
class GitControlDirFormat(ControlDirFormat):
98
_lock_class = lockable_files.TransportLock
100
colocated_branches = True
101
fixed_components = True
103
def __eq__(self, other):
104
return type(self) == type(other)
106
def is_supported(self):
109
def network_name(self):
113
class GitDir(ControlDir):
72
114
"""An adapter to the '.git' dir used by git."""
74
116
def is_supported(self):
119
def can_convert_format(self):
122
def break_lock(self):
77
125
def cloning_metadir(self, stacked=False):
126
return format_registry.make_bzrdir("default")
128
def checkout_metadir(self, stacked=False):
129
return format_registry.make_bzrdir("default")
131
def _get_selected_ref(self, branch):
132
if branch is None and getattr(self, "_get_selected_branch", False):
133
branch = self._get_selected_branch()
134
if branch is not None:
135
from bzrlib.plugins.git.refs import branch_name_to_ref
136
return branch_name_to_ref(branch, None)
137
segment_parameters = getattr(
138
self.user_transport, "get_segment_parameters", lambda: {})()
139
ref = segment_parameters.get("ref")
141
ref = urlutils.unescape(ref)
144
def get_config(self):
145
return GitDirConfig()
147
def _available_backup_name(self, base):
148
return osutils.available_backup_name(base, self.root_transport.has)
150
def sprout(self, url, revision_id=None, force_new_repo=False,
151
recurse='down', possible_transports=None,
152
accelerator_tree=None, hardlink=False, stacked=False,
153
source_branch=None, create_tree_if_local=True):
154
from bzrlib.repository import InterRepository
155
from bzrlib.transport.local import LocalTransport
156
from bzrlib.transport import get_transport
157
target_transport = get_transport(url, possible_transports)
158
target_transport.ensure_base()
159
cloning_format = self.cloning_metadir()
160
# Create/update the result branch
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 bzr_errors.NoRepositoryPresent:
167
result_repo = result.create_repository()
168
target_is_empty = True
170
target_is_empty = None # Unknown
79
return bzrlib.bzrdir.format_registry.make_bzrdir("pack-0.92")
81
return bzrlib.bzrdir.format_registry.make_bzrdir("1.6")
172
raise bzr_errors.IncompatibleRepositories(source_repository, result_repo)
173
interrepo = InterRepository.get(source_repository, result_repo)
175
if revision_id is not None:
176
determine_wants = interrepo.get_determine_wants_revids(
177
[revision_id], include_tags=True)
179
determine_wants = interrepo.determine_wants_all
180
interrepo.fetch_objects(determine_wants=determine_wants,
181
mapping=source_branch.mapping)
182
result_branch = source_branch.sprout(result,
183
revision_id=revision_id, repository=result_repo)
184
if (create_tree_if_local and isinstance(target_transport, LocalTransport)
185
and (result_repo is None or result_repo.make_working_trees())):
186
wt = result.create_workingtree(accelerator_tree=accelerator_tree,
187
hardlink=hardlink, from_branch=result_branch)
190
if wt.path2id('') is None:
192
wt.set_root_id(self.open_workingtree.get_root_id())
193
except bzr_errors.NoWorkingTree:
199
def clone_on_transport(self, transport, revision_id=None,
200
force_new_repo=False, preserve_stacking=False, stacked_on=None,
201
create_prefix=False, use_existing_dir=True, no_tree=False):
202
"""See ControlDir.clone_on_transport."""
203
from bzrlib.repository import InterRepository
204
from bzrlib.plugins.git.mapping import default_mapping
206
format = BareLocalGitControlDirFormat()
208
format = LocalGitControlDirFormat()
209
(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)
210
target_git_repo = target_repo._git
211
source_repo = self.open_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
lockfiles = GitLockableFiles(transport, GitLock())
223
return self.__class__(transport, lockfiles, target_git_repo, format)
225
def find_repository(self):
226
"""Find the repository that should be used.
228
This does not require a branch as we use it to find the repo for
229
new branches as well as to hook existing branches up to their
232
return self.open_repository()
235
class LocalGitControlDirFormat(GitControlDirFormat):
236
"""The .git directory control format."""
241
def _known_formats(self):
242
return set([LocalGitControlDirFormat()])
245
def repository_format(self):
246
from bzrlib.plugins.git.repository import GitRepositoryFormat
247
return GitRepositoryFormat()
249
def get_branch_format(self):
250
from bzrlib.plugins.git.branch import GitBranchFormat
251
return GitBranchFormat()
253
def open(self, transport, _found=None):
254
"""Open this directory.
257
from bzrlib.plugins.git.transportgit import TransportRepo
258
gitrepo = TransportRepo(transport)
259
lockfiles = GitLockableFiles(transport, GitLock())
260
return LocalGitDir(transport, lockfiles, gitrepo, self)
262
def get_format_description(self):
263
return "Local Git Repository"
265
def initialize_on_transport(self, transport):
266
from bzrlib.plugins.git.transportgit import TransportRepo
267
TransportRepo.init(transport, bare=self.bare)
268
return self.open(transport)
270
def initialize_on_transport_ex(self, transport, use_existing_dir=False,
271
create_prefix=False, force_new_repo=False, stacked_on=None,
272
stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
273
shared_repo=False, vfs_only=False):
274
def make_directory(transport):
277
def redirected(transport, e, redirection_notice):
278
trace.note(redirection_notice)
279
return transport._redirected_to(e.source, e.target)
281
transport = do_catching_redirections(make_directory, transport,
283
except bzr_errors.FileExists:
284
if not use_existing_dir:
286
except bzr_errors.NoSuchFile:
287
if not create_prefix:
289
transport.create_prefix()
290
controldir = self.initialize_on_transport(transport)
291
repository = controldir.open_repository()
292
repository.lock_write()
293
return (repository, controldir, False, CreateRepository(controldir))
295
def is_supported(self):
299
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
302
supports_workingtrees = False
304
def get_format_description(self):
305
return "Local Git Repository (bare)"
84
308
class LocalGitDir(GitDir):
85
309
"""An adapter to the '.git' dir used by git."""
87
_gitrepository_class = repository.LocalGitRepository
311
def _get_gitrepository_class(self):
312
from bzrlib.plugins.git.repository import LocalGitRepository
313
return LocalGitRepository
316
return "<%s at %r>" % (
317
self.__class__.__name__, self.root_transport.base)
319
_gitrepository_class = property(_get_gitrepository_class)
322
def user_transport(self):
323
return self.root_transport
326
def control_transport(self):
327
return self.transport
89
329
def __init__(self, transport, lockfiles, gitrepo, format):
90
330
self._format = format
91
331
self.root_transport = transport
332
self._mode_check_done = False
92
333
self._git = gitrepo
94
335
self.transport = transport
96
337
self.transport = transport.clone('.git')
97
338
self._lockfiles = lockfiles
99
def get_branch_transport(self, branch_format):
339
self._mode_check_done = None
341
def is_control_filename(self, filename):
342
return (filename == '.git' or filename.startswith('.git/'))
344
def _get_symref(self, ref):
345
from dulwich.repo import SYMREF
346
refcontents = self._git.refs.read_ref(ref)
347
if refcontents is None: # no such ref
349
if refcontents.startswith(SYMREF):
350
return refcontents[len(SYMREF):].rstrip("\n")
353
def set_branch_reference(self, name, target):
354
ref = self._get_selected_ref(name)
357
if not getattr(target, "ref", None):
358
raise bzr_errors.BzrError("Can only set symrefs to Git refs")
359
self._git.refs.set_symbolic_ref(ref, target.ref)
361
def get_branch_reference(self, name=None):
362
ref = self._get_selected_ref(name)
365
target_ref = self._get_symref(ref)
366
if target_ref is not None:
367
return ",ref=%s" % urllib.quote(target_ref)
370
def find_branch_format(self, name=None):
371
from bzrlib.plugins.git.branch import (
373
GitSymrefBranchFormat,
375
ref = self._get_selected_ref(name)
378
if self._get_symref(ref) is not None:
379
return GitSymrefBranchFormat()
381
return GitBranchFormat()
383
def get_branch_transport(self, branch_format, name=None):
100
384
if branch_format is None:
101
385
return self.transport
102
if isinstance(branch_format, LocalGitBzrDirFormat):
103
return self.transport
104
raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
106
get_repository_transport = get_branch_transport
107
get_workingtree_transport = get_branch_transport
109
def open_branch(self, ignored=None):
386
if isinstance(branch_format, LocalGitControlDirFormat):
387
return self.transport
388
raise bzr_errors.IncompatibleFormat(branch_format, self._format)
390
def get_repository_transport(self, format):
392
return self.transport
393
if isinstance(format, LocalGitControlDirFormat):
394
return self.transport
395
raise bzr_errors.IncompatibleFormat(format, self._format)
397
def get_workingtree_transport(self, format):
399
return self.transport
400
if isinstance(format, LocalGitControlDirFormat):
401
return self.transport
402
raise bzr_errors.IncompatibleFormat(format, self._format)
404
def open_branch(self, name=None, unsupported=False, ignore_fallbacks=None):
110
405
"""'create' a branch for this dir."""
111
406
repo = self.open_repository()
112
return branch.LocalGitBranch(self, repo, "HEAD", repo._git.head(), self._lockfiles)
114
def open_repository(self, shared=False):
407
from bzrlib.plugins.git.branch import LocalGitBranch
408
ref = self._get_selected_ref(name)
411
ref, sha = self._git.refs._follow(ref)
412
if not ref in self._git.refs:
413
raise bzr_errors.NotBranchError(self.root_transport.base,
415
return LocalGitBranch(self, repo, ref, self._lockfiles)
417
def destroy_branch(self, name=None):
418
refname = self._get_selected_ref(name)
419
if not refname in self._git.refs:
420
raise bzr_errors.NotBranchError(self.root_transport.base,
422
del self._git.refs[refname]
424
def destroy_repository(self):
425
raise bzr_errors.UnsupportedOperation(self.destroy_repository, self)
427
def destroy_workingtree(self):
428
raise bzr_errors.UnsupportedOperation(self.destroy_workingtree, self)
430
def needs_format_conversion(self, format=None):
431
return not isinstance(self._format, format.__class__)
433
def list_branches(self):
435
for name in self._git.refs.keys():
436
if name.startswith("refs/heads/"):
437
ret.append(self.open_branch(name=name))
440
def open_repository(self):
115
441
"""'open' a repository for this dir."""
116
442
return self._gitrepository_class(self, self._lockfiles)
118
444
def open_workingtree(self, recommend_upgrade=True):
120
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
121
raise errors.bzr_errors.NoWorkingTree(loc)
123
return workingtree.GitWorkingTree(self, self.open_repository(),
445
if not self._git.bare:
446
from dulwich.errors import NoIndexPresent
447
repo = self.open_repository()
449
index = repo._git.open_index()
450
except NoIndexPresent:
453
from bzrlib.plugins.git.workingtree import GitWorkingTree
455
branch = self.open_branch()
456
except bzr_errors.NotBranchError:
459
return GitWorkingTree(self, repo, branch, index)
460
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
461
raise bzr_errors.NoWorkingTree(loc)
126
463
def create_repository(self, shared=False):
464
from bzrlib.plugins.git.repository import GitRepositoryFormat
466
raise bzr_errors.IncompatibleFormat(GitRepositoryFormat(), self._format)
127
467
return self.open_repository()
130
class GitBzrDirFormat(bzrdir.BzrDirFormat):
131
_lock_class = TransportLock
133
def is_supported(self):
137
class LocalGitBzrDirFormat(GitBzrDirFormat):
138
"""The .git directory control format."""
140
_gitdir_class = LocalGitDir
143
def _known_formats(self):
144
return set([LocalGitBzrDirFormat()])
146
def open(self, transport, _found=None):
147
"""Open this directory.
150
from bzrlib.plugins.git import git
151
# we dont grok readonly - git isn't integrated with transport.
153
if url.startswith('readonly+'):
154
url = url[len('readonly+'):]
157
gitrepo = git.repo.Repo(transport.local_abspath("."))
158
except errors.bzr_errors.NotLocalUrl:
159
raise errors.bzr_errors.NotBranchError(path=transport.base)
160
lockfiles = GitLockableFiles(transport, GitLock())
161
return self._gitdir_class(transport, lockfiles, gitrepo, self)
164
def probe_transport(klass, transport):
165
"""Our format is present if the transport ends in '.not/'."""
166
from bzrlib.plugins.git import git
167
# little ugly, but works
169
# delegate to the main opening code. This pays a double rtt cost at the
170
# moment, so perhaps we want probe_transport to return the opened thing
171
# rather than an openener ? or we could return a curried thing with the
172
# dir to open already instantiated ? Needs more thought.
174
format.open(transport)
176
except git.errors.NotGitRepository, e:
177
raise errors.bzr_errors.NotBranchError(path=transport.base)
178
raise errors.bzr_errors.NotBranchError(path=transport.base)
180
def get_format_description(self):
181
return "Local Git Repository"
183
def get_format_string(self):
184
return "Local Git Repository"
186
def initialize_on_transport(self, transport):
187
from bzrlib.transport.local import LocalTransport
188
from bzrlib.plugins.git import git
190
if not isinstance(transport, LocalTransport):
191
raise NotImplementedError(self.initialize,
192
"Can't create Git Repositories/branches on "
193
"non-local transports")
195
git.repo.Repo.create(transport.local_abspath("."))
196
return self.open(transport)
198
def is_supported(self):
202
class RemoteGitBzrDirFormat(GitBzrDirFormat):
203
"""The .git directory control format."""
206
def _known_formats(self):
207
return set([RemoteGitBzrDirFormat()])
209
def open(self, transport, _found=None):
210
"""Open this directory.
213
from bzrlib.plugins.git.remote import RemoteGitDir, GitSmartTransport
214
if not isinstance(transport, GitSmartTransport):
215
raise errors.bzr_errors.NotBranchError(transport.base)
216
# we dont grok readonly - git isn't integrated with transport.
218
if url.startswith('readonly+'):
219
url = url[len('readonly+'):]
221
lockfiles = GitLockableFiles(transport, GitLock())
222
return RemoteGitDir(transport, lockfiles, self)
225
def probe_transport(klass, transport):
226
"""Our format is present if the transport ends in '.not/'."""
227
# little ugly, but works
229
from bzrlib.plugins.git.remote import GitSmartTransport
230
if not isinstance(transport, GitSmartTransport):
231
raise errors.bzr_errors.NotBranchError(transport.base)
232
# The only way to know a path exists and contains a valid repository
233
# is to do a request against it:
235
transport.fetch_pack(lambda x: [], None, lambda x: None,
236
lambda x: mutter("git: %s" % x))
237
except GitProtocolException, e:
238
raise errors.bzr_errors.NotBranchError(path=transport.base)
241
raise errors.bzr_errors.NotBranchError(path=transport.base)
243
def get_format_description(self):
244
return "Remote Git Repository"
246
def get_format_string(self):
247
return "Remote Git Repository"
249
def initialize_on_transport(self, transport):
250
raise errors.bzr_errors.UninitializableFormat(self)
469
def create_branch(self, name=None, repository=None):
470
refname = self._get_selected_ref(name)
471
from dulwich.protocol import ZERO_SHA
472
# FIXME: This is a bit awkward. Perhaps we should have a
473
# a separate method for changing the default branch?
475
refname = "refs/heads/master"
476
self._git.refs.set_symbolic_ref("HEAD", refname)
477
self._git.refs[refname] = ZERO_SHA
478
return self.open_branch(name)
480
def backup_bzrdir(self):
482
self.root_transport.copy_tree(".git", ".git.backup")
483
return (self.root_transport.abspath(".git"),
484
self.root_transport.abspath(".git.backup"))
486
raise bzr_errors.BzrError("Unable to backup bare repositories")
488
def create_workingtree(self, revision_id=None, from_branch=None,
489
accelerator_tree=None, hardlink=False):
491
raise bzr_errors.UnsupportedOperation(self.create_workingtree, self)
492
from dulwich.index import write_index
493
from dulwich.pack import SHA1Writer
494
f = open(self.transport.local_abspath("index"), 'w+')
500
return self.open_workingtree()
502
def _find_or_create_repository(self, force_new_repo=None):
503
return self.create_repository(shared=False)
505
def _find_creation_modes(self):
506
"""Determine the appropriate modes for files and directories.
508
They're always set to be consistent with the base directory,
509
assuming that this transport allows setting modes.
511
# TODO: Do we need or want an option (maybe a config setting) to turn
512
# this off or override it for particular locations? -- mbp 20080512
513
if self._mode_check_done:
515
self._mode_check_done = True
517
st = self.transport.stat('.')
518
except bzr_errors.TransportNotPossible:
519
self._dir_mode = None
520
self._file_mode = None
522
# Check the directory mode, but also make sure the created
523
# directories and files are read-write for this user. This is
524
# mostly a workaround for filesystems which lie about being able to
525
# write to a directory (cygwin & win32)
526
if (st.st_mode & 07777 == 00000):
527
# FTP allows stat but does not return dir/file modes
528
self._dir_mode = None
529
self._file_mode = None
531
self._dir_mode = (st.st_mode & 07777) | 00700
532
# Remove the sticky and execute bits for files
533
self._file_mode = self._dir_mode & ~07111
535
def _get_file_mode(self):
536
"""Return Unix mode for newly created files, or None.
538
if not self._mode_check_done:
539
self._find_creation_modes()
540
return self._file_mode
542
def _get_dir_mode(self):
543
"""Return Unix mode for newly created directories, or None.
545
if not self._mode_check_done:
546
self._find_creation_modes()
547
return self._dir_mode