1
# Copyright (C) 2008-2011 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 (
24
from collections import defaultdict
26
from dulwich.index import (
29
from dulwich.object_store import (
32
from dulwich.objects import (
44
conflicts as _mod_conflicts,
55
from bzrlib.decorators import (
60
from bzrlib.plugins.git.dir import (
63
from bzrlib.plugins.git.tree import (
64
changes_from_git_changes,
65
tree_delta_from_git_changes,
67
from bzrlib.plugins.git.mapping import (
72
IGNORE_FILENAME = ".gitignore"
75
class GitWorkingTree(workingtree.WorkingTree):
76
"""A Git working tree."""
78
def __init__(self, bzrdir, repo, branch, index):
79
self.basedir = bzrdir.root_transport.local_abspath('.')
81
self.repository = repo
82
self.store = self.repository._git.object_store
83
self.mapping = self.repository.get_mapping()
85
self._transport = bzrdir.transport
87
self.controldir = self.bzrdir.transport.local_abspath('bzr')
90
os.makedirs(self.controldir)
91
os.makedirs(os.path.join(self.controldir, 'lock'))
95
self._control_files = lockable_files.LockableFiles(
96
transport.get_transport(self.controldir), 'lock', lockdir.LockDir)
97
self._format = GitWorkingTreeFormat()
99
self.views = self._make_views()
100
self._rules_searcher = None
101
self._detect_case_handling()
103
self._fileid_map = self._basis_fileid_map.copy()
105
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
106
self.set_parent_ids([p for p, t in parents_list])
108
def _index_add_entry(self, path, file_id, kind):
109
assert isinstance(path, basestring)
110
assert type(file_id) == str
111
if kind == "directory":
112
# Git indexes don't contain directories
117
file, stat_val = self.get_file_with_stat(file_id, path)
118
except (errors.NoSuchFile, IOError):
119
# TODO: Rather than come up with something here, use the old index
121
from posix import stat_result
122
stat_val = stat_result((stat.S_IFREG | 0644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
123
blob.set_raw_string(file.read())
124
elif kind == "symlink":
127
stat_val = os.lstat(self.abspath(path))
128
except (errors.NoSuchFile, OSError):
129
# TODO: Rather than come up with something here, use the
131
from posix import stat_result
132
stat_val = stat_result((stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
133
blob.set_raw_string(self.get_symlink_target(file_id).encode("utf-8"))
135
raise AssertionError("unknown kind '%s'" % kind)
136
# Add object to the repository if it didn't exist yet
137
if not blob.id in self.store:
138
self.store.add_object(blob)
139
# Add an entry to the index or update the existing entry
141
self.index[path.encode("utf-8")] = (stat_val.st_ctime,
142
stat_val.st_mtime, stat_val.st_dev, stat_val.st_ino,
143
stat_val.st_mode, stat_val.st_uid, stat_val.st_gid,
144
stat_val.st_size, blob.id, flags)
146
def _unversion_path(self, path):
147
encoded_path = path.encode("utf-8")
149
del self.index[encoded_path]
151
# A directory, perhaps?
152
for p in list(self.index):
153
if p.startswith(encoded_path+"/"):
156
def unversion(self, file_ids):
157
for file_id in file_ids:
158
path = self.id2path(file_id)
159
self._unversion_path(path)
161
def remove(self, files, verbose=False, to_file=None, keep_files=True,
163
"""Remove nominated files from the working tree metadata.
165
:param files: File paths relative to the basedir.
166
:param keep_files: If true, the files will also be kept.
167
:param force: Delete files and directories, even if they are changed
168
and even if the directories are not empty.
170
all_files = set() # specified and nested files
172
if isinstance(files, basestring):
178
files = list(all_files)
181
return # nothing to do
183
# Sort needed to first handle directory content before the directory
184
files.sort(reverse=True)
186
def backup(file_to_backup):
187
abs_path = self.abspath(file_to_backup)
188
backup_name = self.bzrdir._available_backup_name(file_to_backup)
189
osutils.rename(abs_path, self.abspath(backup_name))
190
return "removed %s (but kept a copy: %s)" % (
191
file_to_backup, backup_name)
194
fid = self.path2id(f)
196
message = "%s is not versioned." % (f,)
198
abs_path = self.abspath(f)
200
# having removed it, it must be either ignored or unknown
201
if self.is_ignored(f):
205
# XXX: Really should be a more abstract reporter interface
206
kind_ch = osutils.kind_marker(self.kind(fid))
207
to_file.write(new_status + ' ' + f + kind_ch + '\n')
209
# FIXME: _unversion_path() is O(size-of-index) for directories
210
self._unversion_path(f)
211
message = "removed %s" % (f,)
212
if osutils.lexists(abs_path):
213
if (osutils.isdir(abs_path) and
214
len(os.listdir(abs_path)) > 0):
216
osutils.rmtree(abs_path)
217
message = "deleted %s" % (f,)
222
osutils.delete_any(abs_path)
223
message = "deleted %s" % (f,)
225
# print only one message (if any) per file.
226
if message is not None:
229
def _add(self, files, ids, kinds):
230
for (path, file_id, kind) in zip(files, ids, kinds):
231
if file_id is not None:
232
self._fileid_map.set_file_id(path.encode("utf-8"), file_id)
234
file_id = self._fileid_map.lookup_file_id(path.encode("utf-8"))
235
self._index_add_entry(path, file_id, kind)
237
def _set_root_id(self, file_id):
238
self._fileid_map.set_file_id("", file_id)
240
def move(self, from_paths, to_dir=None, after=False):
242
to_abs = self.abspath(to_dir)
243
if not os.path.isdir(to_abs):
244
raise errors.BzrMoveFailedError('', to_dir,
245
errors.NotADirectory(to_abs))
247
for from_rel in from_paths:
248
from_tail = os.path.split(from_rel)[-1]
249
to_rel = os.path.join(to_dir, from_tail)
250
self.rename_one(from_rel, to_rel, after=after)
251
rename_tuples.append((from_rel, to_rel))
254
def rename_one(self, from_rel, to_rel, after=False):
256
os.rename(self.abspath(from_rel), self.abspath(to_rel))
257
from_path = from_rel.encode("utf-8")
258
to_path = to_rel.encode("utf-8")
259
if not self.has_filename(to_rel):
260
raise errors.BzrMoveFailedError(from_rel, to_rel,
261
errors.NoSuchFile(to_rel))
262
if not from_path in self.index:
263
raise errors.BzrMoveFailedError(from_rel, to_rel,
264
errors.NotVersionedError(path=from_rel))
265
self.index[to_path] = self.index[from_path]
266
del self.index[from_path]
268
def get_root_id(self):
269
return self.path2id("")
272
def path2id(self, path):
273
return self._fileid_map.lookup_file_id(path.encode("utf-8"))
276
"""Yield all unversioned files in this WorkingTree.
278
for (dirpath, dirnames, filenames) in os.walk(self.basedir):
279
if self.bzrdir.is_control_filename(dirpath[len(self.basedir):].strip("/")):
281
for filename in filenames:
282
relpath = os.path.join(dirpath[len(self.basedir):].strip("/"), filename)
283
if not relpath in self.index:
287
# non-implementation specific cleanup
290
# reverse order of locking.
292
return self._control_files.unlock()
297
# TODO: Maybe this should only write on dirty ?
298
if self._control_files._lock_mode != 'w':
299
raise errors.NotWriteLocked(self)
303
for path in self.index:
304
yield self.path2id(path)
306
def has_or_had_id(self, file_id):
307
if self.has_id(file_id):
309
if self.had_id(file_id):
313
def had_id(self, file_id):
314
path = self._basis_fileid_map.lookup_file_id(file_id)
316
head = self.repository._git.head()
318
# Assume no if basis is not accessible
322
root_tree = self.store[head].tree
324
tree_lookup_path(self.store.__getitem__, root_tree, path)
330
def has_id(self, file_id):
332
self.id2path(file_id)
333
except errors.NoSuchId:
338
def id2path(self, file_id):
339
if type(file_id) != str:
341
path = self._fileid_map.lookup_path(file_id)
342
# FIXME: What about directories?
343
if path in self.index:
345
raise errors.NoSuchId(self, file_id)
347
def get_file_mtime(self, file_id, path=None):
348
"""See Tree.get_file_mtime."""
350
path = self.id2path(file_id)
351
return os.lstat(self.abspath(path)).st_mtime
353
def get_ignore_list(self):
354
ignoreset = getattr(self, '_ignoreset', None)
355
if ignoreset is not None:
359
ignore_globs.update(ignores.get_runtime_ignores())
360
ignore_globs.update(ignores.get_user_ignores())
361
if self.has_filename(IGNORE_FILENAME):
362
f = self.get_file_byname(IGNORE_FILENAME)
364
# FIXME: Parse git file format, rather than assuming it's
365
# the same as for bzr's native formats.
366
ignore_globs.update(ignores.parse_ignore_file(f))
369
self._ignoreset = ignore_globs
372
def set_last_revision(self, revid):
373
self._change_last_revision(revid)
375
def _reset_data(self):
377
head = self.repository._git.head()
378
except KeyError, name:
379
raise errors.NotBranchError("branch %s at %s" % (name, self.repository.base))
381
self._basis_fileid_map = GitFileIdMap({}, self.mapping)
383
self._basis_fileid_map = self.mapping.get_fileid_map(self.store.__getitem__,
384
self.store[head].tree)
387
def get_file_sha1(self, file_id, path=None, stat_value=None):
389
path = self.id2path(file_id)
391
return osutils.sha_file_by_name(self.abspath(path).encode(osutils._fs_enc))
392
except OSError, (num, msg):
393
if num in (errno.EISDIR, errno.ENOENT):
397
def revision_tree(self, revid):
398
return self.repository.revision_tree(revid)
400
def _get_dir_ie(self, path, parent_id):
401
file_id = self.path2id(path)
402
return inventory.InventoryDirectory(file_id,
403
posixpath.basename(path).strip("/"), parent_id)
405
def _add_missing_parent_ids(self, path, dir_ids):
408
parent = posixpath.dirname(path).strip("/")
409
ret = self._add_missing_parent_ids(parent, dir_ids)
410
parent_id = dir_ids[parent]
411
ie = self._get_dir_ie(path, parent_id)
412
dir_ids[path] = ie.file_id
413
ret.append((path, ie))
416
def _get_file_ie(self, path, value, parent_id):
417
assert isinstance(path, unicode)
418
assert isinstance(value, tuple) and len(value) == 10
419
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
420
file_id = self.path2id(path)
421
if type(file_id) != str:
423
kind = mode_kind(mode)
424
ie = inventory.entry_factory[kind](file_id,
425
posixpath.basename(path), parent_id)
426
if kind == 'symlink':
427
ie.symlink_target = self.get_symlink_target(file_id)
429
data = self.get_file_text(file_id, path)
430
ie.text_sha1 = osutils.sha_string(data)
431
ie.text_size = len(data)
432
ie.executable = self.is_executable(file_id, path)
436
def _is_executable_from_path_and_stat_from_stat(self, path, stat_result):
437
mode = stat_result.st_mode
438
return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
440
def stored_kind(self, file_id, path=None):
442
path = self.id2path(file_id)
443
head = self.repository._git.head()
445
raise errors.NoSuchId(self, file_id)
446
root_tree = self.store[head].tree
447
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, root_tree, path)
448
return mode_kind(mode)
450
if not osutils.supports_executable():
451
def is_executable(self, file_id, path=None):
452
basis_tree = self.basis_tree()
453
if file_id in basis_tree:
454
return basis_tree.is_executable(file_id)
455
# Default to not executable
458
def is_executable(self, file_id, path=None):
460
path = self.id2path(file_id)
461
mode = os.lstat(self.abspath(path)).st_mode
462
return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
464
_is_executable_from_path_and_stat = \
465
_is_executable_from_path_and_stat_from_stat
467
def list_files(self, include_root=False, from_dir=None, recursive=True):
468
# FIXME: Yield non-versioned files
469
# FIXME: support from_dir
470
# FIXME: Support recursive
472
root_ie = self._get_dir_ie(u"", None)
473
if include_root and not from_dir:
474
yield "", "V", root_ie.kind, root_ie.file_id, root_ie
475
dir_ids[u""] = root_ie.file_id
476
for path, value in self.index.iteritems():
477
path = path.decode("utf-8")
478
parent = posixpath.dirname(path).strip("/")
479
for dir_path, dir_ie in self._add_missing_parent_ids(parent, dir_ids):
480
yield dir_path, "V", dir_ie.kind, dir_ie.file_id, dir_ie
481
ie = self._get_file_ie(path, value, dir_ids[parent])
482
yield path, "V", ie.kind, ie.file_id, ie
484
def all_file_ids(self):
485
ids = {u"": self.path2id("")}
486
for path in self.index:
487
path = path.decode("utf-8")
488
parent = posixpath.dirname(path).strip("/")
489
for e in self._add_missing_parent_ids(parent, ids):
491
ids[path] = self.path2id(path)
492
return set(ids.values())
494
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
495
# FIXME: Support specific_file_ids
496
# FIXME: Is return order correct?
497
if specific_file_ids is not None:
498
raise NotImplementedError(self.iter_entries_by_dir)
499
root_ie = self._get_dir_ie(u"", None)
501
dir_ids = {u"": root_ie.file_id}
502
for path, value in self.index.iteritems():
503
path = path.decode("utf-8")
504
parent = posixpath.dirname(path).strip("/")
505
for (dir_path, dir_ie) in self._add_missing_parent_ids(parent,
507
yield dir_path, dir_ie
508
parent_id = self.path2id(parent)
509
yield path, self._get_file_ie(path, value, parent_id)
514
return _mod_conflicts.ConflictList()
516
def update_basis_by_delta(self, new_revid, delta):
517
# The index just contains content, which won't have changed.
520
def _walkdirs(self, prefix=""):
523
per_dir = defaultdict(list)
524
for path, value in self.index.iteritems():
525
if not path.startswith(prefix):
527
(dirname, child_name) = posixpath.split(path)
528
dirname = dirname.decode("utf-8")
529
dir_file_id = self.path2id(dirname)
530
assert isinstance(value, tuple) and len(value) == 10
531
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
532
stat_result = posix.stat_result((mode, ino,
533
dev, 1, uid, gid, size,
535
per_dir[(dirname, dir_file_id)].append(
536
(path.decode("utf-8"), child_name.decode("utf-8"),
537
mode_kind(mode), stat_result,
538
self.path2id(path.decode("utf-8")),
540
return per_dir.iteritems()
542
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):
544
_tree_class = GitWorkingTree
547
def _matchingbzrdir(self):
548
from bzrlib.plugins.git.dir import LocalGitControlDirFormat
549
return LocalGitControlDirFormat()
551
def get_format_description(self):
552
return "Git Working Tree"
554
def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
555
accelerator_tree=None, hardlink=False):
556
"""See WorkingTreeFormat.initialize()."""
557
if not isinstance(a_bzrdir, LocalGitDir):
558
raise errors.IncompatibleFormat(self, a_bzrdir)
559
index = Index(a_bzrdir.root_transport.local_abspath(".git/index"))
561
return GitWorkingTree(a_bzrdir, a_bzrdir.open_repository(),
562
a_bzrdir.open_branch(), index)
565
class InterIndexGitTree(tree.InterTree):
566
"""InterTree that works between a Git revision tree and an index."""
568
def __init__(self, source, target):
569
super(InterIndexGitTree, self).__init__(source, target)
570
self._index = target.index
573
def is_compatible(cls, source, target):
574
from bzrlib.plugins.git.repository import GitRevisionTree
575
return (isinstance(source, GitRevisionTree) and
576
isinstance(target, GitWorkingTree))
578
def compare(self, want_unchanged=False, specific_files=None,
579
extra_trees=None, require_versioned=False, include_root=False,
580
want_unversioned=False):
581
changes = self._index.changes_from_tree(
582
self.source.store, self.source.tree,
583
want_unchanged=want_unchanged)
584
source_fileid_map = self.source.mapping.get_fileid_map(
585
self.source.store.__getitem__,
587
if self.target.mapping.BZR_FILE_IDS_FILE is not None:
588
file_id = self.target.path2id(
589
self.target.mapping.BZR_FILE_IDS_FILE)
591
target_fileid_map = {}
593
target_fileid_map = self.target.mapping.import_fileid_map(
594
Blob.from_string(self.target.get_file_text(file_id)))
596
target_fileid_map = {}
597
target_fileid_map = GitFileIdMap(target_fileid_map,
599
ret = tree_delta_from_git_changes(changes, self.target.mapping,
600
(source_fileid_map, target_fileid_map),
601
specific_file=specific_files, require_versioned=require_versioned)
603
for e in self.target.extras():
604
ret.unversioned.append((e, None,
605
osutils.file_kind(self.target.abspath(e))))
608
def iter_changes(self, include_unchanged=False, specific_files=None,
609
pb=None, extra_trees=[], require_versioned=True,
610
want_unversioned=False):
611
changes = self._index.changes_from_tree(
612
self.source.store, self.source.tree,
613
want_unchanged=include_unchanged)
614
# FIXME: Handle want_unversioned
615
return changes_from_git_changes(changes, self.target.mapping,
616
specific_file=specific_files)
619
tree.InterTree.register_optimiser(InterIndexGitTree)