14
14
# along with this program; if not, write to the Free Software
15
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"""
22
from bzrlib.lazy_import import lazy_import
17
"""An adapter between a Git control dir and a Bazaar BzrDir."""
23
19
from bzrlib import (
24
version_info as bzrlib_version,
29
lazy_import(globals(), """
30
from bzrlib.lockable_files import TransportLock
27
LockWarner = getattr(lockable_files, "_LockWarner", None)
31
29
from bzrlib.plugins.git import (
39
from bzrlib.plugins.git import LocalGitBzrDirFormat
43
35
class GitLock(object):
79
78
def cloning_metadir(self, stacked=False):
80
return bzrlib.bzrdir.format_registry.make_bzrdir("1.9-rich-root")
79
return get_rich_root_format(stacked)
81
def _branch_name_to_ref(self, name):
82
from bzrlib.plugins.git.branch import branch_name_to_ref
83
return branch_name_to_ref(name)
83
86
class LocalGitDir(GitDir):
84
87
"""An adapter to the '.git' dir used by git."""
86
_gitrepository_class = repository.LocalGitRepository
89
def _get_gitrepository_class(self):
90
from bzrlib.plugins.git.repository import LocalGitRepository
91
return LocalGitRepository
93
_gitrepository_class = property(_get_gitrepository_class)
88
95
def __init__(self, transport, lockfiles, gitrepo, format):
89
96
self._format = format
95
102
self.transport = transport.clone('.git')
96
103
self._lockfiles = lockfiles
104
self._mode_check_done = None
106
def is_control_filename(self, filename):
107
return filename == '.git' or filename.startswith('.git/')
98
109
def get_branch_transport(self, branch_format):
99
110
if branch_format is None:
100
111
return self.transport
101
112
if isinstance(branch_format, LocalGitBzrDirFormat):
102
113
return self.transport
103
raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
114
raise bzr_errors.IncompatibleFormat(branch_format, self._format)
105
116
get_repository_transport = get_branch_transport
106
117
get_workingtree_transport = get_branch_transport
108
def open_branch(self, ignored=None):
119
def _open_branch(self, name=None, ignore_fallbacks=None, unsupported=False):
109
120
"""'create' a branch for this dir."""
110
121
repo = self.open_repository()
111
return branch.LocalGitBranch(self, repo, "HEAD", repo._git.head(), self._lockfiles)
122
from bzrlib.plugins.git.branch import LocalGitBranch
123
return LocalGitBranch(self, repo, self._branch_name_to_ref(name),
126
if bzrlib_version >= (2, 2):
127
open_branch = _open_branch
129
def open_branch(self, ignore_fallbacks=None, unsupported=False):
130
return self._open_branch(name=None,
131
ignore_fallbacks=ignore_fallbacks, unsupported=unsupported)
133
def destroy_branch(self, name=None):
134
del self._git.refs[self._branch_name_to_ref(name)]
136
def list_branches(self):
138
for name in self._git.get_refs():
139
if name.startswith("refs/heads/") or name == "HEAD":
140
ret.append(self.open_branch(name=name))
113
143
def open_repository(self, shared=False):
114
144
"""'open' a repository for this dir."""
115
145
return self._gitrepository_class(self, self._lockfiles)
117
147
def open_workingtree(self, recommend_upgrade=True):
118
if (not self._git.bare and
119
os.path.exists(os.path.join(self._git.controldir(), "index"))):
120
return workingtree.GitWorkingTree(self, self.open_repository(),
148
if not self._git.bare:
149
from dulwich.errors import NoIndexPresent
151
from bzrlib.plugins.git.workingtree import GitWorkingTree
152
return GitWorkingTree(self, self.open_repository(),
154
except NoIndexPresent:
122
156
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
123
raise errors.bzr_errors.NoWorkingTree(loc)
157
raise bzr_errors.NoWorkingTree(loc)
125
159
def create_repository(self, shared=False):
126
160
return self.open_repository()
162
def create_branch(self, name=None):
163
refname = self._branch_name_to_ref(name)
164
self._git.refs[refname] = "0" * 40
165
return self.open_branch(name)
167
def backup_bzrdir(self):
169
self.root_transport.copy_tree(".git", ".git.backup")
170
return (self.root_transport.abspath(".git"),
171
self.root_transport.abspath(".git.backup"))
173
raise bzr_errors.BzrError("Unable to backup bare repositories")
175
def create_workingtree(self, revision_id=None, from_branch=None,
176
accelerator_tree=None, hardlink=False):
178
raise bzr_errors.BzrError("Can't create working tree in a bare repo")
179
from dulwich.index import write_index
180
from dulwich.pack import SHA1Writer
181
f = open(self.transport.local_abspath("index"), 'w+')
187
return self.open_workingtree()