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)
80
def _branch_name_to_ref(self, name):
81
if name is None and name != "HEAD":
84
return "refs/heads/%s" % name
89
class LocalGitDir(GitDir):
90
"""An adapter to the '.git' dir used by git."""
92
def _get_gitrepository_class(self):
93
from bzrlib.plugins.git.repository import LocalGitRepository
94
return LocalGitRepository
96
_gitrepository_class = property(_get_gitrepository_class)
98
def __init__(self, transport, lockfiles, gitrepo, format):
100
self.root_transport = transport
103
self.transport = transport
105
self.transport = transport.clone('.git')
106
self._lockfiles = lockfiles
107
self._mode_check_done = None
109
def is_control_filename(self, filename):
110
return filename == '.git' or filename.startswith('.git/')
112
def get_branch_transport(self, branch_format):
113
if branch_format is None:
114
return self.transport
115
if isinstance(branch_format, LocalGitBzrDirFormat):
116
return self.transport
117
raise bzr_errors.IncompatibleFormat(branch_format, self._format)
119
get_repository_transport = get_branch_transport
120
get_workingtree_transport = get_branch_transport
123
def open_branch(self, ignore_fallbacks=None, name=None, unsupported=False):
124
"""'create' a branch for this dir."""
125
repo = self.open_repository()
126
from bzrlib.plugins.git.branch import LocalGitBranch
127
return LocalGitBranch(self, repo, self._branch_name_to_ref(name),
130
def destroy_branch(self, name=None):
131
del self._git.refs[self._branch_name_to_ref(name)]
133
def list_branches(self):
135
for name in self._git.get_refs():
136
ret.append(self.open_branch(name=name))
139
def open_repository(self, shared=False):
140
"""'open' a repository for this dir."""
141
return self._gitrepository_class(self, self._lockfiles)
143
def open_workingtree(self, recommend_upgrade=True):
144
if not self._git.bare:
145
from dulwich.errors import NoIndexPresent
147
from bzrlib.plugins.git.workingtree import GitWorkingTree
148
return GitWorkingTree(self, self.open_repository(),
150
except NoIndexPresent:
152
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
153
raise bzr_errors.NoWorkingTree(loc)
155
def create_repository(self, shared=False):
156
return self.open_repository()
158
def create_branch(self, name=None):
159
refname = self._branch_name_to_ref(name)
160
self._git.refs[refname] = "0" * 40
161
return self.open_branch(name)
163
def backup_bzrdir(self):
165
self.root_transport.copy_tree(".git", ".git.backup")
166
return (self.root_transport.abspath(".git"),
167
self.root_transport.abspath(".git.backup"))
169
raise bzr_errors.BzrError("Unable to backup bare repositories")
171
def create_workingtree(self, revision_id=None, from_branch=None,
172
accelerator_tree=None, hardlink=False):
174
raise bzr_errors.BzrError("Can't create working tree in a bare repo")
175
from dulwich.index import write_index
176
from dulwich.pack import SHA1Writer
177
f = open(self.transport.local_abspath("index"), 'w+')
183
return self.open_workingtree()