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):
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
def _get_gitrepository_class(self):
85
from bzrlib.plugins.git.repository import LocalGitRepository
86
return LocalGitRepository
88
_gitrepository_class = property(_get_gitrepository_class)
90
def __init__(self, transport, lockfiles, gitrepo, format):
92
self.root_transport = transport
95
self.transport = transport
97
self.transport = transport.clone('.git')
98
self._lockfiles = lockfiles
99
self._mode_check_done = None
101
def is_control_filename(self, filename):
102
return filename == '.git' or filename.startswith('.git/')
104
def get_branch_transport(self, branch_format):
105
if branch_format is None:
106
return self.transport
107
if isinstance(branch_format, LocalGitBzrDirFormat):
108
return self.transport
109
raise bzr_errors.IncompatibleFormat(branch_format, self._format)
111
get_repository_transport = get_branch_transport
112
get_workingtree_transport = get_branch_transport
114
def open_branch(self, ignore_fallbacks=None):
115
"""'create' a branch for this dir."""
116
repo = self.open_repository()
117
from bzrlib.plugins.git.branch import LocalGitBranch
118
return LocalGitBranch(self, repo, "HEAD", self._lockfiles)
120
def open_repository(self, shared=False):
121
"""'open' a repository for this dir."""
122
return self._gitrepository_class(self, self._lockfiles)
124
def open_workingtree(self, recommend_upgrade=True):
125
if not self._git.bare and self._git.has_index():
126
from bzrlib.plugins.git.workingtree import GitWorkingTree
127
return GitWorkingTree(self, self.open_repository(),
129
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
130
raise bzr_errors.NoWorkingTree(loc)
132
def create_repository(self, shared=False):
133
return self.open_repository()
135
def create_branch(self):
136
return self.open_branch()
138
def backup_bzrdir(self):
140
self.root_transport.copy_tree(".git", ".git.backup")
141
return (self.root_transport.abspath(".git"),
142
self.root_transport.abspath(".git.backup"))
144
raise bzr_errors.BzrError("Unable to backup bare repositories")
146
def create_workingtree(self, revision_id=None, from_branch=None,
147
accelerator_tree=None, hardlink=False):
149
raise bzr_errors.BzrError("Can't create working tree in a bare repo")
150
from dulwich.index import write_index
151
from dulwich.pack import SHA1Writer
152
f = open(self.transport.local_abspath("index"), 'w+')
158
return self.open_workingtree()