~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/changeset.py

  • Committer: Martin Pool
  • Date: 2005-07-07 08:02:16 UTC
  • Revision ID: mbp@sourcefrog.net-20050707080216-4e4d884bcf89eb8d
- Merge merge updates from aaron

  aaron.bentley@utoronto.ca-20050706170931-9d2551019af3578d

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
NULL_ID = "!NULL"
26
26
 
27
 
 
 
27
class OldFailedTreeOp(Exception):
 
28
    def __init__(self):
 
29
        Exception.__init__(self, "bzr-tree-change contains files from a"
 
30
                           " previous failed merge operation.")
28
31
def invert_dict(dict):
29
32
    newdict = {}
30
33
    for (key,value) in dict.iteritems():
826
829
    my_sort(target_entries, shortest_to_longest)
827
830
    return (source_entries, target_entries)
828
831
 
829
 
def rename_to_temp_delete(source_entries, inventory, dir, conflict_handler,
830
 
                          reverse):
 
832
def rename_to_temp_delete(source_entries, inventory, dir, temp_dir, 
 
833
                          conflict_handler, reverse):
831
834
    """Delete and rename entries as appropriate.  Entries are renamed to temp
832
 
    names.  A map of id -> temp name is returned.
 
835
    names.  A map of id -> temp name (or None, for deletions) is returned.
833
836
 
834
837
    :param source_entries: The entries to rename and delete
835
838
    :type source_entries: List of `ChangesetEntry`
842
845
    :return: a mapping of id to temporary name
843
846
    :rtype: Dictionary
844
847
    """
845
 
    temp_dir = os.path.join(dir, "temp")
846
848
    temp_name = {}
847
849
    for i in range(len(source_entries)):
848
850
        entry = source_entries[i]
849
851
        if entry.is_deletion(reverse):
850
852
            path = os.path.join(dir, inventory[entry.id])
851
853
            entry.apply(path, conflict_handler, reverse)
 
854
            temp_name[entry.id] = None
852
855
 
853
856
        else:
854
 
            to_name = temp_dir+"/"+str(i)
 
857
            to_name = os.path.join(temp_dir, str(i))
855
858
            src_path = inventory.get(entry.id)
856
859
            if src_path is not None:
857
860
                src_path = os.path.join(dir, src_path)
867
870
    return temp_name
868
871
 
869
872
 
870
 
def rename_to_new_create(temp_name, target_entries, inventory, changeset, dir,
871
 
                         conflict_handler, reverse):
 
873
def rename_to_new_create(changed_inventory, target_entries, inventory, 
 
874
                         changeset, dir, conflict_handler, reverse):
872
875
    """Rename entries with temp names to their final names, create new files.
873
876
 
874
 
    :param temp_name: A mapping of id to temporary name
875
 
    :type temp_name: Dictionary
 
877
    :param changed_inventory: A mapping of id to temporary name
 
878
    :type changed_inventory: Dictionary
876
879
    :param target_entries: The entries to apply changes to
877
880
    :type target_entries: List of `ChangesetEntry`
878
881
    :param changeset: The changeset to apply
883
886
    :type reverse: bool
884
887
    """
885
888
    for entry in target_entries:
886
 
        new_path = entry.get_new_path(inventory, changeset, reverse)
887
 
        if new_path is None:
 
889
        new_tree_path = entry.get_new_path(inventory, changeset, reverse)
 
890
        if new_tree_path is None:
888
891
            continue
889
 
        new_path = os.path.join(dir, new_path)
890
 
        old_path = temp_name.get(entry.id)
 
892
        new_path = os.path.join(dir, new_tree_path)
 
893
        old_path = changed_inventory.get(entry.id)
891
894
        if os.path.exists(new_path):
892
895
            if conflict_handler.target_exists(entry, new_path, old_path) == \
893
896
                "skip":
894
897
                continue
895
898
        if entry.is_creation(reverse):
896
899
            entry.apply(new_path, conflict_handler, reverse)
 
900
            changed_inventory[entry.id] = new_tree_path
897
901
        else:
898
902
            if old_path is None:
899
903
                continue
900
904
            try:
901
905
                os.rename(old_path, new_path)
 
906
                changed_inventory[entry.id] = new_tree_path
902
907
            except OSError, e:
903
908
                raise Exception ("%s is missing" % new_path)
904
909
 
1006
1011
        Exception.__init__(self, msg)
1007
1012
        self.filename = filename
1008
1013
 
 
1014
class NewContentsConflict(Exception):
 
1015
    def __init__(self, filename):
 
1016
        msg = "Conflicting contents for new file %s" % (filename)
 
1017
        Exception.__init__(self, msg)
 
1018
 
 
1019
 
 
1020
class MissingForMerge(Exception):
 
1021
    def __init__(self, filename):
 
1022
        msg = "The file %s was modified, but does not exist in this tree"\
 
1023
            % (filename)
 
1024
        Exception.__init__(self, msg)
 
1025
 
 
1026
 
1009
1027
class ExceptionConflictHandler(object):
1010
1028
    def __init__(self, dir):
1011
1029
        self.dir = dir
1066
1084
    def missing_for_rename(self, filename):
1067
1085
        raise MissingForRename(filename)
1068
1086
 
 
1087
    def missing_for_merge(self, file_id, inventory):
 
1088
        raise MissingForMerge(inventory.other.get_path(file_id))
 
1089
 
 
1090
    def new_contents_conflict(self, filename, other_contents):
 
1091
        raise NewContentsConflict(filename)
 
1092
 
1069
1093
    def finalize():
1070
1094
        pass
1071
1095
 
1086
1110
    """
1087
1111
    if conflict_handler is None:
1088
1112
        conflict_handler = ExceptionConflictHandler(dir)
1089
 
    temp_dir = dir+"/temp"
1090
 
    os.mkdir(temp_dir)
 
1113
    temp_dir = os.path.join(dir, "bzr-tree-change")
 
1114
    try:
 
1115
        os.mkdir(temp_dir)
 
1116
    except OSError, e:
 
1117
        if e.errno == errno.EEXIST:
 
1118
            try:
 
1119
                os.rmdir(temp_dir)
 
1120
            except OSError, e:
 
1121
                if e.errno == errno.ENOTEMPTY:
 
1122
                    raise OldFailedTreeOp()
 
1123
            os.mkdir(temp_dir)
 
1124
        else:
 
1125
            raise
1091
1126
    
1092
1127
    #apply changes that don't affect filenames
1093
1128
    for entry in changeset.entries.itervalues():
1102
1137
    (source_entries, target_entries) = get_rename_entries(changeset, inventory,
1103
1138
                                                          reverse)
1104
1139
 
1105
 
    temp_name = rename_to_temp_delete(source_entries, inventory, dir,
1106
 
                                      conflict_handler, reverse)
 
1140
    changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
 
1141
                                              temp_dir, conflict_handler,
 
1142
                                              reverse)
1107
1143
 
1108
 
    rename_to_new_create(temp_name, target_entries, inventory, changeset, dir,
1109
 
                         conflict_handler, reverse)
 
1144
    rename_to_new_create(changed_inventory, target_entries, inventory,
 
1145
                         changeset, dir, conflict_handler, reverse)
1110
1146
    os.rmdir(temp_dir)
1111
 
    r_inventory = invert_dict(inventory)
1112
 
    new_entries, removed_entries = get_inventory_change(inventory,
1113
 
    r_inventory, changeset, reverse)
1114
 
    new_inventory = {}
1115
 
    for path, file_id in new_entries.iteritems():
1116
 
        new_inventory[file_id] = path
1117
 
    for file_id in removed_entries:
1118
 
        new_inventory[file_id] = None
1119
 
    return new_inventory
 
1147
    return changed_inventory
1120
1148
 
1121
1149
 
1122
1150
def apply_changeset_tree(cset, tree, reverse=False):
1133
1161
def get_inventory_change(inventory, new_inventory, cset, reverse=False):
1134
1162
    new_entries = {}
1135
1163
    remove_entries = []
1136
 
    r_inventory = invert_dict(inventory)
1137
 
    r_new_inventory = invert_dict(new_inventory)
1138
1164
    for entry in cset.entries.itervalues():
1139
1165
        if entry.needs_rename():
1140
 
            old_path = r_inventory.get(entry.id)
1141
 
            if old_path is not None:
1142
 
                remove_entries.append(old_path)
 
1166
            new_path = entry.get_new_path(inventory, cset)
 
1167
            if new_path is None:
 
1168
                remove_entries.append(entry.id)
1143
1169
            else:
1144
 
                new_path = entry.get_new_path(inventory, cset)
1145
 
                if new_path is not None:
1146
 
                    new_entries[new_path] = entry.id
 
1170
                new_entries[new_path] = entry.id
1147
1171
    return new_entries, remove_entries
1148
1172
 
1149
1173
 
1517
1541
        return self.inventory.get(id)
1518
1542
 
1519
1543
    def get_name(self, id):
1520
 
        return os.path.basename(self.get_path(id))
 
1544
        path = self.get_path(id)
 
1545
        if path is None:
 
1546
            return None
 
1547
        else:
 
1548
            return os.path.basename(path)
1521
1549
 
1522
1550
    def get_dir(self, id):
1523
1551
        path = self.get_path(id)
1524
1552
        if path == "":
1525
1553
            return None
 
1554
        if path is None:
 
1555
            return None
1526
1556
        return os.path.dirname(path)
1527
1557
 
1528
1558
    def get_parent(self, id):
 
1559
        if self.get_path(id) is None:
 
1560
            return None
1529
1561
        directory = self.get_dir(id)
1530
1562
        if directory == '.':
1531
1563
            directory = './.'