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)
83
81
class LocalGitDir(GitDir):
84
82
"""An adapter to the '.git' dir used by git."""
86
_gitrepository_class = repository.LocalGitRepository
84
def _get_gitrepository_class(self):
85
from bzrlib.plugins.git.repository import LocalGitRepository
86
return LocalGitRepository
88
_gitrepository_class = property(_get_gitrepository_class)
88
90
def __init__(self, transport, lockfiles, gitrepo, format):
89
91
self._format = format
95
97
self.transport = transport.clone('.git')
96
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/')
98
104
def get_branch_transport(self, branch_format):
99
105
if branch_format is None:
100
106
return self.transport
101
107
if isinstance(branch_format, LocalGitBzrDirFormat):
102
108
return self.transport
103
raise errors.bzr_errors.IncompatibleFormat(branch_format, self._format)
109
raise bzr_errors.IncompatibleFormat(branch_format, self._format)
105
111
get_repository_transport = get_branch_transport
106
112
get_workingtree_transport = get_branch_transport
108
def open_branch(self, ignored=None):
114
def _branch_name_to_ref(self, name):
118
def open_branch(self, ignore_fallbacks=None, name=None):
109
119
"""'create' a branch for this dir."""
110
120
repo = self.open_repository()
111
return branch.LocalGitBranch(self, repo, "HEAD", repo._git.head(), self._lockfiles)
121
from bzrlib.plugins.git.branch import LocalGitBranch
122
return LocalGitBranch(self, repo, self._branch_name_to_ref(name),
125
def destroy_branch(self, name=None):
126
del self._git.refs[self._branch_name_to_ref(name)]
128
def list_branches(self):
130
for name in self._git.get_refs():
131
ret.append(self.open_branch(name=name))
113
134
def open_repository(self, shared=False):
114
135
"""'open' a repository for this dir."""
115
136
return self._gitrepository_class(self, self._lockfiles)
117
138
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(),
139
if not self._git.bare:
140
from dulwich.errors import NoIndexPresent
142
from bzrlib.plugins.git.workingtree import GitWorkingTree
143
return GitWorkingTree(self, self.open_repository(),
145
except NoIndexPresent:
122
147
loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
123
raise errors.bzr_errors.NoWorkingTree(loc)
148
raise bzr_errors.NoWorkingTree(loc)
125
150
def create_repository(self, shared=False):
126
151
return self.open_repository()
153
def create_branch(self, name=None):
154
refname = self._branch_name_to_ref(name)
155
self._git.refs[refname] = "0" * 40
156
return self.open_branch()
158
def backup_bzrdir(self):
160
self.root_transport.copy_tree(".git", ".git.backup")
161
return (self.root_transport.abspath(".git"),
162
self.root_transport.abspath(".git.backup"))
164
raise bzr_errors.BzrError("Unable to backup bare repositories")
166
def create_workingtree(self, revision_id=None, from_branch=None,
167
accelerator_tree=None, hardlink=False):
169
raise bzr_errors.BzrError("Can't create working tree in a bare repo")
170
from dulwich.index import write_index
171
from dulwich.pack import SHA1Writer
172
f = open(self.transport.local_abspath("index"), 'w+')
178
return self.open_workingtree()