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."""
26
LockWarner = getattr(lockable_files, "_LockWarner", None)
28
from bzrlib.plugins.git import (
34
class GitLock(object):
35
"""A lock that thunks through to Git."""
37
def lock_write(self, token=None):
49
def validate_token(self, token):
53
class GitLockableFiles(lockable_files.LockableFiles):
54
"""Git specific lockable files abstraction."""
56
def __init__(self, transport, lock):
58
self._transaction = None
59
self._lock_mode = None
60
self._transport = transport
61
if LockWarner is None:
65
self._lock_warner = LockWarner(repr(self))
68
class GitDir(bzrdir.BzrDir):
69
"""An adapter to the '.git' dir used by git."""
71
def is_supported(self):
74
def cloning_metadir(self, stacked=False):
75
return get_rich_root_format(stacked)
78
class LocalGitDir(GitDir):
79
"""An adapter to the '.git' dir used by git."""
81
def _get_gitrepository_class(self):
82
from bzrlib.plugins.git.repository import LocalGitRepository
83
return LocalGitRepository
85
_gitrepository_class = property(_get_gitrepository_class)
87
def __init__(self, transport, lockfiles, gitrepo, format):
89
self.root_transport = transport
92
self.transport = transport
94
self.transport = transport.clone('.git')
95
self._lockfiles = lockfiles
96
self._mode_check_done = None
98
def is_control_filename(self, filename):
99
return filename == '.git' or filename.startswith('.git/')
101
def get_branch_transport(self, branch_format):
102
if branch_format is None:
103
return self.transport
104
if isinstance(branch_format, LocalGitBzrDirFormat):
105
return self.transport
106
raise bzr_errors.IncompatibleFormat(branch_format, self._format)
108
get_repository_transport = get_branch_transport
109
get_workingtree_transport = get_branch_transport
111
def open_branch(self, ignore_fallbacks=None):
112
"""'create' a branch for this dir."""
113
repo = self.open_repository()
114
from bzrlib.plugins.git.branch import LocalGitBranch
115
return LocalGitBranch(self, repo, "HEAD", self._lockfiles)
117
def open_repository(self, shared=False):
118
"""'open' a repository for this dir."""
119
return self._gitrepository_class(self, self._lockfiles)
121
def open_workingtree(self, recommend_upgrade=True):
122
if not self._git.bare and self._git.has_index():
123
from bzrlib.plugins.git.workingtree import GitWorkingTree
124
return GitWorkingTree(self, self.open_repository(),
126
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
127
raise bzr_errors.NoWorkingTree(loc)
129
def create_repository(self, shared=False):
130
return self.open_repository()
132
def create_branch(self):
133
return self.open_branch()
135
def backup_bzrdir(self):
137
self.root_transport.copy_tree(".git", ".git.backup")
138
return (self.root_transport.abspath(".git"),
139
self.root_transport.abspath(".git.backup"))
141
raise bzr_errors.BzrError("Unable to backup bare repositories")
143
def create_workingtree(self, revision_id=None, from_branch=None,
144
accelerator_tree=None, hardlink=False):
146
raise bzr_errors.BzrError("Can't create working tree in a bare repo")
147
from dulwich.index import write_index
148
from dulwich.pack import SHA1Writer
149
f = open(self.transport.local_abspath("index"), 'w+')
155
return self.open_workingtree()