83
97
transport.get_transport(self.controldir), 'lock', lockdir.LockDir)
84
98
self._format = GitWorkingTreeFormat()
100
self._versioned_dirs = None
86
101
self.views = self._make_views()
102
self._rules_searcher = None
87
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)
90
354
"""Yield all unversioned files in this WorkingTree.
356
present_files = set()
92
357
for (dirpath, dirnames, filenames) in os.walk(self.basedir):
93
if self.bzrdir.is_control_filename(dirpath[len(self.basedir):].strip("/")):
358
dir_relpath = dirpath[len(self.basedir):].strip("/")
359
if self.bzrdir.is_control_filename(dir_relpath):
95
361
for filename in filenames:
96
relpath = os.path.join(dirpath[len(self.basedir):].strip("/"), filename)
97
if not relpath in self.index:
362
relpath = os.path.join(dir_relpath, filename)
363
present_files.add(relpath)
364
return present_files - set(self.index)
101
366
def unlock(self):
102
367
# non-implementation specific cleanup
109
374
self.branch.unlock()
111
def is_control_filename(self, path):
112
return os.path.basename(path) == ".git"
114
def _rewrite_index(self):
116
for path, entry in self._inventory.iter_entries():
117
if entry.kind == "directory":
118
# Git indexes don't contain directories
120
if entry.kind == "file":
123
file, stat_val = self.get_file_with_stat(entry.file_id, path)
124
except (errors.NoSuchFile, IOError):
125
# TODO: Rather than come up with something here, use the old index
127
from posix import stat_result
128
stat_val = stat_result((stat.S_IFREG | 0644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
129
blob.set_raw_string(file.read())
130
elif entry.kind == "symlink":
133
stat_val = os.lstat(self.abspath(path))
134
except (errors.NoSuchFile, OSError):
135
# TODO: Rather than come up with something here, use the
137
from posix import stat_result
138
stat_val = stat_result((stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
139
blob.set_raw_string(entry.symlink_target)
141
raise AssertionError("unknown kind '%s'" % entry.kind)
142
# Add object to the repository if it didn't exist yet
143
if not blob.id in self.repository._git.object_store:
144
self.repository._git.object_store.add_object(blob)
145
# Add an entry to the index or update the existing entry
147
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)
150
377
# TODO: Maybe this should only write on dirty ?
151
378
if self._control_files._lock_mode != 'w':
152
379
raise errors.NotWriteLocked(self)
153
self._rewrite_index()
154
380
self.index.write()
155
self._inventory_is_modified = False
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
157
436
def get_ignore_list(self):
158
437
ignoreset = getattr(self, '_ignoreset', None)
205
488
def revision_tree(self, revid):
206
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)
209
623
def conflicts(self):
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()
214
654
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):
656
_tree_class = GitWorkingTree
658
supports_versioned_directories = False
217
661
def _matchingbzrdir(self):
218
from bzrlib.plugins.git import LocalGitControlDirFormat
662
from bzrlib.plugins.git.dir import LocalGitControlDirFormat
219
663
return LocalGitControlDirFormat()
221
665
def get_format_description(self):
222
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)
225
679
class InterIndexGitTree(tree.InterTree):
226
680
"""InterTree that works between a Git revision tree and an index."""
250
704
if file_id is None:
251
705
target_fileid_map = {}
253
target_fileid_map = self.target.mapping.import_fileid_map(Blob.from_string(self.target.get_file_text(file_id)))
707
target_fileid_map = self.target.mapping.import_fileid_map(
708
Blob.from_string(self.target.get_file_text(file_id)))
255
710
target_fileid_map = {}
256
target_fileid_map = GitFileIdMap(target_fileid_map, self.target.mapping)
711
target_fileid_map = GitFileIdMap(target_fileid_map,
257
713
ret = tree_delta_from_git_changes(changes, self.target.mapping,
258
714
(source_fileid_map, target_fileid_map),
259
715
specific_file=specific_files, require_versioned=require_versioned)
260
716
if want_unversioned:
261
717
for e in self.target.extras():
262
ret.unversioned.append((e, None, osutils.file_kind(self.target.abspath(e))))
718
ret.unversioned.append((e, None,
719
osutils.file_kind(self.target.abspath(e))))
265
722
def iter_changes(self, include_unchanged=False, specific_files=None,
266
pb=None, extra_trees=[], require_versioned=True, want_unversioned=False):
723
pb=None, extra_trees=[], require_versioned=True,
724
want_unversioned=False):
267
725
changes = self._index.changes_from_tree(
268
self.source._repository._git.object_store, self.source.tree,
726
self.source.store, self.source.tree,
269
727
want_unchanged=include_unchanged)
270
728
# FIXME: Handle want_unversioned
271
return changes_from_git_changes(changes, self.target.mapping,
729
return changes_from_git_changes(changes, self.target.mapping,
272
730
specific_file=specific_files)
274
733
tree.InterTree.register_optimiser(InterIndexGitTree)