1
# Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
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
18
"""An adapter between a Git index and a Bazaar Working Tree"""
21
from cStringIO import (
25
from dulwich.objects import (
41
from bzrlib.decorators import (
46
from bzrlib.plugins.git.inventory import (
49
from bzrlib.plugins.git.mapping import (
52
from bzrlib.plugins.git.tree import (
53
changes_from_git_changes,
54
tree_delta_from_git_changes,
58
IGNORE_FILENAME = ".gitignore"
61
class GitWorkingTree(workingtree.WorkingTree):
62
"""A Git working tree."""
64
def __init__(self, bzrdir, repo, branch, index):
65
self.basedir = bzrdir.root_transport.local_abspath('.')
67
self.repository = repo
68
self.mapping = self.repository.get_mapping()
70
self._transport = bzrdir.transport
72
self.controldir = self.bzrdir.transport.local_abspath('bzr')
75
os.makedirs(self.controldir)
76
os.makedirs(os.path.join(self.controldir, 'lock'))
80
self._control_files = lockable_files.LockableFiles(
81
transport.get_transport(self.controldir), 'lock', lockdir.LockDir)
82
self._format = GitWorkingTreeFormat()
84
self.views = self._make_views()
85
self._detect_case_handling()
88
"""Yield all unversioned files in this WorkingTree.
90
for (dirpath, dirnames, filenames) in os.walk(self.basedir):
91
if self.bzrdir.is_control_filename(dirpath[len(self.basedir):].strip("/")):
93
for filename in filenames:
94
relpath = os.path.join(dirpath[len(self.basedir):].strip("/"), filename)
95
if not relpath in self.index:
100
# non-implementation specific cleanup
103
# reverse order of locking.
105
return self._control_files.unlock()
109
def is_control_filename(self, path):
110
return os.path.basename(path) == ".git"
112
def _rewrite_index(self):
114
for path, entry in self._inventory.iter_entries():
115
if entry.kind == "directory":
116
# Git indexes don't contain directories
118
if entry.kind == "file":
121
file, stat_val = self.get_file_with_stat(entry.file_id, path)
122
except (errors.NoSuchFile, IOError):
123
# TODO: Rather than come up with something here, use the old index
125
from posix import stat_result
126
stat_val = stat_result((stat.S_IFREG | 0644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
127
blob.set_raw_string(file.read())
128
elif entry.kind == "symlink":
131
stat_val = os.lstat(self.abspath(path))
132
except (errors.NoSuchFile, OSError):
133
# TODO: Rather than come up with something here, use the
135
from posix import stat_result
136
stat_val = stat_result((stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
137
blob.set_raw_string(entry.symlink_target)
139
raise AssertionError("unknown kind '%s'" % entry.kind)
140
# Add object to the repository if it didn't exist yet
141
if not blob.id in self.repository._git.object_store:
142
self.repository._git.object_store.add_object(blob)
143
# Add an entry to the index or update the existing entry
145
self.index[path.encode("utf-8")] = (stat_val.st_ctime, stat_val.st_mtime, stat_val.st_dev, stat_val.st_ino, stat_val.st_mode, stat_val.st_uid, stat_val.st_gid, stat_val.st_size, blob.id, flags)
148
# TODO: Maybe this should only write on dirty ?
149
if self._control_files._lock_mode != 'w':
150
raise errors.NotWriteLocked(self)
151
self._rewrite_index()
153
self._inventory_is_modified = False
155
def get_ignore_list(self):
156
ignoreset = getattr(self, '_ignoreset', None)
157
if ignoreset is not None:
161
ignore_globs.update(ignores.get_runtime_ignores())
162
ignore_globs.update(ignores.get_user_ignores())
163
if self.has_filename(IGNORE_FILENAME):
164
f = self.get_file_byname(IGNORE_FILENAME)
166
ignore_globs.update(ignores.parse_ignore_file(f))
169
self._ignoreset = ignore_globs
172
def set_last_revision(self, revid):
173
self._change_last_revision(revid)
175
def _reset_data(self):
176
self._inventory_is_modified = False
178
head = self.repository._git.head()
179
except KeyError, name:
180
raise errors.NotBranchError("branch %s at %s" % (name, self.repository.base))
181
basis_inv = self.repository.get_inventory(self.mapping.revision_id_foreign_to_bzr(head))
182
store = self.repository._git.object_store
185
file_id_map_sha = store[commit.tree][self.mapping.BZR_FILE_IDS_FILE][1]
189
file_ids = self.mapping.import_fileid_map(store[file_id_map_sha])
190
fileid_map = GitFileIdMap(file_ids, self.mapping)
191
result = GitIndexInventory(basis_inv, fileid_map, self.index, store)
192
self._set_inventory(result, dirty=False)
195
def get_file_sha1(self, file_id, path=None, stat_value=None):
197
path = self._inventory.id2path(file_id)
199
return osutils.sha_file_by_name(self.abspath(path).encode(osutils._fs_enc))
200
except OSError, (num, msg):
201
if num in (errno.EISDIR, errno.ENOENT):
205
def revision_tree(self, revid):
206
return self.repository.revision_tree(revid)
214
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):
217
def _matchingbzrdir(self):
218
from bzrlib.plugins.git import LocalGitBzrDirFormat
219
return LocalGitBzrDirFormat()
221
def get_format_description(self):
222
return "Git Working Tree"
225
class InterIndexGitTree(tree.InterTree):
226
"""InterTree that works between a Git revision tree and an index."""
228
def __init__(self, source, target):
229
super(InterIndexGitTree, self).__init__(source, target)
230
self._index = target.index
233
def is_compatible(cls, source, target):
234
from bzrlib.plugins.git.repository import GitRevisionTree
235
return (isinstance(source, GitRevisionTree) and
236
isinstance(target, GitWorkingTree))
238
def compare(self, want_unchanged=False, specific_files=None,
239
extra_trees=None, require_versioned=False, include_root=False,
240
want_unversioned=False):
241
changes = self._index.changes_from_tree(
242
self.source._repository._git.object_store, self.source.tree,
243
want_unchanged=want_unchanged)
244
ret = tree_delta_from_git_changes(changes, self.target.mapping,
245
specific_file=specific_files, require_versioned=require_versioned)
247
for e in self.target.extras():
248
ret.unversioned.append((e, None, osutils.file_kind(self.target.abspath(e))))
251
def iter_changes(self, include_unchanged=False, specific_files=None,
252
pb=None, extra_trees=[], require_versioned=True, want_unversioned=False):
253
changes = self._index.changes_from_tree(
254
self.source._repository._git.object_store, self.source.tree,
255
want_unchanged=include_unchanged)
256
# FIXME: Handle want_unversioned
257
return changes_from_git_changes(changes, self.target.mapping,
258
specific_file=specific_files)
260
tree.InterTree.register_optimiser(InterIndexGitTree)