91
93
from bzrlib.progress import DummyProgress, ProgressPhase
92
94
from bzrlib.revision import NULL_REVISION
95
import bzrlib.revisiontree
93
96
from bzrlib.rio import RioReader, rio_file, Stanza
94
97
from bzrlib.symbol_versioning import (deprecated_passed,
96
99
deprecated_function,
97
100
DEPRECATED_PARAMETER,
100
104
from bzrlib.trace import mutter, note
101
105
from bzrlib.transform import build_tree
102
106
from bzrlib.transport import get_transport
103
107
from bzrlib.transport.local import LocalTransport
104
108
from bzrlib.textui import show_status
107
110
import bzrlib.xml5
324
327
def _set_inventory(self, inv):
325
328
assert inv.root is not None
326
329
self._inventory = inv
327
self.path2id = self._inventory.path2id
329
def is_control_filename(self, filename):
330
"""True if filename is the name of a control file in this tree.
332
:param filename: A filename within the tree. This is a relative path
333
from the root of this tree.
335
This is true IF and ONLY IF the filename is part of the meta data
336
that bzr controls in this tree. I.E. a random .bzr directory placed
337
on disk will not be a control file for this tree.
339
return self.bzrdir.is_control_filename(filename)
342
332
def open(path=None, _unsupported=False):
399
389
If the left most parent is a ghost then the returned tree will be an
400
390
empty tree - one obtained by calling repository.revision_tree(None).
402
revision_id = self.last_revision()
403
if revision_id is not None:
393
revision_id = self.get_parent_ids()[0]
395
# no parents, return an empty revision tree.
396
# in the future this should return the tree for
397
# 'empty:' - the implicit root empty tree.
398
return self.branch.repository.revision_tree(None)
405
401
xml = self.read_basis_inventory()
406
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(xml)
407
inv.root.revision = revision_id
410
if inv is not None and inv.revision_id == revision_id:
411
return bzrlib.tree.RevisionTree(self.branch.repository, inv,
413
# FIXME? RBC 20060403 should we cache the inventory here ?
402
inv = bzrlib.xml6.serializer_v6.read_inventory_from_string(xml)
403
if inv is not None and inv.revision_id == revision_id:
404
return bzrlib.tree.RevisionTree(self.branch.repository,
406
except (NoSuchFile, errors.BadInventoryFormat):
408
# No cached copy available, retrieve from the repository.
409
# FIXME? RBC 20060403 should we cache the inventory locally
415
412
return self.branch.repository.revision_tree(revision_id)
416
413
except errors.RevisionNotPresent:
543
540
transform_tree(tree, self)
544
541
tree.set_parent_ids([revision_id])
547
def commit(self, message=None, revprops=None, *args, **kwargs):
548
# avoid circular imports
549
from bzrlib.commit import Commit
552
if not 'branch-nick' in revprops:
553
revprops['branch-nick'] = self.branch.nick
554
# args for wt.commit start at message from the Commit.commit method,
555
# but with branch a kwarg now, passing in args as is results in the
556
#message being used for the branch
557
args = (DEPRECATED_PARAMETER, message, ) + args
558
committed_id = Commit().commit( working_tree=self, revprops=revprops,
560
self._set_inventory(self.read_working_inventory())
563
543
def id2abspath(self, file_id):
564
544
return self.abspath(self.id2path(file_id))
603
583
return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
605
585
@needs_write_lock
606
def add(self, files, ids=None):
607
"""Make files versioned.
609
Note that the command line normally calls smart_add instead,
610
which can automatically recurse.
612
This adds the files to the inventory, so that they will be
613
recorded by the next commit.
616
List of paths to add, relative to the base of the tree.
619
If set, use these instead of automatically generated ids.
620
Must be the same length as the list of files, but may
621
contain None for ids that are to be autogenerated.
623
TODO: Perhaps have an option to add the ids even if the files do
626
TODO: Perhaps callback with the ids and paths as they're added.
586
def _add(self, files, ids, kinds):
587
"""See MutableTree._add."""
628
588
# TODO: Re-adding a file that is removed in the working copy
629
589
# should probably put it back with the previous ID.
630
if isinstance(files, basestring):
631
assert(ids is None or isinstance(ids, basestring))
637
ids = [None] * len(files)
639
assert(len(ids) == len(files))
590
# the read and write working inventory should not occur in this
591
# function - they should be part of lock_write and unlock.
641
592
inv = self.read_working_inventory()
642
for f,file_id in zip(files, ids):
643
if self.is_control_filename(f):
644
raise errors.ForbiddenControlFileError(filename=f)
649
raise BzrError("cannot add top-level %r" % f)
651
fullpath = normpath(self.abspath(f))
653
kind = file_kind(fullpath)
655
if e.errno == errno.ENOENT:
656
raise NoSuchFile(fullpath)
657
if not InventoryEntry.versionable_kind(kind):
658
raise errors.BadFileKindError(filename=f, kind=kind)
593
for f, file_id, kind in zip(files, ids, kinds):
594
assert kind is not None
659
595
if file_id is None:
660
596
inv.add_path(f, kind=kind)
662
598
inv.add_path(f, kind=kind, file_id=file_id)
664
599
self._write_inventory(inv)
601
@needs_tree_write_lock
602
def _gather_kinds(self, files, kinds):
603
"""See MutableTree._gather_kinds."""
604
for pos, f in enumerate(files):
605
if kinds[pos] is None:
606
fullpath = normpath(self.abspath(f))
608
kinds[pos] = file_kind(fullpath)
610
if e.errno == errno.ENOENT:
611
raise NoSuchFile(fullpath)
666
613
@needs_write_lock
667
614
def add_parent_tree_id(self, revision_id, allow_leftmost_as_ghost=False):
668
615
"""Add revision_id as a parent.
716
663
self.set_parent_ids(parents, allow_leftmost_as_ghost=True)
665
@deprecated_method(zero_eleven)
719
667
def pending_merges(self):
720
668
"""Return a list of pending merges.
722
670
These are revisions that have been merged into the working
723
671
directory but not yet committed.
673
As of 0.11 this is deprecated. Please see WorkingTree.get_parent_ids()
674
instead - which is available on all tree objects.
725
676
return self.get_parent_ids()[1:]
678
def _check_parents_for_ghosts(self, revision_ids, allow_leftmost_as_ghost):
679
"""Common ghost checking functionality from set_parent_*.
681
This checks that the left hand-parent exists if there are any
684
if len(revision_ids) > 0:
685
leftmost_id = revision_ids[0]
686
if (not allow_leftmost_as_ghost and not
687
self.branch.repository.has_revision(leftmost_id)):
688
raise errors.GhostRevisionUnusableHere(leftmost_id)
690
def _set_merges_from_parent_ids(self, parent_ids):
691
merges = parent_ids[1:]
692
self._control_files.put_utf8('pending-merges', '\n'.join(merges))
694
@needs_tree_write_lock
728
695
def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
729
696
"""Set the parent ids to revision_ids.
737
704
:param revision_ids: The revision_ids to set as the parent ids of this
738
705
working tree. Any of these may be ghosts.
707
self._check_parents_for_ghosts(revision_ids,
708
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
740
710
if len(revision_ids) > 0:
741
leftmost_id = revision_ids[0]
742
if (not allow_leftmost_as_ghost and not
743
self.branch.repository.has_revision(leftmost_id)):
744
raise errors.GhostRevisionUnusableHere(leftmost_id)
745
self.set_last_revision(leftmost_id)
711
self.set_last_revision(revision_ids[0])
747
713
self.set_last_revision(None)
748
merges = revision_ids[1:]
749
self._control_files.put_utf8('pending-merges', '\n'.join(merges))
715
self._set_merges_from_parent_ids(revision_ids)
717
@needs_tree_write_lock
752
718
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
753
"""Set the parents of the working tree.
719
"""See MutableTree.set_parent_trees."""
720
parent_ids = [rev for (rev, tree) in parents_list]
755
:param parents_list: A list of (revision_id, tree) tuples.
756
If tree is None, then that element is treated as an unreachable
757
parent tree - i.e. a ghost.
759
# parent trees are not used in current format trees, delegate to
761
self.set_parent_ids([rev for (rev, tree) in parents_list],
722
self._check_parents_for_ghosts(parent_ids,
762
723
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
725
if len(parent_ids) == 0:
726
leftmost_parent_id = None
727
leftmost_parent_tree = None
729
leftmost_parent_id, leftmost_parent_tree = parents_list[0]
731
if self._change_last_revision(leftmost_parent_id):
732
if leftmost_parent_tree is None:
733
# If we don't have a tree, fall back to reading the
734
# parent tree from the repository.
735
self._cache_basis_inventory(leftmost_parent_id)
737
inv = leftmost_parent_tree.inventory
738
xml = self._create_basis_xml_from_inventory(
739
leftmost_parent_id, inv)
740
self._write_basis_inventory(xml)
741
self._set_merges_from_parent_ids(parent_ids)
743
@needs_tree_write_lock
765
744
def set_pending_merges(self, rev_list):
766
745
parents = self.get_parent_ids()
767
746
leftmost = parents[:1]
768
747
new_parents = leftmost + rev_list
769
748
self.set_parent_ids(new_parents)
750
@needs_tree_write_lock
772
751
def set_merge_modified(self, modified_hashes):
773
752
def iter_stanzas():
774
753
for file_id, hash in modified_hashes.iteritems():
775
754
yield Stanza(file_id=file_id, hash=hash)
776
755
self._put_rio('merge-hashes', iter_stanzas(), MERGE_MODIFIED_HEADER_1)
757
@needs_tree_write_lock
779
758
def _put_rio(self, filename, stanzas, header):
780
759
my_file = rio_file(stanzas, header)
781
760
self._control_files.put(filename, my_file)
762
@needs_write_lock # because merge pulls data into the branch.
784
763
def merge_from_branch(self, branch, to_revision=None):
785
764
"""Merge from a branch into this working tree.
844
823
merge_hashes[file_id] = hash
845
824
return merge_hashes
827
def mkdir(self, path, file_id=None):
828
"""See MutableTree.mkdir()."""
830
file_id = gen_file_id(os.path.basename(path))
831
os.mkdir(self.abspath(path))
832
self.add(path, file_id, 'directory')
847
835
def get_symlink_target(self, file_id):
848
836
return os.readlink(self.id2abspath(file_id))
1092
1081
for subp in self.extras():
1093
1082
if not self.is_ignored(subp):
1085
@needs_tree_write_lock
1086
def unversion(self, file_ids):
1087
"""Remove the file ids in file_ids from the current versioned set.
1089
When a file_id is unversioned, all of its children are automatically
1092
:param file_ids: The file ids to stop versioning.
1093
:raises: NoSuchId if any fileid is not currently versioned.
1095
for file_id in file_ids:
1096
if self._inventory.has_id(file_id):
1097
self._inventory.remove_recursive_id(file_id)
1099
raise errors.NoSuchId(self, file_id)
1101
# in the future this should just set a dirty bit to wait for the
1102
# final unlock. However, until all methods of workingtree start
1103
# with the current in -memory inventory rather than triggering
1104
# a read, it is more complex - we need to teach read_inventory
1105
# to know when to read, and when to not read first... and possibly
1106
# to save first when the in memory one may be corrupted.
1107
# so for now, we just only write it if it is indeed dirty.
1109
self._write_inventory(self._inventory)
1096
1111
@deprecated_method(zero_eight)
1097
1112
def iter_conflicts(self):
1098
1113
"""List all files in the tree that have text or content conflicts.
1320
1345
def kind(self, file_id):
1321
1346
return file_kind(self.id2abspath(file_id))
1324
1348
def last_revision(self):
1325
"""Return the last revision id of this working tree.
1327
In early branch formats this was == the branch last_revision,
1328
but that cannot be relied upon - for working tree operations,
1329
always use tree.last_revision().
1349
"""Return the last revision of the branch for this tree.
1351
This format tree does not support a separate marker for last-revision
1352
compared to the branch.
1354
See MutableTree.last_revision
1356
return self._last_revision()
1359
def _last_revision(self):
1360
"""helper for get_parent_ids."""
1331
1361
return self.branch.last_revision()
1333
1363
def is_locked(self):
1379
1418
self.branch.set_revision_history([new_revision])
1421
def _write_basis_inventory(self, xml):
1422
"""Write the basis inventory XML to the basis-inventory file"""
1423
assert isinstance(xml, str), 'serialised xml must be bytestring.'
1424
path = self._basis_inventory_name()
1426
self._control_files.put(path, sio)
1428
def _create_basis_xml_from_inventory(self, revision_id, inventory):
1429
"""Create the text that will be saved in basis-inventory"""
1430
inventory.revision_id = revision_id
1431
return bzrlib.xml6.serializer_v6.write_inventory_to_string(inventory)
1382
1433
def _cache_basis_inventory(self, new_revision):
1383
1434
"""Cache new_revision as the basis inventory."""
1384
1435
# TODO: this should allow the ready-to-use inventory to be passed in,
1396
1447
# root node id can legitimately look like 'revision_id' but cannot
1397
1448
# contain a '"'.
1398
1449
xml = self.branch.repository.get_inventory_xml(new_revision)
1399
if not 'revision_id="' in xml.split('\n', 1)[0]:
1450
firstline = xml.split('\n', 1)[0]
1451
if (not 'revision_id="' in firstline or
1452
'format="6"' not in firstline):
1400
1453
inv = self.branch.repository.deserialise_inventory(
1401
1454
new_revision, xml)
1402
inv.revision_id = new_revision
1403
xml = bzrlib.xml5.serializer_v5.write_inventory_to_string(inv)
1404
assert isinstance(xml, str), 'serialised xml must be bytestring.'
1405
path = self._basis_inventory_name()
1407
self._control_files.put(path, sio)
1455
xml = self._create_basis_xml_from_inventory(new_revision, inv)
1456
self._write_basis_inventory(xml)
1408
1457
except (errors.NoSuchRevision, errors.RevisionNotPresent):
1584
1637
parent_trees.append(
1585
1638
(old_tip, self.branch.repository.revision_tree(old_tip)))
1586
1639
self.set_parent_trees(parent_trees)
1640
last_rev = parent_trees[0][0]
1588
1642
# the working tree had the same last-revision as the master
1589
1643
# branch did. We may still have pivot local work from the local
1590
1644
# branch into old_tip:
1591
1645
if old_tip is not None:
1592
1646
self.add_parent_tree_id(old_tip)
1593
if old_tip and old_tip != self.last_revision():
1647
if old_tip and old_tip != last_rev:
1594
1648
# our last revision was not the prior branch last revision
1595
1649
# and we have converted that last revision to a pending merge.
1596
1650
# base is somewhere between the branch tip now