28
class OldFailedTreeOp(Exception):
30
Exception.__init__(self, "bzr-tree-change contains files from a"
31
" previous failed merge operation.")
32
28
def invert_dict(dict):
34
30
for (key,value) in dict.iteritems():
719
714
if from_dir == to_dir:
720
715
dir = os.path.dirname(id_map[self.id])
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:
832
826
my_sort(target_entries, shortest_to_longest)
833
827
return (source_entries, target_entries)
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,
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.
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
845
temp_dir = os.path.join(dir, "temp")
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
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)
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.
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
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)
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) == \
901
895
if entry.is_creation(reverse):
902
896
entry.apply(new_path, conflict_handler, reverse)
903
changed_inventory[entry.id] = new_tree_path
905
898
if old_path is None:
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)
1014
1006
Exception.__init__(self, msg)
1015
1007
self.filename = filename
1017
class NewContentsConflict(Exception):
1018
def __init__(self, filename):
1019
msg = "Conflicting contents for new file %s" % (filename)
1020
Exception.__init__(self, msg)
1023
class MissingForMerge(Exception):
1024
def __init__(self, filename):
1025
msg = "The file %s was modified, but does not exist in this tree"\
1027
Exception.__init__(self, msg)
1030
1009
class ExceptionConflictHandler(object):
1031
1010
def __init__(self, dir):
1087
1066
def missing_for_rename(self, filename):
1088
1067
raise MissingForRename(filename)
1090
def missing_for_merge(self, file_id, inventory):
1091
raise MissingForMerge(inventory.other.get_path(file_id))
1093
def new_contents_conflict(self, filename, other_contents):
1094
raise NewContentsConflict(filename)
1096
1069
def finalize():
1114
1087
if conflict_handler is None:
1115
1088
conflict_handler = ExceptionConflictHandler(dir)
1116
temp_dir = os.path.join(dir, "bzr-tree-change")
1120
if e.errno == errno.EEXIST:
1124
if e.errno == errno.ENOTEMPTY:
1125
raise OldFailedTreeOp()
1089
temp_dir = dir+"/temp"
1130
1092
#apply changes that don't affect filenames
1131
1093
for entry in changeset.entries.itervalues():
1140
1102
(source_entries, target_entries) = get_rename_entries(changeset, inventory,
1143
changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
1144
temp_dir, conflict_handler,
1105
temp_name = rename_to_temp_delete(source_entries, inventory, dir,
1106
conflict_handler, reverse)
1147
rename_to_new_create(changed_inventory, target_entries, inventory,
1148
changeset, dir, conflict_handler, reverse)
1108
rename_to_new_create(temp_name, target_entries, inventory, changeset, dir,
1109
conflict_handler, reverse)
1149
1110
os.rmdir(temp_dir)
1150
return changed_inventory
1111
r_inventory = invert_dict(inventory)
1112
new_entries, removed_entries = get_inventory_change(inventory,
1113
r_inventory, changeset, reverse)
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
1153
1122
def apply_changeset_tree(cset, tree, reverse=False):
1164
1133
def get_inventory_change(inventory, new_inventory, cset, reverse=False):
1165
1134
new_entries = {}
1166
1135
remove_entries = []
1136
r_inventory = invert_dict(inventory)
1137
r_new_inventory = invert_dict(new_inventory)
1167
1138
for entry in cset.entries.itervalues():
1168
1139
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)
1140
old_path = r_inventory.get(entry.id)
1141
if old_path is not None:
1142
remove_entries.append(old_path)
1173
new_entries[new_path] = entry.id
1144
new_path = entry.get_new_path(inventory, cset)
1145
if new_path is not None:
1146
new_entries[new_path] = entry.id
1174
1147
return new_entries, remove_entries
1544
1517
return self.inventory.get(id)
1546
1519
def get_name(self, id):
1547
path = self.get_path(id)
1551
return os.path.basename(path)
1520
return os.path.basename(self.get_path(id))
1553
1522
def get_dir(self, id):
1554
1523
path = self.get_path(id)
1559
1526
return os.path.dirname(path)
1561
1528
def get_parent(self, id):
1562
if self.get_path(id) is None:
1564
1529
directory = self.get_dir(id)
1565
1530
if directory == '.':
1566
1531
directory = './.'