80
78
def cloning_metadir(self, stacked=False):
81
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)
84
86
class LocalGitDir(GitDir):
85
87
"""An adapter to the '.git' dir used by git."""
87
_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)
89
95
def __init__(self, transport, lockfiles, gitrepo, format):
90
96
self._format = format
105
111
return self.transport
106
112
if isinstance(branch_format, LocalGitBzrDirFormat):
107
113
return self.transport
108
raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
114
raise bzr_errors.IncompatibleFormat(branch_format, self._format)
110
116
get_repository_transport = get_branch_transport
111
117
get_workingtree_transport = get_branch_transport
113
def open_branch(self, ignore_fallbacks=None):
119
def _open_branch(self, name=None, ignore_fallbacks=None, unsupported=False):
114
120
"""'create' a branch for this dir."""
115
121
repo = self.open_repository()
116
return branch.LocalGitBranch(self, repo, "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))
118
143
def open_repository(self, shared=False):
119
144
"""'open' a repository for this dir."""
120
145
return self._gitrepository_class(self, self._lockfiles)
122
147
def open_workingtree(self, recommend_upgrade=True):
123
if not self._git.bare and self._git.has_index():
124
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:
126
156
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
127
raise errors.bzr_errors.NoWorkingTree(loc)
157
raise bzr_errors.NoWorkingTree(loc)
129
159
def create_repository(self, shared=False):
130
160
return self.open_repository()
132
def create_branch(self):
133
return self.open_branch()
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)
135
167
def backup_bzrdir(self):
136
168
if self._git.bare:
138
170
return (self.root_transport.abspath(".git"),
139
171
self.root_transport.abspath(".git.backup"))
141
raise errors.bzr_errors.BzrError("Unable to backup bare repositories")
173
raise bzr_errors.BzrError("Unable to backup bare repositories")
143
175
def create_workingtree(self, revision_id=None, from_branch=None,
144
176
accelerator_tree=None, hardlink=False):
145
177
if self._git.bare:
146
raise errors.bzr_errors.BzrError("Can't create working tree in a bare repo")
178
raise bzr_errors.BzrError("Can't create working tree in a bare repo")
147
179
from dulwich.index import write_index
148
write_index(self.root_transport.abspath(".git/index"), [])
180
from dulwich.pack import SHA1Writer
181
f = open(self.transport.local_abspath("index"), 'w+')
149
187
return self.open_workingtree()