392
434
return not (self == other)
394
436
def dump_file(self, temp_dir, name, tree):
395
out_path = pathjoin(temp_dir, name)
437
out_path = os.path.join(temp_dir, name)
396
438
out_file = file(out_path, "wb")
397
439
in_file = tree.get_file(self.file_id)
398
440
for line in in_file:
399
441
out_file.write(line)
402
def apply(self, filename, conflict_handler):
444
def apply(self, filename, conflict_handler, reverse=False):
403
445
import bzrlib.patch
404
temp_dir = mkdtemp(prefix="bzr-", dir=os.path.dirname(filename))
446
temp_dir = mkdtemp(prefix="bzr-")
406
new_file = os.path.join(temp_dir, filename)
448
new_file = filename+".new"
407
449
base_file = self.dump_file(temp_dir, "base", self.base)
408
450
other_file = self.dump_file(temp_dir, "other", self.other)
411
457
status = bzrlib.patch.diff3(new_file, filename, base, other)
413
459
os.chmod(new_file, os.stat(filename).st_mode)
718
759
if orig_path == mod_path:
721
return "%s => %s" % (orig_path, mod_path)
723
def get_new_path(self, id_map, changeset):
763
return "%s => %s" % (orig_path, mod_path)
765
return "%s => %s" % (mod_path, orig_path)
768
def get_new_path(self, id_map, changeset, reverse=False):
724
769
"""Determine the full pathname to rename to
726
771
:param id_map: The map of ids to filenames for the tree
727
772
:type id_map: Dictionary
728
773
:param changeset: The changeset to get data from
729
774
:type changeset: `Changeset`
775
:param reverse: If true, we're applying the changeset in reverse
732
779
mutter("Finding new path for %s", self.summarize_name())
733
parent = self.new_parent
734
to_dir = self.new_dir
736
to_name = self.new_name
737
from_name = self.name
783
from_dir = self.new_dir
785
from_name = self.new_name
787
parent = self.new_parent
788
to_dir = self.new_dir
790
to_name = self.new_name
791
from_name = self.name
739
793
if to_name is None:
777
def apply(self, filename, conflict_handler):
831
def apply(self, filename, conflict_handler, reverse=False):
778
832
"""Applies the file content and/or metadata changes.
780
834
:param filename: the filename of the entry
781
835
:type filename: str
836
:param reverse: If true, apply the changes in reverse
783
if self.is_deletion() and self.metadata_change is not None:
784
self.metadata_change.apply(filename, conflict_handler)
839
if self.is_deletion(reverse) and self.metadata_change is not None:
840
self.metadata_change.apply(filename, conflict_handler, reverse)
785
841
if self.contents_change is not None:
786
self.contents_change.apply(filename, conflict_handler)
787
if not self.is_deletion() and self.metadata_change is not None:
788
self.metadata_change.apply(filename, conflict_handler)
842
self.contents_change.apply(filename, conflict_handler, reverse)
843
if not self.is_deletion(reverse) and self.metadata_change is not None:
844
self.metadata_change.apply(filename, conflict_handler, reverse)
791
846
class IDPresent(Exception):
792
847
def __init__(self, id):
807
861
raise IDPresent(entry.id)
808
862
self.entries[entry.id] = entry
864
def my_sort(sequence, key, reverse=False):
865
"""A sort function that supports supplying a key for comparison
867
:param sequence: The sequence to sort
868
:param key: A callable object that returns the values to be compared
869
:param reverse: If true, sort in reverse order
872
def cmp_by_key(entry_a, entry_b):
877
return cmp(key(entry_a), key(entry_b))
878
sequence.sort(cmp_by_key)
811
def get_rename_entries(changeset, inventory):
880
def get_rename_entries(changeset, inventory, reverse):
812
881
"""Return a list of entries that will be renamed. Entries are sorted from
813
882
longest to shortest source path and from shortest to longest target path.
832
source_entries.sort(None, longest_to_shortest, True)
903
my_sort(source_entries, longest_to_shortest, reverse=True)
834
905
target_entries = source_entries[:]
835
906
# These are done from shortest to longest path, to avoid creating a
836
907
# child before its parent has been created/renamed
837
908
def shortest_to_longest(entry):
838
path = entry.get_new_path(inventory, changeset)
909
path = entry.get_new_path(inventory, changeset, reverse)
843
target_entries.sort(None, shortest_to_longest)
914
my_sort(target_entries, shortest_to_longest)
844
915
return (source_entries, target_entries)
847
917
def rename_to_temp_delete(source_entries, inventory, dir, temp_dir,
918
conflict_handler, reverse):
849
919
"""Delete and rename entries as appropriate. Entries are renamed to temp
850
920
names. A map of id -> temp name (or None, for deletions) is returned.
855
925
:type inventory: Dictionary
856
926
:param dir: The directory to apply changes to
928
:param reverse: Apply changes in reverse
858
930
:return: a mapping of id to temporary name
859
931
:rtype: Dictionary
862
934
for i in range(len(source_entries)):
863
935
entry = source_entries[i]
864
if entry.is_deletion():
865
path = pathjoin(dir, inventory[entry.id])
866
entry.apply(path, conflict_handler)
936
if entry.is_deletion(reverse):
937
path = os.path.join(dir, inventory[entry.id])
938
entry.apply(path, conflict_handler, reverse)
867
939
temp_name[entry.id] = None
869
941
elif entry.needs_rename():
870
if entry.is_creation():
942
if entry.is_creation(reverse):
872
to_name = pathjoin(temp_dir, str(i))
944
to_name = os.path.join(temp_dir, str(i))
873
945
src_path = inventory.get(entry.id)
874
946
if src_path is not None:
875
src_path = pathjoin(dir, src_path)
947
src_path = os.path.join(dir, src_path)
877
949
rename(src_path, to_name)
878
950
temp_name[entry.id] = to_name
898
970
:type changeset: `Changeset`
899
971
:param dir: The directory to apply changes to
973
:param reverse: If true, apply changes in reverse
902
976
for entry in target_entries:
903
new_tree_path = entry.get_new_path(inventory, changeset)
977
new_tree_path = entry.get_new_path(inventory, changeset, reverse)
904
978
if new_tree_path is None:
906
new_path = pathjoin(dir, new_tree_path)
980
new_path = os.path.join(dir, new_tree_path)
907
981
old_path = changed_inventory.get(entry.id)
908
982
if bzrlib.osutils.lexists(new_path):
909
983
if conflict_handler.target_exists(entry, new_path, old_path) == \
912
if entry.is_creation():
913
entry.apply(new_path, conflict_handler)
986
if entry.is_creation(reverse):
987
entry.apply(new_path, conflict_handler, reverse)
914
988
changed_inventory[entry.id] = new_tree_path
915
989
elif entry.needs_rename():
916
if entry.is_deletion():
990
if entry.is_deletion(reverse):
918
992
if old_path is None:
1159
1222
warning("entry {%s} no longer present, can't be updated",
1162
path = pathjoin(dir, inventory[entry.id])
1163
entry.apply(path, conflict_handler)
1225
path = os.path.join(dir, inventory[entry.id])
1226
entry.apply(path, conflict_handler, reverse)
1165
1228
# Apply renames in stages, to minimize conflicts:
1166
1229
# Only files whose name or parent change are interesting, because their
1167
1230
# target name may exist in the source tree. If a directory's name changes,
1168
1231
# that doesn't make its children interesting.
1169
(source_entries, target_entries) = get_rename_entries(changeset, inventory)
1232
(source_entries, target_entries) = get_rename_entries(changeset, inventory,
1171
1235
changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
1172
temp_dir, conflict_handler)
1236
temp_dir, conflict_handler,
1174
1239
rename_to_new_create(changed_inventory, target_entries, inventory,
1175
changeset, dir, conflict_handler)
1240
changeset, dir, conflict_handler, reverse)
1176
1241
os.rmdir(temp_dir)
1177
1242
return changed_inventory
1245
def apply_changeset_tree(cset, tree, reverse=False):
1247
for entry in tree.source_inventory().itervalues():
1248
inventory[entry.id] = entry.path
1249
new_inventory = apply_changeset(cset, r_inventory, tree.basedir,
1251
new_entries, remove_entries = \
1252
get_inventory_change(inventory, new_inventory, cset, reverse)
1253
tree.update_source_inventory(new_entries, remove_entries)
1256
def get_inventory_change(inventory, new_inventory, cset, reverse=False):
1259
for entry in cset.entries.itervalues():
1260
if entry.needs_rename():
1261
new_path = entry.get_new_path(inventory, cset)
1262
if new_path is None:
1263
remove_entries.append(entry.id)
1265
new_entries[new_path] = entry.id
1266
return new_entries, remove_entries
1180
1269
def print_changeset(cset):
1181
1270
"""Print all non-boring changeset entries
1190
1279
print entry.summarize_name(cset)
1281
class CompositionFailure(Exception):
1282
def __init__(self, old_entry, new_entry, problem):
1283
msg = "Unable to conpose entries.\n %s" % problem
1284
Exception.__init__(self, msg)
1286
class IDMismatch(CompositionFailure):
1287
def __init__(self, old_entry, new_entry):
1288
problem = "Attempt to compose entries with different ids: %s and %s" %\
1289
(old_entry.id, new_entry.id)
1290
CompositionFailure.__init__(self, old_entry, new_entry, problem)
1292
def compose_changesets(old_cset, new_cset):
1293
"""Combine two changesets into one. This works well for exact patching.
1294
Otherwise, not so well.
1296
:param old_cset: The first changeset that would be applied
1297
:type old_cset: `Changeset`
1298
:param new_cset: The second changeset that would be applied
1299
:type new_cset: `Changeset`
1300
:return: A changeset that combines the changes in both changesets
1303
composed = Changeset()
1304
for old_entry in old_cset.entries.itervalues():
1305
new_entry = new_cset.entries.get(old_entry.id)
1306
if new_entry is None:
1307
composed.add_entry(old_entry)
1309
composed_entry = compose_entries(old_entry, new_entry)
1310
if composed_entry.parent is not None or\
1311
composed_entry.new_parent is not None:
1312
composed.add_entry(composed_entry)
1313
for new_entry in new_cset.entries.itervalues():
1314
if not old_cset.entries.has_key(new_entry.id):
1315
composed.add_entry(new_entry)
1318
def compose_entries(old_entry, new_entry):
1319
"""Combine two entries into one.
1321
:param old_entry: The first entry that would be applied
1322
:type old_entry: ChangesetEntry
1323
:param old_entry: The second entry that would be applied
1324
:type old_entry: ChangesetEntry
1325
:return: A changeset entry combining both entries
1326
:rtype: `ChangesetEntry`
1328
if old_entry.id != new_entry.id:
1329
raise IDMismatch(old_entry, new_entry)
1330
output = ChangesetEntry(old_entry.id, old_entry.parent, old_entry.path)
1332
if (old_entry.parent != old_entry.new_parent or
1333
new_entry.parent != new_entry.new_parent):
1334
output.new_parent = new_entry.new_parent
1336
if (old_entry.path != old_entry.new_path or
1337
new_entry.path != new_entry.new_path):
1338
output.new_path = new_entry.new_path
1340
output.contents_change = compose_contents(old_entry, new_entry)
1341
output.metadata_change = compose_metadata(old_entry, new_entry)
1344
def compose_contents(old_entry, new_entry):
1345
"""Combine the contents of two changeset entries. Entries are combined
1346
intelligently where possible, but the fallback behavior returns an
1349
:param old_entry: The first entry that would be applied
1350
:type old_entry: `ChangesetEntry`
1351
:param new_entry: The second entry that would be applied
1352
:type new_entry: `ChangesetEntry`
1353
:return: A combined contents change
1354
:rtype: anything supporting the apply(reverse=False) method
1356
old_contents = old_entry.contents_change
1357
new_contents = new_entry.contents_change
1358
if old_entry.contents_change is None:
1359
return new_entry.contents_change
1360
elif new_entry.contents_change is None:
1361
return old_entry.contents_change
1362
elif isinstance(old_contents, ReplaceContents) and \
1363
isinstance(new_contents, ReplaceContents):
1364
if old_contents.old_contents == new_contents.new_contents:
1367
return ReplaceContents(old_contents.old_contents,
1368
new_contents.new_contents)
1369
elif isinstance(old_contents, ApplySequence):
1370
output = ApplySequence(old_contents.changes)
1371
if isinstance(new_contents, ApplySequence):
1372
output.changes.extend(new_contents.changes)
1374
output.changes.append(new_contents)
1376
elif isinstance(new_contents, ApplySequence):
1377
output = ApplySequence((old_contents.changes,))
1378
output.extend(new_contents.changes)
1381
return ApplySequence((old_contents, new_contents))
1383
def compose_metadata(old_entry, new_entry):
1384
old_meta = old_entry.metadata_change
1385
new_meta = new_entry.metadata_change
1386
if old_meta is None:
1388
elif new_meta is None:
1390
elif (isinstance(old_meta, ChangeExecFlag) and
1391
isinstance(new_meta, ChangeExecFlag)):
1392
return ChangeExecFlag(old_meta.old_exec_flag, new_meta.new_exec_flag)
1394
return ApplySequence(old_meta, new_meta)
1397
def changeset_is_null(changeset):
1398
for entry in changeset.entries.itervalues():
1399
if not entry.is_boring():
1193
1403
class UnsupportedFiletype(Exception):
1194
1404
def __init__(self, kind, full_path):