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
18
"""An adapter between a Git index and a Bazaar Working Tree"""
21
from cStringIO import (
24
from dulwich.objects import (
21
30
from bzrlib import (
30
from dulwich.index import Index
41
from bzrlib.decorators import (
46
from bzrlib.plugins.git.inventory import (
51
IGNORE_FILENAME = ".gitignore"
32
54
class GitWorkingTree(workingtree.WorkingTree):
33
55
"""A Git working tree."""
35
57
def __init__(self, bzrdir, repo, branch):
36
self.basedir = bzrdir.transport.base
58
self.basedir = bzrdir.root_transport.local_abspath('.')
37
59
self.bzrdir = bzrdir
38
60
self.repository = repo
61
self.mapping = self.repository.get_mapping()
39
62
self._branch = branch
40
63
self._transport = bzrdir.transport
42
self.controldir = urlutils.join(self.repository._git.path, 'bzr')
65
self.controldir = urlutils.join(self.repository._git._controldir, 'bzr')
45
68
os.makedirs(self.controldir)
68
90
def is_control_filename(self, path):
69
91
return os.path.basename(path) == ".git"
71
def _get_inventory(self):
72
return inventory.Inventory()
74
inventory = property(_get_inventory,
75
doc="Inventory of this Tree")
93
def _rewrite_index(self):
95
for path, entry in self._inventory.iter_entries():
96
if entry.kind == "directory":
97
# Git indexes don't contain directories
99
if entry.kind == "file":
102
file, stat_val = self.get_file_with_stat(entry.file_id, path)
103
except (errors.NoSuchFile, IOError):
104
# TODO: Rather than come up with something here, use the old index
106
stat_val = (0, 0, 0, 0, stat.S_IFREG | 0644, 0, 0, 0, 0, 0)
107
blob.set_raw_string(file.read())
108
elif entry.kind == "symlink":
111
stat_val = os.lstat(self.abspath(path))
112
except (errors.NoSuchFile, OSError):
113
# TODO: Rather than come up with something here, use the
115
stat_val = (0, 0, 0, 0, stat.S_IFLNK, 0, 0, 0, 0, 0)
116
blob.set_raw_string(entry.symlink_target)
117
# Add object to the repository if it didn't exist yet
118
if not blob.id in self.repository._git.object_store:
119
self.repository._git.object_store.add_object(blob)
120
# Add an entry to the index or update the existing entry
122
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)
125
# TODO: Maybe this should only write on dirty ?
126
if self._control_files._lock_mode != 'w':
127
raise errors.NotWriteLocked(self)
128
self._rewrite_index()
130
self._inventory_is_modified = False
132
def get_ignore_list(self):
133
ignoreset = getattr(self, '_ignoreset', None)
134
if ignoreset is not None:
138
ignore_globs.update(ignores.get_runtime_ignores())
139
ignore_globs.update(ignores.get_user_ignores())
140
if self.has_filename(IGNORE_FILENAME):
141
f = self.get_file_byname(IGNORE_FILENAME)
143
ignore_globs.update(ignores.parse_ignore_file(f))
146
self._ignoreset = ignore_globs
149
def set_last_revision(self, revid):
150
self._change_last_revision(revid)
152
def _reset_data(self):
153
self._inventory_is_modified = False
154
basis_inv = self.repository.get_inventory(self.mapping.revision_id_foreign_to_bzr(self.repository._git.head()))
155
result = GitIndexInventory(basis_inv, self.mapping, self.index,
156
self.repository._git.object_store)
157
self._set_inventory(result, dirty=False)
160
def get_file_sha1(self, file_id, path=None, stat_value=None):
162
path = self._inventory.id2path(file_id)
163
return osutils.sha_file_by_name(self.abspath(path).encode(osutils._fs_enc))
165
def iter_changes(self, from_tree, include_unchanged=False,
166
specific_files=None, pb=None, extra_trees=None,
167
require_versioned=True, want_unversioned=False):
169
intertree = tree.InterTree.get(from_tree, self)
170
return intertree.iter_changes(include_unchanged, specific_files, pb,
171
extra_trees, require_versioned, want_unversioned=want_unversioned)
78
174
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):