/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/transform.py

Add a NEWS entry and prepare submission.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008, 2009 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
17
17
import os
18
18
import errno
19
19
from stat import S_ISREG, S_IEXEC
20
 
import time
21
20
 
22
21
from bzrlib.lazy_import import lazy_import
23
22
lazy_import(globals(), """
25
24
    annotate,
26
25
    bencode,
27
26
    bzrdir,
28
 
    commit,
29
27
    delta,
30
28
    errors,
31
29
    inventory,
32
30
    multiparent,
33
31
    osutils,
34
32
    revision as _mod_revision,
35
 
    ui,
36
33
    )
37
34
""")
38
35
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
39
 
                           ReusingTransform, CantMoveRoot,
 
36
                           ReusingTransform, NotVersionedError, CantMoveRoot,
40
37
                           ExistingLimbo, ImmortalLimbo, NoFinalPath,
41
38
                           UnableCreateSymlink)
42
39
from bzrlib.filters import filtered_output_bytes, ContentFilterContext
51
48
    splitpath,
52
49
    supports_executable,
53
50
)
54
 
from bzrlib.progress import ProgressPhase
 
51
from bzrlib.progress import DummyProgress, ProgressPhase
55
52
from bzrlib.symbol_versioning import (
56
53
        deprecated_function,
57
54
        deprecated_in,
81
78
class TreeTransformBase(object):
82
79
    """The base class for TreeTransform and its kin."""
83
80
 
84
 
    def __init__(self, tree, pb=None,
 
81
    def __init__(self, tree, pb=DummyProgress(),
85
82
                 case_sensitive=True):
86
83
        """Constructor.
87
84
 
88
85
        :param tree: The tree that will be transformed, but not necessarily
89
86
            the output tree.
90
 
        :param pb: ignored
 
87
        :param pb: A ProgressTask indicating how much progress is being made
91
88
        :param case_sensitive: If True, the target of the transform is
92
89
            case sensitive, not just case preserving.
93
90
        """
164
161
 
165
162
    def adjust_path(self, name, parent, trans_id):
166
163
        """Change the path that is assigned to a transaction id."""
167
 
        if parent is None:
168
 
            raise ValueError("Parent trans-id may not be None")
169
164
        if trans_id == self._new_root:
170
165
            raise CantMoveRoot
171
166
        self._new_name[trans_id] = name
172
167
        self._new_parent[trans_id] = parent
 
168
        if parent == ROOT_PARENT:
 
169
            if self._new_root is not None:
 
170
                raise ValueError("Cannot have multiple roots.")
 
171
            self._new_root = trans_id
173
172
 
174
173
    def adjust_root_path(self, name, parent):
175
174
        """Emulate moving the root by moving all children, instead.
203
202
        self.version_file(old_root_file_id, old_root)
204
203
        self.unversion_file(self._new_root)
205
204
 
206
 
    def fixup_new_roots(self):
207
 
        """Reinterpret requests to change the root directory
208
 
 
209
 
        Instead of creating a root directory, or moving an existing directory,
210
 
        all the attributes and children of the new root are applied to the
211
 
        existing root directory.
212
 
 
213
 
        This means that the old root trans-id becomes obsolete, so it is
214
 
        recommended only to invoke this after the root trans-id has become
215
 
        irrelevant.
216
 
        """
217
 
        new_roots = [k for k, v in self._new_parent.iteritems() if v is
218
 
                     ROOT_PARENT]
219
 
        if len(new_roots) < 1:
220
 
            return
221
 
        if len(new_roots) != 1:
222
 
            raise ValueError('A tree cannot have two roots!')
223
 
        if self._new_root is None:
224
 
            self._new_root = new_roots[0]
225
 
            return
226
 
        old_new_root = new_roots[0]
227
 
        # TODO: What to do if a old_new_root is present, but self._new_root is
228
 
        #       not listed as being removed? This code explicitly unversions
229
 
        #       the old root and versions it with the new file_id. Though that
230
 
        #       seems like an incomplete delta
231
 
 
232
 
        # unversion the new root's directory.
233
 
        file_id = self.final_file_id(old_new_root)
234
 
        if old_new_root in self._new_id:
235
 
            self.cancel_versioning(old_new_root)
236
 
        else:
237
 
            self.unversion_file(old_new_root)
238
 
        # if, at this stage, root still has an old file_id, zap it so we can
239
 
        # stick a new one in.
240
 
        if (self.tree_file_id(self._new_root) is not None and
241
 
            self._new_root not in self._removed_id):
242
 
            self.unversion_file(self._new_root)
243
 
        self.version_file(file_id, self._new_root)
244
 
 
245
 
        # Now move children of new root into old root directory.
246
 
        # Ensure all children are registered with the transaction, but don't
247
 
        # use directly-- some tree children have new parents
248
 
        list(self.iter_tree_children(old_new_root))
249
 
        # Move all children of new root into old root directory.
250
 
        for child in self.by_parent().get(old_new_root, []):
251
 
            self.adjust_path(self.final_name(child), self._new_root, child)
252
 
 
253
 
        # Ensure old_new_root has no directory.
254
 
        if old_new_root in self._new_contents:
255
 
            self.cancel_creation(old_new_root)
256
 
        else:
257
 
            self.delete_contents(old_new_root)
258
 
 
259
 
        # prevent deletion of root directory.
260
 
        if self._new_root in self._removed_contents:
261
 
            self.cancel_deletion(self._new_root)
262
 
 
263
 
        # destroy path info for old_new_root.
264
 
        del self._new_parent[old_new_root]
265
 
        del self._new_name[old_new_root]
266
 
 
267
205
    def trans_id_tree_file_id(self, inventory_id):
268
206
        """Determine the transaction id of a working tree file.
269
207
 
315
253
 
316
254
    def delete_contents(self, trans_id):
317
255
        """Schedule the contents of a path entry for deletion"""
318
 
        # Ensure that the object exists in the WorkingTree, this will raise an
319
 
        # exception if there is a problem
320
256
        self.tree_kind(trans_id)
321
257
        self._removed_contents.add(trans_id)
322
258
 
928
864
        """
929
865
        return _PreviewTree(self)
930
866
 
931
 
    def commit(self, branch, message, merge_parents=None, strict=False,
932
 
               timestamp=None, timezone=None, committer=None, authors=None,
933
 
               revprops=None, revision_id=None):
 
867
    def commit(self, branch, message, merge_parents=None, strict=False):
934
868
        """Commit the result of this TreeTransform to a branch.
935
869
 
936
870
        :param branch: The branch to commit to.
937
871
        :param message: The message to attach to the commit.
938
 
        :param merge_parents: Additional parent revision-ids specified by
939
 
            pending merges.
940
 
        :param strict: If True, abort the commit if there are unversioned
941
 
            files.
942
 
        :param timestamp: if not None, seconds-since-epoch for the time and
943
 
            date.  (May be a float.)
944
 
        :param timezone: Optional timezone for timestamp, as an offset in
945
 
            seconds.
946
 
        :param committer: Optional committer in email-id format.
947
 
            (e.g. "J Random Hacker <jrandom@example.com>")
948
 
        :param authors: Optional list of authors in email-id format.
949
 
        :param revprops: Optional dictionary of revision properties.
950
 
        :param revision_id: Optional revision id.  (Specifying a revision-id
951
 
            may reduce performance for some non-native formats.)
 
872
        :param merge_parents: Additional parents specified by pending merges.
952
873
        :return: The revision_id of the revision committed.
953
874
        """
954
875
        self._check_malformed()
971
892
        if self._tree.get_revision_id() != last_rev_id:
972
893
            raise ValueError('TreeTransform not based on branch basis: %s' %
973
894
                             self._tree.get_revision_id())
974
 
        revprops = commit.Commit.update_revprops(revprops, branch, authors)
975
 
        builder = branch.get_commit_builder(parent_ids,
976
 
                                            timestamp=timestamp,
977
 
                                            timezone=timezone,
978
 
                                            committer=committer,
979
 
                                            revprops=revprops,
980
 
                                            revision_id=revision_id)
 
895
        builder = branch.get_commit_builder(parent_ids)
981
896
        preview = self.get_preview_tree()
982
897
        list(builder.record_iter_changes(preview, last_rev_id,
983
898
                                         self.iter_changes()))
1085
1000
class DiskTreeTransform(TreeTransformBase):
1086
1001
    """Tree transform storing its contents on disk."""
1087
1002
 
1088
 
    def __init__(self, tree, limbodir, pb=None,
 
1003
    def __init__(self, tree, limbodir, pb=DummyProgress(),
1089
1004
                 case_sensitive=True):
1090
1005
        """Constructor.
1091
1006
        :param tree: The tree that will be transformed, but not necessarily
1092
1007
            the output tree.
1093
1008
        :param limbodir: A directory where new files can be stored until
1094
1009
            they are installed in their proper places
1095
 
        :param pb: ignored
 
1010
        :param pb: A ProgressBar indicating how much progress is being made
1096
1011
        :param case_sensitive: If True, the target of the transform is
1097
1012
            case sensitive, not just case preserving.
1098
1013
        """
1108
1023
        self._limbo_children_names = {}
1109
1024
        # List of transform ids that need to be renamed from limbo into place
1110
1025
        self._needs_rename = set()
1111
 
        self._creation_mtime = None
1112
1026
 
1113
1027
    def finalize(self):
1114
1028
        """Release the working tree lock, if held, clean up limbo dir.
1161
1075
        if (trans_id in self._limbo_files and
1162
1076
            trans_id not in self._needs_rename):
1163
1077
            self._rename_in_limbo([trans_id])
1164
 
            if previous_parent != parent:
1165
 
                self._limbo_children[previous_parent].remove(trans_id)
1166
 
            if previous_parent != parent or previous_name != name:
1167
 
                del self._limbo_children_names[previous_parent][previous_name]
 
1078
            self._limbo_children[previous_parent].remove(trans_id)
 
1079
            del self._limbo_children_names[previous_parent][previous_name]
1168
1080
 
1169
1081
    def _rename_in_limbo(self, trans_ids):
1170
1082
        """Fix limbo names so that the right final path is produced.
1182
1094
            if trans_id not in self._new_contents:
1183
1095
                continue
1184
1096
            new_path = self._limbo_name(trans_id)
1185
 
            osutils.rename(old_path, new_path)
 
1097
            os.rename(old_path, new_path)
1186
1098
            for descendant in self._limbo_descendants(trans_id):
1187
1099
                desc_path = self._limbo_files[descendant]
1188
1100
                desc_path = new_path + desc_path[len(old_path):]
1221
1133
            f.writelines(contents)
1222
1134
        finally:
1223
1135
            f.close()
1224
 
        self._set_mtime(name)
1225
1136
        self._set_mode(trans_id, mode_id, S_ISREG)
1226
1137
 
1227
1138
    def _read_file_chunks(self, trans_id):
1234
1145
    def _read_symlink_target(self, trans_id):
1235
1146
        return os.readlink(self._limbo_name(trans_id))
1236
1147
 
1237
 
    def _set_mtime(self, path):
1238
 
        """All files that are created get the same mtime.
1239
 
 
1240
 
        This time is set by the first object to be created.
1241
 
        """
1242
 
        if self._creation_mtime is None:
1243
 
            self._creation_mtime = time.time()
1244
 
        os.utime(path, (self._creation_mtime, self._creation_mtime))
1245
 
 
1246
1148
    def create_hardlink(self, path, trans_id):
1247
1149
        """Schedule creation of a hard link"""
1248
1150
        name = self._limbo_name(trans_id)
1362
1264
    FileMover does not delete files until it is sure that a rollback will not
1363
1265
    happen.
1364
1266
    """
1365
 
    def __init__(self, tree, pb=None):
 
1267
    def __init__(self, tree, pb=DummyProgress()):
1366
1268
        """Note: a tree_write lock is taken on the tree.
1367
1269
 
1368
1270
        Use TreeTransform.finalize() to release the lock (can be omitted if
1651
1553
                child_pb.update('removing file', num, len(tree_paths))
1652
1554
                full_path = self._tree.abspath(path)
1653
1555
                if trans_id in self._removed_contents:
1654
 
                    delete_path = os.path.join(self._deletiondir, trans_id)
1655
 
                    mover.pre_delete(full_path, delete_path)
1656
 
                elif (trans_id in self._new_name
1657
 
                      or trans_id in self._new_parent):
 
1556
                    mover.pre_delete(full_path, os.path.join(self._deletiondir,
 
1557
                                     trans_id))
 
1558
                elif trans_id in self._new_name or trans_id in \
 
1559
                    self._new_parent:
1658
1560
                    try:
1659
1561
                        mover.rename(full_path, self._limbo_name(trans_id))
1660
 
                    except errors.TransformRenameFailed, e:
 
1562
                    except OSError, e:
1661
1563
                        if e.errno != errno.ENOENT:
1662
1564
                            raise
1663
1565
                    else:
1688
1590
                if trans_id in self._needs_rename:
1689
1591
                    try:
1690
1592
                        mover.rename(self._limbo_name(trans_id), full_path)
1691
 
                    except errors.TransformRenameFailed, e:
 
1593
                    except OSError, e:
1692
1594
                        # We may be renaming a dangling inventory id
1693
1595
                        if e.errno != errno.ENOENT:
1694
1596
                            raise
1714
1616
    unversioned files in the input tree.
1715
1617
    """
1716
1618
 
1717
 
    def __init__(self, tree, pb=None, case_sensitive=True):
 
1619
    def __init__(self, tree, pb=DummyProgress(), case_sensitive=True):
1718
1620
        tree.lock_read()
1719
1621
        limbodir = osutils.mkdtemp(prefix='bzr-limbo-')
1720
1622
        DiskTreeTransform.__init__(self, tree, limbodir, pb, case_sensitive)
1792
1694
        parent_keys = [(file_id, self._file_revision(t, file_id)) for t in
1793
1695
                       self._iter_parent_trees()]
1794
1696
        vf.add_lines((file_id, tree_revision), parent_keys,
1795
 
                     self.get_file_lines(file_id))
 
1697
                     self.get_file(file_id).readlines())
1796
1698
        repo = self._get_repository()
1797
1699
        base_vf = repo.texts
1798
1700
        if base_vf not in vf.fallback_versionedfiles:
1820
1722
            executable = self.is_executable(file_id, path)
1821
1723
        return kind, executable, None
1822
1724
 
1823
 
    def is_locked(self):
1824
 
        return False
1825
 
 
1826
1725
    def lock_read(self):
1827
1726
        # Perhaps in theory, this should lock the TreeTransform?
1828
 
        return self
 
1727
        pass
1829
1728
 
1830
1729
    def unlock(self):
1831
1730
        pass
2460
2359
    if entry.kind == "directory":
2461
2360
        return True
2462
2361
    if entry.kind == "file":
2463
 
        f = file(target_path, 'rb')
2464
 
        try:
2465
 
            if tree.get_file_text(file_id) == f.read():
2466
 
                return True
2467
 
        finally:
2468
 
            f.close()
 
2362
        if tree.get_file(file_id).read() == file(target_path, 'rb').read():
 
2363
            return True
2469
2364
    elif entry.kind == "symlink":
2470
2365
        if tree.get_symlink_target(file_id) == os.readlink(target_path):
2471
2366
            return True
2619
2514
 
2620
2515
 
2621
2516
def revert(working_tree, target_tree, filenames, backups=False,
2622
 
           pb=None, change_reporter=None):
 
2517
           pb=DummyProgress(), change_reporter=None):
2623
2518
    """Revert a working tree's contents to those of a target tree."""
2624
2519
    target_tree.lock_read()
2625
 
    pb = ui.ui_factory.nested_progress_bar()
2626
2520
    tt = TreeTransform(working_tree, pb)
2627
2521
    try:
2628
2522
        pp = ProgressPhase("Revert phase", 3, pb)
2647
2541
def _prepare_revert_transform(working_tree, target_tree, tt, filenames,
2648
2542
                              backups, pp, basis_tree=None,
2649
2543
                              merge_modified=None):
 
2544
    pp.next_phase()
2650
2545
    child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2651
2546
    try:
2652
2547
        if merge_modified is None:
2656
2551
                                      merge_modified, basis_tree)
2657
2552
    finally:
2658
2553
        child_pb.finished()
 
2554
    pp.next_phase()
2659
2555
    child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
2660
2556
    try:
2661
2557
        raw_conflicts = resolve_conflicts(tt, child_pb,
2754
2650
                    parent_trans = ROOT_PARENT
2755
2651
                else:
2756
2652
                    parent_trans = tt.trans_id_file_id(parent[1])
2757
 
                if parent[0] is None and versioned[0]:
2758
 
                    tt.adjust_root_path(name[1], parent_trans)
2759
 
                else:
2760
 
                    tt.adjust_path(name[1], parent_trans, trans_id)
 
2653
                tt.adjust_path(name[1], parent_trans, trans_id)
2761
2654
            if executable[0] != executable[1] and kind[1] == "file":
2762
2655
                tt.set_executability(executable[1], trans_id)
2763
2656
        if working_tree.supports_content_filtering():
2776
2669
            for (trans_id, mode_id), bytes in target_tree.iter_files_bytes(
2777
2670
                deferred_files):
2778
2671
                tt.create_file(bytes, trans_id, mode_id)
2779
 
        tt.fixup_new_roots()
2780
2672
    finally:
2781
2673
        if basis_tree is not None:
2782
2674
            basis_tree.unlock()
2783
2675
    return merge_modified
2784
2676
 
2785
2677
 
2786
 
def resolve_conflicts(tt, pb=None, pass_func=None):
 
2678
def resolve_conflicts(tt, pb=DummyProgress(), pass_func=None):
2787
2679
    """Make many conflict-resolution attempts, but die if they fail"""
2788
2680
    if pass_func is None:
2789
2681
        pass_func = conflict_pass
2790
2682
    new_conflicts = set()
2791
 
    pb = ui.ui_factory.nested_progress_bar()
2792
2683
    try:
2793
2684
        for n in range(10):
2794
2685
            pb.update('Resolution pass', n+1, 10)
2798
2689
            new_conflicts.update(pass_func(tt, conflicts))
2799
2690
        raise MalformedTransform(conflicts=conflicts)
2800
2691
    finally:
2801
 
        pb.finished()
 
2692
        pb.clear()
2802
2693
 
2803
2694
 
2804
2695
def conflict_pass(tt, conflicts, path_tree=None):
2853
2744
                        # special-case the other tree root (move its
2854
2745
                        # children to current root)
2855
2746
                        if entry.parent_id is None:
2856
 
                            create = False
 
2747
                            create=False
2857
2748
                            moved = _reparent_transform_children(
2858
2749
                                tt, trans_id, tt.root)
2859
2750
                            for child in moved:
2927
2818
        self.pending_deletions = []
2928
2819
 
2929
2820
    def rename(self, from_, to):
2930
 
        """Rename a file from one path to another."""
 
2821
        """Rename a file from one path to another.  Functions like os.rename"""
2931
2822
        try:
2932
 
            osutils.rename(from_, to)
2933
 
        except (IOError, OSError), e:
 
2823
            os.rename(from_, to)
 
2824
        except OSError, e:
2934
2825
            if e.errno in (errno.EEXIST, errno.ENOTEMPTY):
2935
2826
                raise errors.FileExists(to, str(e))
2936
 
            # normal OSError doesn't include filenames so it's hard to see where
2937
 
            # the problem is, see https://bugs.launchpad.net/bzr/+bug/491763
2938
 
            raise errors.TransformRenameFailed(from_, to, str(e), e.errno)
 
2827
            raise
2939
2828
        self.past_renames.append((from_, to))
2940
2829
 
2941
2830
    def pre_delete(self, from_, to):
2951
2840
    def rollback(self):
2952
2841
        """Reverse all renames that have been performed"""
2953
2842
        for from_, to in reversed(self.past_renames):
2954
 
            try:
2955
 
                osutils.rename(to, from_)
2956
 
            except (OSError, IOError), e:
2957
 
                raise errors.TransformRenameFailed(to, from_, str(e), e.errno)                
 
2843
            os.rename(to, from_)
2958
2844
        # after rollback, don't reuse _FileMover
2959
2845
        past_renames = None
2960
2846
        pending_deletions = None