~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transform.py

(vila) Fix bug #701212,
 don't set the tag dict of the master branch you are updating from. (John A
 Meinel)

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
19
19
from stat import S_ISREG, S_IEXEC
20
20
import time
21
21
 
 
22
import bzrlib
22
23
from bzrlib import (
23
24
    errors,
24
25
    lazy_import,
25
26
    registry,
26
 
    trace,
27
27
    tree,
28
28
    )
29
29
lazy_import.lazy_import(globals(), """
32
32
    bencode,
33
33
    bzrdir,
34
34
    commit,
35
 
    conflicts,
36
35
    delta,
37
36
    errors,
38
37
    inventory,
39
38
    multiparent,
40
39
    osutils,
41
40
    revision as _mod_revision,
 
41
    trace,
42
42
    ui,
43
 
    urlutils,
44
43
    )
45
44
""")
46
45
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
48
47
                           ExistingLimbo, ImmortalLimbo, NoFinalPath,
49
48
                           UnableCreateSymlink)
50
49
from bzrlib.filters import filtered_output_bytes, ContentFilterContext
 
50
from bzrlib.inventory import InventoryEntry
51
51
from bzrlib.osutils import (
52
52
    delete_any,
53
53
    file_kind,
63
63
    deprecated_in,
64
64
    deprecated_method,
65
65
    )
 
66
from bzrlib.trace import warning
 
67
import bzrlib.ui
 
68
import bzrlib.urlutils as urlutils
66
69
 
67
70
 
68
71
ROOT_PARENT = "root-parent"
103
106
        self._new_parent = {}
104
107
        # mapping of trans_id with new contents -> new file_kind
105
108
        self._new_contents = {}
106
 
        # mapping of trans_id => (sha1 of content, stat_value)
107
 
        self._observed_sha1s = {}
108
109
        # Set of trans_ids whose contents will be removed
109
110
        self._removed_contents = set()
110
111
        # Mapping of trans_id -> new execute-bit value
138
139
        # A counter of how many files have been renamed
139
140
        self.rename_count = 0
140
141
 
141
 
    def __enter__(self):
142
 
        """Support Context Manager API."""
143
 
        return self
144
 
 
145
 
    def __exit__(self, exc_type, exc_val, exc_tb):
146
 
        """Support Context Manager API."""
147
 
        self.finalize()
148
 
 
149
142
    def finalize(self):
150
143
        """Release the working tree lock, if held.
151
144
 
226
219
        This means that the old root trans-id becomes obsolete, so it is
227
220
        recommended only to invoke this after the root trans-id has become
228
221
        irrelevant.
229
 
 
230
222
        """
231
223
        new_roots = [k for k, v in self._new_parent.iteritems() if v is
232
224
                     ROOT_PARENT]
233
225
        if len(new_roots) < 1:
234
 
            if self.final_kind(self.root) is None:
235
 
                self.cancel_deletion(self.root)
236
 
            if self.final_file_id(self.root) is None:
237
 
                self.version_file(self.tree_file_id(self.root),
238
 
                                     self.root)
239
226
            return
240
227
        if len(new_roots) != 1:
241
228
            raise ValueError('A tree cannot have two roots!')
243
230
            self._new_root = new_roots[0]
244
231
            return
245
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
 
246
238
        # unversion the new root's directory.
247
 
        if self.final_kind(self._new_root) is None:
248
 
            file_id = self.final_file_id(old_new_root)
249
 
        else:
250
 
            file_id = self.final_file_id(self._new_root)
 
239
        file_id = self.final_file_id(old_new_root)
251
240
        if old_new_root in self._new_id:
252
241
            self.cancel_versioning(old_new_root)
253
242
        else:
257
246
        if (self.tree_file_id(self._new_root) is not None and
258
247
            self._new_root not in self._removed_id):
259
248
            self.unversion_file(self._new_root)
260
 
        if file_id is not None:
261
 
            self.version_file(file_id, self._new_root)
 
249
        self.version_file(file_id, self._new_root)
262
250
 
263
251
        # Now move children of new root into old root directory.
264
252
        # Ensure all children are registered with the transaction, but don't
398
386
        return sorted(FinalPaths(self).get_paths(new_ids))
399
387
 
400
388
    def _inventory_altered(self):
401
 
        """Determine which trans_ids need new Inventory entries.
402
 
 
403
 
        An new entry is needed when anything that would be reflected by an
404
 
        inventory entry changes, including file name, file_id, parent file_id,
405
 
        file kind, and the execute bit.
406
 
 
407
 
        Some care is taken to return entries with real changes, not cases
408
 
        where the value is deleted and then restored to its original value,
409
 
        but some actually unchanged values may be returned.
410
 
 
411
 
        :returns: A list of (path, trans_id) for all items requiring an
412
 
            inventory change. Ordered by path.
413
 
        """
414
 
        changed_ids = set()
415
 
        # Find entries whose file_ids are new (or changed).
416
 
        new_file_id = set(t for t in self._new_id
417
 
                          if self._new_id[t] != self.tree_file_id(t))
418
 
        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,
419
392
                       self._new_executability]:
420
 
            changed_ids.update(id_set)
421
 
        # removing implies a kind change
 
393
            new_ids.update(id_set)
422
394
        changed_kind = set(self._removed_contents)
423
 
        # so does adding
424
395
        changed_kind.intersection_update(self._new_contents)
425
 
        # Ignore entries that are already known to have changed.
426
 
        changed_kind.difference_update(changed_ids)
427
 
        #  to keep only the truly changed ones
 
396
        changed_kind.difference_update(new_ids)
428
397
        changed_kind = (t for t in changed_kind
429
398
                        if self.tree_kind(t) != self.final_kind(t))
430
 
        # all kind changes will alter the inventory
431
 
        changed_ids.update(changed_kind)
432
 
        # To find entries with changed parent_ids, find parents which existed,
433
 
        # but changed file_id.
434
 
        changed_file_id = set(t for t in new_file_id if t in self._removed_id)
435
 
        # Now add all their children to the set.
436
 
        for parent_trans_id in new_file_id:
437
 
            changed_ids.update(self.iter_tree_children(parent_trans_id))
438
 
        return sorted(FinalPaths(self).get_paths(changed_ids))
 
399
        new_ids.update(changed_kind)
 
400
        return sorted(FinalPaths(self).get_paths(new_ids))
439
401
 
440
402
    def final_kind(self, trans_id):
441
403
        """Determine the final file kind, after any changes applied.
566
528
        for trans_id in self._removed_id:
567
529
            file_id = self.tree_file_id(trans_id)
568
530
            if file_id is not None:
569
 
                # XXX: This seems like something that should go via a different
570
 
                #      indirection.
571
531
                if self._tree.inventory[file_id].kind == 'directory':
572
532
                    parents.append(trans_id)
573
533
            elif self.tree_kind(trans_id) == 'directory':
668
628
            if kind is None:
669
629
                conflicts.append(('versioning no contents', trans_id))
670
630
                continue
671
 
            if not inventory.InventoryEntry.versionable_kind(kind):
 
631
            if not InventoryEntry.versionable_kind(kind):
672
632
                conflicts.append(('versioning bad kind', trans_id, kind))
673
633
        return conflicts
674
634
 
793
753
        return trans_id
794
754
 
795
755
    def new_file(self, name, parent_id, contents, file_id=None,
796
 
                 executable=None, sha1=None):
 
756
                 executable=None):
797
757
        """Convenience method to create files.
798
758
 
799
759
        name is the name of the file to create.
806
766
        trans_id = self._new_entry(name, parent_id, file_id)
807
767
        # TODO: rather than scheduling a set_executable call,
808
768
        # have create_file create the file with the right mode.
809
 
        self.create_file(contents, trans_id, sha1=sha1)
 
769
        self.create_file(contents, trans_id)
810
770
        if executable is not None:
811
771
            self.set_executability(executable, trans_id)
812
772
        return trans_id
1194
1154
        self._deletiondir = None
1195
1155
        # A mapping of transform ids to their limbo filename
1196
1156
        self._limbo_files = {}
1197
 
        self._possibly_stale_limbo_files = set()
1198
1157
        # A mapping of transform ids to a set of the transform ids of children
1199
1158
        # that their limbo directory has
1200
1159
        self._limbo_children = {}
1213
1172
        if self._tree is None:
1214
1173
            return
1215
1174
        try:
1216
 
            limbo_paths = self._limbo_files.values() + list(
1217
 
                self._possibly_stale_limbo_files)
1218
 
            limbo_paths = sorted(limbo_paths, reverse=True)
1219
 
            for path in limbo_paths:
1220
 
                try:
1221
 
                    delete_any(path)
1222
 
                except OSError, e:
1223
 
                    if e.errno != errno.ENOENT:
1224
 
                        raise
1225
 
                    # XXX: warn? perhaps we just got interrupted at an
1226
 
                    # inconvenient moment, but perhaps files are disappearing
1227
 
                    # 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)
1228
1180
            try:
1229
1181
                delete_any(self._limbodir)
1230
1182
            except OSError:
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)
1728
1675
        """
1729
1676
        if not no_conflicts:
1730
1677
            self._check_malformed()
1731
 
        child_pb = ui.ui_factory.nested_progress_bar()
 
1678
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1732
1679
        try:
1733
1680
            if precomputed_delta is None:
1734
1681
                child_pb.update('Apply phase', 0, 2)
1754
1701
        finally:
1755
1702
            child_pb.finished()
1756
1703
        self._tree.apply_inventory_delta(inventory_delta)
1757
 
        self._apply_observed_sha1s()
1758
1704
        self._done = True
1759
1705
        self.finalize()
1760
1706
        return _TransformResults(modified_paths, self.rename_count)
1762
1708
    def _generate_inventory_delta(self):
1763
1709
        """Generate an inventory delta for the current transform."""
1764
1710
        inventory_delta = []
1765
 
        child_pb = ui.ui_factory.nested_progress_bar()
 
1711
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1766
1712
        new_paths = self._inventory_altered()
1767
1713
        total_entries = len(new_paths) + len(self._removed_id)
1768
1714
        try:
1830
1776
        """
1831
1777
        tree_paths = list(self._tree_path_ids.iteritems())
1832
1778
        tree_paths.sort(reverse=True)
1833
 
        child_pb = ui.ui_factory.nested_progress_bar()
 
1779
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1834
1780
        try:
1835
 
            for num, (path, trans_id) in enumerate(tree_paths):
1836
 
                # do not attempt to move root into a subdirectory of itself.
1837
 
                if path == '':
1838
 
                    continue
 
1781
            for num, data in enumerate(tree_paths):
 
1782
                path, trans_id = data
1839
1783
                child_pb.update('removing file', num, len(tree_paths))
1840
1784
                full_path = self._tree.abspath(path)
1841
1785
                if trans_id in self._removed_contents:
1867
1811
        modified_paths = []
1868
1812
        new_path_file_ids = dict((t, self.final_file_id(t)) for p, t in
1869
1813
                                 new_paths)
1870
 
        child_pb = ui.ui_factory.nested_progress_bar()
 
1814
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1871
1815
        try:
1872
1816
            for num, (path, trans_id) in enumerate(new_paths):
1873
1817
                if (num % 10) == 0:
1882
1826
                            raise
1883
1827
                    else:
1884
1828
                        self.rename_count += 1
1885
 
                    # TODO: if trans_id in self._observed_sha1s, we should
1886
 
                    #       re-stat the final target, since ctime will be
1887
 
                    #       updated by the change.
1888
1829
                if (trans_id in self._new_contents or
1889
1830
                    self.path_changed(trans_id)):
1890
1831
                    if trans_id in self._new_contents:
1891
1832
                        modified_paths.append(full_path)
1892
1833
                if trans_id in self._new_executability:
1893
1834
                    self._set_executability(path, trans_id)
1894
 
                if trans_id in self._observed_sha1s:
1895
 
                    o_sha1, o_st_val = self._observed_sha1s[trans_id]
1896
 
                    st = osutils.lstat(full_path)
1897
 
                    self._observed_sha1s[trans_id] = (o_sha1, st)
1898
1835
        finally:
1899
1836
            child_pb.finished()
1900
 
        for path, trans_id in new_paths:
1901
 
            # new_paths includes stuff like workingtree conflicts. Only the
1902
 
            # stuff in new_contents actually comes from limbo.
1903
 
            if trans_id in self._limbo_files:
1904
 
                del self._limbo_files[trans_id]
1905
1837
        self._new_contents.clear()
1906
1838
        return modified_paths
1907
1839
 
1908
 
    def _apply_observed_sha1s(self):
1909
 
        """After we have finished renaming everything, update observed sha1s
1910
 
 
1911
 
        This has to be done after self._tree.apply_inventory_delta, otherwise
1912
 
        it doesn't know anything about the files we are updating. Also, we want
1913
 
        to do this as late as possible, so that most entries end up cached.
1914
 
        """
1915
 
        # TODO: this doesn't update the stat information for directories. So
1916
 
        #       the first 'bzr status' will still need to rewrite
1917
 
        #       .bzr/checkout/dirstate. However, we at least don't need to
1918
 
        #       re-read all of the files.
1919
 
        # TODO: If the operation took a while, we could do a time.sleep(3) here
1920
 
        #       to allow the clock to tick over and ensure we won't have any
1921
 
        #       problems. (we could observe start time, and finish time, and if
1922
 
        #       it is less than eg 10% overhead, add a sleep call.)
1923
 
        paths = FinalPaths(self)
1924
 
        for trans_id, observed in self._observed_sha1s.iteritems():
1925
 
            path = paths.get_path(trans_id)
1926
 
            # We could get the file_id, but dirstate prefers to use the path
1927
 
            # anyway, and it is 'cheaper' to determine.
1928
 
            # file_id = self._new_id[trans_id]
1929
 
            self._tree._observed_sha1(None, path, observed)
1930
 
 
1931
1840
 
1932
1841
class TransformPreview(DiskTreeTransform):
1933
1842
    """A TreeTransform for generating preview trees.
1949
1858
        path = self._tree_id_paths.get(trans_id)
1950
1859
        if path is None:
1951
1860
            return None
1952
 
        kind = self._tree.path_content_summary(path)[0]
1953
 
        if kind == 'missing':
1954
 
            kind = None
1955
 
        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
1956
1866
 
1957
1867
    def _set_mode(self, trans_id, mode_id, typefunc):
1958
1868
        """Set the mode of new file contents.
1982
1892
        raise NotImplementedError(self.new_orphan)
1983
1893
 
1984
1894
 
1985
 
class _PreviewTree(tree.InventoryTree):
 
1895
class _PreviewTree(tree.Tree):
1986
1896
    """Partial implementation of Tree to support show_diff_trees"""
1987
1897
 
1988
1898
    def __init__(self, transform):
2017
1927
                yield self._get_repository().revision_tree(revision_id)
2018
1928
 
2019
1929
    def _get_file_revision(self, file_id, vf, tree_revision):
2020
 
        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
2021
1931
                       self._iter_parent_trees()]
2022
1932
        vf.add_lines((file_id, tree_revision), parent_keys,
2023
1933
                     self.get_file_lines(file_id))
2027
1937
            vf.fallback_versionedfiles.append(base_vf)
2028
1938
        return tree_revision
2029
1939
 
2030
 
    def _stat_limbo_file(self, file_id=None, trans_id=None):
2031
 
        if trans_id is None:
2032
 
            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)
2033
1942
        name = self._transform._limbo_name(trans_id)
2034
1943
        return os.lstat(name)
2035
1944
 
2250
2159
 
2251
2160
    def get_file_size(self, file_id):
2252
2161
        """See Tree.get_file_size"""
2253
 
        trans_id = self._transform.trans_id_file_id(file_id)
2254
 
        kind = self._transform.final_kind(trans_id)
2255
 
        if kind != 'file':
2256
 
            return None
2257
 
        if trans_id in self._transform._new_contents:
2258
 
            return self._stat_limbo_file(trans_id=trans_id).st_size
2259
2162
        if self.kind(file_id) == 'file':
2260
2163
            return self._transform._tree.get_file_size(file_id)
2261
2164
        else:
2289
2192
            except errors.NoSuchId:
2290
2193
                return False
2291
2194
 
2292
 
    def has_filename(self, path):
2293
 
        trans_id = self._path2trans_id(path)
2294
 
        if trans_id in self._transform._new_contents:
2295
 
            return True
2296
 
        elif trans_id in self._transform._removed_contents:
2297
 
            return False
2298
 
        else:
2299
 
            return self._transform._tree.has_filename(path)
2300
 
 
2301
2195
    def path_content_summary(self, path):
2302
2196
        trans_id = self._path2trans_id(path)
2303
2197
        tt = self._transform
2391
2285
                                   self.get_file(file_id).readlines(),
2392
2286
                                   default_revision)
2393
2287
 
2394
 
    def get_symlink_target(self, file_id, path=None):
 
2288
    def get_symlink_target(self, file_id):
2395
2289
        """See Tree.get_symlink_target"""
2396
2290
        if not self._content_change(file_id):
2397
2291
            return self._transform._tree.get_symlink_target(file_id)
2535
2429
        if num > 0:  # more than just a root
2536
2430
            raise errors.WorkingTreeAlreadyPopulated(base=wt.basedir)
2537
2431
    file_trans_id = {}
2538
 
    top_pb = ui.ui_factory.nested_progress_bar()
 
2432
    top_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2539
2433
    pp = ProgressPhase("Build phase", 2, top_pb)
2540
2434
    if tree.inventory.root is not None:
2541
2435
        # This is kind of a hack: we should be altering the root
2554
2448
        pp.next_phase()
2555
2449
        file_trans_id[wt.get_root_id()] = \
2556
2450
            tt.trans_id_tree_file_id(wt.get_root_id())
2557
 
        pb = ui.ui_factory.nested_progress_bar()
 
2451
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
2558
2452
        try:
2559
2453
            deferred_contents = []
2560
2454
            num = 0
2607
2501
                    executable = tree.is_executable(file_id, tree_path)
2608
2502
                    if executable:
2609
2503
                        tt.set_executability(executable, trans_id)
2610
 
                    trans_data = (trans_id, tree_path, entry.text_sha1)
 
2504
                    trans_data = (trans_id, tree_path)
2611
2505
                    deferred_contents.append((file_id, trans_data))
2612
2506
                else:
2613
2507
                    file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
2629
2523
            precomputed_delta = None
2630
2524
        conflicts = cook_conflicts(raw_conflicts, tt)
2631
2525
        for conflict in conflicts:
2632
 
            trace.warning(unicode(conflict))
 
2526
            warning(conflict)
2633
2527
        try:
2634
2528
            wt.add_conflicts(conflicts)
2635
2529
        except errors.UnsupportedOperation:
2658
2552
        unchanged = dict(unchanged)
2659
2553
        new_desired_files = []
2660
2554
        count = 0
2661
 
        for file_id, (trans_id, tree_path, text_sha1) in desired_files:
 
2555
        for file_id, (trans_id, tree_path) in desired_files:
2662
2556
            accelerator_path = unchanged.get(file_id)
2663
2557
            if accelerator_path is None:
2664
 
                new_desired_files.append((file_id,
2665
 
                    (trans_id, tree_path, text_sha1)))
 
2558
                new_desired_files.append((file_id, (trans_id, tree_path)))
2666
2559
                continue
2667
2560
            pb.update('Adding file contents', count + offset, total)
2668
2561
            if hardlink:
2675
2568
                    contents = filtered_output_bytes(contents, filters,
2676
2569
                        ContentFilterContext(tree_path, tree))
2677
2570
                try:
2678
 
                    tt.create_file(contents, trans_id, sha1=text_sha1)
 
2571
                    tt.create_file(contents, trans_id)
2679
2572
                finally:
2680
2573
                    try:
2681
2574
                        contents.close()
2684
2577
                        pass
2685
2578
            count += 1
2686
2579
        offset += count
2687
 
    for count, ((trans_id, tree_path, text_sha1), contents) in enumerate(
 
2580
    for count, ((trans_id, tree_path), contents) in enumerate(
2688
2581
            tree.iter_files_bytes(new_desired_files)):
2689
2582
        if wt.supports_content_filtering():
2690
2583
            filters = wt._content_filter_stack(tree_path)
2691
2584
            contents = filtered_output_bytes(contents, filters,
2692
2585
                ContentFilterContext(tree_path, tree))
2693
 
        tt.create_file(contents, trans_id, sha1=text_sha1)
 
2586
        tt.create_file(contents, trans_id)
2694
2587
        pb.update('Adding file contents', count + offset, total)
2695
2588
 
2696
2589
 
2871
2764
                unversioned_filter=working_tree.is_ignored)
2872
2765
            delta.report_changes(tt.iter_changes(), change_reporter)
2873
2766
        for conflict in conflicts:
2874
 
            trace.warning(unicode(conflict))
 
2767
            warning(conflict)
2875
2768
        pp.next_phase()
2876
2769
        tt.apply()
2877
2770
        working_tree.set_merge_modified(merge_modified)
2885
2778
def _prepare_revert_transform(working_tree, target_tree, tt, filenames,
2886
2779
                              backups, pp, basis_tree=None,
2887
2780
                              merge_modified=None):
2888
 
    child_pb = ui.ui_factory.nested_progress_bar()
 
2781
    child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2889
2782
    try:
2890
2783
        if merge_modified is None:
2891
2784
            merge_modified = working_tree.merge_modified()
2894
2787
                                      merge_modified, basis_tree)
2895
2788
    finally:
2896
2789
        child_pb.finished()
2897
 
    child_pb = ui.ui_factory.nested_progress_bar()
 
2790
    child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2898
2791
    try:
2899
2792
        raw_conflicts = resolve_conflicts(tt, child_pb,
2900
2793
            lambda t, c: conflict_pass(t, c, target_tree))
2908
2801
                 backups, merge_modified, basis_tree=None):
2909
2802
    if basis_tree is not None:
2910
2803
        basis_tree.lock_read()
2911
 
    # We ask the working_tree for its changes relative to the target, rather
2912
 
    # than the target changes relative to the working tree. Because WT4 has an
2913
 
    # optimizer to compare itself to a target, but no optimizer for the
2914
 
    # reverse.
2915
 
    change_list = working_tree.iter_changes(target_tree,
 
2804
    change_list = target_tree.iter_changes(working_tree,
2916
2805
        specific_files=specific_files, pb=pb)
2917
2806
    if target_tree.get_root_id() is None:
2918
2807
        skip_root = True
2922
2811
        deferred_files = []
2923
2812
        for id_num, (file_id, path, changed_content, versioned, parent, name,
2924
2813
                kind, executable) in enumerate(change_list):
2925
 
            target_path, wt_path = path
2926
 
            target_versioned, wt_versioned = versioned
2927
 
            target_parent, wt_parent = parent
2928
 
            target_name, wt_name = name
2929
 
            target_kind, wt_kind = kind
2930
 
            target_executable, wt_executable = executable
2931
 
            if skip_root and wt_parent is None:
 
2814
            if skip_root and file_id[0] is not None and parent[0] is None:
2932
2815
                continue
2933
2816
            trans_id = tt.trans_id_file_id(file_id)
2934
2817
            mode_id = None
2935
2818
            if changed_content:
2936
2819
                keep_content = False
2937
 
                if wt_kind == 'file' and (backups or target_kind is None):
 
2820
                if kind[0] == 'file' and (backups or kind[1] is None):
2938
2821
                    wt_sha1 = working_tree.get_file_sha1(file_id)
2939
2822
                    if merge_modified.get(file_id) != wt_sha1:
2940
2823
                        # acquire the basis tree lazily to prevent the
2943
2826
                        if basis_tree is None:
2944
2827
                            basis_tree = working_tree.basis_tree()
2945
2828
                            basis_tree.lock_read()
2946
 
                        if basis_tree.has_id(file_id):
 
2829
                        if file_id in basis_tree:
2947
2830
                            if wt_sha1 != basis_tree.get_file_sha1(file_id):
2948
2831
                                keep_content = True
2949
 
                        elif target_kind is None and not target_versioned:
 
2832
                        elif kind[1] is None and not versioned[1]:
2950
2833
                            keep_content = True
2951
 
                if wt_kind is not None:
 
2834
                if kind[0] is not None:
2952
2835
                    if not keep_content:
2953
2836
                        tt.delete_contents(trans_id)
2954
 
                    elif target_kind is not None:
2955
 
                        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])
2956
2839
                        backup_name = tt._available_backup_name(
2957
 
                            wt_name, parent_trans_id)
 
2840
                            name[0], parent_trans_id)
2958
2841
                        tt.adjust_path(backup_name, parent_trans_id, trans_id)
2959
 
                        new_trans_id = tt.create_path(wt_name, parent_trans_id)
2960
 
                        if wt_versioned and target_versioned:
 
2842
                        new_trans_id = tt.create_path(name[0], parent_trans_id)
 
2843
                        if versioned == (True, True):
2961
2844
                            tt.unversion_file(trans_id)
2962
2845
                            tt.version_file(file_id, new_trans_id)
2963
2846
                        # New contents should have the same unix perms as old
2964
2847
                        # contents
2965
2848
                        mode_id = trans_id
2966
2849
                        trans_id = new_trans_id
2967
 
                if target_kind in ('directory', 'tree-reference'):
 
2850
                if kind[1] in ('directory', 'tree-reference'):
2968
2851
                    tt.create_directory(trans_id)
2969
 
                    if target_kind == 'tree-reference':
 
2852
                    if kind[1] == 'tree-reference':
2970
2853
                        revision = target_tree.get_reference_revision(file_id,
2971
 
                                                                      target_path)
 
2854
                                                                      path[1])
2972
2855
                        tt.set_tree_reference(revision, trans_id)
2973
 
                elif target_kind == 'symlink':
 
2856
                elif kind[1] == 'symlink':
2974
2857
                    tt.create_symlink(target_tree.get_symlink_target(file_id),
2975
2858
                                      trans_id)
2976
 
                elif target_kind == 'file':
 
2859
                elif kind[1] == 'file':
2977
2860
                    deferred_files.append((file_id, (trans_id, mode_id)))
2978
2861
                    if basis_tree is None:
2979
2862
                        basis_tree = working_tree.basis_tree()
2980
2863
                        basis_tree.lock_read()
2981
2864
                    new_sha1 = target_tree.get_file_sha1(file_id)
2982
 
                    if (basis_tree.has_id(file_id) and
2983
 
                        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)):
2984
2867
                        if file_id in merge_modified:
2985
2868
                            del merge_modified[file_id]
2986
2869
                    else:
2987
2870
                        merge_modified[file_id] = new_sha1
2988
2871
 
2989
2872
                    # preserve the execute bit when backing up
2990
 
                    if keep_content and wt_executable == target_executable:
2991
 
                        tt.set_executability(target_executable, trans_id)
2992
 
                elif target_kind is not None:
2993
 
                    raise AssertionError(target_kind)
2994
 
            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):
2995
2878
                tt.version_file(file_id, trans_id)
2996
 
            if wt_versioned and not target_versioned:
 
2879
            if versioned == (True, False):
2997
2880
                tt.unversion_file(trans_id)
2998
 
            if (target_name is not None and
2999
 
                (wt_name != target_name or wt_parent != target_parent)):
3000
 
                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:
3001
2884
                    parent_trans = ROOT_PARENT
3002
2885
                else:
3003
 
                    parent_trans = tt.trans_id_file_id(target_parent)
3004
 
                if wt_parent is None and wt_versioned:
3005
 
                    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)
3006
2889
                else:
3007
 
                    tt.adjust_path(target_name, parent_trans, trans_id)
3008
 
            if wt_executable != target_executable and target_kind == "file":
3009
 
                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)
3010
2893
        if working_tree.supports_content_filtering():
3011
2894
            for index, ((trans_id, mode_id), bytes) in enumerate(
3012
2895
                target_tree.iter_files_bytes(deferred_files)):
3115
2998
                        file_id = tt.final_file_id(trans_id)
3116
2999
                        if file_id is None:
3117
3000
                            file_id = tt.inactive_file_id(trans_id)
3118
 
                        _, entry = path_tree.iter_entries_by_dir(
3119
 
                            [file_id]).next()
 
3001
                        entry = path_tree.inventory[file_id]
3120
3002
                        # special-case the other tree root (move its
3121
3003
                        # children to current root)
3122
3004
                        if entry.parent_id is None:
3137
3019
        elif c_type == 'unversioned parent':
3138
3020
            file_id = tt.inactive_file_id(conflict[1])
3139
3021
            # special-case the other tree root (move its children instead)
3140
 
            if path_tree and path_tree.has_id(file_id):
3141
 
                if path_tree.path2id('') == file_id:
3142
 
                    # 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:
3143
3024
                    continue
3144
3025
            tt.version_file(file_id, conflict[1])
3145
3026
            new_conflicts.add((c_type, 'Versioned directory', conflict[1]))
3161
3042
 
3162
3043
def cook_conflicts(raw_conflicts, tt):
3163
3044
    """Generate a list of cooked conflicts, sorted by file path"""
 
3045
    from bzrlib.conflicts import Conflict
3164
3046
    conflict_iter = iter_cook_conflicts(raw_conflicts, tt)
3165
 
    return sorted(conflict_iter, key=conflicts.Conflict.sort_key)
 
3047
    return sorted(conflict_iter, key=Conflict.sort_key)
3166
3048
 
3167
3049
 
3168
3050
def iter_cook_conflicts(raw_conflicts, tt):
 
3051
    from bzrlib.conflicts import Conflict
3169
3052
    fp = FinalPaths(tt)
3170
3053
    for conflict in raw_conflicts:
3171
3054
        c_type = conflict[0]
3173
3056
        modified_path = fp.get_path(conflict[2])
3174
3057
        modified_id = tt.final_file_id(conflict[2])
3175
3058
        if len(conflict) == 3:
3176
 
            yield conflicts.Conflict.factory(
3177
 
                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)
3178
3061
 
3179
3062
        else:
3180
3063
            conflicting_path = fp.get_path(conflict[3])
3181
3064
            conflicting_id = tt.final_file_id(conflict[3])
3182
 
            yield conflicts.Conflict.factory(
3183
 
                c_type, action=action, path=modified_path,
3184
 
                file_id=modified_id,
3185
 
                conflict_path=conflicting_path,
3186
 
                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)
3187
3069
 
3188
3070
 
3189
3071
class _FileMover(object):