~lifeless/bzr/index.range_map

« back to all changes in this revision

Viewing changes to bzrlib/transform.py

  • Committer: Robert Collins
  • Date: 2008-06-19 01:17:19 UTC
  • mfrom: (3218.1.277 +trunk)
  • Revision ID: robertc@robertcollins.net-20080619011719-1c4g4uxzzhdls2wf
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
    bzrdir,
26
26
    delta,
27
27
    errors,
28
 
    inventory
 
28
    inventory,
 
29
    revision as _mod_revision,
29
30
    )
30
31
""")
31
32
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
38
39
from bzrlib.progress import DummyProgress, ProgressPhase
39
40
from bzrlib.symbol_versioning import (
40
41
        deprecated_function,
41
 
        zero_fifteen,
42
 
        zero_ninety,
43
42
        )
44
43
from bzrlib.trace import mutter, warning
45
44
from bzrlib import tree
250
249
        This reflects only files that already exist, not ones that will be
251
250
        added by transactions.
252
251
        """
253
 
        path = self._tree.inventory.id2path(inventory_id)
 
252
        path = self._tree.id2path(inventory_id)
254
253
        return self.trans_id_tree_path(path)
255
254
 
256
255
    def trans_id_file_id(self, file_id):
354
353
        if typefunc(mode):
355
354
            os.chmod(self._limbo_name(trans_id), mode)
356
355
 
 
356
    def create_hardlink(self, path, trans_id):
 
357
        """Schedule creation of a hard link"""
 
358
        name = self._limbo_name(trans_id)
 
359
        try:
 
360
            os.link(path, name)
 
361
        except OSError, e:
 
362
            if e.errno != errno.EPERM:
 
363
                raise
 
364
            raise errors.HardLinkNotSupported(path)
 
365
        try:
 
366
            unique_add(self._new_contents, trans_id, 'file')
 
367
        except:
 
368
            # Clean up the file, it never got registered so
 
369
            # TreeTransform.finalize() won't clean it up.
 
370
            os.unlink(name)
 
371
            raise
 
372
 
357
373
    def create_directory(self, trans_id):
358
374
        """Schedule creation of a new directory.
359
375
        
423
439
 
424
440
    def version_file(self, file_id, trans_id):
425
441
        """Schedule a file to become versioned."""
426
 
        assert file_id is not None
 
442
        if file_id is None:
 
443
            raise ValueError()
427
444
        unique_add(self._new_id, trans_id, file_id)
428
445
        unique_add(self._r_new_id, file_id, trans_id)
429
446
 
433
450
        del self._new_id[trans_id]
434
451
        del self._r_new_id[file_id]
435
452
 
436
 
    def new_paths(self):
437
 
        """Determine the paths of all new and changed files"""
 
453
    def new_paths(self, filesystem_only=False):
 
454
        """Determine the paths of all new and changed files.
 
455
 
 
456
        :param filesystem_only: if True, only calculate values for files
 
457
            that require renames or execute bit changes.
 
458
        """
438
459
        new_ids = set()
439
 
        fp = FinalPaths(self)
440
 
        for id_set in (self._new_name, self._new_parent, self._new_contents,
441
 
                       self._new_id, self._new_executability):
 
460
        if filesystem_only:
 
461
            id_sets = (self._needs_rename, self._new_executability)
 
462
        else:
 
463
            id_sets = (self._new_name, self._new_parent, self._new_contents,
 
464
                       self._new_id, self._new_executability)
 
465
        for id_set in id_sets:
442
466
            new_ids.update(id_set)
443
 
        new_paths = [(fp.get_path(t), t) for t in new_ids]
444
 
        new_paths.sort()
445
 
        return new_paths
 
467
        return sorted(FinalPaths(self).get_paths(new_ids))
446
468
 
447
469
    def tree_kind(self, trans_id):
448
470
        """Determine the file kind in the working tree.
493
515
        applied.
494
516
        """
495
517
        try:
496
 
            # there is a new id for this file
497
 
            assert self._new_id[trans_id] is not None
498
518
            return self._new_id[trans_id]
499
519
        except KeyError:
500
520
            if trans_id in self._removed_id:
753
773
        conflicts = []
754
774
        removed_tree_ids = set((self.tree_file_id(trans_id) for trans_id in
755
775
                                self._removed_id))
756
 
        active_tree_ids = set((f for f in self._tree.inventory if
757
 
                               f not in removed_tree_ids))
 
776
        all_ids = self._tree.all_file_ids()
 
777
        active_tree_ids = all_ids.difference(removed_tree_ids)
758
778
        for trans_id, file_id in self._new_id.iteritems():
759
779
            if file_id in active_tree_ids:
760
780
                old_trans_id = self.trans_id_tree_file_id(file_id)
841
861
    def _set_executability(self, path, entry, trans_id):
842
862
        """Set the executability of versioned files """
843
863
        new_executability = self._new_executability[trans_id]
844
 
        entry.executable = new_executability
 
864
        if entry is not None:
 
865
            entry.executable = new_executability
845
866
        if supports_executable():
846
867
            abspath = self._tree.abspath(path)
847
868
            current_mode = os.stat(abspath).st_mode
985
1006
            to_executable = False
986
1007
        return to_name, to_parent, to_kind, to_executable
987
1008
 
988
 
    def _iter_changes(self):
989
 
        """Produce output in the same format as Tree._iter_changes.
 
1009
    def iter_changes(self):
 
1010
        """Produce output in the same format as Tree.iter_changes.
990
1011
 
991
1012
        Will produce nonsensical results if invoked while inventory/filesystem
992
1013
        conflicts (as reported by TreeTransform.find_conflicts()) are present.
1130
1151
        tree.lock_tree_write()
1131
1152
 
1132
1153
        try:
1133
 
            control_files = tree._control_files
1134
1154
            limbodir = urlutils.local_path_from_url(
1135
 
                control_files.controlfilename('limbo'))
 
1155
                tree._transport.abspath('limbo'))
1136
1156
            try:
1137
1157
                os.mkdir(limbodir)
1138
1158
            except OSError, e:
1139
1159
                if e.errno == errno.EEXIST:
1140
1160
                    raise ExistingLimbo(limbodir)
1141
1161
            deletiondir = urlutils.local_path_from_url(
1142
 
                control_files.controlfilename('pending-deletion'))
 
1162
                tree._transport.abspath('pending-deletion'))
1143
1163
            try:
1144
1164
                os.mkdir(deletiondir)
1145
1165
            except OSError, e:
1153
1173
                                   tree.case_sensitive)
1154
1174
        self._deletiondir = deletiondir
1155
1175
 
1156
 
    def apply(self, no_conflicts=False, _mover=None):
 
1176
    def apply(self, no_conflicts=False, precomputed_delta=None, _mover=None):
1157
1177
        """Apply all changes to the inventory and filesystem.
1158
1178
 
1159
1179
        If filesystem or inventory conflicts are present, MalformedTransform
1163
1183
 
1164
1184
        :param no_conflicts: if True, the caller guarantees there are no
1165
1185
            conflicts, so no check is made.
 
1186
        :param precomputed_delta: An inventory delta to use instead of
 
1187
            calculating one.
1166
1188
        :param _mover: Supply an alternate FileMover, for testing
1167
1189
        """
1168
1190
        if not no_conflicts:
1169
1191
            conflicts = self.find_conflicts()
1170
1192
            if len(conflicts) != 0:
1171
1193
                raise MalformedTransform(conflicts=conflicts)
1172
 
        inv = self._tree.inventory
1173
 
        inventory_delta = []
 
1194
        if precomputed_delta is None:
 
1195
            new_inventory_delta = []
 
1196
            inventory_delta = new_inventory_delta
 
1197
        else:
 
1198
            new_inventory_delta = None
 
1199
            inventory_delta = precomputed_delta
1174
1200
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1175
1201
        try:
1176
1202
            if _mover is None:
1179
1205
                mover = _mover
1180
1206
            try:
1181
1207
                child_pb.update('Apply phase', 0, 2)
1182
 
                self._apply_removals(inv, inventory_delta, mover)
 
1208
                self._apply_removals(new_inventory_delta, mover)
1183
1209
                child_pb.update('Apply phase', 1, 2)
1184
 
                modified_paths = self._apply_insertions(inv, inventory_delta,
 
1210
                modified_paths = self._apply_insertions(new_inventory_delta,
1185
1211
                                                        mover)
1186
1212
            except:
1187
1213
                mover.rollback()
1195
1221
        self.finalize()
1196
1222
        return _TransformResults(modified_paths, self.rename_count)
1197
1223
 
1198
 
    def _apply_removals(self, inv, inventory_delta, mover):
 
1224
    def _apply_removals(self, inventory_delta, mover):
1199
1225
        """Perform tree operations that remove directory/inventory names.
1200
1226
 
1201
1227
        That is, delete files that are to be deleted, and put any files that
1202
1228
        need renaming into limbo.  This must be done in strict child-to-parent
1203
1229
        order.
 
1230
 
 
1231
        If inventory_delta is None, no inventory delta generation is performed.
1204
1232
        """
1205
1233
        tree_paths = list(self._tree_path_ids.iteritems())
1206
1234
        tree_paths.sort(reverse=True)
1222
1250
                            raise
1223
1251
                    else:
1224
1252
                        self.rename_count += 1
1225
 
                if trans_id in self._removed_id:
 
1253
                if (trans_id in self._removed_id
 
1254
                    and inventory_delta is not None):
1226
1255
                    if trans_id == self._new_root:
1227
1256
                        file_id = self._tree.get_root_id()
1228
1257
                    else:
1229
1258
                        file_id = self.tree_file_id(trans_id)
1230
 
                    if file_id is not None:
1231
 
                        inventory_delta.append((path, None, file_id, None))
 
1259
                    # File-id isn't really being deleted, just moved
 
1260
                    if file_id in self._r_new_id:
 
1261
                        continue
 
1262
                    inventory_delta.append((path, None, file_id, None))
1232
1263
        finally:
1233
1264
            child_pb.finished()
1234
1265
 
1235
 
    def _apply_insertions(self, inv, inventory_delta, mover):
 
1266
    def _apply_insertions(self, inventory_delta, mover):
1236
1267
        """Perform tree operations that insert directory/inventory names.
1237
1268
 
1238
1269
        That is, create any files that need to be created, and restore from
1239
1270
        limbo any files that needed renaming.  This must be done in strict
1240
1271
        parent-to-child order.
 
1272
 
 
1273
        If inventory_delta is None, no inventory delta is calculated, and
 
1274
        no list of modified paths is returned.
1241
1275
        """
1242
 
        new_paths = self.new_paths()
 
1276
        new_paths = self.new_paths(filesystem_only=(inventory_delta is None))
1243
1277
        modified_paths = []
1244
 
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1245
1278
        completed_new = []
 
1279
        new_path_file_ids = dict((t, self.final_file_id(t)) for p, t in
 
1280
                                 new_paths)
 
1281
        if inventory_delta is not None:
 
1282
            entries = self._tree.iter_entries_by_dir(
 
1283
                new_path_file_ids.values())
 
1284
            old_paths = dict((e.file_id, p) for p, e in entries)
 
1285
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1246
1286
        try:
1247
1287
            for num, (path, trans_id) in enumerate(new_paths):
1248
1288
                new_entry = None
1249
 
                child_pb.update('adding file', num, len(new_paths))
1250
 
                try:
1251
 
                    kind = self._new_contents[trans_id]
1252
 
                except KeyError:
1253
 
                    kind = contents = None
1254
 
                if trans_id in self._new_contents or \
1255
 
                    self.path_changed(trans_id):
1256
 
                    full_path = self._tree.abspath(path)
1257
 
                    if trans_id in self._needs_rename:
 
1289
                if (num % 10) == 0:
 
1290
                    child_pb.update('adding file', num, len(new_paths))
 
1291
                full_path = self._tree.abspath(path)
 
1292
                if trans_id in self._needs_rename:
 
1293
                    try:
 
1294
                        mover.rename(self._limbo_name(trans_id), full_path)
 
1295
                    except OSError, e:
 
1296
                        # We may be renaming a dangling inventory id
 
1297
                        if e.errno != errno.ENOENT:
 
1298
                            raise
 
1299
                    else:
 
1300
                        self.rename_count += 1
 
1301
                if inventory_delta is not None:
 
1302
                    if (trans_id in self._new_contents or
 
1303
                        self.path_changed(trans_id)):
 
1304
                        if trans_id in self._new_contents:
 
1305
                            modified_paths.append(full_path)
 
1306
                            completed_new.append(trans_id)
 
1307
                    file_id = new_path_file_ids[trans_id]
 
1308
                    if file_id is not None and (trans_id in self._new_id or
 
1309
                        trans_id in self._new_name or
 
1310
                        trans_id in self._new_parent
 
1311
                        or trans_id in self._new_executability):
1258
1312
                        try:
1259
 
                            mover.rename(self._limbo_name(trans_id), full_path)
1260
 
                        except OSError, e:
1261
 
                            # We may be renaming a dangling inventory id
1262
 
                            if e.errno != errno.ENOENT:
1263
 
                                raise
 
1313
                            kind = self.final_kind(trans_id)
 
1314
                        except NoSuchFile:
 
1315
                            kind = self._tree.stored_kind(file_id)
 
1316
                        parent_trans_id = self.final_parent(trans_id)
 
1317
                        parent_file_id = new_path_file_ids.get(parent_trans_id)
 
1318
                        if parent_file_id is None:
 
1319
                            parent_file_id = self.final_file_id(
 
1320
                                parent_trans_id)
 
1321
                        if trans_id in self._new_reference_revision:
 
1322
                            new_entry = inventory.TreeReference(
 
1323
                                file_id,
 
1324
                                self._new_name[trans_id],
 
1325
                                self.final_file_id(self._new_parent[trans_id]),
 
1326
                                None, self._new_reference_revision[trans_id])
1264
1327
                        else:
1265
 
                            self.rename_count += 1
1266
 
                    if trans_id in self._new_contents:
1267
 
                        modified_paths.append(full_path)
1268
 
                        completed_new.append(trans_id)
1269
 
 
1270
 
                if trans_id in self._new_id:
1271
 
                    if kind is None:
1272
 
                        kind = file_kind(self._tree.abspath(path))
1273
 
                    if trans_id in self._new_reference_revision:
1274
 
                        new_entry = inventory.TreeReference(
1275
 
                            self._new_id[trans_id],
1276
 
                            self._new_name[trans_id],
1277
 
                            self.final_file_id(self._new_parent[trans_id]),
1278
 
                            None, self._new_reference_revision[trans_id])
1279
 
                    else:
1280
 
                        new_entry = inventory.make_entry(kind,
1281
 
                            self.final_name(trans_id),
1282
 
                            self.final_file_id(self.final_parent(trans_id)),
1283
 
                            self._new_id[trans_id])
1284
 
                else:
1285
 
                    if trans_id in self._new_name or trans_id in\
1286
 
                        self._new_parent or\
1287
 
                        trans_id in self._new_executability:
1288
 
                        file_id = self.final_file_id(trans_id)
1289
 
                        if file_id is not None:
1290
 
                            entry = inv[file_id]
1291
 
                            new_entry = entry.copy()
1292
 
 
1293
 
                    if trans_id in self._new_name or trans_id in\
1294
 
                        self._new_parent:
1295
 
                            if new_entry is not None:
1296
 
                                new_entry.name = self.final_name(trans_id)
1297
 
                                parent = self.final_parent(trans_id)
1298
 
                                parent_id = self.final_file_id(parent)
1299
 
                                new_entry.parent_id = parent_id
 
1328
                            new_entry = inventory.make_entry(kind,
 
1329
                                self.final_name(trans_id),
 
1330
                                parent_file_id, file_id)
 
1331
                        old_path = old_paths.get(new_entry.file_id)
 
1332
                        inventory_delta.append(
 
1333
                            (old_path, path, new_entry.file_id, new_entry))
1300
1334
 
1301
1335
                if trans_id in self._new_executability:
1302
1336
                    self._set_executability(path, new_entry, trans_id)
1303
 
                if new_entry is not None:
1304
 
                    if new_entry.file_id in inv:
1305
 
                        old_path = inv.id2path(new_entry.file_id)
1306
 
                    else:
1307
 
                        old_path = None
1308
 
                    inventory_delta.append((old_path, path,
1309
 
                                            new_entry.file_id,
1310
 
                                            new_entry))
1311
1337
        finally:
1312
1338
            child_pb.finished()
1313
 
        for trans_id in completed_new:
1314
 
            del self._new_contents[trans_id]
 
1339
        if inventory_delta is None:
 
1340
            self._new_contents.clear()
 
1341
        else:
 
1342
            for trans_id in completed_new:
 
1343
                del self._new_contents[trans_id]
1315
1344
        return modified_paths
1316
1345
 
1317
1346
 
1359
1388
            yield self.trans_id_tree_path(childpath)
1360
1389
 
1361
1390
 
1362
 
class _PreviewTree(object):
 
1391
class _PreviewTree(tree.Tree):
1363
1392
    """Partial implementation of Tree to support show_diff_trees"""
1364
1393
 
1365
1394
    def __init__(self, transform):
1366
1395
        self._transform = transform
 
1396
        self._final_paths = FinalPaths(transform)
 
1397
 
 
1398
    def _changes(self, file_id):
 
1399
        for changes in self._transform.iter_changes():
 
1400
            if changes[0] == file_id:
 
1401
                return changes
 
1402
 
 
1403
    def _content_change(self, file_id):
 
1404
        """Return True if the content of this file changed"""
 
1405
        changes = self._changes(file_id)
 
1406
        # changes[2] is true if the file content changed.  See
 
1407
        # InterTree.iter_changes.
 
1408
        return (changes is not None and changes[2])
 
1409
 
 
1410
    def _get_file_revision(self, file_id, vf, tree_revision):
 
1411
        return self._transform._tree._get_file_revision(file_id, vf,
 
1412
                                                        tree_revision)
 
1413
 
 
1414
    def _stat_limbo_file(self, file_id):
 
1415
        trans_id = self._transform.trans_id_file_id(file_id)
 
1416
        name = self._transform._limbo_name(trans_id)
 
1417
        return os.lstat(name)
1367
1418
 
1368
1419
    def lock_read(self):
1369
1420
        # Perhaps in theory, this should lock the TreeTransform?
1372
1423
    def unlock(self):
1373
1424
        pass
1374
1425
 
1375
 
    def _iter_changes(self, from_tree, include_unchanged=False,
 
1426
    @property
 
1427
    def inventory(self):
 
1428
        """This Tree does not use inventory as its backing data."""
 
1429
        raise NotImplementedError(_PreviewTree.inventory)
 
1430
 
 
1431
    def get_root_id(self):
 
1432
        return self._transform.final_file_id(self._transform.root)
 
1433
 
 
1434
    def all_file_ids(self):
 
1435
        return self._transform._tree.all_file_ids()
 
1436
 
 
1437
    def __iter__(self):
 
1438
        return iter(self.all_file_ids())
 
1439
 
 
1440
    def paths2ids(self, specific_files, trees=None, require_versioned=False):
 
1441
        """See Tree.paths2ids"""
 
1442
        to_find = set(specific_files)
 
1443
        result = set()
 
1444
        for (file_id, paths, changed, versioned, parent, name, kind,
 
1445
             executable) in self._transform.iter_changes():
 
1446
            if paths[1] in to_find:
 
1447
                result.append(file_id)
 
1448
                to_find.remove(paths[1])
 
1449
        result.update(self._transform._tree.paths2ids(to_find,
 
1450
                      trees=[], require_versioned=require_versioned))
 
1451
        return result
 
1452
 
 
1453
    def path2id(self, path):
 
1454
        return self._transform._tree.path2id(path)
 
1455
 
 
1456
    def id2path(self, file_id):
 
1457
        trans_id = self._transform.trans_id_file_id(file_id)
 
1458
        try:
 
1459
            return self._final_paths._determine_path(trans_id)
 
1460
        except NoFinalPath:
 
1461
            raise errors.NoSuchId(self, file_id)
 
1462
 
 
1463
    def iter_entries_by_dir(self, specific_file_ids=None):
 
1464
        return self._transform._tree.iter_entries_by_dir(specific_file_ids)
 
1465
 
 
1466
    def kind(self, file_id):
 
1467
        trans_id = self._transform.trans_id_file_id(file_id)
 
1468
        return self._transform.final_kind(trans_id)
 
1469
 
 
1470
    def stored_kind(self, file_id):
 
1471
        return self._transform._tree.stored_kind(file_id)
 
1472
 
 
1473
    def get_file_mtime(self, file_id, path=None):
 
1474
        """See Tree.get_file_mtime"""
 
1475
        if not self._content_change(file_id):
 
1476
            return self._transform._tree.get_file_mtime(file_id, path)
 
1477
        return self._stat_limbo_file(file_id).st_mtime
 
1478
 
 
1479
    def get_file_size(self, file_id):
 
1480
        """See Tree.get_file_size"""
 
1481
        if self.kind(file_id) == 'file':
 
1482
            return self._transform._tree.get_file_size(file_id)
 
1483
        else:
 
1484
            return None
 
1485
 
 
1486
    def get_file_sha1(self, file_id, path=None, stat_value=None):
 
1487
        return self._transform._tree.get_file_sha1(file_id)
 
1488
 
 
1489
    def is_executable(self, file_id, path=None):
 
1490
        return self._transform._tree.is_executable(file_id, path)
 
1491
 
 
1492
    def path_content_summary(self, path):
 
1493
        return self._transform._tree.path_content_summary(path)
 
1494
 
 
1495
    def iter_changes(self, from_tree, include_unchanged=False,
1376
1496
                      specific_files=None, pb=None, extra_trees=None,
1377
1497
                      require_versioned=True, want_unversioned=False):
1378
 
        """See InterTree._iter_changes.
 
1498
        """See InterTree.iter_changes.
1379
1499
 
1380
1500
        This implementation does not support include_unchanged, specific_files,
1381
1501
        or want_unversioned.  extra_trees, require_versioned, and pb are
1389
1509
            raise ValueError('specific_files is not supported')
1390
1510
        if want_unversioned:
1391
1511
            raise ValueError('want_unversioned is not supported')
1392
 
        return self._transform._iter_changes()
1393
 
 
1394
 
    def kind(self, file_id):
1395
 
        trans_id = self._transform.trans_id_file_id(file_id)
1396
 
        return self._transform.final_kind(trans_id)
1397
 
 
1398
 
    def get_file_mtime(self, file_id, path=None):
1399
 
        """See Tree.get_file_mtime"""
1400
 
        trans_id = self._transform.trans_id_file_id(file_id)
1401
 
        name = self._transform._limbo_name(trans_id)
1402
 
        return os.stat(name).st_mtime
1403
 
 
1404
 
    def get_file(self, file_id):
 
1512
        return self._transform.iter_changes()
 
1513
 
 
1514
    def get_file(self, file_id, path=None):
1405
1515
        """See Tree.get_file"""
 
1516
        if not self._content_change(file_id):
 
1517
            return self._transform._tree.get_file(file_id, path)
1406
1518
        trans_id = self._transform.trans_id_file_id(file_id)
1407
1519
        name = self._transform._limbo_name(trans_id)
1408
1520
        return open(name, 'rb')
1409
1521
 
1410
 
    def paths2ids(self, specific_files, trees=None, require_versioned=False):
1411
 
        """See Tree.paths2ids"""
1412
 
        return 'not_empty'
 
1522
    def get_file_text(self, file_id):
 
1523
        text_file = self.get_file(file_id)
 
1524
        try:
 
1525
            return text_file.read()
 
1526
        finally:
 
1527
            text_file.close()
 
1528
 
 
1529
    def annotate_iter(self, file_id,
 
1530
                      default_revision=_mod_revision.CURRENT_REVISION):
 
1531
        return self._transform._tree.annotate_iter(file_id,
 
1532
            default_revision=default_revision)
 
1533
 
 
1534
    def get_symlink_target(self, file_id):
 
1535
        """See Tree.get_symlink_target"""
 
1536
        if not self._content_change(file_id):
 
1537
            return self._transform._tree.get_symlink_target(file_id)
 
1538
        trans_id = self._transform.trans_id_file_id(file_id)
 
1539
        name = self._transform._limbo_name(trans_id)
 
1540
        return os.readlink(name)
 
1541
 
 
1542
    def list_files(self, include_root=False):
 
1543
        return self._transform._tree.list_files(include_root)
 
1544
 
 
1545
    def walkdirs(self, prefix=""):
 
1546
        return self._transform._tree.walkdirs(prefix)
 
1547
 
 
1548
    def get_parent_ids(self):
 
1549
        return self._transform._tree.get_parent_ids()
 
1550
 
 
1551
    def get_revision_tree(self, revision_id):
 
1552
        return self._transform._tree.get_revision_tree(revision_id)
1413
1553
 
1414
1554
 
1415
1555
def joinpath(parent, child):
1447
1587
            self._known_paths[trans_id] = self._determine_path(trans_id)
1448
1588
        return self._known_paths[trans_id]
1449
1589
 
 
1590
    def get_paths(self, trans_ids):
 
1591
        return [(self.get_path(t), t) for t in trans_ids]
 
1592
 
 
1593
 
1450
1594
 
1451
1595
def topology_sorted_ids(tree):
1452
1596
    """Determine the topological order of the ids in a tree"""
1455
1599
    return file_ids
1456
1600
 
1457
1601
 
1458
 
def build_tree(tree, wt, accelerator_tree=None):
 
1602
def build_tree(tree, wt, accelerator_tree=None, hardlink=False,
 
1603
               delta_from_tree=False):
1459
1604
    """Create working tree for a branch, using a TreeTransform.
1460
1605
    
1461
1606
    This function should be used on empty trees, having a tree root at most.
1474
1619
    :param accelerator_tree: A tree which can be used for retrieving file
1475
1620
        contents more quickly than tree itself, i.e. a workingtree.  tree
1476
1621
        will be used for cases where accelerator_tree's content is different.
 
1622
    :param hardlink: If true, hard-link files to accelerator_tree, where
 
1623
        possible.  accelerator_tree must implement abspath, i.e. be a
 
1624
        working tree.
 
1625
    :param delta_from_tree: If true, build_tree may use the input Tree to
 
1626
        generate the inventory delta.
1477
1627
    """
1478
1628
    wt.lock_tree_write()
1479
1629
    try:
1482
1632
            if accelerator_tree is not None:
1483
1633
                accelerator_tree.lock_read()
1484
1634
            try:
1485
 
                return _build_tree(tree, wt, accelerator_tree)
 
1635
                return _build_tree(tree, wt, accelerator_tree, hardlink,
 
1636
                                   delta_from_tree)
1486
1637
            finally:
1487
1638
                if accelerator_tree is not None:
1488
1639
                    accelerator_tree.unlock()
1492
1643
        wt.unlock()
1493
1644
 
1494
1645
 
1495
 
def _build_tree(tree, wt, accelerator_tree):
 
1646
def _build_tree(tree, wt, accelerator_tree, hardlink, delta_from_tree):
1496
1647
    """See build_tree."""
1497
 
    if len(wt.inventory) > 1:  # more than just a root
1498
 
        raise errors.WorkingTreeAlreadyPopulated(base=wt.basedir)
 
1648
    for num, _unused in enumerate(wt.all_file_ids()):
 
1649
        if num > 0:  # more than just a root
 
1650
            raise errors.WorkingTreeAlreadyPopulated(base=wt.basedir)
 
1651
    existing_files = set()
 
1652
    for dir, files in wt.walkdirs():
 
1653
        existing_files.update(f[0] for f in files)
1499
1654
    file_trans_id = {}
1500
1655
    top_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1501
1656
    pp = ProgressPhase("Build phase", 2, top_pb)
1519
1674
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
1520
1675
        try:
1521
1676
            deferred_contents = []
 
1677
            num = 0
 
1678
            total = len(tree.inventory)
 
1679
            if delta_from_tree:
 
1680
                precomputed_delta = []
 
1681
            else:
 
1682
                precomputed_delta = None
1522
1683
            for num, (tree_path, entry) in \
1523
1684
                enumerate(tree.inventory.iter_entries_by_dir()):
1524
 
                pb.update("Building tree", num - len(deferred_contents),
1525
 
                          len(tree.inventory))
 
1685
                pb.update("Building tree", num - len(deferred_contents), total)
1526
1686
                if entry.parent_id is None:
1527
1687
                    continue
1528
1688
                reparent = False
1529
1689
                file_id = entry.file_id
1530
 
                target_path = wt.abspath(tree_path)
1531
 
                try:
 
1690
                if delta_from_tree:
 
1691
                    precomputed_delta.append((None, tree_path, file_id, entry))
 
1692
                if tree_path in existing_files:
 
1693
                    target_path = wt.abspath(tree_path)
1532
1694
                    kind = file_kind(target_path)
1533
 
                except NoSuchFile:
1534
 
                    pass
1535
 
                else:
1536
1695
                    if kind == "directory":
1537
1696
                        try:
1538
1697
                            bzrdir.BzrDir.open(target_path)
1546
1705
                        tt.delete_contents(tt.trans_id_tree_path(tree_path))
1547
1706
                        if kind == 'directory':
1548
1707
                            reparent = True
1549
 
                if entry.parent_id not in file_trans_id:
1550
 
                    raise AssertionError(
1551
 
                        'entry %s parent id %r is not in file_trans_id %r'
1552
 
                        % (entry, entry.parent_id, file_trans_id))
1553
1708
                parent_id = file_trans_id[entry.parent_id]
1554
1709
                if entry.kind == 'file':
1555
1710
                    # We *almost* replicate new_by_entry, so that we can defer
1556
1711
                    # getting the file text, and get them all at once.
1557
1712
                    trans_id = tt.create_path(entry.name, parent_id)
1558
1713
                    file_trans_id[file_id] = trans_id
1559
 
                    tt.version_file(entry.file_id, trans_id)
1560
 
                    executable = tree.is_executable(entry.file_id, tree_path)
1561
 
                    if executable is not None:
 
1714
                    tt.version_file(file_id, trans_id)
 
1715
                    executable = tree.is_executable(file_id, tree_path)
 
1716
                    if executable:
1562
1717
                        tt.set_executability(executable, trans_id)
1563
 
                    deferred_contents.append((entry.file_id, trans_id))
 
1718
                    deferred_contents.append((file_id, trans_id))
1564
1719
                else:
1565
1720
                    file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
1566
1721
                                                          tree)
1568
1723
                    new_trans_id = file_trans_id[file_id]
1569
1724
                    old_parent = tt.trans_id_tree_path(tree_path)
1570
1725
                    _reparent_children(tt, old_parent, new_trans_id)
1571
 
            for num, (trans_id, bytes) in enumerate(
1572
 
                _iter_files_bytes_accelerated(tree, accelerator_tree,
1573
 
                                              deferred_contents)):
1574
 
                tt.create_file(bytes, trans_id)
1575
 
                pb.update('Adding file contents',
1576
 
                          (num + len(tree.inventory) - len(deferred_contents)),
1577
 
                          len(tree.inventory))
 
1726
            offset = num + 1 - len(deferred_contents)
 
1727
            _create_files(tt, tree, deferred_contents, pb, offset,
 
1728
                          accelerator_tree, hardlink)
1578
1729
        finally:
1579
1730
            pb.finished()
1580
1731
        pp.next_phase()
1581
1732
        divert_trans = set(file_trans_id[f] for f in divert)
1582
1733
        resolver = lambda t, c: resolve_checkout(t, c, divert_trans)
1583
1734
        raw_conflicts = resolve_conflicts(tt, pass_func=resolver)
 
1735
        if len(raw_conflicts) > 0:
 
1736
            precomputed_delta = None
1584
1737
        conflicts = cook_conflicts(raw_conflicts, tt)
1585
1738
        for conflict in conflicts:
1586
1739
            warning(conflict)
1588
1741
            wt.add_conflicts(conflicts)
1589
1742
        except errors.UnsupportedOperation:
1590
1743
            pass
1591
 
        result = tt.apply(no_conflicts=True)
 
1744
        result = tt.apply(no_conflicts=True,
 
1745
                          precomputed_delta=precomputed_delta)
1592
1746
    finally:
1593
1747
        tt.finalize()
1594
1748
        top_pb.finished()
1595
1749
    return result
1596
1750
 
1597
1751
 
1598
 
def _iter_files_bytes_accelerated(tree, accelerator_tree, desired_files):
 
1752
def _create_files(tt, tree, desired_files, pb, offset, accelerator_tree,
 
1753
                  hardlink):
 
1754
    total = len(desired_files) + offset
1599
1755
    if accelerator_tree is None:
1600
1756
        new_desired_files = desired_files
1601
1757
    else:
1602
 
        iter = accelerator_tree._iter_changes(tree, include_unchanged=True)
 
1758
        iter = accelerator_tree.iter_changes(tree, include_unchanged=True)
1603
1759
        unchanged = dict((f, p[1]) for (f, p, c, v, d, n, k, e)
1604
 
                         in iter if not c)
 
1760
                         in iter if not (c or e[0] != e[1]))
1605
1761
        new_desired_files = []
1606
 
        for file_id, identifier in desired_files:
 
1762
        count = 0
 
1763
        for file_id, trans_id in desired_files:
1607
1764
            accelerator_path = unchanged.get(file_id)
1608
1765
            if accelerator_path is None:
1609
 
                new_desired_files.append((file_id, identifier))
 
1766
                new_desired_files.append((file_id, trans_id))
1610
1767
                continue
1611
 
            contents = accelerator_tree.get_file(file_id, accelerator_path)
1612
 
            try:
1613
 
                want_new = False
1614
 
                contents_bytes = (contents.read(),)
1615
 
            finally:
1616
 
                contents.close()
1617
 
            yield identifier, contents_bytes
1618
 
    for result in tree.iter_files_bytes(new_desired_files):
1619
 
        yield result
 
1768
            pb.update('Adding file contents', count + offset, total)
 
1769
            if hardlink:
 
1770
                tt.create_hardlink(accelerator_tree.abspath(accelerator_path),
 
1771
                                   trans_id)
 
1772
            else:
 
1773
                contents = accelerator_tree.get_file(file_id, accelerator_path)
 
1774
                try:
 
1775
                    tt.create_file(contents, trans_id)
 
1776
                finally:
 
1777
                    contents.close()
 
1778
            count += 1
 
1779
        offset += count
 
1780
    for count, (trans_id, contents) in enumerate(tree.iter_files_bytes(
 
1781
                                                 new_desired_files)):
 
1782
        tt.create_file(contents, trans_id)
 
1783
        pb.update('Adding file contents', count + offset, total)
1620
1784
 
1621
1785
 
1622
1786
def _reparent_children(tt, old_parent, new_parent):
1627
1791
    by_parent = tt.by_parent()
1628
1792
    for child in by_parent[old_parent]:
1629
1793
        tt.adjust_path(tt.final_name(child), new_parent, child)
 
1794
    return by_parent[old_parent]
1630
1795
 
1631
1796
def _content_match(tree, entry, file_id, kind, target_path):
1632
1797
    if entry.kind != kind:
1646
1811
    new_conflicts = set()
1647
1812
    for c_type, conflict in ((c[0], c) for c in conflicts):
1648
1813
        # Anything but a 'duplicate' would indicate programmer error
1649
 
        assert c_type == 'duplicate', c_type
 
1814
        if c_type != 'duplicate':
 
1815
            raise AssertionError(c_type)
1650
1816
        # Now figure out which is new and which is old
1651
1817
        if tt.new_contents(conflict[1]):
1652
1818
            new_file = conflict[1]
1710
1876
        tt.set_executability(entry.executable, trans_id)
1711
1877
 
1712
1878
 
1713
 
@deprecated_function(zero_fifteen)
1714
 
def find_interesting(working_tree, target_tree, filenames):
1715
 
    """Find the ids corresponding to specified filenames.
1716
 
    
1717
 
    Deprecated: Please use tree1.paths2ids(filenames, [tree2]).
1718
 
    """
1719
 
    working_tree.lock_read()
1720
 
    try:
1721
 
        target_tree.lock_read()
1722
 
        try:
1723
 
            return working_tree.paths2ids(filenames, [target_tree])
1724
 
        finally:
1725
 
            target_tree.unlock()
1726
 
    finally:
1727
 
        working_tree.unlock()
1728
 
 
1729
 
 
1730
 
@deprecated_function(zero_ninety)
1731
 
def change_entry(tt, file_id, working_tree, target_tree, 
1732
 
                 trans_id_file_id, backups, trans_id, by_parent):
1733
 
    """Replace a file_id's contents with those from a target tree."""
1734
 
    if file_id is None and target_tree is None:
1735
 
        # skip the logic altogether in the deprecation test
1736
 
        return
1737
 
    e_trans_id = trans_id_file_id(file_id)
1738
 
    entry = target_tree.inventory[file_id]
1739
 
    has_contents, contents_mod, meta_mod, = _entry_changes(file_id, entry, 
1740
 
                                                           working_tree)
1741
 
    if contents_mod:
1742
 
        mode_id = e_trans_id
1743
 
        if has_contents:
1744
 
            if not backups:
1745
 
                tt.delete_contents(e_trans_id)
1746
 
            else:
1747
 
                parent_trans_id = trans_id_file_id(entry.parent_id)
1748
 
                backup_name = get_backup_name(entry, by_parent,
1749
 
                                              parent_trans_id, tt)
1750
 
                tt.adjust_path(backup_name, parent_trans_id, e_trans_id)
1751
 
                tt.unversion_file(e_trans_id)
1752
 
                e_trans_id = tt.create_path(entry.name, parent_trans_id)
1753
 
                tt.version_file(file_id, e_trans_id)
1754
 
                trans_id[file_id] = e_trans_id
1755
 
        create_by_entry(tt, entry, target_tree, e_trans_id, mode_id=mode_id)
1756
 
        create_entry_executability(tt, entry, e_trans_id)
1757
 
 
1758
 
    elif meta_mod:
1759
 
        tt.set_executability(entry.executable, e_trans_id)
1760
 
    if tt.final_name(e_trans_id) != entry.name:
1761
 
        adjust_path  = True
1762
 
    else:
1763
 
        parent_id = tt.final_parent(e_trans_id)
1764
 
        parent_file_id = tt.final_file_id(parent_id)
1765
 
        if parent_file_id != entry.parent_id:
1766
 
            adjust_path = True
1767
 
        else:
1768
 
            adjust_path = False
1769
 
    if adjust_path:
1770
 
        parent_trans_id = trans_id_file_id(entry.parent_id)
1771
 
        tt.adjust_path(entry.name, parent_trans_id, e_trans_id)
1772
 
 
1773
 
 
1774
1879
def get_backup_name(entry, by_parent, parent_trans_id, tt):
1775
1880
    return _get_backup_name(entry.name, by_parent, parent_trans_id, tt)
1776
1881
 
1839
1944
        if change_reporter:
1840
1945
            change_reporter = delta._ChangeReporter(
1841
1946
                unversioned_filter=working_tree.is_ignored)
1842
 
            delta.report_changes(tt._iter_changes(), change_reporter)
 
1947
            delta.report_changes(tt.iter_changes(), change_reporter)
1843
1948
        for conflict in conflicts:
1844
1949
            warning(conflict)
1845
1950
        pp.next_phase()
1855
1960
def _alter_files(working_tree, target_tree, tt, pb, specific_files,
1856
1961
                 backups):
1857
1962
    merge_modified = working_tree.merge_modified()
1858
 
    change_list = target_tree._iter_changes(working_tree,
 
1963
    change_list = target_tree.iter_changes(working_tree,
1859
1964
        specific_files=specific_files, pb=pb)
1860
1965
    if target_tree.inventory.root is None:
1861
1966
        skip_root = True
1924
2029
                    # preserve the execute bit when backing up
1925
2030
                    if keep_content and executable[0] == executable[1]:
1926
2031
                        tt.set_executability(executable[1], trans_id)
1927
 
                else:
1928
 
                    assert kind[1] is None
 
2032
                elif kind[1] is not None:
 
2033
                    raise AssertionError(kind[1])
1929
2034
            if versioned == (False, True):
1930
2035
                tt.version_file(file_id, trans_id)
1931
2036
            if versioned == (True, False):
2002
2107
                new_conflicts.add(('deleting parent', 'Not deleting', 
2003
2108
                                   trans_id))
2004
2109
            except KeyError:
2005
 
                tt.create_directory(trans_id)
2006
 
                new_conflicts.add((c_type, 'Created directory', trans_id))
 
2110
                create = True
2007
2111
                try:
2008
2112
                    tt.final_name(trans_id)
2009
2113
                except NoFinalPath:
2010
2114
                    if path_tree is not None:
2011
2115
                        file_id = tt.final_file_id(trans_id)
 
2116
                        if file_id is None:
 
2117
                            file_id = tt.inactive_file_id(trans_id)
2012
2118
                        entry = path_tree.inventory[file_id]
2013
 
                        parent_trans_id = tt.trans_id_file_id(entry.parent_id)
2014
 
                        tt.adjust_path(entry.name, parent_trans_id, trans_id)
 
2119
                        # special-case the other tree root (move its
 
2120
                        # children to current root)
 
2121
                        if entry.parent_id is None:
 
2122
                            create=False
 
2123
                            moved = _reparent_transform_children(
 
2124
                                tt, trans_id, tt.root)
 
2125
                            for child in moved:
 
2126
                                new_conflicts.add((c_type, 'Moved to root',
 
2127
                                                   child))
 
2128
                        else:
 
2129
                            parent_trans_id = tt.trans_id_file_id(
 
2130
                                entry.parent_id)
 
2131
                            tt.adjust_path(entry.name, parent_trans_id,
 
2132
                                           trans_id)
 
2133
                if create:
 
2134
                    tt.create_directory(trans_id)
 
2135
                    new_conflicts.add((c_type, 'Created directory', trans_id))
2015
2136
        elif c_type == 'unversioned parent':
2016
 
            tt.version_file(tt.inactive_file_id(conflict[1]), conflict[1])
 
2137
            file_id = tt.inactive_file_id(conflict[1])
 
2138
            # special-case the other tree root (move its children instead)
 
2139
            if path_tree and file_id in path_tree:
 
2140
                if path_tree.inventory[file_id].parent_id is None:
 
2141
                    continue
 
2142
            tt.version_file(file_id, conflict[1])
2017
2143
            new_conflicts.add((c_type, 'Versioned directory', conflict[1]))
2018
2144
        elif c_type == 'non-directory parent':
2019
2145
            parent_id = conflict[1]
2023
2149
            new_parent_id = tt.new_directory(parent_name + '.new',
2024
2150
                parent_parent, parent_file_id)
2025
2151
            _reparent_transform_children(tt, parent_id, new_parent_id)
2026
 
            tt.unversion_file(parent_id)
 
2152
            if parent_file_id is not None:
 
2153
                tt.unversion_file(parent_id)
2027
2154
            new_conflicts.add((c_type, 'Created directory', new_parent_id))
2028
2155
    return new_conflicts
2029
2156