~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transform.py

Optimize common case where unique_lcs returns a set of lines all in a row

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2006 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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
import errno
19
19
from stat import S_ISREG
20
20
 
21
 
from bzrlib import bzrdir, errors
22
21
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
23
22
                           ReusingTransform, NotVersionedError, CantMoveRoot,
24
 
                           ExistingLimbo, ImmortalLimbo, NoFinalPath)
 
23
                           ExistingLimbo, ImmortalLimbo)
25
24
from bzrlib.inventory import InventoryEntry
26
25
from bzrlib.osutils import (file_kind, supports_executable, pathjoin, lexists,
27
26
                            delete_any)
28
27
from bzrlib.progress import DummyProgress, ProgressPhase
29
28
from bzrlib.trace import mutter, warning
30
 
from bzrlib import tree
31
29
import bzrlib.ui 
32
 
import bzrlib.urlutils as urlutils
33
30
 
34
31
 
35
32
ROOT_PARENT = "root-parent"
73
70
     * set_executability
74
71
    """
75
72
    def __init__(self, tree, pb=DummyProgress()):
76
 
        """Note: a tree_write lock is taken on the tree.
 
73
        """Note: a write lock is taken on the tree.
77
74
        
78
75
        Use TreeTransform.finalize() to release the lock
79
76
        """
80
77
        object.__init__(self)
81
78
        self._tree = tree
82
 
        self._tree.lock_tree_write()
 
79
        self._tree.lock_write()
83
80
        try:
84
81
            control_files = self._tree._control_files
85
 
            self._limbodir = urlutils.local_path_from_url(
86
 
                control_files.controlfilename('limbo'))
 
82
            self._limbodir = control_files.controlfilename('limbo')
87
83
            try:
88
84
                os.mkdir(self._limbodir)
89
85
            except OSError, e:
261
257
        New file takes the permissions of any existing file with that id,
262
258
        unless mode_id is specified.
263
259
        """
264
 
        name = self._limbo_name(trans_id)
265
 
        f = open(name, 'wb')
266
 
        try:
267
 
            try:
268
 
                unique_add(self._new_contents, trans_id, 'file')
269
 
            except:
270
 
                # Clean up the file, it never got registered so
271
 
                # TreeTransform.finalize() won't clean it up.
272
 
                f.close()
273
 
                os.unlink(name)
274
 
                raise
275
 
 
276
 
            for segment in contents:
277
 
                f.write(segment)
278
 
        finally:
279
 
            f.close()
 
260
        f = file(self._limbo_name(trans_id), 'wb')
 
261
        unique_add(self._new_contents, trans_id, 'file')
 
262
        for segment in contents:
 
263
            f.write(segment)
 
264
        f.close()
280
265
        self._set_mode(trans_id, mode_id, S_ISREG)
281
266
 
282
267
    def _set_mode(self, trans_id, mode_id, typefunc):
292
277
        except KeyError:
293
278
            return
294
279
        try:
295
 
            mode = os.stat(self._tree.abspath(old_path)).st_mode
 
280
            mode = os.stat(old_path).st_mode
296
281
        except OSError, e:
297
282
            if e.errno == errno.ENOENT:
298
283
                return
457
442
        try:
458
443
            return self._new_name[trans_id]
459
444
        except KeyError:
460
 
            try:
461
 
                return os.path.basename(self._tree_id_paths[trans_id])
462
 
            except KeyError:
463
 
                raise NoFinalPath(trans_id, self)
 
445
            return os.path.basename(self._tree_id_paths[trans_id])
464
446
 
465
447
    def by_parent(self):
466
448
        """Return a map of parent: children for known parents.
479
461
 
480
462
    def path_changed(self, trans_id):
481
463
        """Return True if a trans_id's path has changed."""
482
 
        return (trans_id in self._new_name) or (trans_id in self._new_parent)
483
 
 
484
 
    def new_contents(self, trans_id):
485
 
        return (trans_id in self._new_contents)
 
464
        return trans_id in self._new_name or trans_id in self._new_parent
486
465
 
487
466
    def find_conflicts(self):
488
467
        """Find any violations of inventory or filesystem invariants"""
514
493
                        self.tree_kind(t) == 'directory'])
515
494
        for trans_id in self._removed_id:
516
495
            file_id = self.tree_file_id(trans_id)
517
 
            if self._tree.inventory[file_id].kind == 'directory':
 
496
            if self._tree.inventory[file_id].kind in ('directory', 
 
497
                                                      'root_directory'):
518
498
                parents.append(trans_id)
519
499
 
520
500
        for parent_id in parents:
557
537
        if child_id is None:
558
538
            return lexists(self._tree.abspath(childpath))
559
539
        else:
560
 
            if self.final_parent(child_id) != parent_id:
 
540
            if tt.final_parent(child_id) != parent_id:
561
541
                return False
562
 
            if child_id in self._removed_contents:
 
542
            if child_id in tt._removed_contents:
563
543
                # XXX What about dangling file-ids?
564
544
                return False
565
545
            else:
573
553
            parent_id = trans_id
574
554
            while parent_id is not ROOT_PARENT:
575
555
                seen.add(parent_id)
576
 
                try:
577
 
                    parent_id = self.final_parent(parent_id)
578
 
                except KeyError:
579
 
                    break
 
556
                parent_id = self.final_parent(parent_id)
580
557
                if parent_id == trans_id:
581
558
                    conflicts.append(('parent loop', trans_id))
582
559
                if parent_id in seen:
656
633
            last_name = None
657
634
            last_trans_id = None
658
635
            for name, trans_id in name_ids:
 
636
                if name == last_name:
 
637
                    conflicts.append(('duplicate', last_trans_id, trans_id,
 
638
                    name))
659
639
                try:
660
640
                    kind = self.final_kind(trans_id)
661
641
                except NoSuchFile:
662
642
                    kind = None
663
643
                file_id = self.final_file_id(trans_id)
664
 
                if kind is None and file_id is None:
665
 
                    continue
666
 
                if name == last_name:
667
 
                    conflicts.append(('duplicate', last_trans_id, trans_id,
668
 
                    name))
669
 
                last_name = name
670
 
                last_trans_id = trans_id
 
644
                if kind is not None or file_id is not None:
 
645
                    last_name = name
 
646
                    last_trans_id = trans_id
671
647
        return conflicts
672
648
 
673
649
    def _duplicate_ids(self):
869
845
        parent_id is the transaction id of the parent directory of the file.
870
846
        contents is an iterator of bytestrings, which will be used to produce
871
847
        the file.
872
 
        :param file_id: The inventory ID of the file, if it is to be versioned.
873
 
        :param executable: Only valid when a file_id has been supplied.
 
848
        file_id is the inventory ID of the file, if it is to be versioned.
874
849
        """
875
850
        trans_id = self._new_entry(name, parent_id, file_id)
876
 
        # TODO: rather than scheduling a set_executable call,
877
 
        # have create_file create the file with the right mode.
878
851
        self.create_file(contents, trans_id)
879
852
        if executable is not None:
880
853
            self.set_executability(executable, trans_id)
945
918
    file_ids.sort(key=tree.id2path)
946
919
    return file_ids
947
920
 
948
 
 
949
921
def build_tree(tree, wt):
950
 
    """Create working tree for a branch, using a TreeTransform.
951
 
    
952
 
    This function should be used on empty trees, having a tree root at most.
953
 
    (see merge and revert functionality for working with existing trees)
954
 
 
955
 
    Existing files are handled like so:
956
 
    
957
 
    - Existing bzrdirs take precedence over creating new items.  They are
958
 
      created as '%s.diverted' % name.
959
 
    - Otherwise, if the content on disk matches the content we are building,
960
 
      it is silently replaced.
961
 
    - Otherwise, conflict resolution will move the old file to 'oldname.moved'.
962
 
    """
963
 
    if len(wt.inventory) > 1:  # more than just a root
964
 
        raise errors.WorkingTreeAlreadyPopulated(base=wt.basedir)
 
922
    """Create working tree for a branch, using a Transaction."""
965
923
    file_trans_id = {}
966
924
    top_pb = bzrlib.ui.ui_factory.nested_progress_bar()
967
925
    pp = ProgressPhase("Build phase", 2, top_pb)
968
 
    if tree.inventory.root is not None:
969
 
        wt.set_root_id(tree.inventory.root.file_id)
970
926
    tt = TreeTransform(wt)
971
 
    divert = set()
972
927
    try:
973
928
        pp.next_phase()
974
 
        file_trans_id[wt.get_root_id()] = \
975
 
            tt.trans_id_tree_file_id(wt.get_root_id())
 
929
        file_trans_id[wt.get_root_id()] = tt.trans_id_tree_file_id(wt.get_root_id())
 
930
        file_ids = topology_sorted_ids(tree)
976
931
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
977
932
        try:
978
 
            for num, (tree_path, entry) in \
979
 
                enumerate(tree.inventory.iter_entries_by_dir()):
980
 
                pb.update("Building tree", num, len(tree.inventory))
 
933
            for num, file_id in enumerate(file_ids):
 
934
                pb.update("Building tree", num, len(file_ids))
 
935
                entry = tree.inventory[file_id]
981
936
                if entry.parent_id is None:
982
937
                    continue
983
 
                reparent = False
984
 
                file_id = entry.file_id
985
 
                target_path = wt.abspath(tree_path)
986
 
                try:
987
 
                    kind = file_kind(target_path)
988
 
                except NoSuchFile:
989
 
                    pass
990
 
                else:
991
 
                    if kind == "directory":
992
 
                        try:
993
 
                            bzrdir.BzrDir.open(target_path)
994
 
                        except errors.NotBranchError:
995
 
                            pass
996
 
                        else:
997
 
                            divert.add(file_id)
998
 
                    if (file_id not in divert and
999
 
                        _content_match(tree, entry, file_id, kind,
1000
 
                        target_path)):
1001
 
                        tt.delete_contents(tt.trans_id_tree_path(tree_path))
1002
 
                        if kind == 'directory':
1003
 
                            reparent = True
1004
938
                if entry.parent_id not in file_trans_id:
1005
939
                    raise repr(entry.parent_id)
1006
940
                parent_id = file_trans_id[entry.parent_id]
1007
 
                file_trans_id[file_id] = new_by_entry(tt, entry, parent_id,
 
941
                file_trans_id[file_id] = new_by_entry(tt, entry, parent_id, 
1008
942
                                                      tree)
1009
 
                if reparent:
1010
 
                    new_trans_id = file_trans_id[file_id]
1011
 
                    old_parent = tt.trans_id_tree_path(tree_path)
1012
 
                    _reparent_children(tt, old_parent, new_trans_id)
1013
943
        finally:
1014
944
            pb.finished()
1015
945
        pp.next_phase()
1016
 
        divert_trans = set(file_trans_id[f] for f in divert)
1017
 
        resolver = lambda t, c: resolve_checkout(t, c, divert_trans)
1018
 
        raw_conflicts = resolve_conflicts(tt, pass_func=resolver)
1019
 
        conflicts = cook_conflicts(raw_conflicts, tt)
1020
 
        for conflict in conflicts:
1021
 
            warning(conflict)
1022
 
        try:
1023
 
            wt.add_conflicts(conflicts)
1024
 
        except errors.UnsupportedOperation:
1025
 
            pass
1026
946
        tt.apply()
1027
947
    finally:
1028
948
        tt.finalize()
1029
949
        top_pb.finished()
1030
950
 
1031
 
 
1032
 
def _reparent_children(tt, old_parent, new_parent):
1033
 
    for child in tt.iter_tree_children(old_parent):
1034
 
        tt.adjust_path(tt.final_name(child), new_parent, child)
1035
 
 
1036
 
 
1037
 
def _content_match(tree, entry, file_id, kind, target_path):
1038
 
    if entry.kind != kind:
1039
 
        return False
1040
 
    if entry.kind == "directory":
1041
 
        return True
1042
 
    if entry.kind == "file":
1043
 
        if tree.get_file(file_id).read() == file(target_path, 'rb').read():
1044
 
            return True
1045
 
    elif entry.kind == "symlink":
1046
 
        if tree.get_symlink_target(file_id) == os.readlink(target_path):
1047
 
            return True
1048
 
    return False
1049
 
 
1050
 
 
1051
 
def resolve_checkout(tt, conflicts, divert):
1052
 
    new_conflicts = set()
1053
 
    for c_type, conflict in ((c[0], c) for c in conflicts):
1054
 
        # Anything but a 'duplicate' would indicate programmer error
1055
 
        assert c_type == 'duplicate', c_type
1056
 
        # Now figure out which is new and which is old
1057
 
        if tt.new_contents(conflict[1]):
1058
 
            new_file = conflict[1]
1059
 
            old_file = conflict[2]
1060
 
        else:
1061
 
            new_file = conflict[2]
1062
 
            old_file = conflict[1]
1063
 
 
1064
 
        # We should only get here if the conflict wasn't completely
1065
 
        # resolved
1066
 
        final_parent = tt.final_parent(old_file)
1067
 
        if new_file in divert:
1068
 
            new_name = tt.final_name(old_file)+'.diverted'
1069
 
            tt.adjust_path(new_name, final_parent, new_file)
1070
 
            new_conflicts.add((c_type, 'Diverted to',
1071
 
                               new_file, old_file))
1072
 
        else:
1073
 
            new_name = tt.final_name(old_file)+'.moved'
1074
 
            tt.adjust_path(new_name, final_parent, old_file)
1075
 
            new_conflicts.add((c_type, 'Moved existing file to',
1076
 
                               old_file, new_file))
1077
 
    return new_conflicts
1078
 
 
1079
 
 
1080
951
def new_by_entry(tt, entry, parent_id, tree):
1081
952
    """Create a new file according to its inventory entry"""
1082
953
    name = entry.name
1095
966
def create_by_entry(tt, entry, tree, trans_id, lines=None, mode_id=None):
1096
967
    """Create new file contents according to an inventory entry."""
1097
968
    if entry.kind == "file":
1098
 
        if lines is None:
 
969
        if lines == None:
1099
970
            lines = tree.get_file(entry.file_id).readlines()
1100
971
        tt.create_file(lines, trans_id, mode_id=mode_id)
1101
972
    elif entry.kind == "symlink":
1111
982
 
1112
983
def find_interesting(working_tree, target_tree, filenames):
1113
984
    """Find the ids corresponding to specified filenames."""
1114
 
    trees = (working_tree, target_tree)
1115
 
    return tree.find_ids_across_trees(filenames, trees)
 
985
    if not filenames:
 
986
        interesting_ids = None
 
987
    else:
 
988
        interesting_ids = set()
 
989
        for tree_path in filenames:
 
990
            not_found = True
 
991
            for tree in (working_tree, target_tree):
 
992
                file_id = tree.inventory.path2id(tree_path)
 
993
                if file_id is not None:
 
994
                    interesting_ids.add(file_id)
 
995
                    not_found = False
 
996
            if not_found:
 
997
                raise NotVersionedError(path=tree_path)
 
998
    return interesting_ids
1116
999
 
1117
1000
 
1118
1001
def change_entry(tt, file_id, working_tree, target_tree, 
1178
1061
    try:
1179
1062
        working_kind = working_tree.kind(file_id)
1180
1063
        has_contents = True
1181
 
    except NoSuchFile:
 
1064
    except OSError, e:
 
1065
        if e.errno != errno.ENOENT:
 
1066
            raise
1182
1067
        has_contents = False
1183
1068
        contents_mod = True
1184
1069
        meta_mod = False
1185
1070
    if has_contents is True:
1186
 
        if entry.kind != working_kind:
 
1071
        real_e_kind = entry.kind
 
1072
        if real_e_kind == 'root_directory':
 
1073
            real_e_kind = 'directory'
 
1074
        if real_e_kind != working_kind:
1187
1075
            contents_mod, meta_mod = True, False
1188
1076
        else:
1189
1077
            cur_entry._read_tree_state(working_tree.id2path(file_id), 
1198
1086
    """Revert a working tree's contents to those of a target tree."""
1199
1087
    interesting_ids = find_interesting(working_tree, target_tree, filenames)
1200
1088
    def interesting(file_id):
1201
 
        return interesting_ids is None or (file_id in interesting_ids)
 
1089
        return interesting_ids is None or file_id in interesting_ids
1202
1090
 
1203
1091
    tt = TreeTransform(working_tree, pb)
1204
1092
    try:
1238
1126
        pp.next_phase()
1239
1127
        wt_interesting = [i for i in working_tree.inventory if interesting(i)]
1240
1128
        child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
1241
 
        basis_tree = None
1242
1129
        try:
1243
1130
            for id_num, file_id in enumerate(wt_interesting):
1244
 
                if (working_tree.inventory.is_root(file_id) and 
1245
 
                    len(target_tree.inventory) == 0):
1246
 
                    continue
1247
1131
                child_pb.update("New file check", id_num+1, 
1248
1132
                                len(sorted_interesting))
1249
1133
                if file_id not in target_tree:
1250
1134
                    trans_id = tt.trans_id_tree_file_id(file_id)
1251
1135
                    tt.unversion_file(trans_id)
1252
 
                    try:
1253
 
                        file_kind = working_tree.kind(file_id)
1254
 
                    except NoSuchFile:
1255
 
                        file_kind = None
1256
 
                    delete_merge_modified = (file_id in merge_modified)
1257
 
                    if file_kind != 'file' and file_kind is not None:
1258
 
                        keep_contents = False
1259
 
                    else:
1260
 
                        if basis_tree is None:
1261
 
                            basis_tree = working_tree.basis_tree()
1262
 
                        wt_sha1 = working_tree.get_file_sha1(file_id)
1263
 
                        if (file_id in merge_modified and 
1264
 
                            merge_modified[file_id] == wt_sha1):
1265
 
                            keep_contents = False
1266
 
                        elif (file_id in basis_tree and 
1267
 
                            basis_tree.get_file_sha1(file_id) == wt_sha1):
1268
 
                            keep_contents = False
1269
 
                        else:
1270
 
                            keep_contents = True
1271
 
                    if not keep_contents:
 
1136
                    if file_id in merge_modified:
1272
1137
                        tt.delete_contents(trans_id)
1273
 
                    if delete_merge_modified:
1274
1138
                        del merge_modified[file_id]
1275
1139
        finally:
1276
1140
            child_pb.finished()
1292
1156
    return conflicts
1293
1157
 
1294
1158
 
1295
 
def resolve_conflicts(tt, pb=DummyProgress(), pass_func=None):
 
1159
def resolve_conflicts(tt, pb=DummyProgress()):
1296
1160
    """Make many conflict-resolution attempts, but die if they fail"""
1297
 
    if pass_func is None:
1298
 
        pass_func = conflict_pass
1299
1161
    new_conflicts = set()
1300
1162
    try:
1301
1163
        for n in range(10):
1303
1165
            conflicts = tt.find_conflicts()
1304
1166
            if len(conflicts) == 0:
1305
1167
                return new_conflicts
1306
 
            new_conflicts.update(pass_func(tt, conflicts))
 
1168
            new_conflicts.update(conflict_pass(tt, conflicts))
1307
1169
        raise MalformedTransform(conflicts=conflicts)
1308
1170
    finally:
1309
1171
        pb.clear()
1342
1204
            trans_id = conflict[1]
1343
1205
            try:
1344
1206
                tt.cancel_deletion(trans_id)
1345
 
                new_conflicts.add(('deleting parent', 'Not deleting', 
1346
 
                                   trans_id))
 
1207
                new_conflicts.add((c_type, 'Not deleting', trans_id))
1347
1208
            except KeyError:
1348
1209
                tt.create_directory(trans_id)
1349
 
                new_conflicts.add((c_type, 'Created directory', trans_id))
 
1210
                new_conflicts.add((c_type, 'Created directory.', trans_id))
1350
1211
        elif c_type == 'unversioned parent':
1351
1212
            tt.version_file(tt.inactive_file_id(conflict[1]), conflict[1])
1352
1213
            new_conflicts.add((c_type, 'Versioned directory', conflict[1]))