434
392
return not (self == other)
436
394
def dump_file(self, temp_dir, name, tree):
437
out_path = os.path.join(temp_dir, name)
395
out_path = pathjoin(temp_dir, name)
438
396
out_file = file(out_path, "wb")
439
397
in_file = tree.get_file(self.file_id)
440
398
for line in in_file:
441
399
out_file.write(line)
444
def apply(self, filename, conflict_handler, reverse=False):
402
def apply(self, filename, conflict_handler):
445
403
import bzrlib.patch
446
404
temp_dir = mkdtemp(prefix="bzr-")
448
406
new_file = filename+".new"
449
407
base_file = self.dump_file(temp_dir, "base", self.base)
450
408
other_file = self.dump_file(temp_dir, "other", self.other)
457
411
status = bzrlib.patch.diff3(new_file, filename, base, other)
459
413
os.chmod(new_file, os.stat(filename).st_mode)
760
720
if orig_path == mod_path:
764
return "%s => %s" % (orig_path, mod_path)
766
return "%s => %s" % (mod_path, orig_path)
769
def get_new_path(self, id_map, changeset, reverse=False):
723
return "%s => %s" % (orig_path, mod_path)
725
def get_new_path(self, id_map, changeset):
770
726
"""Determine the full pathname to rename to
772
728
:param id_map: The map of ids to filenames for the tree
773
729
:type id_map: Dictionary
774
730
:param changeset: The changeset to get data from
775
731
:type changeset: `Changeset`
776
:param reverse: If true, we're applying the changeset in reverse
780
734
mutter("Finding new path for %s", self.summarize_name())
784
from_dir = self.new_dir
786
from_name = self.new_name
788
parent = self.new_parent
789
to_dir = self.new_dir
791
to_name = self.new_name
792
from_name = self.name
735
parent = self.new_parent
736
to_dir = self.new_dir
738
to_name = self.new_name
739
from_name = self.name
794
741
if to_name is None:
832
def apply(self, filename, conflict_handler, reverse=False):
779
def apply(self, filename, conflict_handler):
833
780
"""Applies the file content and/or metadata changes.
835
782
:param filename: the filename of the entry
836
783
:type filename: str
837
:param reverse: If true, apply the changes in reverse
840
if self.is_deletion(reverse) and self.metadata_change is not None:
841
self.metadata_change.apply(filename, conflict_handler, reverse)
785
if self.is_deletion() and self.metadata_change is not None:
786
self.metadata_change.apply(filename, conflict_handler)
842
787
if self.contents_change is not None:
843
self.contents_change.apply(filename, conflict_handler, reverse)
844
if not self.is_deletion(reverse) and self.metadata_change is not None:
845
self.metadata_change.apply(filename, conflict_handler, reverse)
788
self.contents_change.apply(filename, conflict_handler)
789
if not self.is_deletion() and self.metadata_change is not None:
790
self.metadata_change.apply(filename, conflict_handler)
847
793
class IDPresent(Exception):
848
794
def __init__(self, id):
862
809
raise IDPresent(entry.id)
863
810
self.entries[entry.id] = entry
865
def my_sort(sequence, key, reverse=False):
866
"""A sort function that supports supplying a key for comparison
868
:param sequence: The sequence to sort
869
:param key: A callable object that returns the values to be compared
870
:param reverse: If true, sort in reverse order
873
def cmp_by_key(entry_a, entry_b):
878
return cmp(key(entry_a), key(entry_b))
879
sequence.sort(cmp_by_key)
881
def get_rename_entries(changeset, inventory, reverse):
813
def get_rename_entries(changeset, inventory):
882
814
"""Return a list of entries that will be renamed. Entries are sorted from
883
815
longest to shortest source path and from shortest to longest target path.
904
my_sort(source_entries, longest_to_shortest, reverse=True)
834
source_entries.sort(None, longest_to_shortest, True)
906
836
target_entries = source_entries[:]
907
837
# These are done from shortest to longest path, to avoid creating a
908
838
# child before its parent has been created/renamed
909
839
def shortest_to_longest(entry):
910
path = entry.get_new_path(inventory, changeset, reverse)
840
path = entry.get_new_path(inventory, changeset)
915
my_sort(target_entries, shortest_to_longest)
845
target_entries.sort(None, shortest_to_longest)
916
846
return (source_entries, target_entries)
918
849
def rename_to_temp_delete(source_entries, inventory, dir, temp_dir,
919
conflict_handler, reverse):
920
851
"""Delete and rename entries as appropriate. Entries are renamed to temp
921
852
names. A map of id -> temp name (or None, for deletions) is returned.
926
857
:type inventory: Dictionary
927
858
:param dir: The directory to apply changes to
929
:param reverse: Apply changes in reverse
931
860
:return: a mapping of id to temporary name
932
861
:rtype: Dictionary
935
864
for i in range(len(source_entries)):
936
865
entry = source_entries[i]
937
if entry.is_deletion(reverse):
938
path = os.path.join(dir, inventory[entry.id])
939
entry.apply(path, conflict_handler, reverse)
866
if entry.is_deletion():
867
path = pathjoin(dir, inventory[entry.id])
868
entry.apply(path, conflict_handler)
940
869
temp_name[entry.id] = None
942
871
elif entry.needs_rename():
943
if entry.is_creation(reverse):
872
if entry.is_creation():
945
to_name = os.path.join(temp_dir, str(i))
874
to_name = pathjoin(temp_dir, str(i))
946
875
src_path = inventory.get(entry.id)
947
876
if src_path is not None:
948
src_path = os.path.join(dir, src_path)
877
src_path = pathjoin(dir, src_path)
950
879
rename(src_path, to_name)
951
880
temp_name[entry.id] = to_name
971
900
:type changeset: `Changeset`
972
901
:param dir: The directory to apply changes to
974
:param reverse: If true, apply changes in reverse
977
904
for entry in target_entries:
978
new_tree_path = entry.get_new_path(inventory, changeset, reverse)
905
new_tree_path = entry.get_new_path(inventory, changeset)
979
906
if new_tree_path is None:
981
new_path = os.path.join(dir, new_tree_path)
908
new_path = pathjoin(dir, new_tree_path)
982
909
old_path = changed_inventory.get(entry.id)
983
910
if bzrlib.osutils.lexists(new_path):
984
911
if conflict_handler.target_exists(entry, new_path, old_path) == \
987
if entry.is_creation(reverse):
988
entry.apply(new_path, conflict_handler, reverse)
914
if entry.is_creation():
915
entry.apply(new_path, conflict_handler)
989
916
changed_inventory[entry.id] = new_tree_path
990
917
elif entry.needs_rename():
991
if entry.is_deletion(reverse):
918
if entry.is_deletion():
993
920
if old_path is None:
1223
1161
warning("entry {%s} no longer present, can't be updated",
1226
path = os.path.join(dir, inventory[entry.id])
1227
entry.apply(path, conflict_handler, reverse)
1164
path = pathjoin(dir, inventory[entry.id])
1165
entry.apply(path, conflict_handler)
1229
1167
# Apply renames in stages, to minimize conflicts:
1230
1168
# Only files whose name or parent change are interesting, because their
1231
1169
# target name may exist in the source tree. If a directory's name changes,
1232
1170
# that doesn't make its children interesting.
1233
(source_entries, target_entries) = get_rename_entries(changeset, inventory,
1171
(source_entries, target_entries) = get_rename_entries(changeset, inventory)
1236
1173
changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
1237
temp_dir, conflict_handler,
1174
temp_dir, conflict_handler)
1240
1176
rename_to_new_create(changed_inventory, target_entries, inventory,
1241
changeset, dir, conflict_handler, reverse)
1177
changeset, dir, conflict_handler)
1242
1178
os.rmdir(temp_dir)
1243
1179
return changed_inventory
1246
def apply_changeset_tree(cset, tree, reverse=False):
1182
def apply_changeset_tree(cset, tree):
1247
1183
r_inventory = {}
1248
1184
for entry in tree.source_inventory().itervalues():
1249
1185
inventory[entry.id] = entry.path
1250
new_inventory = apply_changeset(cset, r_inventory, tree.basedir,
1186
new_inventory = apply_changeset(cset, r_inventory, tree.basedir)
1252
1187
new_entries, remove_entries = \
1253
get_inventory_change(inventory, new_inventory, cset, reverse)
1188
get_inventory_change(inventory, new_inventory, cset)
1254
1189
tree.update_source_inventory(new_entries, remove_entries)
1257
def get_inventory_change(inventory, new_inventory, cset, reverse=False):
1192
def get_inventory_change(inventory, new_inventory, cset):
1258
1193
new_entries = {}
1259
1194
remove_entries = []
1260
1195
for entry in cset.entries.itervalues():
1280
1215
print entry.summarize_name(cset)
1282
class CompositionFailure(Exception):
1283
def __init__(self, old_entry, new_entry, problem):
1284
msg = "Unable to conpose entries.\n %s" % problem
1285
Exception.__init__(self, msg)
1287
class IDMismatch(CompositionFailure):
1288
def __init__(self, old_entry, new_entry):
1289
problem = "Attempt to compose entries with different ids: %s and %s" %\
1290
(old_entry.id, new_entry.id)
1291
CompositionFailure.__init__(self, old_entry, new_entry, problem)
1293
def compose_changesets(old_cset, new_cset):
1294
"""Combine two changesets into one. This works well for exact patching.
1295
Otherwise, not so well.
1297
:param old_cset: The first changeset that would be applied
1298
:type old_cset: `Changeset`
1299
:param new_cset: The second changeset that would be applied
1300
:type new_cset: `Changeset`
1301
:return: A changeset that combines the changes in both changesets
1304
composed = Changeset()
1305
for old_entry in old_cset.entries.itervalues():
1306
new_entry = new_cset.entries.get(old_entry.id)
1307
if new_entry is None:
1308
composed.add_entry(old_entry)
1310
composed_entry = compose_entries(old_entry, new_entry)
1311
if composed_entry.parent is not None or\
1312
composed_entry.new_parent is not None:
1313
composed.add_entry(composed_entry)
1314
for new_entry in new_cset.entries.itervalues():
1315
if not old_cset.entries.has_key(new_entry.id):
1316
composed.add_entry(new_entry)
1319
def compose_entries(old_entry, new_entry):
1320
"""Combine two entries into one.
1322
:param old_entry: The first entry that would be applied
1323
:type old_entry: ChangesetEntry
1324
:param old_entry: The second entry that would be applied
1325
:type old_entry: ChangesetEntry
1326
:return: A changeset entry combining both entries
1327
:rtype: `ChangesetEntry`
1329
if old_entry.id != new_entry.id:
1330
raise IDMismatch(old_entry, new_entry)
1331
output = ChangesetEntry(old_entry.id, old_entry.parent, old_entry.path)
1333
if (old_entry.parent != old_entry.new_parent or
1334
new_entry.parent != new_entry.new_parent):
1335
output.new_parent = new_entry.new_parent
1337
if (old_entry.path != old_entry.new_path or
1338
new_entry.path != new_entry.new_path):
1339
output.new_path = new_entry.new_path
1341
output.contents_change = compose_contents(old_entry, new_entry)
1342
output.metadata_change = compose_metadata(old_entry, new_entry)
1345
def compose_contents(old_entry, new_entry):
1346
"""Combine the contents of two changeset entries. Entries are combined
1347
intelligently where possible, but the fallback behavior returns an
1350
:param old_entry: The first entry that would be applied
1351
:type old_entry: `ChangesetEntry`
1352
:param new_entry: The second entry that would be applied
1353
:type new_entry: `ChangesetEntry`
1354
:return: A combined contents change
1355
:rtype: anything supporting the apply(reverse=False) method
1357
old_contents = old_entry.contents_change
1358
new_contents = new_entry.contents_change
1359
if old_entry.contents_change is None:
1360
return new_entry.contents_change
1361
elif new_entry.contents_change is None:
1362
return old_entry.contents_change
1363
elif isinstance(old_contents, ReplaceContents) and \
1364
isinstance(new_contents, ReplaceContents):
1365
if old_contents.old_contents == new_contents.new_contents:
1368
return ReplaceContents(old_contents.old_contents,
1369
new_contents.new_contents)
1370
elif isinstance(old_contents, ApplySequence):
1371
output = ApplySequence(old_contents.changes)
1372
if isinstance(new_contents, ApplySequence):
1373
output.changes.extend(new_contents.changes)
1375
output.changes.append(new_contents)
1377
elif isinstance(new_contents, ApplySequence):
1378
output = ApplySequence((old_contents.changes,))
1379
output.extend(new_contents.changes)
1382
return ApplySequence((old_contents, new_contents))
1384
def compose_metadata(old_entry, new_entry):
1385
old_meta = old_entry.metadata_change
1386
new_meta = new_entry.metadata_change
1387
if old_meta is None:
1389
elif new_meta is None:
1391
elif (isinstance(old_meta, ChangeExecFlag) and
1392
isinstance(new_meta, ChangeExecFlag)):
1393
return ChangeExecFlag(old_meta.old_exec_flag, new_meta.new_exec_flag)
1395
return ApplySequence(old_meta, new_meta)
1398
def changeset_is_null(changeset):
1399
for entry in changeset.entries.itervalues():
1400
if not entry.is_boring():
1404
1218
class UnsupportedFiletype(Exception):
1405
1219
def __init__(self, kind, full_path):