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 (
29
lazy_import(globals(), """
30
from bzrlib.lockable_files import TransportLock
26
LockWarner = getattr(lockable_files, "_LockWarner", None)
31
28
from bzrlib.plugins.git import (
39
from bzrlib.plugins.git import LocalGitBzrDirFormat
43
34
class GitLock(object):
79
77
def cloning_metadir(self, stacked=False):
80
return bzrlib.bzrdir.format_registry.make_bzrdir("1.9-rich-root")
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
83
89
class LocalGitDir(GitDir):
84
90
"""An adapter to the '.git' dir used by git."""
86
_gitrepository_class = repository.LocalGitRepository
92
def _get_gitrepository_class(self):
93
from bzrlib.plugins.git.repository import LocalGitRepository
94
return LocalGitRepository
96
_gitrepository_class = property(_get_gitrepository_class)
88
98
def __init__(self, transport, lockfiles, gitrepo, format):
89
99
self._format = format
95
105
self.transport = transport.clone('.git')
96
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/')
98
112
def get_branch_transport(self, branch_format):
99
113
if branch_format is None:
100
114
return self.transport
101
115
if isinstance(branch_format, LocalGitBzrDirFormat):
102
116
return self.transport
103
raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
117
raise bzr_errors.IncompatibleFormat(branch_format, self._format)
105
119
get_repository_transport = get_branch_transport
106
120
get_workingtree_transport = get_branch_transport
108
def open_branch(self, ignored=None):
123
def open_branch(self, ignore_fallbacks=None, name=None, unsupported=False):
109
124
"""'create' a branch for this dir."""
110
125
repo = self.open_repository()
111
return branch.LocalGitBranch(self, repo, "HEAD", repo._git.head(), self._lockfiles)
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))
113
139
def open_repository(self, shared=False):
114
140
"""'open' a repository for this dir."""
115
141
return self._gitrepository_class(self, self._lockfiles)
117
143
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(),
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:
122
152
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
123
raise errors.bzr_errors.NoWorkingTree(loc)
153
raise bzr_errors.NoWorkingTree(loc)
125
155
def create_repository(self, shared=False):
126
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()