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 (
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 set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
108
self.set_parent_ids([p for p, t in parents_list])
110
def _index_add_entry(self, path, file_id, kind):
111
assert isinstance(path, basestring)
112
assert type(file_id) == str or file_id is None
113
if kind == "directory":
114
# Git indexes don't contain directories
119
file, stat_val = self.get_file_with_stat(file_id, path)
120
except (errors.NoSuchFile, IOError):
121
# TODO: Rather than come up with something here, use the old index
123
from posix import stat_result
124
stat_val = stat_result((stat.S_IFREG | 0644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
125
blob.set_raw_string(file.read())
126
elif kind == "symlink":
129
stat_val = os.lstat(self.abspath(path))
130
except (errors.NoSuchFile, OSError):
131
# TODO: Rather than come up with something here, use the
133
from posix import stat_result
134
stat_val = stat_result((stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
135
blob.set_raw_string(self.get_symlink_target(file_id).encode("utf-8"))
137
raise AssertionError("unknown kind '%s'" % kind)
138
# Add object to the repository if it didn't exist yet
139
if not blob.id in self.store:
140
self.store.add_object(blob)
141
# Add an entry to the index or update the existing entry
143
encoded_path = path.encode("utf-8")
144
self.index[encoded_path] = (stat_val.st_ctime,
145
stat_val.st_mtime, stat_val.st_dev, stat_val.st_ino,
146
stat_val.st_mode, stat_val.st_uid, stat_val.st_gid,
147
stat_val.st_size, blob.id, flags)
148
if self._versioned_dirs is not None:
149
self._ensure_versioned_dir(encoded_path)
151
def _ensure_versioned_dir(self, dirname):
152
if dirname in self._versioned_dirs:
155
self._ensure_versioned_dir(posixpath.dirname(dirname))
156
self._versioned_dirs.add(dirname)
158
def _load_dirs(self):
159
self._versioned_dirs = set()
161
self._ensure_versioned_dir(posixpath.dirname(p))
163
def _unversion_path(self, path):
164
encoded_path = path.encode("utf-8")
166
del self.index[encoded_path]
168
# A directory, perhaps?
169
for p in list(self.index):
170
if p.startswith(encoded_path+"/"):
172
# FIXME: remove empty directories
174
def unversion(self, file_ids):
175
for file_id in file_ids:
176
path = self.id2path(file_id)
177
self._unversion_path(path)
179
def check_state(self):
180
"""Check that the working state is/isn't valid."""
183
def remove(self, files, verbose=False, to_file=None, keep_files=True,
185
"""Remove nominated files from the working tree metadata.
187
:param files: File paths relative to the basedir.
188
:param keep_files: If true, the files will also be kept.
189
:param force: Delete files and directories, even if they are changed
190
and even if the directories are not empty.
192
all_files = set() # specified and nested files
194
if isinstance(files, basestring):
200
files = list(all_files)
203
return # nothing to do
205
# Sort needed to first handle directory content before the directory
206
files.sort(reverse=True)
208
def backup(file_to_backup):
209
abs_path = self.abspath(file_to_backup)
210
backup_name = self.bzrdir._available_backup_name(file_to_backup)
211
osutils.rename(abs_path, self.abspath(backup_name))
212
return "removed %s (but kept a copy: %s)" % (
213
file_to_backup, backup_name)
216
fid = self.path2id(f)
218
message = "%s is not versioned." % (f,)
220
abs_path = self.abspath(f)
222
# having removed it, it must be either ignored or unknown
223
if self.is_ignored(f):
227
# XXX: Really should be a more abstract reporter interface
228
kind_ch = osutils.kind_marker(self.kind(fid))
229
to_file.write(new_status + ' ' + f + kind_ch + '\n')
231
# FIXME: _unversion_path() is O(size-of-index) for directories
232
self._unversion_path(f)
233
message = "removed %s" % (f,)
234
if osutils.lexists(abs_path):
235
if (osutils.isdir(abs_path) and
236
len(os.listdir(abs_path)) > 0):
238
osutils.rmtree(abs_path)
239
message = "deleted %s" % (f,)
244
osutils.delete_any(abs_path)
245
message = "deleted %s" % (f,)
247
# print only one message (if any) per file.
248
if message is not None:
251
def _add(self, files, ids, kinds):
252
for (path, file_id, kind) in zip(files, ids, kinds):
253
if file_id is not None:
254
self._fileid_map.set_file_id(path.encode("utf-8"), file_id)
256
file_id = self._fileid_map.lookup_file_id(path.encode("utf-8"))
257
self._index_add_entry(path, file_id, kind)
259
@needs_tree_write_lock
260
def smart_add(self, file_list, recurse=True, action=None, save=True):
264
for filepath in osutils.canonical_relpaths(self.basedir, file_list):
265
abspath = self.abspath(filepath)
266
kind = osutils.file_kind(abspath)
267
if action is not None:
268
file_id = action(self, None, filepath, kind)
271
if kind in ("file", "symlink"):
273
self._index_add_entry(filepath, file_id, kind)
274
added.append(filepath)
275
elif kind == "directory":
277
user_dirs.append(filepath)
279
raise errors.BadFileKindError(filename=abspath, kind=kind)
280
for user_dir in user_dirs:
281
abs_user_dir = self.abspath(user_dir)
282
for name in os.listdir(abs_user_dir):
283
subp = os.path.join(user_dir, name)
284
if self.is_control_filename(subp):
286
ignore_glob = self.is_ignored(subp)
287
if ignore_glob is not None:
288
ignored.setdefault(ignore_glob, []).append(subp)
290
abspath = self.abspath(subp)
291
kind = osutils.file_kind(abspath)
292
if kind == "directory":
293
user_dirs.append(subp)
295
if action is not None:
300
self._index_add_entry(subp, file_id, kind)
303
return added, ignored
305
def _set_root_id(self, file_id):
306
self._fileid_map.set_file_id("", file_id)
308
@needs_tree_write_lock
309
def move(self, from_paths, to_dir=None, after=False):
311
to_abs = self.abspath(to_dir)
312
if not os.path.isdir(to_abs):
313
raise errors.BzrMoveFailedError('', to_dir,
314
errors.NotADirectory(to_abs))
316
for from_rel in from_paths:
317
from_tail = os.path.split(from_rel)[-1]
318
to_rel = os.path.join(to_dir, from_tail)
319
self.rename_one(from_rel, to_rel, after=after)
320
rename_tuples.append((from_rel, to_rel))
323
@needs_tree_write_lock
324
def rename_one(self, from_rel, to_rel, after=False):
326
os.rename(self.abspath(from_rel), self.abspath(to_rel))
327
from_path = from_rel.encode("utf-8")
328
to_path = to_rel.encode("utf-8")
329
if not self.has_filename(to_rel):
330
raise errors.BzrMoveFailedError(from_rel, to_rel,
331
errors.NoSuchFile(to_rel))
332
if not from_path in self.index:
333
raise errors.BzrMoveFailedError(from_rel, to_rel,
334
errors.NotVersionedError(path=from_rel))
335
self.index[to_path] = self.index[from_path]
336
del self.index[from_path]
338
def get_root_id(self):
339
return self.path2id("")
341
def _has_dir(self, path):
342
if self._versioned_dirs is None:
344
return path in self._versioned_dirs
347
def path2id(self, path):
348
encoded_path = path.encode("utf-8")
349
if self._is_versioned(encoded_path):
350
return self._fileid_map.lookup_file_id(encoded_path)
354
"""Yield all unversioned files in this WorkingTree.
356
present_files = set()
357
for (dirpath, dirnames, filenames) in os.walk(self.basedir):
358
dir_relpath = dirpath[len(self.basedir):].strip("/")
359
if self.bzrdir.is_control_filename(dir_relpath):
361
for filename in filenames:
362
relpath = os.path.join(dir_relpath, filename)
363
present_files.add(relpath)
364
return present_files - set(self.index)
367
# non-implementation specific cleanup
370
# reverse order of locking.
372
return self._control_files.unlock()
377
# TODO: Maybe this should only write on dirty ?
378
if self._control_files._lock_mode != 'w':
379
raise errors.NotWriteLocked(self)
383
for path in self.index:
384
yield self.path2id(path)
386
for path in self._versioned_dirs:
387
yield self.path2id(path)
389
def has_or_had_id(self, file_id):
390
if self.has_id(file_id):
392
if self.had_id(file_id):
396
def had_id(self, file_id):
397
path = self._basis_fileid_map.lookup_file_id(file_id)
399
head = self.repository._git.head()
401
# Assume no if basis is not accessible
405
root_tree = self.store[head].tree
407
tree_lookup_path(self.store.__getitem__, root_tree, path)
413
def has_id(self, file_id):
415
self.id2path(file_id)
416
except errors.NoSuchId:
421
def id2path(self, file_id):
422
if type(file_id) != str:
424
path = self._fileid_map.lookup_path(file_id)
425
# FIXME: What about directories?
426
if self._is_versioned(path):
427
return path.decode("utf-8")
428
raise errors.NoSuchId(self, file_id)
430
def get_file_mtime(self, file_id, path=None):
431
"""See Tree.get_file_mtime."""
433
path = self.id2path(file_id)
434
return os.lstat(self.abspath(path)).st_mtime
436
def get_ignore_list(self):
437
ignoreset = getattr(self, '_ignoreset', None)
438
if ignoreset is not None:
442
ignore_globs.update(ignores.get_runtime_ignores())
443
ignore_globs.update(ignores.get_user_ignores())
444
if self.has_filename(IGNORE_FILENAME):
445
f = self.get_file_byname(IGNORE_FILENAME)
447
# FIXME: Parse git file format, rather than assuming it's
448
# the same as for bzr's native formats.
449
ignore_globs.update(ignores.parse_ignore_file(f))
452
self._ignoreset = ignore_globs
455
def set_last_revision(self, revid):
456
self._change_last_revision(revid)
458
def _reset_data(self):
460
head = self.repository._git.head()
461
except KeyError, name:
462
raise errors.NotBranchError("branch %s at %s" % (name,
463
self.repository.base))
465
self._basis_fileid_map = GitFileIdMap({}, self.mapping)
467
self._basis_fileid_map = self.mapping.get_fileid_map(
468
self.store.__getitem__, self.store[head].tree)
471
def get_file_verifier(self, file_id, path=None, stat_value=None):
473
path = self.id2path(file_id)
474
return ("GIT", self.index[path][-2])
477
def get_file_sha1(self, file_id, path=None, stat_value=None):
479
path = self.id2path(file_id)
480
abspath = self.abspath(path).encode(osutils._fs_enc)
482
return osutils.sha_file_by_name(abspath)
483
except OSError, (num, msg):
484
if num in (errno.EISDIR, errno.ENOENT):
488
def revision_tree(self, revid):
489
return self.repository.revision_tree(revid)
491
def _is_versioned(self, path):
492
return (path in self.index or self._has_dir(path))
494
def filter_unversioned_files(self, files):
495
return set([p for p in files if self._is_versioned(p.encode("utf-8"))])
497
def _get_dir_ie(self, path, parent_id):
498
file_id = self.path2id(path)
499
return inventory.InventoryDirectory(file_id,
500
posixpath.basename(path).strip("/"), parent_id)
502
def _add_missing_parent_ids(self, path, dir_ids):
505
parent = posixpath.dirname(path).strip("/")
506
ret = self._add_missing_parent_ids(parent, dir_ids)
507
parent_id = dir_ids[parent]
508
ie = self._get_dir_ie(path, parent_id)
509
dir_ids[path] = ie.file_id
510
ret.append((path, ie))
513
def _get_file_ie(self, path, value, parent_id):
514
assert isinstance(path, unicode)
515
assert isinstance(value, tuple) and len(value) == 10
516
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
517
file_id = self.path2id(path)
518
if type(file_id) != str:
520
kind = mode_kind(mode)
521
ie = inventory.entry_factory[kind](file_id,
522
posixpath.basename(path), parent_id)
523
if kind == 'symlink':
524
ie.symlink_target = self.get_symlink_target(file_id)
526
data = self.get_file_text(file_id, path)
527
ie.text_sha1 = osutils.sha_string(data)
528
ie.text_size = len(data)
529
ie.executable = self.is_executable(file_id, path)
533
def _is_executable_from_path_and_stat_from_stat(self, path, stat_result):
534
mode = stat_result.st_mode
535
return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
537
def stored_kind(self, file_id, path=None):
539
path = self.id2path(file_id)
540
head = self.repository._git.head()
542
raise errors.NoSuchId(self, file_id)
543
root_tree = self.store[head].tree
544
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, root_tree, path)
545
return mode_kind(mode)
547
if not osutils.supports_executable():
548
def is_executable(self, file_id, path=None):
549
basis_tree = self.basis_tree()
550
if file_id in basis_tree:
551
return basis_tree.is_executable(file_id)
552
# Default to not executable
555
def is_executable(self, file_id, path=None):
557
path = self.id2path(file_id)
558
mode = os.lstat(self.abspath(path)).st_mode
559
return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
561
_is_executable_from_path_and_stat = \
562
_is_executable_from_path_and_stat_from_stat
564
def list_files(self, include_root=False, from_dir=None, recursive=True):
565
# FIXME: Yield non-versioned files
566
# FIXME: support from_dir
567
# FIXME: Support recursive
569
root_ie = self._get_dir_ie(u"", None)
570
if include_root and not from_dir:
571
yield "", "V", root_ie.kind, root_ie.file_id, root_ie
572
dir_ids[u""] = root_ie.file_id
573
for path, value in self.index.iteritems():
574
path = path.decode("utf-8")
575
parent = posixpath.dirname(path).strip("/")
576
for dir_path, dir_ie in self._add_missing_parent_ids(parent, dir_ids):
577
yield dir_path, "V", dir_ie.kind, dir_ie.file_id, dir_ie
578
ie = self._get_file_ie(path, value, dir_ids[parent])
579
yield path, "V", ie.kind, ie.file_id, ie
581
def all_file_ids(self):
582
ids = {u"": self.path2id("")}
583
for path in self.index:
584
path = path.decode("utf-8")
585
parent = posixpath.dirname(path).strip("/")
586
for e in self._add_missing_parent_ids(parent, ids):
588
ids[path] = self.path2id(path)
589
return set(ids.values())
591
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
592
# FIXME: Is return order correct?
594
raise NotImplementedError(self.iter_entries_by_dir)
595
if specific_file_ids is not None:
596
specific_paths = [self.id2path(file_id) for file_id in specific_file_ids]
597
if specific_paths in ([u""], []):
598
specific_paths = None
600
specific_paths = set(specific_paths)
602
specific_paths = None
603
root_ie = self._get_dir_ie(u"", None)
604
if specific_paths is None:
606
dir_ids = {u"": root_ie.file_id}
607
for path, value in self.index.iteritems():
608
path = path.decode("utf-8")
609
if specific_paths is not None and not path in specific_paths:
612
file_ie = self._get_file_ie(path, value, None)
615
parent = posixpath.dirname(path).strip("/")
616
for (dir_path, dir_ie) in self._add_missing_parent_ids(parent,
618
yield dir_path, dir_ie
619
file_ie.parent_id = self.path2id(parent)
625
return _mod_conflicts.ConflictList()
627
def update_basis_by_delta(self, new_revid, delta):
628
# The index just contains content, which won't have changed.
631
def _walkdirs(self, prefix=""):
634
per_dir = defaultdict(list)
635
for path, value in self.index.iteritems():
636
if not path.startswith(prefix):
638
(dirname, child_name) = posixpath.split(path)
639
dirname = dirname.decode("utf-8")
640
dir_file_id = self.path2id(dirname)
641
assert isinstance(value, tuple) and len(value) == 10
642
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
643
stat_result = posix.stat_result((mode, ino,
644
dev, 1, uid, gid, size,
646
per_dir[(dirname, dir_file_id)].append(
647
(path.decode("utf-8"), child_name.decode("utf-8"),
648
mode_kind(mode), stat_result,
649
self.path2id(path.decode("utf-8")),
651
return per_dir.iteritems()
654
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):
656
_tree_class = GitWorkingTree
658
supports_versioned_directories = False
661
def _matchingbzrdir(self):
662
from bzrlib.plugins.git.dir import LocalGitControlDirFormat
663
return LocalGitControlDirFormat()
665
def get_format_description(self):
666
return "Git Working Tree"
668
def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
669
accelerator_tree=None, hardlink=False):
670
"""See WorkingTreeFormat.initialize()."""
671
if not isinstance(a_bzrdir, LocalGitDir):
672
raise errors.IncompatibleFormat(self, a_bzrdir)
673
index = Index(a_bzrdir.root_transport.local_abspath(".git/index"))
675
return GitWorkingTree(a_bzrdir, a_bzrdir.open_repository(),
676
a_bzrdir.open_branch(), index)
679
class InterIndexGitTree(tree.InterTree):
680
"""InterTree that works between a Git revision tree and an index."""
682
def __init__(self, source, target):
683
super(InterIndexGitTree, self).__init__(source, target)
684
self._index = target.index
687
def is_compatible(cls, source, target):
688
from bzrlib.plugins.git.repository import GitRevisionTree
689
return (isinstance(source, GitRevisionTree) and
690
isinstance(target, GitWorkingTree))
692
def compare(self, want_unchanged=False, specific_files=None,
693
extra_trees=None, require_versioned=False, include_root=False,
694
want_unversioned=False):
695
changes = self._index.changes_from_tree(
696
self.source.store, self.source.tree,
697
want_unchanged=want_unchanged)
698
source_fileid_map = self.source.mapping.get_fileid_map(
699
self.source.store.__getitem__,
701
if self.target.mapping.BZR_FILE_IDS_FILE is not None:
702
file_id = self.target.path2id(
703
self.target.mapping.BZR_FILE_IDS_FILE)
705
target_fileid_map = {}
707
target_fileid_map = self.target.mapping.import_fileid_map(
708
Blob.from_string(self.target.get_file_text(file_id)))
710
target_fileid_map = {}
711
target_fileid_map = GitFileIdMap(target_fileid_map,
713
ret = tree_delta_from_git_changes(changes, self.target.mapping,
714
(source_fileid_map, target_fileid_map),
715
specific_file=specific_files, require_versioned=require_versioned)
717
for e in self.target.extras():
718
ret.unversioned.append((e, None,
719
osutils.file_kind(self.target.abspath(e))))
722
def iter_changes(self, include_unchanged=False, specific_files=None,
723
pb=None, extra_trees=[], require_versioned=True,
724
want_unversioned=False):
725
changes = self._index.changes_from_tree(
726
self.source.store, self.source.tree,
727
want_unchanged=include_unchanged)
728
# FIXME: Handle want_unversioned
729
return changes_from_git_changes(changes, self.target.mapping,
730
specific_file=specific_files)
733
tree.InterTree.register_optimiser(InterIndexGitTree)