/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/workingtree.py

  • Committer: Daniel Watkins
  • Date: 2007-11-06 09:33:05 UTC
  • mfrom: (2967 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2993.
  • Revision ID: d.m.watkins@warwick.ac.uk-20071106093305-zfef3c0jbcvunnuz
Merged bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
    hashcache,
65
65
    ignores,
66
66
    merge,
67
 
    osutils,
68
67
    revision as _mod_revision,
69
68
    revisiontree,
70
69
    repository,
90
89
from bzrlib.lockdir import LockDir
91
90
import bzrlib.mutabletree
92
91
from bzrlib.mutabletree import needs_tree_write_lock
 
92
from bzrlib import osutils
93
93
from bzrlib.osutils import (
94
94
    compact_date,
95
95
    file_kind,
453
453
 
454
454
    def get_file(self, file_id, path=None):
455
455
        if path is 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)
459
458
 
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()
463
461
 
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).
477
475
        """
478
 
        file_id = osutils.safe_file_id(file_id)
479
476
        basis = self.basis_tree()
480
477
        basis.lock_read()
481
478
        try:
526
523
            pass
527
524
        else:
528
525
            for l in merges_file.readlines():
529
 
                revision_id = osutils.safe_revision_id(l.rstrip('\n'))
 
526
                revision_id = l.rstrip('\n')
530
527
                parents.append(revision_id)
531
528
        return parents
532
529
 
537
534
        
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))
542
538
 
543
539
    @needs_read_lock
572
568
            tree.set_parent_ids([revision_id])
573
569
 
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))
577
572
 
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):
583
577
            return False
585
579
        return osutils.lexists(self.abspath(path))
586
580
 
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:
590
583
            return True
591
584
        return self.inventory.has_id(file_id)
593
586
    __contains__ = has_id
594
587
 
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))
598
590
 
599
591
    @needs_read_lock
600
592
    def get_file_sha1(self, file_id, path=None, stat_value=None):
601
 
        file_id = osutils.safe_file_id(file_id)
602
593
        if not path:
603
594
            path = self._inventory.id2path(file_id)
604
595
        return self._hashcache.get_sha1(path, stat_value)
605
596
 
606
597
    def get_file_mtime(self, file_id, path=None):
607
 
        file_id = osutils.safe_file_id(file_id)
608
598
        if not path:
609
599
            path = self.inventory.id2path(file_id)
610
600
        return os.lstat(self.abspath(path)).st_mtime
611
601
 
 
602
    def _is_executable_from_path_and_stat_from_basis(self, path, stat_result):
 
603
        file_id = self.path2id(path)
 
604
        return self._inventory[file_id].executable
 
605
 
 
606
    def _is_executable_from_path_and_stat_from_stat(self, path, stat_result):
 
607
        mode = stat_result.st_mode
 
608
        return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
 
609
 
612
610
    if not supports_executable():
613
611
        def is_executable(self, file_id, path=None):
614
 
            file_id = osutils.safe_file_id(file_id)
615
612
            return self._inventory[file_id].executable
 
613
 
 
614
        _is_executable_from_path_and_stat = \
 
615
            _is_executable_from_path_and_stat_from_basis
616
616
    else:
617
617
        def is_executable(self, file_id, path=None):
618
618
            if not path:
619
 
                file_id = osutils.safe_file_id(file_id)
620
619
                path = self.id2path(file_id)
621
620
            mode = os.lstat(self.abspath(path)).st_mode
622
621
            return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
623
622
 
 
623
        _is_executable_from_path_and_stat = \
 
624
            _is_executable_from_path_and_stat_from_stat
 
625
 
624
626
    @needs_tree_write_lock
625
627
    def _add(self, files, ids, kinds):
626
628
        """See MutableTree._add."""
634
636
            if file_id is None:
635
637
                inv.add_path(f, kind=kind)
636
638
            else:
637
 
                file_id = osutils.safe_file_id(file_id)
638
639
                inv.add_path(f, kind=kind, file_id=file_id)
639
640
            self._inventory_is_modified = True
640
641
 
702
703
        if updated:
703
704
            self.set_parent_ids(parents, allow_leftmost_as_ghost=True)
704
705
 
 
706
    def path_content_summary(self, path, _lstat=os.lstat,
 
707
        _mapper=osutils.file_kind_from_stat_mode):
 
708
        """See Tree.path_content_summary."""
 
709
        abspath = self.abspath(path)
 
710
        try:
 
711
            stat_result = _lstat(abspath)
 
712
        except OSError, e:
 
713
            if getattr(e, 'errno', None) == errno.ENOENT:
 
714
                # no file.
 
715
                return ('missing', None, None, None)
 
716
            # propagate other errors
 
717
            raise
 
718
        kind = _mapper(stat_result.st_mode)
 
719
        if kind == 'file':
 
720
            size = stat_result.st_size
 
721
            # try for a stat cache lookup
 
722
            executable = self._is_executable_from_path_and_stat(path, stat_result)
 
723
            return (kind, size, executable, self._sha_from_stat(
 
724
                path, stat_result))
 
725
        elif kind == 'directory':
 
726
            # perhaps it looks like a plain directory, but it's really a
 
727
            # reference.
 
728
            if self._directory_is_tree_reference(path):
 
729
                kind = 'tree-reference'
 
730
            return kind, None, None, None
 
731
        elif kind == 'symlink':
 
732
            return ('symlink', None, None, os.readlink(abspath))
 
733
        else:
 
734
            return (kind, None, None, None)
 
735
 
705
736
    @deprecated_method(zero_eleven)
706
737
    @needs_read_lock
707
738
    def pending_merges(self):
744
775
        :param revision_ids: The revision_ids to set as the parent ids of this
745
776
            working tree. Any of these may be ghosts.
746
777
        """
747
 
        revision_ids = [osutils.safe_revision_id(r) for r in revision_ids]
748
778
        self._check_parents_for_ghosts(revision_ids,
749
779
            allow_leftmost_as_ghost=allow_leftmost_as_ghost)
750
780
        for revision_id in revision_ids:
760
790
    @needs_tree_write_lock
761
791
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
762
792
        """See MutableTree.set_parent_trees."""
763
 
        parent_ids = [osutils.safe_revision_id(rev) for (rev, tree) in parents_list]
 
793
        parent_ids = [rev for (rev, tree) in parents_list]
764
794
        for revision_id in parent_ids:
765
795
            _mod_revision.check_not_reserved_id(revision_id)
766
796
 
799
829
                yield Stanza(file_id=file_id.decode('utf8'), hash=hash)
800
830
        self._put_rio('merge-hashes', iter_stanzas(), MERGE_MODIFIED_HEADER_1)
801
831
 
 
832
    def _sha_from_stat(self, path, stat_result):
 
833
        """Get a sha digest from the tree's stat cache.
 
834
 
 
835
        The default implementation assumes no stat cache is present.
 
836
 
 
837
        :param path: The path.
 
838
        :param stat_result: The stat result being looked up.
 
839
        """
 
840
        return None
 
841
 
802
842
    def _put_rio(self, filename, stanzas, header):
803
843
        self._must_be_locked()
804
844
        my_file = rio_file(stanzas, header)
826
866
            merger.check_basis(check_clean=True, require_commits=False)
827
867
            if to_revision is None:
828
868
                to_revision = _mod_revision.ensure_null(branch.last_revision())
829
 
            else:
830
 
                to_revision = osutils.safe_revision_id(to_revision)
831
869
            merger.other_rev_id = to_revision
832
870
            if _mod_revision.is_null(merger.other_rev_id):
833
871
                raise errors.NoCommits(branch)
898
936
        return file_id
899
937
 
900
938
    def get_symlink_target(self, file_id):
901
 
        file_id = osutils.safe_file_id(file_id)
902
939
        return os.readlink(self.id2abspath(file_id))
903
940
 
904
941
    @needs_write_lock
943
980
            other_tree.unlock()
944
981
        other_tree.bzrdir.retire_bzrdir()
945
982
 
 
983
    def _directory_is_tree_reference(self, relpath):
 
984
        # as a special case, if a directory contains control files then 
 
985
        # it's a tree reference, except that the root of the tree is not
 
986
        return relpath and osutils.isdir(self.abspath(relpath) + u"/.bzr")
 
987
        # TODO: We could ask all the control formats whether they
 
988
        # recognize this directory, but at the moment there's no cheap api
 
989
        # to do that.  Since we probably can only nest bzr checkouts and
 
990
        # they always use this name it's ok for now.  -- mbp 20060306
 
991
        #
 
992
        # FIXME: There is an unhandled case here of a subdirectory
 
993
        # containing .bzr but not a branch; that will probably blow up
 
994
        # when you try to commit it.  It might happen if there is a
 
995
        # checkout in a subdirectory.  This can be avoided by not adding
 
996
        # it.  mbp 20070306
 
997
 
946
998
    @needs_tree_write_lock
947
999
    def extract(self, file_id, format=None):
948
1000
        """Extract a subtree from this tree.
1428
1480
        :raises: NoSuchId if any fileid is not currently versioned.
1429
1481
        """
1430
1482
        for file_id in file_ids:
1431
 
            file_id = osutils.safe_file_id(file_id)
1432
1483
            if self._inventory.has_id(file_id):
1433
1484
                self._inventory.remove_recursive_id(file_id)
1434
1485
            else:
1463
1514
 
1464
1515
    @needs_write_lock
1465
1516
    def pull(self, source, overwrite=False, stop_revision=None,
1466
 
             change_reporter=None):
 
1517
             change_reporter=None, possible_transports=None):
1467
1518
        top_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1468
1519
        source.lock_read()
1469
1520
        try:
1471
1522
            pp.next_phase()
1472
1523
            old_revision_info = self.branch.last_revision_info()
1473
1524
            basis_tree = self.basis_tree()
1474
 
            count = self.branch.pull(source, overwrite, stop_revision)
 
1525
            count = self.branch.pull(source, overwrite, stop_revision,
 
1526
                                     possible_transports=possible_transports)
1475
1527
            new_revision_info = self.branch.last_revision_info()
1476
1528
            if new_revision_info != old_revision_info:
1477
1529
                pp.next_phase()
1515
1567
    @needs_write_lock
1516
1568
    def put_file_bytes_non_atomic(self, file_id, bytes):
1517
1569
        """See MutableTree.put_file_bytes_non_atomic."""
1518
 
        file_id = osutils.safe_file_id(file_id)
1519
1570
        stream = file(self.id2abspath(file_id), 'wb')
1520
1571
        try:
1521
1572
            stream.write(bytes)
1700
1751
    @needs_tree_write_lock
1701
1752
    def set_last_revision(self, new_revision):
1702
1753
        """Change the last revision in the working tree."""
1703
 
        new_revision = osutils.safe_revision_id(new_revision)
1704
1754
        if self._change_last_revision(new_revision):
1705
1755
            self._cache_basis_inventory(new_revision)
1706
1756
 
1729
1779
 
1730
1780
    def _create_basis_xml_from_inventory(self, revision_id, inventory):
1731
1781
        """Create the text that will be saved in basis-inventory"""
1732
 
        # TODO: jam 20070209 This should be redundant, as the revision_id
1733
 
        #       as all callers should have already converted the revision_id to
1734
 
        #       utf8
1735
 
        inventory.revision_id = osutils.safe_revision_id(revision_id)
 
1782
        inventory.revision_id = revision_id
1736
1783
        return xml7.serializer_v7.write_inventory_to_string(inventory)
1737
1784
 
1738
1785
    def _cache_basis_inventory(self, new_revision):
1913
1960
                ' as of bzr 0.91.  Please use None (the default) instead.',
1914
1961
                DeprecationWarning, stacklevel=2)
1915
1962
        if old_tree is None:
1916
 
            old_tree = self.basis_tree()
1917
 
        conflicts = transform.revert(self, old_tree, filenames, backups, pb,
1918
 
                                     report_changes)
1919
 
        if filenames is None:
1920
 
            self.set_parent_ids(self.get_parent_ids()[:1])
1921
 
            resolve(self)
 
1963
            basis_tree = self.basis_tree()
 
1964
            basis_tree.lock_read()
 
1965
            old_tree = basis_tree
1922
1966
        else:
1923
 
            resolve(self, filenames, ignore_misses=True)
 
1967
            basis_tree = None
 
1968
        try:
 
1969
            conflicts = transform.revert(self, old_tree, filenames, backups, pb,
 
1970
                                         report_changes)
 
1971
            if filenames is None and len(self.get_parent_ids()) > 1:
 
1972
                parent_trees = []
 
1973
                last_revision = self.last_revision()
 
1974
                if last_revision != NULL_REVISION:
 
1975
                    if basis_tree is None:
 
1976
                        basis_tree = self.basis_tree()
 
1977
                        basis_tree.lock_read()
 
1978
                    parent_trees.append((last_revision, basis_tree))
 
1979
                self.set_parent_trees(parent_trees)
 
1980
                resolve(self)
 
1981
            else:
 
1982
                resolve(self, filenames, ignore_misses=True)
 
1983
        finally:
 
1984
            if basis_tree is not None:
 
1985
                basis_tree.unlock()
1924
1986
        return conflicts
1925
1987
 
1926
1988
    def revision_tree(self, revision_id):
2459
2521
    def _last_revision(self):
2460
2522
        """See Mutable.last_revision."""
2461
2523
        try:
2462
 
            return osutils.safe_revision_id(
2463
 
                        self._control_files.get('last-revision').read())
 
2524
            return self._control_files.get('last-revision').read()
2464
2525
        except errors.NoSuchFile:
2465
2526
            return _mod_revision.NULL_REVISION
2466
2527
 
2655
2716
        branch = a_bzrdir.open_branch()
2656
2717
        if revision_id is None:
2657
2718
            revision_id = _mod_revision.ensure_null(branch.last_revision())
2658
 
        else:
2659
 
            revision_id = osutils.safe_revision_id(revision_id)
2660
2719
        branch.lock_write()
2661
2720
        try:
2662
2721
            branch.generate_revision_history(revision_id)
2755
2814
        branch = a_bzrdir.open_branch()
2756
2815
        if revision_id is None:
2757
2816
            revision_id = _mod_revision.ensure_null(branch.last_revision())
2758
 
        else:
2759
 
            revision_id = osutils.safe_revision_id(revision_id)
2760
2817
        # WorkingTree3 can handle an inventory which has a unique root id.
2761
2818
        # as of bzr 0.12. However, bzr 0.11 and earlier fail to handle
2762
2819
        # those trees. And because there isn't a format bump inbetween, we