~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/changeset.py

  • Committer: Martin Pool
  • Date: 2005-05-15 02:36:04 UTC
  • Revision ID: mbp@sourcefrog.net-20050515023603-7328f6cbabd2b09a
- Merge aaron's merge command

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import errno
18
18
import patch
19
19
import stat
20
 
from bzrlib.trace import mutter
21
20
"""
22
21
Represent and apply a changeset
23
22
"""
25
24
 
26
25
NULL_ID = "!NULL"
27
26
 
28
 
class OldFailedTreeOp(Exception):
29
 
    def __init__(self):
30
 
        Exception.__init__(self, "bzr-tree-change contains files from a"
31
 
                           " previous failed merge operation.")
 
27
 
32
28
def invert_dict(dict):
33
29
    newdict = {}
34
30
    for (key,value) in dict.iteritems():
36
32
    return newdict
37
33
 
38
34
 
39
 
class PatchApply(object):
 
35
class PatchApply:
40
36
    """Patch application as a kind of content change"""
41
37
    def __init__(self, contents):
42
38
        """Constructor.
85
81
            conflict_handler.failed_hunks(filename)
86
82
 
87
83
        
88
 
class ChangeUnixPermissions(object):
 
84
class ChangeUnixPermissions:
89
85
    """This is two-way change, suitable for file modification, creation,
90
86
    deletion"""
91
87
    def __init__(self, old_mode, new_mode):
167
163
                
168
164
            
169
165
 
170
 
class SymlinkCreate(object):
 
166
class SymlinkCreate:
171
167
    """Creates or deletes a symlink (for use with ReplaceContents)"""
172
168
    def __init__(self, contents):
173
169
        """Constructor.
206
202
    def __ne__(self, other):
207
203
        return not (self == other)
208
204
 
209
 
class FileCreate(object):
 
205
class FileCreate:
210
206
    """Create or delete a file (for use with ReplaceContents)"""
211
207
    def __init__(self, contents):
212
208
        """Constructor
269
265
    for i in range(len(sequence)):
270
266
        yield sequence[max - i]
271
267
 
272
 
class ReplaceContents(object):
 
268
class ReplaceContents:
273
269
    """A contents-replacement framework.  It allows a file/directory/symlink to
274
270
    be created, deleted, or replaced with another file/directory/symlink.
275
271
    Arguments must be callable with (filename, reverse).
336
332
            if mode is not None:
337
333
                os.chmod(filename, mode)
338
334
 
339
 
class ApplySequence(object):
 
335
class ApplySequence:
340
336
    def __init__(self, changes=None):
341
337
        self.changes = []
342
338
        if changes is not None:
366
362
            change.apply(filename, conflict_handler, reverse)
367
363
 
368
364
 
369
 
class Diff3Merge(object):
 
365
class Diff3Merge:
370
366
    def __init__(self, base_file, other_file):
371
367
        self.base_file = base_file
372
368
        self.other_file = other_file
694
690
        :type reverse: bool
695
691
        :rtype: str
696
692
        """
697
 
        mutter("Finding new path for %s" % self.summarize_name(changeset))
698
693
        if reverse:
699
694
            parent = self.parent
700
695
            to_dir = self.dir
719
714
        if from_dir == to_dir:
720
715
            dir = os.path.dirname(id_map[self.id])
721
716
        else:
722
 
            mutter("path, new_path: %r %r" % (self.path, self.new_path))
723
717
            parent_entry = changeset.entries[parent]
724
718
            dir = parent_entry.get_new_path(id_map, changeset, reverse)
725
719
        if from_name == to_name:
768
762
        Exception.__init__(self, msg)
769
763
        self.id = id
770
764
 
771
 
class Changeset(object):
 
765
class Changeset:
772
766
    """A set of changes to apply"""
773
767
    def __init__(self):
774
768
        self.entries = {}
832
826
    my_sort(target_entries, shortest_to_longest)
833
827
    return (source_entries, target_entries)
834
828
 
835
 
def rename_to_temp_delete(source_entries, inventory, dir, temp_dir, 
836
 
                          conflict_handler, reverse):
 
829
def rename_to_temp_delete(source_entries, inventory, dir, conflict_handler,
 
830
                          reverse):
837
831
    """Delete and rename entries as appropriate.  Entries are renamed to temp
838
 
    names.  A map of id -> temp name (or None, for deletions) is returned.
 
832
    names.  A map of id -> temp name is returned.
839
833
 
840
834
    :param source_entries: The entries to rename and delete
841
835
    :type source_entries: List of `ChangesetEntry`
848
842
    :return: a mapping of id to temporary name
849
843
    :rtype: Dictionary
850
844
    """
 
845
    temp_dir = os.path.join(dir, "temp")
851
846
    temp_name = {}
852
847
    for i in range(len(source_entries)):
853
848
        entry = source_entries[i]
854
849
        if entry.is_deletion(reverse):
855
850
            path = os.path.join(dir, inventory[entry.id])
856
851
            entry.apply(path, conflict_handler, reverse)
857
 
            temp_name[entry.id] = None
858
852
 
859
853
        else:
860
 
            to_name = os.path.join(temp_dir, str(i))
 
854
            to_name = temp_dir+"/"+str(i)
861
855
            src_path = inventory.get(entry.id)
862
856
            if src_path is not None:
863
857
                src_path = os.path.join(dir, src_path)
873
867
    return temp_name
874
868
 
875
869
 
876
 
def rename_to_new_create(changed_inventory, target_entries, inventory, 
877
 
                         changeset, dir, conflict_handler, reverse):
 
870
def rename_to_new_create(temp_name, target_entries, inventory, changeset, dir,
 
871
                         conflict_handler, reverse):
878
872
    """Rename entries with temp names to their final names, create new files.
879
873
 
880
 
    :param changed_inventory: A mapping of id to temporary name
881
 
    :type changed_inventory: Dictionary
 
874
    :param temp_name: A mapping of id to temporary name
 
875
    :type temp_name: Dictionary
882
876
    :param target_entries: The entries to apply changes to
883
877
    :type target_entries: List of `ChangesetEntry`
884
878
    :param changeset: The changeset to apply
889
883
    :type reverse: bool
890
884
    """
891
885
    for entry in target_entries:
892
 
        new_tree_path = entry.get_new_path(inventory, changeset, reverse)
893
 
        if new_tree_path is None:
 
886
        new_path = entry.get_new_path(inventory, changeset, reverse)
 
887
        if new_path is None:
894
888
            continue
895
 
        new_path = os.path.join(dir, new_tree_path)
896
 
        old_path = changed_inventory.get(entry.id)
 
889
        new_path = os.path.join(dir, new_path)
 
890
        old_path = temp_name.get(entry.id)
897
891
        if os.path.exists(new_path):
898
892
            if conflict_handler.target_exists(entry, new_path, old_path) == \
899
893
                "skip":
900
894
                continue
901
895
        if entry.is_creation(reverse):
902
896
            entry.apply(new_path, conflict_handler, reverse)
903
 
            changed_inventory[entry.id] = new_tree_path
904
897
        else:
905
898
            if old_path is None:
906
899
                continue
907
900
            try:
908
901
                os.rename(old_path, new_path)
909
 
                changed_inventory[entry.id] = new_tree_path
910
902
            except OSError, e:
911
903
                raise Exception ("%s is missing" % new_path)
912
904
 
1014
1006
        Exception.__init__(self, msg)
1015
1007
        self.filename = filename
1016
1008
 
1017
 
class NewContentsConflict(Exception):
1018
 
    def __init__(self, filename):
1019
 
        msg = "Conflicting contents for new file %s" % (filename)
1020
 
        Exception.__init__(self, msg)
1021
 
 
1022
 
 
1023
 
class MissingForMerge(Exception):
1024
 
    def __init__(self, filename):
1025
 
        msg = "The file %s was modified, but does not exist in this tree"\
1026
 
            % (filename)
1027
 
        Exception.__init__(self, msg)
1028
 
 
1029
 
 
1030
 
class ExceptionConflictHandler(object):
 
1009
class ExceptionConflictHandler:
1031
1010
    def __init__(self, dir):
1032
1011
        self.dir = dir
1033
1012
    
1087
1066
    def missing_for_rename(self, filename):
1088
1067
        raise MissingForRename(filename)
1089
1068
 
1090
 
    def missing_for_merge(self, file_id, inventory):
1091
 
        raise MissingForMerge(inventory.other.get_path(file_id))
1092
 
 
1093
 
    def new_contents_conflict(self, filename, other_contents):
1094
 
        raise NewContentsConflict(filename)
1095
 
 
1096
 
    def finalize():
1097
 
        pass
1098
 
 
1099
1069
def apply_changeset(changeset, inventory, dir, conflict_handler=None, 
1100
1070
                    reverse=False):
1101
1071
    """Apply a changeset to a directory.
1113
1083
    """
1114
1084
    if conflict_handler is None:
1115
1085
        conflict_handler = ExceptionConflictHandler(dir)
1116
 
    temp_dir = os.path.join(dir, "bzr-tree-change")
1117
 
    try:
1118
 
        os.mkdir(temp_dir)
1119
 
    except OSError, e:
1120
 
        if e.errno == errno.EEXIST:
1121
 
            try:
1122
 
                os.rmdir(temp_dir)
1123
 
            except OSError, e:
1124
 
                if e.errno == errno.ENOTEMPTY:
1125
 
                    raise OldFailedTreeOp()
1126
 
            os.mkdir(temp_dir)
1127
 
        else:
1128
 
            raise
 
1086
    temp_dir = dir+"/temp"
 
1087
    os.mkdir(temp_dir)
1129
1088
    
1130
1089
    #apply changes that don't affect filenames
1131
1090
    for entry in changeset.entries.itervalues():
1140
1099
    (source_entries, target_entries) = get_rename_entries(changeset, inventory,
1141
1100
                                                          reverse)
1142
1101
 
1143
 
    changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
1144
 
                                              temp_dir, conflict_handler,
1145
 
                                              reverse)
 
1102
    temp_name = rename_to_temp_delete(source_entries, inventory, dir,
 
1103
                                      conflict_handler, reverse)
1146
1104
 
1147
 
    rename_to_new_create(changed_inventory, target_entries, inventory,
1148
 
                         changeset, dir, conflict_handler, reverse)
 
1105
    rename_to_new_create(temp_name, target_entries, inventory, changeset, dir,
 
1106
                         conflict_handler, reverse)
1149
1107
    os.rmdir(temp_dir)
1150
 
    return changed_inventory
 
1108
    r_inventory = invert_dict(inventory)
 
1109
    new_entries, removed_entries = get_inventory_change(inventory,
 
1110
    r_inventory, changeset, reverse)
 
1111
    new_inventory = {}
 
1112
    for path, file_id in new_entries.iteritems():
 
1113
        new_inventory[file_id] = path
 
1114
    for file_id in removed_entries:
 
1115
        new_inventory[file_id] = None
 
1116
    return new_inventory
1151
1117
 
1152
1118
 
1153
1119
def apply_changeset_tree(cset, tree, reverse=False):
1164
1130
def get_inventory_change(inventory, new_inventory, cset, reverse=False):
1165
1131
    new_entries = {}
1166
1132
    remove_entries = []
 
1133
    r_inventory = invert_dict(inventory)
 
1134
    r_new_inventory = invert_dict(new_inventory)
1167
1135
    for entry in cset.entries.itervalues():
1168
1136
        if entry.needs_rename():
1169
 
            new_path = entry.get_new_path(inventory, cset)
1170
 
            if new_path is None:
1171
 
                remove_entries.append(entry.id)
 
1137
            old_path = r_inventory.get(entry.id)
 
1138
            if old_path is not None:
 
1139
                remove_entries.append(old_path)
1172
1140
            else:
1173
 
                new_entries[new_path] = entry.id
 
1141
                new_path = entry.get_new_path(inventory, cset)
 
1142
                if new_path is not None:
 
1143
                    new_entries[new_path] = entry.id
1174
1144
    return new_entries, remove_entries
1175
1145
 
1176
1146
 
1529
1499
 
1530
1500
 
1531
1501
        
1532
 
# XXX: Can't we unify this with the regular inventory object
1533
 
class Inventory(object):
 
1502
    
 
1503
class Inventory:
1534
1504
    def __init__(self, inventory):
1535
1505
        self.inventory = inventory
1536
1506
        self.rinventory = None
1544
1514
        return self.inventory.get(id)
1545
1515
 
1546
1516
    def get_name(self, id):
1547
 
        path = self.get_path(id)
1548
 
        if path is None:
1549
 
            return None
1550
 
        else:
1551
 
            return os.path.basename(path)
 
1517
        return os.path.basename(self.get_path(id))
1552
1518
 
1553
1519
    def get_dir(self, id):
1554
1520
        path = self.get_path(id)
1555
1521
        if path == "":
1556
1522
            return None
1557
 
        if path is None:
1558
 
            return None
1559
1523
        return os.path.dirname(path)
1560
1524
 
1561
1525
    def get_parent(self, id):
1562
 
        if self.get_path(id) is None:
1563
 
            return None
1564
1526
        directory = self.get_dir(id)
1565
1527
        if directory == '.':
1566
1528
            directory = './.'