393
406
return pathjoin(self.basedir, filename)
395
408
def basis_tree(self):
396
"""Return RevisionTree for the current last revision."""
397
revision_id = self.last_revision()
398
if revision_id is not None:
409
"""Return RevisionTree for the current last revision.
411
If the left most parent is a ghost then the returned tree will be an
412
empty tree - one obtained by calling repository.revision_tree(None).
415
revision_id = self.get_parent_ids()[0]
417
# no parents, return an empty revision tree.
418
# in the future this should return the tree for
419
# 'empty:' - the implicit root empty tree.
420
return self.branch.repository.revision_tree(None)
400
423
xml = self.read_basis_inventory()
401
inv = bzrlib.xml5.serializer_v5.read_inventory_from_string(xml)
404
if inv is not None and inv.revision_id == revision_id:
405
return bzrlib.tree.RevisionTree(self.branch.repository, inv,
407
# FIXME? RBC 20060403 should we cache the inventory here ?
408
return self.branch.repository.revision_tree(revision_id)
424
inv = xml6.serializer_v6.read_inventory_from_string(xml)
425
if inv is not None and inv.revision_id == revision_id:
426
return bzrlib.revisiontree.RevisionTree(
427
self.branch.repository, inv, revision_id)
428
except (NoSuchFile, errors.BadInventoryFormat):
430
# No cached copy available, retrieve from the repository.
431
# FIXME? RBC 20060403 should we cache the inventory locally
434
return self.branch.repository.revision_tree(revision_id)
435
except errors.RevisionNotPresent:
436
# the basis tree *may* be a ghost or a low level error may have
437
# occured. If the revision is present, its a problem, if its not
439
if self.branch.repository.has_revision(revision_id):
441
# the basis tree is a ghost so return an empty tree.
442
return self.branch.repository.revision_tree(None)
411
445
@deprecated_method(zero_eight)
581
606
return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
583
608
@needs_write_lock
584
def add(self, files, ids=None):
585
"""Make files versioned.
587
Note that the command line normally calls smart_add instead,
588
which can automatically recurse.
590
This adds the files to the inventory, so that they will be
591
recorded by the next commit.
594
List of paths to add, relative to the base of the tree.
597
If set, use these instead of automatically generated ids.
598
Must be the same length as the list of files, but may
599
contain None for ids that are to be autogenerated.
601
TODO: Perhaps have an option to add the ids even if the files do
604
TODO: Perhaps callback with the ids and paths as they're added.
609
def _add(self, files, ids, kinds):
610
"""See MutableTree._add."""
606
611
# TODO: Re-adding a file that is removed in the working copy
607
612
# should probably put it back with the previous ID.
608
if isinstance(files, basestring):
609
assert(ids is None or isinstance(ids, basestring))
615
ids = [None] * len(files)
617
assert(len(ids) == len(files))
613
# the read and write working inventory should not occur in this
614
# function - they should be part of lock_write and unlock.
619
615
inv = self.read_working_inventory()
620
for f,file_id in zip(files, ids):
621
if self.is_control_filename(f):
622
raise errors.ForbiddenControlFileError(filename=f)
627
raise BzrError("cannot add top-level %r" % f)
629
fullpath = normpath(self.abspath(f))
631
kind = file_kind(fullpath)
633
if e.errno == errno.ENOENT:
634
raise NoSuchFile(fullpath)
635
if not InventoryEntry.versionable_kind(kind):
636
raise errors.BadFileKindError(filename=f, kind=kind)
616
for f, file_id, kind in zip(files, ids, kinds):
617
assert kind is not None
637
618
if file_id is None:
638
619
inv.add_path(f, kind=kind)
640
621
inv.add_path(f, kind=kind, file_id=file_id)
642
622
self._write_inventory(inv)
624
@needs_tree_write_lock
625
def _gather_kinds(self, files, kinds):
626
"""See MutableTree._gather_kinds."""
627
for pos, f in enumerate(files):
628
if kinds[pos] is None:
629
fullpath = normpath(self.abspath(f))
631
kinds[pos] = file_kind(fullpath)
633
if e.errno == errno.ENOENT:
634
raise NoSuchFile(fullpath)
644
636
@needs_write_lock
637
def add_parent_tree_id(self, revision_id, allow_leftmost_as_ghost=False):
638
"""Add revision_id as a parent.
640
This is equivalent to retrieving the current list of parent ids
641
and setting the list to its value plus revision_id.
643
:param revision_id: The revision id to add to the parent list. It may
644
be a ghost revision as long as its not the first parent to be added,
645
or the allow_leftmost_as_ghost parameter is set True.
646
:param allow_leftmost_as_ghost: Allow the first parent to be a ghost.
648
parents = self.get_parent_ids() + [revision_id]
649
self.set_parent_ids(parents,
650
allow_leftmost_as_ghost=len(parents) > 1 or allow_leftmost_as_ghost)
652
@needs_tree_write_lock
653
def add_parent_tree(self, parent_tuple, allow_leftmost_as_ghost=False):
654
"""Add revision_id, tree tuple as a parent.
656
This is equivalent to retrieving the current list of parent trees
657
and setting the list to its value plus parent_tuple. See also
658
add_parent_tree_id - if you only have a parent id available it will be
659
simpler to use that api. If you have the parent already available, using
660
this api is preferred.
662
:param parent_tuple: The (revision id, tree) to add to the parent list.
663
If the revision_id is a ghost, pass None for the tree.
664
:param allow_leftmost_as_ghost: Allow the first parent to be a ghost.
666
parent_ids = self.get_parent_ids() + [parent_tuple[0]]
667
if len(parent_ids) > 1:
668
# the leftmost may have already been a ghost, preserve that if it
670
allow_leftmost_as_ghost = True
671
self.set_parent_ids(parent_ids,
672
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
674
@needs_tree_write_lock
645
675
def add_pending_merge(self, *revision_ids):
646
676
# TODO: Perhaps should check at this point that the
647
677
# history of the revision is actually present?
648
p = self.pending_merges()
678
parents = self.get_parent_ids()
650
680
for rev_id in revision_ids:
681
if rev_id in parents:
683
parents.append(rev_id)
656
self.set_pending_merges(p)
686
self.set_parent_ids(parents, allow_leftmost_as_ghost=True)
688
@deprecated_method(zero_eleven)
659
690
def pending_merges(self):
660
691
"""Return a list of pending merges.
662
693
These are revisions that have been merged into the working
663
694
directory but not yet committed.
666
merges_file = self._control_files.get_utf8('pending-merges')
670
for l in merges_file.readlines():
671
p.append(l.rstrip('\n'))
696
As of 0.11 this is deprecated. Please see WorkingTree.get_parent_ids()
697
instead - which is available on all tree objects.
699
return self.get_parent_ids()[1:]
701
def _check_parents_for_ghosts(self, revision_ids, allow_leftmost_as_ghost):
702
"""Common ghost checking functionality from set_parent_*.
704
This checks that the left hand-parent exists if there are any
707
if len(revision_ids) > 0:
708
leftmost_id = revision_ids[0]
709
if (not allow_leftmost_as_ghost and not
710
self.branch.repository.has_revision(leftmost_id)):
711
raise errors.GhostRevisionUnusableHere(leftmost_id)
713
def _set_merges_from_parent_ids(self, parent_ids):
714
merges = parent_ids[1:]
715
self._control_files.put_utf8('pending-merges', '\n'.join(merges))
717
@needs_tree_write_lock
718
def set_parent_ids(self, revision_ids, allow_leftmost_as_ghost=False):
719
"""Set the parent ids to revision_ids.
721
See also set_parent_trees. This api will try to retrieve the tree data
722
for each element of revision_ids from the trees repository. If you have
723
tree data already available, it is more efficient to use
724
set_parent_trees rather than set_parent_ids. set_parent_ids is however
725
an easier API to use.
727
:param revision_ids: The revision_ids to set as the parent ids of this
728
working tree. Any of these may be ghosts.
730
self._check_parents_for_ghosts(revision_ids,
731
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
733
if len(revision_ids) > 0:
734
self.set_last_revision(revision_ids[0])
736
self.set_last_revision(None)
738
self._set_merges_from_parent_ids(revision_ids)
740
@needs_tree_write_lock
741
def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
742
"""See MutableTree.set_parent_trees."""
743
parent_ids = [rev for (rev, tree) in parents_list]
745
self._check_parents_for_ghosts(parent_ids,
746
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
748
if len(parent_ids) == 0:
749
leftmost_parent_id = None
750
leftmost_parent_tree = None
752
leftmost_parent_id, leftmost_parent_tree = parents_list[0]
754
if self._change_last_revision(leftmost_parent_id):
755
if leftmost_parent_tree is None:
756
# If we don't have a tree, fall back to reading the
757
# parent tree from the repository.
758
self._cache_basis_inventory(leftmost_parent_id)
760
inv = leftmost_parent_tree.inventory
761
xml = self._create_basis_xml_from_inventory(
762
leftmost_parent_id, inv)
763
self._write_basis_inventory(xml)
764
self._set_merges_from_parent_ids(parent_ids)
766
@needs_tree_write_lock
675
767
def set_pending_merges(self, rev_list):
676
self._control_files.put_utf8('pending-merges', '\n'.join(rev_list))
768
parents = self.get_parent_ids()
769
leftmost = parents[:1]
770
new_parents = leftmost + rev_list
771
self.set_parent_ids(new_parents)
773
@needs_tree_write_lock
679
774
def set_merge_modified(self, modified_hashes):
680
775
def iter_stanzas():
681
776
for file_id, hash in modified_hashes.iteritems():
682
777
yield Stanza(file_id=file_id, hash=hash)
683
778
self._put_rio('merge-hashes', iter_stanzas(), MERGE_MODIFIED_HEADER_1)
780
@needs_tree_write_lock
686
781
def _put_rio(self, filename, stanzas, header):
687
782
my_file = rio_file(stanzas, header)
688
783
self._control_files.put(filename, my_file)
785
@needs_write_lock # because merge pulls data into the branch.
786
def merge_from_branch(self, branch, to_revision=None):
787
"""Merge from a branch into this working tree.
789
:param branch: The branch to merge from.
790
:param to_revision: If non-None, the merge will merge to to_revision, but
791
not beyond it. to_revision does not need to be in the history of
792
the branch when it is supplied. If None, to_revision defaults to
793
branch.last_revision().
795
from bzrlib.merge import Merger, Merge3Merger
796
pb = bzrlib.ui.ui_factory.nested_progress_bar()
798
merger = Merger(self.branch, this_tree=self, pb=pb)
799
merger.pp = ProgressPhase("Merge phase", 5, pb)
800
merger.pp.next_phase()
801
# check that there are no
803
merger.check_basis(check_clean=True, require_commits=False)
804
if to_revision is None:
805
to_revision = branch.last_revision()
806
merger.other_rev_id = to_revision
807
if merger.other_rev_id is None:
808
raise error.NoCommits(branch)
809
self.branch.fetch(branch, last_revision=merger.other_rev_id)
810
merger.other_basis = merger.other_rev_id
811
merger.other_tree = self.branch.repository.revision_tree(
813
merger.pp.next_phase()
815
if merger.base_rev_id == merger.other_rev_id:
816
raise errors.PointlessMerge
817
merger.backup_files = False
818
merger.merge_type = Merge3Merger
819
merger.set_interesting_files(None)
820
merger.show_base = False
821
merger.reprocess = False
822
conflicts = merger.do_merge()
691
829
def merge_modified(self):
1381
1642
raise NotImplementedError(self.unlock)
1384
1644
def update(self):
1385
1645
"""Update a working tree along its branch.
1387
This will update the branch if its bound too, which means we have multiple trees involved:
1388
The new basis tree of the master.
1389
The old basis tree of the branch.
1390
The old basis tree of the working tree.
1391
The current working tree state.
1392
pathologically all three may be different, and non ancestors of each other.
1393
Conceptually we want to:
1394
Preserve the wt.basis->wt.state changes
1395
Transform the wt.basis to the new master basis.
1396
Apply a merge of the old branch basis to get any 'local' changes from it into the tree.
1397
Restore the wt.basis->wt.state changes.
1647
This will update the branch if its bound too, which means we have
1648
multiple trees involved:
1650
- The new basis tree of the master.
1651
- The old basis tree of the branch.
1652
- The old basis tree of the working tree.
1653
- The current working tree state.
1655
Pathologically, all three may be different, and non-ancestors of each
1656
other. Conceptually we want to:
1658
- Preserve the wt.basis->wt.state changes
1659
- Transform the wt.basis to the new master basis.
1660
- Apply a merge of the old branch basis to get any 'local' changes from
1662
- Restore the wt.basis->wt.state changes.
1399
1664
There isn't a single operation at the moment to do that, so we:
1400
Merge current state -> basis tree of the master w.r.t. the old tree basis.
1401
Do a 'normal' merge of the old branch basis if it is relevant.
1665
- Merge current state -> basis tree of the master w.r.t. the old tree
1667
- Do a 'normal' merge of the old branch basis if it is relevant.
1403
old_tip = self.branch.update()
1404
if old_tip is not None:
1405
self.add_pending_merge(old_tip)
1406
self.branch.lock_read()
1669
if self.branch.get_master_branch() is not None:
1671
update_branch = True
1673
self.lock_tree_write()
1674
update_branch = False
1409
if self.last_revision() != self.branch.last_revision():
1410
# merge tree state up to new branch tip.
1411
basis = self.basis_tree()
1412
to_tree = self.branch.basis_tree()
1413
result += merge_inner(self.branch,
1417
self.set_last_revision(self.branch.last_revision())
1418
if old_tip and old_tip != self.last_revision():
1419
# our last revision was not the prior branch last revision
1420
# and we have converted that last revision to a pending merge.
1421
# base is somewhere between the branch tip now
1422
# and the now pending merge
1423
from bzrlib.revision import common_ancestor
1425
base_rev_id = common_ancestor(self.branch.last_revision(),
1427
self.branch.repository)
1428
except errors.NoCommonAncestor:
1430
base_tree = self.branch.repository.revision_tree(base_rev_id)
1431
other_tree = self.branch.repository.revision_tree(old_tip)
1432
result += merge_inner(self.branch,
1677
old_tip = self.branch.update()
1680
return self._update_tree(old_tip)
1438
self.branch.unlock()
1684
@needs_tree_write_lock
1685
def _update_tree(self, old_tip=None):
1686
"""Update a tree to the master branch.
1688
:param old_tip: if supplied, the previous tip revision the branch,
1689
before it was changed to the master branch's tip.
1691
# here if old_tip is not None, it is the old tip of the branch before
1692
# it was updated from the master branch. This should become a pending
1693
# merge in the working tree to preserve the user existing work. we
1694
# cant set that until we update the working trees last revision to be
1695
# one from the new branch, because it will just get absorbed by the
1696
# parent de-duplication logic.
1698
# We MUST save it even if an error occurs, because otherwise the users
1699
# local work is unreferenced and will appear to have been lost.
1703
last_rev = self.get_parent_ids()[0]
1706
if last_rev != self.branch.last_revision():
1707
# merge tree state up to new branch tip.
1708
basis = self.basis_tree()
1709
to_tree = self.branch.basis_tree()
1710
if basis.inventory.root is None:
1711
self.set_root_id(to_tree.inventory.root.file_id)
1712
result += merge.merge_inner(
1717
# TODO - dedup parents list with things merged by pull ?
1718
# reuse the tree we've updated to to set the basis:
1719
parent_trees = [(self.branch.last_revision(), to_tree)]
1720
merges = self.get_parent_ids()[1:]
1721
# Ideally we ask the tree for the trees here, that way the working
1722
# tree can decide whether to give us teh entire tree or give us a
1723
# lazy initialised tree. dirstate for instance will have the trees
1724
# in ram already, whereas a last-revision + basis-inventory tree
1725
# will not, but also does not need them when setting parents.
1726
for parent in merges:
1727
parent_trees.append(
1728
(parent, self.branch.repository.revision_tree(parent)))
1729
if old_tip is not None:
1730
parent_trees.append(
1731
(old_tip, self.branch.repository.revision_tree(old_tip)))
1732
self.set_parent_trees(parent_trees)
1733
last_rev = parent_trees[0][0]
1735
# the working tree had the same last-revision as the master
1736
# branch did. We may still have pivot local work from the local
1737
# branch into old_tip:
1738
if old_tip is not None:
1739
self.add_parent_tree_id(old_tip)
1740
if old_tip and old_tip != last_rev:
1741
# our last revision was not the prior branch last revision
1742
# and we have converted that last revision to a pending merge.
1743
# base is somewhere between the branch tip now
1744
# and the now pending merge
1745
from bzrlib.revision import common_ancestor
1747
base_rev_id = common_ancestor(self.branch.last_revision(),
1749
self.branch.repository)
1750
except errors.NoCommonAncestor:
1752
base_tree = self.branch.repository.revision_tree(base_rev_id)
1753
other_tree = self.branch.repository.revision_tree(old_tip)
1754
result += merge.merge_inner(
1761
@needs_tree_write_lock
1441
1762
def _write_inventory(self, inv):
1442
1763
"""Write inventory as the current inventory."""
1444
bzrlib.xml5.serializer_v5.write_inventory(inv, sio)
1446
self._control_files.put('inventory', sio)
1447
self._set_inventory(inv)
1448
mutter('wrote working inventory')
1764
self._set_inventory(inv, dirty=True)
1450
1767
def set_conflicts(self, arg):
1451
1768
raise UnsupportedOperation(self.set_conflicts, self)