~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transform.py

  • Committer: Jelmer Vernooij
  • Date: 2010-12-20 11:57:14 UTC
  • mto: This revision was merged to the branch mainline in revision 5577.
  • Revision ID: jelmer@samba.org-20101220115714-2ru3hfappjweeg7q
Don't use no-plugins.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
from __future__ import absolute_import
18
 
 
19
17
import os
20
18
import errno
21
19
from stat import S_ISREG, S_IEXEC
25
23
    errors,
26
24
    lazy_import,
27
25
    registry,
28
 
    trace,
29
 
    tree,
30
26
    )
31
27
lazy_import.lazy_import(globals(), """
32
28
from bzrlib import (
33
29
    annotate,
34
30
    bencode,
35
 
    controldir,
 
31
    bzrdir,
36
32
    commit,
37
 
    conflicts,
38
33
    delta,
 
34
    errors,
39
35
    inventory,
40
36
    multiparent,
41
37
    osutils,
42
38
    revision as _mod_revision,
 
39
    trace,
43
40
    ui,
44
 
    urlutils,
45
41
    )
46
 
from bzrlib.i18n import gettext
47
42
""")
48
 
from bzrlib.errors import (DuplicateKey, MalformedTransform,
 
43
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
49
44
                           ReusingTransform, CantMoveRoot,
50
45
                           ExistingLimbo, ImmortalLimbo, NoFinalPath,
51
46
                           UnableCreateSymlink)
52
47
from bzrlib.filters import filtered_output_bytes, ContentFilterContext
 
48
from bzrlib.inventory import InventoryEntry
53
49
from bzrlib.osutils import (
54
50
    delete_any,
55
51
    file_kind,
56
52
    has_symlinks,
 
53
    lexists,
57
54
    pathjoin,
58
55
    sha_file,
59
56
    splitpath,
60
57
    supports_executable,
61
 
    )
 
58
)
62
59
from bzrlib.progress import ProgressPhase
63
60
from bzrlib.symbol_versioning import (
64
 
    deprecated_function,
65
 
    deprecated_in,
66
 
    deprecated_method,
67
 
    )
 
61
        deprecated_function,
 
62
        deprecated_in,
 
63
        deprecated_method,
 
64
        )
 
65
from bzrlib.trace import mutter, warning
 
66
from bzrlib import tree
 
67
import bzrlib.ui
 
68
import bzrlib.urlutils as urlutils
68
69
 
69
70
 
70
71
ROOT_PARENT = "root-parent"
105
106
        self._new_parent = {}
106
107
        # mapping of trans_id with new contents -> new file_kind
107
108
        self._new_contents = {}
108
 
        # mapping of trans_id => (sha1 of content, stat_value)
109
 
        self._observed_sha1s = {}
110
109
        # Set of trans_ids whose contents will be removed
111
110
        self._removed_contents = set()
112
111
        # Mapping of trans_id -> new execute-bit value
140
139
        # A counter of how many files have been renamed
141
140
        self.rename_count = 0
142
141
 
143
 
    def __enter__(self):
144
 
        """Support Context Manager API."""
145
 
        return self
146
 
 
147
 
    def __exit__(self, exc_type, exc_val, exc_tb):
148
 
        """Support Context Manager API."""
149
 
        self.finalize()
150
 
 
151
142
    def finalize(self):
152
143
        """Release the working tree lock, if held.
153
144
 
228
219
        This means that the old root trans-id becomes obsolete, so it is
229
220
        recommended only to invoke this after the root trans-id has become
230
221
        irrelevant.
231
 
 
232
222
        """
233
223
        new_roots = [k for k, v in self._new_parent.iteritems() if v is
234
224
                     ROOT_PARENT]
240
230
            self._new_root = new_roots[0]
241
231
            return
242
232
        old_new_root = new_roots[0]
 
233
        # TODO: What to do if a old_new_root is present, but self._new_root is
 
234
        #       not listed as being removed? This code explicitly unversions
 
235
        #       the old root and versions it with the new file_id. Though that
 
236
        #       seems like an incomplete delta
 
237
 
243
238
        # unversion the new root's directory.
244
 
        if self.final_kind(self._new_root) is None:
245
 
            file_id = self.final_file_id(old_new_root)
246
 
        else:
247
 
            file_id = self.final_file_id(self._new_root)
 
239
        file_id = self.final_file_id(old_new_root)
248
240
        if old_new_root in self._new_id:
249
241
            self.cancel_versioning(old_new_root)
250
242
        else:
254
246
        if (self.tree_file_id(self._new_root) is not None and
255
247
            self._new_root not in self._removed_id):
256
248
            self.unversion_file(self._new_root)
257
 
        if file_id is not None:
258
 
            self.version_file(file_id, self._new_root)
 
249
        self.version_file(file_id, self._new_root)
259
250
 
260
251
        # Now move children of new root into old root directory.
261
252
        # Ensure all children are registered with the transaction, but don't
395
386
        return sorted(FinalPaths(self).get_paths(new_ids))
396
387
 
397
388
    def _inventory_altered(self):
398
 
        """Determine which trans_ids need new Inventory entries.
399
 
 
400
 
        An new entry is needed when anything that would be reflected by an
401
 
        inventory entry changes, including file name, file_id, parent file_id,
402
 
        file kind, and the execute bit.
403
 
 
404
 
        Some care is taken to return entries with real changes, not cases
405
 
        where the value is deleted and then restored to its original value,
406
 
        but some actually unchanged values may be returned.
407
 
 
408
 
        :returns: A list of (path, trans_id) for all items requiring an
409
 
            inventory change. Ordered by path.
410
 
        """
411
 
        changed_ids = set()
412
 
        # Find entries whose file_ids are new (or changed).
413
 
        new_file_id = set(t for t in self._new_id
414
 
                          if self._new_id[t] != self.tree_file_id(t))
415
 
        for id_set in [self._new_name, self._new_parent, new_file_id,
 
389
        """Get the trans_ids and paths of files needing new inv entries."""
 
390
        new_ids = set()
 
391
        for id_set in [self._new_name, self._new_parent, self._new_id,
416
392
                       self._new_executability]:
417
 
            changed_ids.update(id_set)
418
 
        # removing implies a kind change
 
393
            new_ids.update(id_set)
419
394
        changed_kind = set(self._removed_contents)
420
 
        # so does adding
421
395
        changed_kind.intersection_update(self._new_contents)
422
 
        # Ignore entries that are already known to have changed.
423
 
        changed_kind.difference_update(changed_ids)
424
 
        #  to keep only the truly changed ones
 
396
        changed_kind.difference_update(new_ids)
425
397
        changed_kind = (t for t in changed_kind
426
398
                        if self.tree_kind(t) != self.final_kind(t))
427
 
        # all kind changes will alter the inventory
428
 
        changed_ids.update(changed_kind)
429
 
        # To find entries with changed parent_ids, find parents which existed,
430
 
        # but changed file_id.
431
 
        changed_file_id = set(t for t in new_file_id if t in self._removed_id)
432
 
        # Now add all their children to the set.
433
 
        for parent_trans_id in new_file_id:
434
 
            changed_ids.update(self.iter_tree_children(parent_trans_id))
435
 
        return sorted(FinalPaths(self).get_paths(changed_ids))
 
399
        new_ids.update(changed_kind)
 
400
        return sorted(FinalPaths(self).get_paths(new_ids))
436
401
 
437
402
    def final_kind(self, trans_id):
438
403
        """Determine the final file kind, after any changes applied.
563
528
        for trans_id in self._removed_id:
564
529
            file_id = self.tree_file_id(trans_id)
565
530
            if file_id is not None:
566
 
                if self._tree.stored_kind(file_id) == 'directory':
 
531
                if self._tree.inventory[file_id].kind == 'directory':
567
532
                    parents.append(trans_id)
568
533
            elif self.tree_kind(trans_id) == 'directory':
569
534
                parents.append(trans_id)
663
628
            if kind is None:
664
629
                conflicts.append(('versioning no contents', trans_id))
665
630
                continue
666
 
            if not inventory.InventoryEntry.versionable_kind(kind):
 
631
            if not InventoryEntry.versionable_kind(kind):
667
632
                conflicts.append(('versioning bad kind', trans_id, kind))
668
633
        return conflicts
669
634
 
763
728
 
764
729
    def _set_executability(self, path, trans_id):
765
730
        """Set the executability of versioned files """
766
 
        if self._tree._supports_executable():
 
731
        if supports_executable():
767
732
            new_executability = self._new_executability[trans_id]
768
733
            abspath = self._tree.abspath(path)
769
734
            current_mode = os.stat(abspath).st_mode
778
743
                    to_mode |= 0010 & ~umask
779
744
            else:
780
745
                to_mode = current_mode & ~0111
781
 
            osutils.chmod_if_possible(abspath, to_mode)
 
746
            os.chmod(abspath, to_mode)
782
747
 
783
748
    def _new_entry(self, name, parent_id, file_id):
784
749
        """Helper function to create a new filesystem entry."""
788
753
        return trans_id
789
754
 
790
755
    def new_file(self, name, parent_id, contents, file_id=None,
791
 
                 executable=None, sha1=None):
 
756
                 executable=None):
792
757
        """Convenience method to create files.
793
758
 
794
759
        name is the name of the file to create.
801
766
        trans_id = self._new_entry(name, parent_id, file_id)
802
767
        # TODO: rather than scheduling a set_executable call,
803
768
        # have create_file create the file with the right mode.
804
 
        self.create_file(contents, trans_id, sha1=sha1)
 
769
        self.create_file(contents, trans_id)
805
770
        if executable is not None:
806
771
            self.set_executability(executable, trans_id)
807
772
        return trans_id
1189
1154
        self._deletiondir = None
1190
1155
        # A mapping of transform ids to their limbo filename
1191
1156
        self._limbo_files = {}
1192
 
        self._possibly_stale_limbo_files = set()
1193
1157
        # A mapping of transform ids to a set of the transform ids of children
1194
1158
        # that their limbo directory has
1195
1159
        self._limbo_children = {}
1208
1172
        if self._tree is None:
1209
1173
            return
1210
1174
        try:
1211
 
            limbo_paths = self._limbo_files.values() + list(
1212
 
                self._possibly_stale_limbo_files)
1213
 
            limbo_paths = sorted(limbo_paths, reverse=True)
1214
 
            for path in limbo_paths:
1215
 
                try:
1216
 
                    delete_any(path)
1217
 
                except OSError, e:
1218
 
                    if e.errno != errno.ENOENT:
1219
 
                        raise
1220
 
                    # XXX: warn? perhaps we just got interrupted at an
1221
 
                    # inconvenient moment, but perhaps files are disappearing
1222
 
                    # from under us?
 
1175
            entries = [(self._limbo_name(t), t, k) for t, k in
 
1176
                       self._new_contents.iteritems()]
 
1177
            entries.sort(reverse=True)
 
1178
            for path, trans_id, kind in entries:
 
1179
                delete_any(path)
1223
1180
            try:
1224
1181
                delete_any(self._limbodir)
1225
1182
            except OSError:
1233
1190
        finally:
1234
1191
            TreeTransformBase.finalize(self)
1235
1192
 
1236
 
    def _limbo_supports_executable(self):
1237
 
        """Check if the limbo path supports the executable bit."""
1238
 
        # FIXME: Check actual file system capabilities of limbodir
1239
 
        return osutils.supports_executable()
1240
 
 
1241
1193
    def _limbo_name(self, trans_id):
1242
1194
        """Generate the limbo name of a file"""
1243
1195
        limbo_name = self._limbo_files.get(trans_id)
1279
1231
        entries from _limbo_files, because they are now stale.
1280
1232
        """
1281
1233
        for trans_id in trans_ids:
1282
 
            old_path = self._limbo_files[trans_id]
1283
 
            self._possibly_stale_limbo_files.add(old_path)
1284
 
            del self._limbo_files[trans_id]
 
1234
            old_path = self._limbo_files.pop(trans_id)
1285
1235
            if trans_id not in self._new_contents:
1286
1236
                continue
1287
1237
            new_path = self._limbo_name(trans_id)
1288
1238
            os.rename(old_path, new_path)
1289
 
            self._possibly_stale_limbo_files.remove(old_path)
1290
1239
            for descendant in self._limbo_descendants(trans_id):
1291
1240
                desc_path = self._limbo_files[descendant]
1292
1241
                desc_path = new_path + desc_path[len(old_path):]
1299
1248
            descendants.update(self._limbo_descendants(descendant))
1300
1249
        return descendants
1301
1250
 
1302
 
    def create_file(self, contents, trans_id, mode_id=None, sha1=None):
 
1251
    def create_file(self, contents, trans_id, mode_id=None):
1303
1252
        """Schedule creation of a new file.
1304
1253
 
1305
 
        :seealso: new_file.
1306
 
 
1307
 
        :param contents: an iterator of strings, all of which will be written
1308
 
            to the target destination.
1309
 
        :param trans_id: TreeTransform handle
1310
 
        :param mode_id: If not None, force the mode of the target file to match
1311
 
            the mode of the object referenced by mode_id.
1312
 
            Otherwise, we will try to preserve mode bits of an existing file.
1313
 
        :param sha1: If the sha1 of this content is already known, pass it in.
1314
 
            We can use it to prevent future sha1 computations.
 
1254
        See also new_file.
 
1255
 
 
1256
        Contents is an iterator of strings, all of which will be written
 
1257
        to the target destination.
 
1258
 
 
1259
        New file takes the permissions of any existing file with that id,
 
1260
        unless mode_id is specified.
1315
1261
        """
1316
1262
        name = self._limbo_name(trans_id)
1317
1263
        f = open(name, 'wb')
1318
1264
        try:
1319
 
            unique_add(self._new_contents, trans_id, 'file')
 
1265
            try:
 
1266
                unique_add(self._new_contents, trans_id, 'file')
 
1267
            except:
 
1268
                # Clean up the file, it never got registered so
 
1269
                # TreeTransform.finalize() won't clean it up.
 
1270
                f.close()
 
1271
                os.unlink(name)
 
1272
                raise
 
1273
 
1320
1274
            f.writelines(contents)
1321
1275
        finally:
1322
1276
            f.close()
1323
1277
        self._set_mtime(name)
1324
1278
        self._set_mode(trans_id, mode_id, S_ISREG)
1325
 
        # It is unfortunate we have to use lstat instead of fstat, but we just
1326
 
        # used utime and chmod on the file, so we need the accurate final
1327
 
        # details.
1328
 
        if sha1 is not None:
1329
 
            self._observed_sha1s[trans_id] = (sha1, osutils.lstat(name))
1330
1279
 
1331
1280
    def _read_file_chunks(self, trans_id):
1332
1281
        cur_file = open(self._limbo_name(trans_id), 'rb')
1391
1340
    def cancel_creation(self, trans_id):
1392
1341
        """Cancel the creation of new file contents."""
1393
1342
        del self._new_contents[trans_id]
1394
 
        if trans_id in self._observed_sha1s:
1395
 
            del self._observed_sha1s[trans_id]
1396
1343
        children = self._limbo_children.get(trans_id)
1397
1344
        # if this is a limbo directory with children, move them before removing
1398
1345
        # the directory
1414
1361
        if orphan_policy is None:
1415
1362
            orphan_policy = default_policy
1416
1363
        if orphan_policy not in orphaning_registry:
1417
 
            trace.warning('%s (from %s) is not a known policy, defaulting '
1418
 
                'to %s' % (orphan_policy, conf_var_name, default_policy))
 
1364
            trace.warning('%s (from %s) is not a known policy, defaulting to %s'
 
1365
                          % (orphan_policy, conf_var_name, default_policy))
1419
1366
            orphan_policy = default_policy
1420
1367
        handle_orphan = orphaning_registry.get(orphan_policy)
1421
1368
        handle_orphan(self, trans_id, parent_id)
1562
1509
        try:
1563
1510
            limbodir = urlutils.local_path_from_url(
1564
1511
                tree._transport.abspath('limbo'))
1565
 
            osutils.ensure_empty_directory_exists(
1566
 
                limbodir,
1567
 
                errors.ExistingLimbo)
 
1512
            try:
 
1513
                os.mkdir(limbodir)
 
1514
            except OSError, e:
 
1515
                if e.errno == errno.EEXIST:
 
1516
                    raise ExistingLimbo(limbodir)
1568
1517
            deletiondir = urlutils.local_path_from_url(
1569
1518
                tree._transport.abspath('pending-deletion'))
1570
 
            osutils.ensure_empty_directory_exists(
1571
 
                deletiondir,
1572
 
                errors.ExistingPendingDeletion)
 
1519
            try:
 
1520
                os.mkdir(deletiondir)
 
1521
            except OSError, e:
 
1522
                if e.errno == errno.EEXIST:
 
1523
                    raise errors.ExistingPendingDeletion(deletiondir)
1573
1524
        except:
1574
1525
            tree.unlock()
1575
1526
            raise
1638
1589
            else:
1639
1590
                raise
1640
1591
        if typefunc(mode):
1641
 
            osutils.chmod_if_possible(self._limbo_name(trans_id), mode)
 
1592
            os.chmod(self._limbo_name(trans_id), mode)
1642
1593
 
1643
1594
    def iter_tree_children(self, parent_id):
1644
1595
        """Iterate through the entry's tree children, if any"""
1724
1675
        """
1725
1676
        if not no_conflicts:
1726
1677
            self._check_malformed()
1727
 
        child_pb = ui.ui_factory.nested_progress_bar()
 
1678
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1728
1679
        try:
1729
1680
            if precomputed_delta is None:
1730
 
                child_pb.update(gettext('Apply phase'), 0, 2)
 
1681
                child_pb.update('Apply phase', 0, 2)
1731
1682
                inventory_delta = self._generate_inventory_delta()
1732
1683
                offset = 1
1733
1684
            else:
1738
1689
            else:
1739
1690
                mover = _mover
1740
1691
            try:
1741
 
                child_pb.update(gettext('Apply phase'), 0 + offset, 2 + offset)
 
1692
                child_pb.update('Apply phase', 0 + offset, 2 + offset)
1742
1693
                self._apply_removals(mover)
1743
 
                child_pb.update(gettext('Apply phase'), 1 + offset, 2 + offset)
 
1694
                child_pb.update('Apply phase', 1 + offset, 2 + offset)
1744
1695
                modified_paths = self._apply_insertions(mover)
1745
1696
            except:
1746
1697
                mover.rollback()
1749
1700
                mover.apply_deletions()
1750
1701
        finally:
1751
1702
            child_pb.finished()
1752
 
        if self.final_file_id(self.root) is None:
1753
 
            inventory_delta = [e for e in inventory_delta if e[0] != '']
1754
1703
        self._tree.apply_inventory_delta(inventory_delta)
1755
 
        self._apply_observed_sha1s()
1756
1704
        self._done = True
1757
1705
        self.finalize()
1758
1706
        return _TransformResults(modified_paths, self.rename_count)
1760
1708
    def _generate_inventory_delta(self):
1761
1709
        """Generate an inventory delta for the current transform."""
1762
1710
        inventory_delta = []
1763
 
        child_pb = ui.ui_factory.nested_progress_bar()
 
1711
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1764
1712
        new_paths = self._inventory_altered()
1765
1713
        total_entries = len(new_paths) + len(self._removed_id)
1766
1714
        try:
1767
1715
            for num, trans_id in enumerate(self._removed_id):
1768
1716
                if (num % 10) == 0:
1769
 
                    child_pb.update(gettext('removing file'), num, total_entries)
 
1717
                    child_pb.update('removing file', num, total_entries)
1770
1718
                if trans_id == self._new_root:
1771
1719
                    file_id = self._tree.get_root_id()
1772
1720
                else:
1784
1732
            final_kinds = {}
1785
1733
            for num, (path, trans_id) in enumerate(new_paths):
1786
1734
                if (num % 10) == 0:
1787
 
                    child_pb.update(gettext('adding file'),
 
1735
                    child_pb.update('adding file',
1788
1736
                                    num + len(self._removed_id), total_entries)
1789
1737
                file_id = new_path_file_ids[trans_id]
1790
1738
                if file_id is None:
1828
1776
        """
1829
1777
        tree_paths = list(self._tree_path_ids.iteritems())
1830
1778
        tree_paths.sort(reverse=True)
1831
 
        child_pb = ui.ui_factory.nested_progress_bar()
 
1779
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1832
1780
        try:
1833
 
            for num, (path, trans_id) in enumerate(tree_paths):
1834
 
                # do not attempt to move root into a subdirectory of itself.
1835
 
                if path == '':
1836
 
                    continue
1837
 
                child_pb.update(gettext('removing file'), num, len(tree_paths))
 
1781
            for num, data in enumerate(tree_paths):
 
1782
                path, trans_id = data
 
1783
                child_pb.update('removing file', num, len(tree_paths))
1838
1784
                full_path = self._tree.abspath(path)
1839
1785
                if trans_id in self._removed_contents:
1840
1786
                    delete_path = os.path.join(self._deletiondir, trans_id)
1865
1811
        modified_paths = []
1866
1812
        new_path_file_ids = dict((t, self.final_file_id(t)) for p, t in
1867
1813
                                 new_paths)
1868
 
        child_pb = ui.ui_factory.nested_progress_bar()
 
1814
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1869
1815
        try:
1870
1816
            for num, (path, trans_id) in enumerate(new_paths):
1871
1817
                if (num % 10) == 0:
1872
 
                    child_pb.update(gettext('adding file'), num, len(new_paths))
 
1818
                    child_pb.update('adding file', num, len(new_paths))
1873
1819
                full_path = self._tree.abspath(path)
1874
1820
                if trans_id in self._needs_rename:
1875
1821
                    try:
1880
1826
                            raise
1881
1827
                    else:
1882
1828
                        self.rename_count += 1
1883
 
                    # TODO: if trans_id in self._observed_sha1s, we should
1884
 
                    #       re-stat the final target, since ctime will be
1885
 
                    #       updated by the change.
1886
1829
                if (trans_id in self._new_contents or
1887
1830
                    self.path_changed(trans_id)):
1888
1831
                    if trans_id in self._new_contents:
1889
1832
                        modified_paths.append(full_path)
1890
1833
                if trans_id in self._new_executability:
1891
1834
                    self._set_executability(path, trans_id)
1892
 
                if trans_id in self._observed_sha1s:
1893
 
                    o_sha1, o_st_val = self._observed_sha1s[trans_id]
1894
 
                    st = osutils.lstat(full_path)
1895
 
                    self._observed_sha1s[trans_id] = (o_sha1, st)
1896
1835
        finally:
1897
1836
            child_pb.finished()
1898
 
        for path, trans_id in new_paths:
1899
 
            # new_paths includes stuff like workingtree conflicts. Only the
1900
 
            # stuff in new_contents actually comes from limbo.
1901
 
            if trans_id in self._limbo_files:
1902
 
                del self._limbo_files[trans_id]
1903
1837
        self._new_contents.clear()
1904
1838
        return modified_paths
1905
1839
 
1906
 
    def _apply_observed_sha1s(self):
1907
 
        """After we have finished renaming everything, update observed sha1s
1908
 
 
1909
 
        This has to be done after self._tree.apply_inventory_delta, otherwise
1910
 
        it doesn't know anything about the files we are updating. Also, we want
1911
 
        to do this as late as possible, so that most entries end up cached.
1912
 
        """
1913
 
        # TODO: this doesn't update the stat information for directories. So
1914
 
        #       the first 'bzr status' will still need to rewrite
1915
 
        #       .bzr/checkout/dirstate. However, we at least don't need to
1916
 
        #       re-read all of the files.
1917
 
        # TODO: If the operation took a while, we could do a time.sleep(3) here
1918
 
        #       to allow the clock to tick over and ensure we won't have any
1919
 
        #       problems. (we could observe start time, and finish time, and if
1920
 
        #       it is less than eg 10% overhead, add a sleep call.)
1921
 
        paths = FinalPaths(self)
1922
 
        for trans_id, observed in self._observed_sha1s.iteritems():
1923
 
            path = paths.get_path(trans_id)
1924
 
            # We could get the file_id, but dirstate prefers to use the path
1925
 
            # anyway, and it is 'cheaper' to determine.
1926
 
            # file_id = self._new_id[trans_id]
1927
 
            self._tree._observed_sha1(None, path, observed)
1928
 
 
1929
1840
 
1930
1841
class TransformPreview(DiskTreeTransform):
1931
1842
    """A TreeTransform for generating preview trees.
1947
1858
        path = self._tree_id_paths.get(trans_id)
1948
1859
        if path is None:
1949
1860
            return None
1950
 
        kind = self._tree.path_content_summary(path)[0]
1951
 
        if kind == 'missing':
1952
 
            kind = None
1953
 
        return kind
 
1861
        file_id = self._tree.path2id(path)
 
1862
        try:
 
1863
            return self._tree.kind(file_id)
 
1864
        except errors.NoSuchFile:
 
1865
            return None
1954
1866
 
1955
1867
    def _set_mode(self, trans_id, mode_id, typefunc):
1956
1868
        """Set the mode of new file contents.
1980
1892
        raise NotImplementedError(self.new_orphan)
1981
1893
 
1982
1894
 
1983
 
class _PreviewTree(tree.InventoryTree):
 
1895
class _PreviewTree(tree.Tree):
1984
1896
    """Partial implementation of Tree to support show_diff_trees"""
1985
1897
 
1986
1898
    def __init__(self, transform):
2015
1927
                yield self._get_repository().revision_tree(revision_id)
2016
1928
 
2017
1929
    def _get_file_revision(self, file_id, vf, tree_revision):
2018
 
        parent_keys = [(file_id, t.get_file_revision(file_id)) for t in
 
1930
        parent_keys = [(file_id, self._file_revision(t, file_id)) for t in
2019
1931
                       self._iter_parent_trees()]
2020
1932
        vf.add_lines((file_id, tree_revision), parent_keys,
2021
1933
                     self.get_file_lines(file_id))
2025
1937
            vf.fallback_versionedfiles.append(base_vf)
2026
1938
        return tree_revision
2027
1939
 
2028
 
    def _stat_limbo_file(self, file_id=None, trans_id=None):
2029
 
        if trans_id is None:
2030
 
            trans_id = self._transform.trans_id_file_id(file_id)
 
1940
    def _stat_limbo_file(self, file_id):
 
1941
        trans_id = self._transform.trans_id_file_id(file_id)
2031
1942
        name = self._transform._limbo_name(trans_id)
2032
1943
        return os.lstat(name)
2033
1944
 
2248
2159
 
2249
2160
    def get_file_size(self, file_id):
2250
2161
        """See Tree.get_file_size"""
2251
 
        trans_id = self._transform.trans_id_file_id(file_id)
2252
 
        kind = self._transform.final_kind(trans_id)
2253
 
        if kind != 'file':
2254
 
            return None
2255
 
        if trans_id in self._transform._new_contents:
2256
 
            return self._stat_limbo_file(trans_id=trans_id).st_size
2257
2162
        if self.kind(file_id) == 'file':
2258
2163
            return self._transform._tree.get_file_size(file_id)
2259
2164
        else:
2260
2165
            return None
2261
2166
 
2262
 
    def get_file_verifier(self, file_id, path=None, stat_value=None):
2263
 
        trans_id = self._transform.trans_id_file_id(file_id)
2264
 
        kind = self._transform._new_contents.get(trans_id)
2265
 
        if kind is None:
2266
 
            return self._transform._tree.get_file_verifier(file_id)
2267
 
        if kind == 'file':
2268
 
            fileobj = self.get_file(file_id)
2269
 
            try:
2270
 
                return ("SHA1", sha_file(fileobj))
2271
 
            finally:
2272
 
                fileobj.close()
2273
 
 
2274
2167
    def get_file_sha1(self, file_id, path=None, stat_value=None):
2275
2168
        trans_id = self._transform.trans_id_file_id(file_id)
2276
2169
        kind = self._transform._new_contents.get(trans_id)
2299
2192
            except errors.NoSuchId:
2300
2193
                return False
2301
2194
 
2302
 
    def has_filename(self, path):
2303
 
        trans_id = self._path2trans_id(path)
2304
 
        if trans_id in self._transform._new_contents:
2305
 
            return True
2306
 
        elif trans_id in self._transform._removed_contents:
2307
 
            return False
2308
 
        else:
2309
 
            return self._transform._tree.has_filename(path)
2310
 
 
2311
2195
    def path_content_summary(self, path):
2312
2196
        trans_id = self._path2trans_id(path)
2313
2197
        tt = self._transform
2326
2210
            if kind == 'file':
2327
2211
                statval = os.lstat(limbo_name)
2328
2212
                size = statval.st_size
2329
 
                if not tt._limbo_supports_executable():
 
2213
                if not supports_executable():
2330
2214
                    executable = False
2331
2215
                else:
2332
2216
                    executable = statval.st_mode & S_IEXEC
2401
2285
                                   self.get_file(file_id).readlines(),
2402
2286
                                   default_revision)
2403
2287
 
2404
 
    def get_symlink_target(self, file_id, path=None):
 
2288
    def get_symlink_target(self, file_id):
2405
2289
        """See Tree.get_symlink_target"""
2406
2290
        if not self._content_change(file_id):
2407
2291
            return self._transform._tree.get_symlink_target(file_id)
2545
2429
        if num > 0:  # more than just a root
2546
2430
            raise errors.WorkingTreeAlreadyPopulated(base=wt.basedir)
2547
2431
    file_trans_id = {}
2548
 
    top_pb = ui.ui_factory.nested_progress_bar()
 
2432
    top_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2549
2433
    pp = ProgressPhase("Build phase", 2, top_pb)
2550
 
    if tree.get_root_id() is not None:
 
2434
    if tree.inventory.root is not None:
2551
2435
        # This is kind of a hack: we should be altering the root
2552
2436
        # as part of the regular tree shape diff logic.
2553
2437
        # The conditional test here is to avoid doing an
2564
2448
        pp.next_phase()
2565
2449
        file_trans_id[wt.get_root_id()] = \
2566
2450
            tt.trans_id_tree_file_id(wt.get_root_id())
2567
 
        pb = ui.ui_factory.nested_progress_bar()
 
2451
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
2568
2452
        try:
2569
2453
            deferred_contents = []
2570
2454
            num = 0
2571
 
            total = len(tree.all_file_ids())
 
2455
            total = len(tree.inventory)
2572
2456
            if delta_from_tree:
2573
2457
                precomputed_delta = []
2574
2458
            else:
2583
2467
                for dir, files in wt.walkdirs():
2584
2468
                    existing_files.update(f[0] for f in files)
2585
2469
            for num, (tree_path, entry) in \
2586
 
                enumerate(tree.iter_entries_by_dir()):
2587
 
                pb.update(gettext("Building tree"), num - len(deferred_contents), total)
 
2470
                enumerate(tree.inventory.iter_entries_by_dir()):
 
2471
                pb.update("Building tree", num - len(deferred_contents), total)
2588
2472
                if entry.parent_id is None:
2589
2473
                    continue
2590
2474
                reparent = False
2596
2480
                    kind = file_kind(target_path)
2597
2481
                    if kind == "directory":
2598
2482
                        try:
2599
 
                            controldir.ControlDir.open(target_path)
 
2483
                            bzrdir.BzrDir.open(target_path)
2600
2484
                        except errors.NotBranchError:
2601
2485
                            pass
2602
2486
                        else:
2617
2501
                    executable = tree.is_executable(file_id, tree_path)
2618
2502
                    if executable:
2619
2503
                        tt.set_executability(executable, trans_id)
2620
 
                    trans_data = (trans_id, tree_path, entry.text_sha1)
 
2504
                    trans_data = (trans_id, tree_path)
2621
2505
                    deferred_contents.append((file_id, trans_data))
2622
2506
                else:
2623
2507
                    file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
2639
2523
            precomputed_delta = None
2640
2524
        conflicts = cook_conflicts(raw_conflicts, tt)
2641
2525
        for conflict in conflicts:
2642
 
            trace.warning(unicode(conflict))
 
2526
            warning(conflict)
2643
2527
        try:
2644
2528
            wt.add_conflicts(conflicts)
2645
2529
        except errors.UnsupportedOperation:
2668
2552
        unchanged = dict(unchanged)
2669
2553
        new_desired_files = []
2670
2554
        count = 0
2671
 
        for file_id, (trans_id, tree_path, text_sha1) in desired_files:
 
2555
        for file_id, (trans_id, tree_path) in desired_files:
2672
2556
            accelerator_path = unchanged.get(file_id)
2673
2557
            if accelerator_path is None:
2674
 
                new_desired_files.append((file_id,
2675
 
                    (trans_id, tree_path, text_sha1)))
 
2558
                new_desired_files.append((file_id, (trans_id, tree_path)))
2676
2559
                continue
2677
 
            pb.update(gettext('Adding file contents'), count + offset, total)
 
2560
            pb.update('Adding file contents', count + offset, total)
2678
2561
            if hardlink:
2679
2562
                tt.create_hardlink(accelerator_tree.abspath(accelerator_path),
2680
2563
                                   trans_id)
2685
2568
                    contents = filtered_output_bytes(contents, filters,
2686
2569
                        ContentFilterContext(tree_path, tree))
2687
2570
                try:
2688
 
                    tt.create_file(contents, trans_id, sha1=text_sha1)
 
2571
                    tt.create_file(contents, trans_id)
2689
2572
                finally:
2690
2573
                    try:
2691
2574
                        contents.close()
2694
2577
                        pass
2695
2578
            count += 1
2696
2579
        offset += count
2697
 
    for count, ((trans_id, tree_path, text_sha1), contents) in enumerate(
 
2580
    for count, ((trans_id, tree_path), contents) in enumerate(
2698
2581
            tree.iter_files_bytes(new_desired_files)):
2699
2582
        if wt.supports_content_filtering():
2700
2583
            filters = wt._content_filter_stack(tree_path)
2701
2584
            contents = filtered_output_bytes(contents, filters,
2702
2585
                ContentFilterContext(tree_path, tree))
2703
 
        tt.create_file(contents, trans_id, sha1=text_sha1)
2704
 
        pb.update(gettext('Adding file contents'), count + offset, total)
 
2586
        tt.create_file(contents, trans_id)
 
2587
        pb.update('Adding file contents', count + offset, total)
2705
2588
 
2706
2589
 
2707
2590
def _reparent_children(tt, old_parent, new_parent):
2839
2722
            return new_name
2840
2723
 
2841
2724
 
 
2725
def _entry_changes(file_id, entry, working_tree):
 
2726
    """Determine in which ways the inventory entry has changed.
 
2727
 
 
2728
    Returns booleans: has_contents, content_mod, meta_mod
 
2729
    has_contents means there are currently contents, but they differ
 
2730
    contents_mod means contents need to be modified
 
2731
    meta_mod means the metadata needs to be modified
 
2732
    """
 
2733
    cur_entry = working_tree.inventory[file_id]
 
2734
    try:
 
2735
        working_kind = working_tree.kind(file_id)
 
2736
        has_contents = True
 
2737
    except NoSuchFile:
 
2738
        has_contents = False
 
2739
        contents_mod = True
 
2740
        meta_mod = False
 
2741
    if has_contents is True:
 
2742
        if entry.kind != working_kind:
 
2743
            contents_mod, meta_mod = True, False
 
2744
        else:
 
2745
            cur_entry._read_tree_state(working_tree.id2path(file_id),
 
2746
                                       working_tree)
 
2747
            contents_mod, meta_mod = entry.detect_changes(cur_entry)
 
2748
            cur_entry._forget_tree_state()
 
2749
    return has_contents, contents_mod, meta_mod
 
2750
 
 
2751
 
2842
2752
def revert(working_tree, target_tree, filenames, backups=False,
2843
2753
           pb=None, change_reporter=None):
2844
2754
    """Revert a working tree's contents to those of a target tree."""
2854
2764
                unversioned_filter=working_tree.is_ignored)
2855
2765
            delta.report_changes(tt.iter_changes(), change_reporter)
2856
2766
        for conflict in conflicts:
2857
 
            trace.warning(unicode(conflict))
 
2767
            warning(conflict)
2858
2768
        pp.next_phase()
2859
2769
        tt.apply()
2860
2770
        working_tree.set_merge_modified(merge_modified)
2868
2778
def _prepare_revert_transform(working_tree, target_tree, tt, filenames,
2869
2779
                              backups, pp, basis_tree=None,
2870
2780
                              merge_modified=None):
2871
 
    child_pb = ui.ui_factory.nested_progress_bar()
 
2781
    child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2872
2782
    try:
2873
2783
        if merge_modified is None:
2874
2784
            merge_modified = working_tree.merge_modified()
2877
2787
                                      merge_modified, basis_tree)
2878
2788
    finally:
2879
2789
        child_pb.finished()
2880
 
    child_pb = ui.ui_factory.nested_progress_bar()
 
2790
    child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2881
2791
    try:
2882
2792
        raw_conflicts = resolve_conflicts(tt, child_pb,
2883
2793
            lambda t, c: conflict_pass(t, c, target_tree))
2891
2801
                 backups, merge_modified, basis_tree=None):
2892
2802
    if basis_tree is not None:
2893
2803
        basis_tree.lock_read()
2894
 
    # We ask the working_tree for its changes relative to the target, rather
2895
 
    # than the target changes relative to the working tree. Because WT4 has an
2896
 
    # optimizer to compare itself to a target, but no optimizer for the
2897
 
    # reverse.
2898
 
    change_list = working_tree.iter_changes(target_tree,
 
2804
    change_list = target_tree.iter_changes(working_tree,
2899
2805
        specific_files=specific_files, pb=pb)
2900
2806
    if target_tree.get_root_id() is None:
2901
2807
        skip_root = True
2905
2811
        deferred_files = []
2906
2812
        for id_num, (file_id, path, changed_content, versioned, parent, name,
2907
2813
                kind, executable) in enumerate(change_list):
2908
 
            target_path, wt_path = path
2909
 
            target_versioned, wt_versioned = versioned
2910
 
            target_parent, wt_parent = parent
2911
 
            target_name, wt_name = name
2912
 
            target_kind, wt_kind = kind
2913
 
            target_executable, wt_executable = executable
2914
 
            if skip_root and wt_parent is None:
 
2814
            if skip_root and file_id[0] is not None and parent[0] is None:
2915
2815
                continue
2916
2816
            trans_id = tt.trans_id_file_id(file_id)
2917
2817
            mode_id = None
2918
2818
            if changed_content:
2919
2819
                keep_content = False
2920
 
                if wt_kind == 'file' and (backups or target_kind is None):
 
2820
                if kind[0] == 'file' and (backups or kind[1] is None):
2921
2821
                    wt_sha1 = working_tree.get_file_sha1(file_id)
2922
2822
                    if merge_modified.get(file_id) != wt_sha1:
2923
2823
                        # acquire the basis tree lazily to prevent the
2926
2826
                        if basis_tree is None:
2927
2827
                            basis_tree = working_tree.basis_tree()
2928
2828
                            basis_tree.lock_read()
2929
 
                        if basis_tree.has_id(file_id):
 
2829
                        if file_id in basis_tree:
2930
2830
                            if wt_sha1 != basis_tree.get_file_sha1(file_id):
2931
2831
                                keep_content = True
2932
 
                        elif target_kind is None and not target_versioned:
 
2832
                        elif kind[1] is None and not versioned[1]:
2933
2833
                            keep_content = True
2934
 
                if wt_kind is not None:
 
2834
                if kind[0] is not None:
2935
2835
                    if not keep_content:
2936
2836
                        tt.delete_contents(trans_id)
2937
 
                    elif target_kind is not None:
2938
 
                        parent_trans_id = tt.trans_id_file_id(wt_parent)
 
2837
                    elif kind[1] is not None:
 
2838
                        parent_trans_id = tt.trans_id_file_id(parent[0])
2939
2839
                        backup_name = tt._available_backup_name(
2940
 
                            wt_name, parent_trans_id)
 
2840
                            name[0], parent_trans_id)
2941
2841
                        tt.adjust_path(backup_name, parent_trans_id, trans_id)
2942
 
                        new_trans_id = tt.create_path(wt_name, parent_trans_id)
2943
 
                        if wt_versioned and target_versioned:
 
2842
                        new_trans_id = tt.create_path(name[0], parent_trans_id)
 
2843
                        if versioned == (True, True):
2944
2844
                            tt.unversion_file(trans_id)
2945
2845
                            tt.version_file(file_id, new_trans_id)
2946
2846
                        # New contents should have the same unix perms as old
2947
2847
                        # contents
2948
2848
                        mode_id = trans_id
2949
2849
                        trans_id = new_trans_id
2950
 
                if target_kind in ('directory', 'tree-reference'):
 
2850
                if kind[1] in ('directory', 'tree-reference'):
2951
2851
                    tt.create_directory(trans_id)
2952
 
                    if target_kind == 'tree-reference':
 
2852
                    if kind[1] == 'tree-reference':
2953
2853
                        revision = target_tree.get_reference_revision(file_id,
2954
 
                                                                      target_path)
 
2854
                                                                      path[1])
2955
2855
                        tt.set_tree_reference(revision, trans_id)
2956
 
                elif target_kind == 'symlink':
 
2856
                elif kind[1] == 'symlink':
2957
2857
                    tt.create_symlink(target_tree.get_symlink_target(file_id),
2958
2858
                                      trans_id)
2959
 
                elif target_kind == 'file':
 
2859
                elif kind[1] == 'file':
2960
2860
                    deferred_files.append((file_id, (trans_id, mode_id)))
2961
2861
                    if basis_tree is None:
2962
2862
                        basis_tree = working_tree.basis_tree()
2963
2863
                        basis_tree.lock_read()
2964
2864
                    new_sha1 = target_tree.get_file_sha1(file_id)
2965
 
                    if (basis_tree.has_id(file_id) and
2966
 
                        new_sha1 == basis_tree.get_file_sha1(file_id)):
 
2865
                    if (file_id in basis_tree and new_sha1 ==
 
2866
                        basis_tree.get_file_sha1(file_id)):
2967
2867
                        if file_id in merge_modified:
2968
2868
                            del merge_modified[file_id]
2969
2869
                    else:
2970
2870
                        merge_modified[file_id] = new_sha1
2971
2871
 
2972
2872
                    # preserve the execute bit when backing up
2973
 
                    if keep_content and wt_executable == target_executable:
2974
 
                        tt.set_executability(target_executable, trans_id)
2975
 
                elif target_kind is not None:
2976
 
                    raise AssertionError(target_kind)
2977
 
            if not wt_versioned and target_versioned:
 
2873
                    if keep_content and executable[0] == executable[1]:
 
2874
                        tt.set_executability(executable[1], trans_id)
 
2875
                elif kind[1] is not None:
 
2876
                    raise AssertionError(kind[1])
 
2877
            if versioned == (False, True):
2978
2878
                tt.version_file(file_id, trans_id)
2979
 
            if wt_versioned and not target_versioned:
 
2879
            if versioned == (True, False):
2980
2880
                tt.unversion_file(trans_id)
2981
 
            if (target_name is not None and
2982
 
                (wt_name != target_name or wt_parent != target_parent)):
2983
 
                if target_name == '' and target_parent is None:
 
2881
            if (name[1] is not None and
 
2882
                (name[0] != name[1] or parent[0] != parent[1])):
 
2883
                if name[1] == '' and parent[1] is None:
2984
2884
                    parent_trans = ROOT_PARENT
2985
2885
                else:
2986
 
                    parent_trans = tt.trans_id_file_id(target_parent)
2987
 
                if wt_parent is None and wt_versioned:
2988
 
                    tt.adjust_root_path(target_name, parent_trans)
 
2886
                    parent_trans = tt.trans_id_file_id(parent[1])
 
2887
                if parent[0] is None and versioned[0]:
 
2888
                    tt.adjust_root_path(name[1], parent_trans)
2989
2889
                else:
2990
 
                    tt.adjust_path(target_name, parent_trans, trans_id)
2991
 
            if wt_executable != target_executable and target_kind == "file":
2992
 
                tt.set_executability(target_executable, trans_id)
 
2890
                    tt.adjust_path(name[1], parent_trans, trans_id)
 
2891
            if executable[0] != executable[1] and kind[1] == "file":
 
2892
                tt.set_executability(executable[1], trans_id)
2993
2893
        if working_tree.supports_content_filtering():
2994
2894
            for index, ((trans_id, mode_id), bytes) in enumerate(
2995
2895
                target_tree.iter_files_bytes(deferred_files)):
3021
2921
    pb = ui.ui_factory.nested_progress_bar()
3022
2922
    try:
3023
2923
        for n in range(10):
3024
 
            pb.update(gettext('Resolution pass'), n+1, 10)
 
2924
            pb.update('Resolution pass', n+1, 10)
3025
2925
            conflicts = tt.find_conflicts()
3026
2926
            if len(conflicts) == 0:
3027
2927
                return new_conflicts
3051
2951
                existing_file, new_file = conflict[2], conflict[1]
3052
2952
            else:
3053
2953
                existing_file, new_file = conflict[1], conflict[2]
3054
 
            new_name = tt.final_name(existing_file) + '.moved'
 
2954
            new_name = tt.final_name(existing_file)+'.moved'
3055
2955
            tt.adjust_path(new_name, final_parent, existing_file)
3056
2956
            new_conflicts.add((c_type, 'Moved existing file to',
3057
2957
                               existing_file, new_file))
3098
2998
                        file_id = tt.final_file_id(trans_id)
3099
2999
                        if file_id is None:
3100
3000
                            file_id = tt.inactive_file_id(trans_id)
3101
 
                        _, entry = path_tree.iter_entries_by_dir(
3102
 
                            [file_id]).next()
 
3001
                        entry = path_tree.inventory[file_id]
3103
3002
                        # special-case the other tree root (move its
3104
3003
                        # children to current root)
3105
3004
                        if entry.parent_id is None:
3120
3019
        elif c_type == 'unversioned parent':
3121
3020
            file_id = tt.inactive_file_id(conflict[1])
3122
3021
            # special-case the other tree root (move its children instead)
3123
 
            if path_tree and path_tree.has_id(file_id):
3124
 
                if path_tree.path2id('') == file_id:
3125
 
                    # This is the root entry, skip it
 
3022
            if path_tree and file_id in path_tree:
 
3023
                if path_tree.inventory[file_id].parent_id is None:
3126
3024
                    continue
3127
3025
            tt.version_file(file_id, conflict[1])
3128
3026
            new_conflicts.add((c_type, 'Versioned directory', conflict[1]))
3144
3042
 
3145
3043
def cook_conflicts(raw_conflicts, tt):
3146
3044
    """Generate a list of cooked conflicts, sorted by file path"""
 
3045
    from bzrlib.conflicts import Conflict
3147
3046
    conflict_iter = iter_cook_conflicts(raw_conflicts, tt)
3148
 
    return sorted(conflict_iter, key=conflicts.Conflict.sort_key)
 
3047
    return sorted(conflict_iter, key=Conflict.sort_key)
3149
3048
 
3150
3049
 
3151
3050
def iter_cook_conflicts(raw_conflicts, tt):
 
3051
    from bzrlib.conflicts import Conflict
3152
3052
    fp = FinalPaths(tt)
3153
3053
    for conflict in raw_conflicts:
3154
3054
        c_type = conflict[0]
3156
3056
        modified_path = fp.get_path(conflict[2])
3157
3057
        modified_id = tt.final_file_id(conflict[2])
3158
3058
        if len(conflict) == 3:
3159
 
            yield conflicts.Conflict.factory(
3160
 
                c_type, action=action, path=modified_path, file_id=modified_id)
 
3059
            yield Conflict.factory(c_type, action=action, path=modified_path,
 
3060
                                     file_id=modified_id)
3161
3061
 
3162
3062
        else:
3163
3063
            conflicting_path = fp.get_path(conflict[3])
3164
3064
            conflicting_id = tt.final_file_id(conflict[3])
3165
 
            yield conflicts.Conflict.factory(
3166
 
                c_type, action=action, path=modified_path,
3167
 
                file_id=modified_id,
3168
 
                conflict_path=conflicting_path,
3169
 
                conflict_file_id=conflicting_id)
 
3065
            yield Conflict.factory(c_type, action=action, path=modified_path,
 
3066
                                   file_id=modified_id,
 
3067
                                   conflict_path=conflicting_path,
 
3068
                                   conflict_file_id=conflicting_id)
3170
3069
 
3171
3070
 
3172
3071
class _FileMover(object):