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 (
37
from posix import stat_result
44
conflicts as _mod_conflicts,
55
from bzrlib.decorators import (
58
from bzrlib.mutabletree import needs_tree_write_lock
61
from bzrlib.plugins.git.dir import (
64
from bzrlib.plugins.git.tree import (
65
changes_from_git_changes,
66
tree_delta_from_git_changes,
68
from bzrlib.plugins.git.mapping import (
73
IGNORE_FILENAME = ".gitignore"
76
class GitWorkingTree(workingtree.WorkingTree):
77
"""A Git working tree."""
79
def __init__(self, bzrdir, repo, branch, index):
80
self.basedir = bzrdir.root_transport.local_abspath('.')
82
self.repository = repo
83
self.store = self.repository._git.object_store
84
self.mapping = self.repository.get_mapping()
86
self._transport = bzrdir.transport
88
self.controldir = self.bzrdir.transport.local_abspath('bzr')
91
os.makedirs(self.controldir)
92
os.makedirs(os.path.join(self.controldir, 'lock'))
96
self._control_files = lockable_files.LockableFiles(
97
transport.get_transport(self.controldir), 'lock', lockdir.LockDir)
98
self._format = GitWorkingTreeFormat()
100
self._versioned_dirs = None
101
self.views = self._make_views()
102
self._rules_searcher = None
103
self._detect_case_handling()
105
self._fileid_map = self._basis_fileid_map.copy()
107
def _detect_case_handling(self):
109
self._transport.stat(".git/cOnFiG")
110
except errors.NoSuchFile:
111
self.case_sensitive = True
113
self.case_sensitive = False
115
def merge_modified(self):
118
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
119
self.set_parent_ids([p for p, t in parents_list])
121
def _index_add_entry(self, path, file_id, kind):
122
assert isinstance(path, basestring)
123
assert type(file_id) == str or file_id is None
124
if kind == "directory":
125
# Git indexes don't contain directories
130
file, stat_val = self.get_file_with_stat(file_id, path)
131
except (errors.NoSuchFile, IOError):
132
# TODO: Rather than come up with something here, use the old index
134
stat_val = stat_result((stat.S_IFREG | 0644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
135
blob.set_raw_string(file.read())
136
elif kind == "symlink":
139
stat_val = os.lstat(self.abspath(path))
140
except (errors.NoSuchFile, OSError):
141
# TODO: Rather than come up with something here, use the
143
stat_val = stat_result((stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
145
self.get_symlink_target(file_id, path).encode("utf-8"))
147
raise AssertionError("unknown kind '%s'" % kind)
148
# Add object to the repository if it didn't exist yet
149
if not blob.id in self.store:
150
self.store.add_object(blob)
151
# Add an entry to the index or update the existing entry
153
encoded_path = path.encode("utf-8")
154
self.index[encoded_path] = (stat_val.st_ctime,
155
stat_val.st_mtime, stat_val.st_dev, stat_val.st_ino,
156
stat_val.st_mode, stat_val.st_uid, stat_val.st_gid,
157
stat_val.st_size, blob.id, flags)
158
if self._versioned_dirs is not None:
159
self._ensure_versioned_dir(encoded_path)
161
def _ensure_versioned_dir(self, dirname):
162
if dirname in self._versioned_dirs:
165
self._ensure_versioned_dir(posixpath.dirname(dirname))
166
self._versioned_dirs.add(dirname)
168
def _load_dirs(self):
169
self._versioned_dirs = set()
171
self._ensure_versioned_dir(posixpath.dirname(p))
173
def _unversion_path(self, path):
174
encoded_path = path.encode("utf-8")
176
del self.index[encoded_path]
178
# A directory, perhaps?
179
for p in list(self.index):
180
if p.startswith(encoded_path+"/"):
182
# FIXME: remove empty directories
184
@needs_tree_write_lock
185
def unversion(self, file_ids):
186
for file_id in file_ids:
187
path = self.id2path(file_id)
188
self._unversion_path(path)
191
def check_state(self):
192
"""Check that the working state is/isn't valid."""
195
@needs_tree_write_lock
196
def remove(self, files, verbose=False, to_file=None, keep_files=True,
198
"""Remove nominated files from the working tree metadata.
200
:param files: File paths relative to the basedir.
201
:param keep_files: If true, the files will also be kept.
202
:param force: Delete files and directories, even if they are changed
203
and even if the directories are not empty.
205
all_files = set() # specified and nested files
207
if isinstance(files, basestring):
213
files = list(all_files)
216
return # nothing to do
218
# Sort needed to first handle directory content before the directory
219
files.sort(reverse=True)
221
def backup(file_to_backup):
222
abs_path = self.abspath(file_to_backup)
223
backup_name = self.bzrdir._available_backup_name(file_to_backup)
224
osutils.rename(abs_path, self.abspath(backup_name))
225
return "removed %s (but kept a copy: %s)" % (
226
file_to_backup, backup_name)
229
fid = self.path2id(f)
231
message = "%s is not versioned." % (f,)
233
abs_path = self.abspath(f)
235
# having removed it, it must be either ignored or unknown
236
if self.is_ignored(f):
240
# XXX: Really should be a more abstract reporter interface
241
kind_ch = osutils.kind_marker(self.kind(fid))
242
to_file.write(new_status + ' ' + f + kind_ch + '\n')
244
# FIXME: _unversion_path() is O(size-of-index) for directories
245
self._unversion_path(f)
246
message = "removed %s" % (f,)
247
if osutils.lexists(abs_path):
248
if (osutils.isdir(abs_path) and
249
len(os.listdir(abs_path)) > 0):
251
osutils.rmtree(abs_path)
252
message = "deleted %s" % (f,)
257
osutils.delete_any(abs_path)
258
message = "deleted %s" % (f,)
260
# print only one message (if any) per file.
261
if message is not None:
265
def _add(self, files, ids, kinds):
266
for (path, file_id, kind) in zip(files, ids, kinds):
267
if file_id is not None:
268
self._fileid_map.set_file_id(path.encode("utf-8"), file_id)
270
file_id = self._fileid_map.lookup_file_id(path.encode("utf-8"))
271
self._index_add_entry(path, file_id, kind)
273
@needs_tree_write_lock
274
def smart_add(self, file_list, recurse=True, action=None, save=True):
278
for filepath in osutils.canonical_relpaths(self.basedir, file_list):
279
abspath = self.abspath(filepath)
280
kind = osutils.file_kind(abspath)
281
if action is not None:
282
file_id = action(self, None, filepath, kind)
285
if kind in ("file", "symlink"):
287
self._index_add_entry(filepath, file_id, kind)
288
added.append(filepath)
289
elif kind == "directory":
291
user_dirs.append(filepath)
293
raise errors.BadFileKindError(filename=abspath, kind=kind)
294
for user_dir in user_dirs:
295
abs_user_dir = self.abspath(user_dir)
296
for name in os.listdir(abs_user_dir):
297
subp = os.path.join(user_dir, name)
298
if self.is_control_filename(subp) or self.mapping.is_special_file(subp):
300
ignore_glob = self.is_ignored(subp)
301
if ignore_glob is not None:
302
ignored.setdefault(ignore_glob, []).append(subp)
304
abspath = self.abspath(subp)
305
kind = osutils.file_kind(abspath)
306
if kind == "directory":
307
user_dirs.append(subp)
309
if action is not None:
314
self._index_add_entry(subp, file_id, kind)
317
return added, ignored
319
def _set_root_id(self, file_id):
320
self._fileid_map.set_file_id("", file_id)
322
@needs_tree_write_lock
323
def move(self, from_paths, to_dir=None, after=False):
325
to_abs = self.abspath(to_dir)
326
if not os.path.isdir(to_abs):
327
raise errors.BzrMoveFailedError('', to_dir,
328
errors.NotADirectory(to_abs))
330
for from_rel in from_paths:
331
from_tail = os.path.split(from_rel)[-1]
332
to_rel = os.path.join(to_dir, from_tail)
333
self.rename_one(from_rel, to_rel, after=after)
334
rename_tuples.append((from_rel, to_rel))
338
@needs_tree_write_lock
339
def rename_one(self, from_rel, to_rel, after=False):
341
os.rename(self.abspath(from_rel), self.abspath(to_rel))
342
from_path = from_rel.encode("utf-8")
343
to_path = to_rel.encode("utf-8")
344
if not self.has_filename(to_rel):
345
raise errors.BzrMoveFailedError(from_rel, to_rel,
346
errors.NoSuchFile(to_rel))
347
if not from_path in self.index:
348
raise errors.BzrMoveFailedError(from_rel, to_rel,
349
errors.NotVersionedError(path=from_rel))
350
self.index[to_path] = self.index[from_path]
351
del self.index[from_path]
354
def get_root_id(self):
355
return self.path2id("")
357
def _has_dir(self, path):
358
if self._versioned_dirs is None:
360
return path in self._versioned_dirs
363
def path2id(self, path):
364
encoded_path = path.encode("utf-8")
365
if self._is_versioned(encoded_path):
366
return self._fileid_map.lookup_file_id(encoded_path)
369
def _iter_files_recursive(self, from_dir=None):
372
for (dirpath, dirnames, filenames) in os.walk(self.abspath(from_dir)):
373
dir_relpath = dirpath[len(self.basedir):].strip("/")
374
if self.bzrdir.is_control_filename(dir_relpath):
376
for filename in filenames:
377
if not self.mapping.is_special_file(filename):
378
yield os.path.join(dir_relpath, filename)
381
"""Yield all unversioned files in this WorkingTree.
383
return set(self._iter_files_recursive()) - set(self.index)
386
# non-implementation specific cleanup
389
# reverse order of locking.
391
return self._control_files.unlock()
396
# TODO: Maybe this should only write on dirty ?
397
if self._control_files._lock_mode != 'w':
398
raise errors.NotWriteLocked(self)
402
for path in self.index:
403
yield self.path2id(path)
405
for path in self._versioned_dirs:
406
yield self.path2id(path)
408
def has_or_had_id(self, file_id):
409
if self.has_id(file_id):
411
if self.had_id(file_id):
415
def had_id(self, file_id):
416
path = self._basis_fileid_map.lookup_file_id(file_id)
418
head = self.repository._git.head()
420
# Assume no if basis is not accessible
424
root_tree = self.store[head].tree
426
tree_lookup_path(self.store.__getitem__, root_tree, path)
432
def has_id(self, file_id):
434
self.id2path(file_id)
435
except errors.NoSuchId:
440
def id2path(self, file_id):
441
if type(file_id) != str:
443
path = self._fileid_map.lookup_path(file_id)
444
# FIXME: What about directories?
445
if self._is_versioned(path):
446
return path.decode("utf-8")
447
raise errors.NoSuchId(self, file_id)
449
def get_file_mtime(self, file_id, path=None):
450
"""See Tree.get_file_mtime."""
452
path = self.id2path(file_id)
453
return os.lstat(self.abspath(path)).st_mtime
455
def get_ignore_list(self):
456
ignoreset = getattr(self, '_ignoreset', None)
457
if ignoreset is not None:
461
ignore_globs.update(ignores.get_runtime_ignores())
462
ignore_globs.update(ignores.get_user_ignores())
463
if self.has_filename(IGNORE_FILENAME):
464
f = self.get_file_byname(IGNORE_FILENAME)
466
# FIXME: Parse git file format, rather than assuming it's
467
# the same as for bzr's native formats.
468
ignore_globs.update(ignores.parse_ignore_file(f))
471
self._ignoreset = ignore_globs
474
def set_last_revision(self, revid):
475
self._change_last_revision(revid)
477
def _reset_data(self):
479
head = self.repository._git.head()
480
except KeyError, name:
481
raise errors.NotBranchError("branch %s at %s" % (name,
482
self.repository.base))
484
self._basis_fileid_map = GitFileIdMap({}, self.mapping)
486
self._basis_fileid_map = self.mapping.get_fileid_map(
487
self.store.__getitem__, self.store[head].tree)
490
def get_file_verifier(self, file_id, path=None, stat_value=None):
492
path = self.id2path(file_id)
493
return ("GIT", self.index[path][-2])
496
def get_file_sha1(self, file_id, path=None, stat_value=None):
498
path = self.id2path(file_id)
499
abspath = self.abspath(path).encode(osutils._fs_enc)
501
return osutils.sha_file_by_name(abspath)
502
except OSError, (num, msg):
503
if num in (errno.EISDIR, errno.ENOENT):
507
def revision_tree(self, revid):
508
return self.repository.revision_tree(revid)
510
def _is_versioned(self, path):
511
return (path in self.index or self._has_dir(path))
513
def filter_unversioned_files(self, files):
514
return set([p for p in files if not self._is_versioned(p.encode("utf-8"))])
516
def _get_dir_ie(self, path, parent_id):
517
file_id = self.path2id(path)
518
return inventory.InventoryDirectory(file_id,
519
posixpath.basename(path).strip("/"), parent_id)
521
def _add_missing_parent_ids(self, path, dir_ids):
524
parent = posixpath.dirname(path).strip("/")
525
ret = self._add_missing_parent_ids(parent, dir_ids)
526
parent_id = dir_ids[parent]
527
ie = self._get_dir_ie(path, parent_id)
528
dir_ids[path] = ie.file_id
529
ret.append((path, ie))
532
def _get_file_ie(self, name, path, value, parent_id):
533
assert isinstance(name, unicode)
534
assert isinstance(path, unicode)
535
assert isinstance(value, tuple) and len(value) == 10
536
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
537
file_id = self.path2id(path)
538
if type(file_id) != str:
540
kind = mode_kind(mode)
541
ie = inventory.entry_factory[kind](file_id, name, parent_id)
542
if kind == 'symlink':
543
ie.symlink_target = self.get_symlink_target(file_id)
545
data = self.get_file_text(file_id, path)
546
ie.text_sha1 = osutils.sha_string(data)
547
ie.text_size = len(data)
548
ie.executable = self.is_executable(file_id, path)
552
def _is_executable_from_path_and_stat_from_stat(self, path, stat_result):
553
mode = stat_result.st_mode
554
return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
556
def stored_kind(self, file_id, path=None):
558
path = self.id2path(file_id)
560
return mode_kind(self.index[path.encode("utf-8")][4])
562
# Maybe it's a directory?
563
if self._has_dir(path):
565
raise errors.NoSuchId(self, file_id)
567
if not osutils.supports_executable():
568
def is_executable(self, file_id, path=None):
569
basis_tree = self.basis_tree()
570
if file_id in basis_tree:
571
return basis_tree.is_executable(file_id)
572
# Default to not executable
575
def is_executable(self, file_id, path=None):
577
path = self.id2path(file_id)
578
mode = os.lstat(self.abspath(path)).st_mode
579
return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
581
_is_executable_from_path_and_stat = \
582
_is_executable_from_path_and_stat_from_stat
584
def list_files(self, include_root=False, from_dir=None, recursive=True):
585
# FIXME: Yield non-versioned files
589
fk_entries = {'directory': workingtree.TreeDirectory,
590
'file': workingtree.TreeFile,
591
'symlink': workingtree.TreeLink}
592
root_ie = self._get_dir_ie(u"", None)
593
if include_root and not from_dir:
594
yield "", "V", root_ie.kind, root_ie.file_id, root_ie
595
dir_ids[u""] = root_ie.file_id
597
path_iterator = self._iter_files_recursive(from_dir)
602
start = os.path.join(self.basedir, from_dir)
603
path_iterator = sorted([os.path.join(from_dir, name) for name in
604
os.listdir(start) if not self.bzrdir.is_control_filename(name)
605
and not self.mapping.is_special_file(name)])
606
for path in path_iterator:
608
value = self.index[path]
611
path = path.decode("utf-8")
612
parent, name = posixpath.split(path)
613
for dir_path, dir_ie in self._add_missing_parent_ids(parent, dir_ids):
614
yield dir_path, "V", dir_ie.kind, dir_ie.file_id, dir_ie
615
if value is not None:
616
ie = self._get_file_ie(name, path, value, dir_ids[parent])
617
yield path, "V", ie.kind, ie.file_id, ie
619
kind = osutils.file_kind(self.abspath(path))
620
ie = fk_entries[kind]()
621
yield path, "?", kind, None, ie
623
def all_file_ids(self):
624
ids = {u"": self.path2id("")}
625
for path in self.index:
626
if self.mapping.is_special_file(path):
628
path = path.decode("utf-8")
629
parent = posixpath.dirname(path).strip("/")
630
for e in self._add_missing_parent_ids(parent, ids):
632
ids[path] = self.path2id(path)
633
return set(ids.values())
635
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
636
# FIXME: Is return order correct?
638
raise NotImplementedError(self.iter_entries_by_dir)
639
if specific_file_ids is not None:
640
specific_paths = [self.id2path(file_id) for file_id in specific_file_ids]
641
if specific_paths in ([u""], []):
642
specific_paths = None
644
specific_paths = set(specific_paths)
646
specific_paths = None
647
root_ie = self._get_dir_ie(u"", None)
648
if specific_paths is None:
650
dir_ids = {u"": root_ie.file_id}
651
for path, value in self.index.iteritems():
652
if self.mapping.is_special_file(path):
654
path = path.decode("utf-8")
655
if specific_paths is not None and not path in specific_paths:
657
(parent, name) = posixpath.split(path)
659
file_ie = self._get_file_ie(name, path, value, None)
662
for (dir_path, dir_ie) in self._add_missing_parent_ids(parent,
664
yield dir_path, dir_ie
665
file_ie.parent_id = self.path2id(parent)
671
return _mod_conflicts.ConflictList()
673
def update_basis_by_delta(self, new_revid, delta):
674
# The index just contains content, which won't have changed.
677
def get_canonical_inventory_path(self, path):
679
if p.lower() == path.lower():
684
def _walkdirs(self, prefix=""):
687
per_dir = defaultdict(list)
688
for path, value in self.index.iteritems():
689
if self.mapping.is_special_file(path):
691
if not path.startswith(prefix):
693
(dirname, child_name) = posixpath.split(path)
694
dirname = dirname.decode("utf-8")
695
dir_file_id = self.path2id(dirname)
696
assert isinstance(value, tuple) and len(value) == 10
697
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
698
stat_result = posix.stat_result((mode, ino,
699
dev, 1, uid, gid, size,
701
per_dir[(dirname, dir_file_id)].append(
702
(path.decode("utf-8"), child_name.decode("utf-8"),
703
mode_kind(mode), stat_result,
704
self.path2id(path.decode("utf-8")),
706
return per_dir.iteritems()
709
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):
711
_tree_class = GitWorkingTree
713
supports_versioned_directories = False
716
def _matchingbzrdir(self):
717
from bzrlib.plugins.git.dir import LocalGitControlDirFormat
718
return LocalGitControlDirFormat()
720
def get_format_description(self):
721
return "Git Working Tree"
723
def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
724
accelerator_tree=None, hardlink=False):
725
"""See WorkingTreeFormat.initialize()."""
726
if not isinstance(a_bzrdir, LocalGitDir):
727
raise errors.IncompatibleFormat(self, a_bzrdir)
728
index = Index(a_bzrdir.root_transport.local_abspath(".git/index"))
730
return GitWorkingTree(a_bzrdir, a_bzrdir.open_repository(),
731
a_bzrdir.open_branch(), index)
734
class InterIndexGitTree(tree.InterTree):
735
"""InterTree that works between a Git revision tree and an index."""
737
def __init__(self, source, target):
738
super(InterIndexGitTree, self).__init__(source, target)
739
self._index = target.index
742
def is_compatible(cls, source, target):
743
from bzrlib.plugins.git.repository import GitRevisionTree
744
return (isinstance(source, GitRevisionTree) and
745
isinstance(target, GitWorkingTree))
747
def compare(self, want_unchanged=False, specific_files=None,
748
extra_trees=None, require_versioned=False, include_root=False,
749
want_unversioned=False):
750
changes = self._index.changes_from_tree(
751
self.source.store, self.source.tree,
752
want_unchanged=want_unchanged)
753
source_fileid_map = self.source._fileid_map
754
target_fileid_map = self.target._fileid_map
755
ret = tree_delta_from_git_changes(changes, self.target.mapping,
756
(source_fileid_map, target_fileid_map),
757
specific_file=specific_files, require_versioned=require_versioned)
759
for e in self.target.extras():
760
ret.unversioned.append((e, None,
761
osutils.file_kind(self.target.abspath(e))))
764
def iter_changes(self, include_unchanged=False, specific_files=None,
765
pb=None, extra_trees=[], require_versioned=True,
766
want_unversioned=False):
767
changes = self._index.changes_from_tree(
768
self.source.store, self.source.tree,
769
want_unchanged=include_unchanged)
770
# FIXME: Handle want_unversioned
771
return changes_from_git_changes(changes, self.target.mapping,
772
specific_file=specific_files)
775
tree.InterTree.register_optimiser(InterIndexGitTree)