1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# 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."""
25
LockWarner = getattr(lockable_files, "_LockWarner", None)
27
from bzrlib.plugins.git import (
37
class GitLock(object):
38
"""A lock that thunks through to Git."""
40
def lock_write(self, token=None):
52
def validate_token(self, token):
56
class GitLockableFiles(lockable_files.LockableFiles):
57
"""Git specific lockable files abstraction."""
59
def __init__(self, transport, lock):
61
self._transaction = None
62
self._lock_mode = None
63
self._transport = transport
64
if LockWarner is None:
68
self._lock_warner = LockWarner(repr(self))
71
class GitDir(bzrdir.BzrDir):
72
"""An adapter to the '.git' dir used by git."""
74
def is_supported(self):
77
def cloning_metadir(self, stacked=False):
78
return get_rich_root_format(stacked)
81
class LocalGitDir(GitDir):
82
"""An adapter to the '.git' dir used by git."""
84
_gitrepository_class = repository.LocalGitRepository
86
def __init__(self, transport, lockfiles, gitrepo, format):
88
self.root_transport = transport
91
self.transport = transport
93
self.transport = transport.clone('.git')
94
self._lockfiles = lockfiles
95
self._mode_check_done = None
97
def is_control_filename(self, filename):
98
return filename == '.git' or filename.startswith('.git/')
100
def get_branch_transport(self, branch_format):
101
if branch_format is None:
102
return self.transport
103
if isinstance(branch_format, LocalGitBzrDirFormat):
104
return self.transport
105
raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
107
get_repository_transport = get_branch_transport
108
get_workingtree_transport = get_branch_transport
110
def open_branch(self, ignore_fallbacks=None):
111
"""'create' a branch for this dir."""
112
repo = self.open_repository()
113
return branch.LocalGitBranch(self, repo, "HEAD", self._lockfiles)
115
def open_repository(self, shared=False):
116
"""'open' a repository for this dir."""
117
return self._gitrepository_class(self, self._lockfiles)
119
def open_workingtree(self, recommend_upgrade=True):
120
if not self._git.bare and self._git.has_index():
121
return workingtree.GitWorkingTree(self, self.open_repository(),
123
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
124
raise errors.bzr_errors.NoWorkingTree(loc)
126
def create_repository(self, shared=False):
127
return self.open_repository()
129
def create_branch(self):
130
return self.open_branch()
132
def backup_bzrdir(self):
134
self.root_transport.copy_tree(".git", ".git.backup")
135
return (self.root_transport.abspath(".git"),
136
self.root_transport.abspath(".git.backup"))
138
raise errors.bzr_errors.BzrError("Unable to backup bare repositories")
140
def create_workingtree(self, revision_id=None, from_branch=None,
141
accelerator_tree=None, hardlink=False):
143
raise errors.bzr_errors.BzrError("Can't create working tree in a bare repo")
144
from dulwich.index import write_index
145
write_index(self.root_transport.abspath(".git/index"), [])
146
return self.open_workingtree()