454
454
def get_file(self, file_id, path=None):
456
file_id = osutils.safe_file_id(file_id)
457
456
path = self.id2path(file_id)
458
457
return self.get_file_byname(path)
460
459
def get_file_text(self, file_id):
461
file_id = osutils.safe_file_id(file_id)
462
460
return self.get_file(file_id).read()
464
462
def get_file_byname(self, filename):
475
473
incorrectly attributed to CURRENT_REVISION (but after committing, the
476
474
attribution will be correct).
478
file_id = osutils.safe_file_id(file_id)
479
476
basis = self.basis_tree()
480
477
basis.lock_read()
538
535
def _get_store_filename(self, file_id):
539
536
## XXX: badly named; this is not in the store at all
540
file_id = osutils.safe_file_id(file_id)
541
537
return self.abspath(self.id2path(file_id))
572
568
tree.set_parent_ids([revision_id])
574
570
def id2abspath(self, file_id):
575
file_id = osutils.safe_file_id(file_id)
576
571
return self.abspath(self.id2path(file_id))
578
573
def has_id(self, file_id):
579
574
# files that have been deleted are excluded
580
file_id = osutils.safe_file_id(file_id)
581
575
inv = self.inventory
582
576
if not inv.has_id(file_id):
585
579
return osutils.lexists(self.abspath(path))
587
581
def has_or_had_id(self, file_id):
588
file_id = osutils.safe_file_id(file_id)
589
582
if file_id == self.inventory.root.file_id:
591
584
return self.inventory.has_id(file_id)
593
586
__contains__ = has_id
595
588
def get_file_size(self, file_id):
596
file_id = osutils.safe_file_id(file_id)
597
589
return os.path.getsize(self.id2abspath(file_id))
600
592
def get_file_sha1(self, file_id, path=None, stat_value=None):
601
file_id = osutils.safe_file_id(file_id)
603
594
path = self._inventory.id2path(file_id)
604
595
return self._hashcache.get_sha1(path, stat_value)
606
597
def get_file_mtime(self, file_id, path=None):
607
file_id = osutils.safe_file_id(file_id)
609
599
path = self.inventory.id2path(file_id)
610
600
return os.lstat(self.abspath(path)).st_mtime
612
602
if not supports_executable():
613
603
def is_executable(self, file_id, path=None):
614
file_id = osutils.safe_file_id(file_id)
615
604
return self._inventory[file_id].executable
617
606
def is_executable(self, file_id, path=None):
619
file_id = osutils.safe_file_id(file_id)
620
608
path = self.id2path(file_id)
621
609
mode = os.lstat(self.abspath(path)).st_mode
622
610
return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
634
622
if file_id is None:
635
623
inv.add_path(f, kind=kind)
637
file_id = osutils.safe_file_id(file_id)
638
625
inv.add_path(f, kind=kind, file_id=file_id)
639
626
self._inventory_is_modified = True
703
690
self.set_parent_ids(parents, allow_leftmost_as_ghost=True)
692
def path_content_summary(self, path, _lstat=osutils.lstat,
693
_mapper=osutils.file_kind_from_stat_mode):
694
"""See Tree.path_content_summary."""
695
abspath = self.abspath(path)
697
stat_result = _lstat(abspath)
699
if getattr(e, 'errno', None) == errno.ENOENT:
701
return ('missing', None, None, None)
702
# propagate other errors
704
kind = _mapper(stat_result.st_mode)
706
size = stat_result.st_size
707
# try for a stat cache lookup
708
if not supports_executable():
709
executable = None # caller can decide policy.
711
mode = stat_result.st_mode
712
executable = bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
713
return (kind, size, executable, self._sha_from_stat(
715
elif kind == 'directory':
716
# perhaps it looks like a plain directory, but it's really a
718
if self._directory_is_tree_reference(path):
719
kind = 'tree-reference'
720
return kind, None, None, None
721
elif kind == 'symlink':
722
return ('symlink', None, None, os.readlink(abspath))
724
return (kind, None, None, None)
705
726
@deprecated_method(zero_eleven)
707
728
def pending_merges(self):
744
765
:param revision_ids: The revision_ids to set as the parent ids of this
745
766
working tree. Any of these may be ghosts.
747
revision_ids = [osutils.safe_revision_id(r) for r in revision_ids]
748
768
self._check_parents_for_ghosts(revision_ids,
749
769
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
750
770
for revision_id in revision_ids:
760
780
@needs_tree_write_lock
761
781
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
762
782
"""See MutableTree.set_parent_trees."""
763
parent_ids = [osutils.safe_revision_id(rev) for (rev, tree) in parents_list]
783
parent_ids = [rev for (rev, tree) in parents_list]
764
784
for revision_id in parent_ids:
765
785
_mod_revision.check_not_reserved_id(revision_id)
799
819
yield Stanza(file_id=file_id.decode('utf8'), hash=hash)
800
820
self._put_rio('merge-hashes', iter_stanzas(), MERGE_MODIFIED_HEADER_1)
822
def _sha_from_stat(self, path, stat_result):
823
"""Get a sha digest from the tree's stat cache.
825
The default implementation assumes no stat cache is present.
827
:param path: The path.
828
:param stat_result: The stat result being looked up.
802
832
def _put_rio(self, filename, stanzas, header):
803
833
self._must_be_locked()
804
834
my_file = rio_file(stanzas, header)
826
856
merger.check_basis(check_clean=True, require_commits=False)
827
857
if to_revision is None:
828
858
to_revision = _mod_revision.ensure_null(branch.last_revision())
830
to_revision = osutils.safe_revision_id(to_revision)
831
859
merger.other_rev_id = to_revision
832
860
if _mod_revision.is_null(merger.other_rev_id):
833
861
raise errors.NoCommits(branch)
943
970
other_tree.unlock()
944
971
other_tree.bzrdir.retire_bzrdir()
973
def _directory_is_tree_reference(self, relpath):
974
# as a special case, if a directory contains control files then
975
# it's a tree reference, except that the root of the tree is not
976
return relpath and osutils.isdir(self.abspath(relpath) + u"/.bzr")
977
# TODO: We could ask all the control formats whether they
978
# recognize this directory, but at the moment there's no cheap api
979
# to do that. Since we probably can only nest bzr checkouts and
980
# they always use this name it's ok for now. -- mbp 20060306
982
# FIXME: There is an unhandled case here of a subdirectory
983
# containing .bzr but not a branch; that will probably blow up
984
# when you try to commit it. It might happen if there is a
985
# checkout in a subdirectory. This can be avoided by not adding
946
988
@needs_tree_write_lock
947
989
def extract(self, file_id, format=None):
948
990
"""Extract a subtree from this tree.
997
1039
def _serialize(self, inventory, out_file):
998
xml5.serializer_v5.write_inventory(self._inventory, out_file)
1040
xml5.serializer_v5.write_inventory(self._inventory, out_file,
1000
1043
def _deserialize(selt, in_file):
1001
1044
return xml5.serializer_v5.read_inventory(in_file)
1463
1505
@needs_write_lock
1464
1506
def pull(self, source, overwrite=False, stop_revision=None,
1465
change_reporter=None):
1507
change_reporter=None, possible_transports=None):
1466
1508
top_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1467
1509
source.lock_read()
1470
1512
pp.next_phase()
1471
1513
old_revision_info = self.branch.last_revision_info()
1472
1514
basis_tree = self.basis_tree()
1473
count = self.branch.pull(source, overwrite, stop_revision)
1515
count = self.branch.pull(source, overwrite, stop_revision,
1516
possible_transports=possible_transports)
1474
1517
new_revision_info = self.branch.last_revision_info()
1475
1518
if new_revision_info != old_revision_info:
1476
1519
pp.next_phase()
1514
1557
@needs_write_lock
1515
1558
def put_file_bytes_non_atomic(self, file_id, bytes):
1516
1559
"""See MutableTree.put_file_bytes_non_atomic."""
1517
file_id = osutils.safe_file_id(file_id)
1518
1560
stream = file(self.id2abspath(file_id), 'wb')
1520
1562
stream.write(bytes)
1699
1741
@needs_tree_write_lock
1700
1742
def set_last_revision(self, new_revision):
1701
1743
"""Change the last revision in the working tree."""
1702
new_revision = osutils.safe_revision_id(new_revision)
1703
1744
if self._change_last_revision(new_revision):
1704
1745
self._cache_basis_inventory(new_revision)
1729
1770
def _create_basis_xml_from_inventory(self, revision_id, inventory):
1730
1771
"""Create the text that will be saved in basis-inventory"""
1731
# TODO: jam 20070209 This should be redundant, as the revision_id
1732
# as all callers should have already converted the revision_id to
1734
inventory.revision_id = osutils.safe_revision_id(revision_id)
1772
inventory.revision_id = revision_id
1735
1773
return xml7.serializer_v7.write_inventory_to_string(inventory)
1737
1775
def _cache_basis_inventory(self, new_revision):
1903
1941
self.apply_inventory_delta(inv_delta)
1905
1943
@needs_tree_write_lock
1906
def revert(self, filenames, old_tree=None, backups=True,
1944
def revert(self, filenames=None, old_tree=None, backups=True,
1907
1945
pb=DummyProgress(), report_changes=False):
1908
1946
from bzrlib.conflicts import resolve
1949
symbol_versioning.warn('Using [] to revert all files is deprecated'
1950
' as of bzr 0.91. Please use None (the default) instead.',
1951
DeprecationWarning, stacklevel=2)
1909
1952
if old_tree is None:
1910
1953
old_tree = self.basis_tree()
1911
1954
conflicts = transform.revert(self, old_tree, filenames, backups, pb,
1912
1955
report_changes)
1913
if not len(filenames):
1956
if filenames is None:
1914
1957
self.set_parent_ids(self.get_parent_ids()[:1])
2018
2061
raise NotImplementedError(self.unlock)
2020
def update(self, change_reporter=None):
2063
def update(self, change_reporter=None, possible_transports=None):
2021
2064
"""Update a working tree along its branch.
2023
2066
This will update the branch if its bound too, which means we have
2453
2496
def _last_revision(self):
2454
2497
"""See Mutable.last_revision."""
2456
return osutils.safe_revision_id(
2457
self._control_files.get('last-revision').read())
2499
return self._control_files.get('last-revision').read()
2458
2500
except errors.NoSuchFile:
2459
2501
return _mod_revision.NULL_REVISION
2636
2678
sio = StringIO()
2637
2679
inv = Inventory()
2638
xml5.serializer_v5.write_inventory(inv, sio)
2680
xml5.serializer_v5.write_inventory(inv, sio, working=True)
2640
2682
control_files.put('inventory', sio)
2749
2789
branch = a_bzrdir.open_branch()
2750
2790
if revision_id is None:
2751
2791
revision_id = _mod_revision.ensure_null(branch.last_revision())
2753
revision_id = osutils.safe_revision_id(revision_id)
2754
2792
# WorkingTree3 can handle an inventory which has a unique root id.
2755
2793
# as of bzr 0.12. However, bzr 0.11 and earlier fail to handle
2756
2794
# those trees. And because there isn't a format bump inbetween, we