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)
759
719
if orig_path == mod_path:
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):
722
return "%s => %s" % (orig_path, mod_path)
724
def get_new_path(self, id_map, changeset):
769
725
"""Determine the full pathname to rename to
771
727
:param id_map: The map of ids to filenames for the tree
772
728
:type id_map: Dictionary
773
729
:param changeset: The changeset to get data from
774
730
:type changeset: `Changeset`
775
:param reverse: If true, we're applying the changeset in reverse
779
733
mutter("Finding new path for %s", self.summarize_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
734
parent = self.new_parent
735
to_dir = self.new_dir
737
to_name = self.new_name
738
from_name = self.name
793
740
if to_name is None:
831
def apply(self, filename, conflict_handler, reverse=False):
778
def apply(self, filename, conflict_handler):
832
779
"""Applies the file content and/or metadata changes.
834
781
:param filename: the filename of the entry
835
782
:type filename: str
836
:param reverse: If true, apply the changes in reverse
839
if self.is_deletion(reverse) and self.metadata_change is not None:
840
self.metadata_change.apply(filename, conflict_handler, reverse)
784
if self.is_deletion() and self.metadata_change is not None:
785
self.metadata_change.apply(filename, conflict_handler)
841
786
if self.contents_change is not None:
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)
787
self.contents_change.apply(filename, conflict_handler)
788
if not self.is_deletion() and self.metadata_change is not None:
789
self.metadata_change.apply(filename, conflict_handler)
846
792
class IDPresent(Exception):
847
793
def __init__(self, id):
861
808
raise IDPresent(entry.id)
862
809
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)
880
def get_rename_entries(changeset, inventory, reverse):
812
def get_rename_entries(changeset, inventory):
881
813
"""Return a list of entries that will be renamed. Entries are sorted from
882
814
longest to shortest source path and from shortest to longest target path.
903
my_sort(source_entries, longest_to_shortest, reverse=True)
833
source_entries.sort(None, longest_to_shortest, True)
905
835
target_entries = source_entries[:]
906
836
# These are done from shortest to longest path, to avoid creating a
907
837
# child before its parent has been created/renamed
908
838
def shortest_to_longest(entry):
909
path = entry.get_new_path(inventory, changeset, reverse)
839
path = entry.get_new_path(inventory, changeset)
914
my_sort(target_entries, shortest_to_longest)
844
target_entries.sort(None, shortest_to_longest)
915
845
return (source_entries, target_entries)
917
848
def rename_to_temp_delete(source_entries, inventory, dir, temp_dir,
918
conflict_handler, reverse):
919
850
"""Delete and rename entries as appropriate. Entries are renamed to temp
920
851
names. A map of id -> temp name (or None, for deletions) is returned.
925
856
:type inventory: Dictionary
926
857
:param dir: The directory to apply changes to
928
:param reverse: Apply changes in reverse
930
859
:return: a mapping of id to temporary name
931
860
:rtype: Dictionary
934
863
for i in range(len(source_entries)):
935
864
entry = source_entries[i]
936
if entry.is_deletion(reverse):
937
path = os.path.join(dir, inventory[entry.id])
938
entry.apply(path, conflict_handler, reverse)
865
if entry.is_deletion():
866
path = pathjoin(dir, inventory[entry.id])
867
entry.apply(path, conflict_handler)
939
868
temp_name[entry.id] = None
941
870
elif entry.needs_rename():
942
if entry.is_creation(reverse):
871
if entry.is_creation():
944
to_name = os.path.join(temp_dir, str(i))
873
to_name = pathjoin(temp_dir, str(i))
945
874
src_path = inventory.get(entry.id)
946
875
if src_path is not None:
947
src_path = os.path.join(dir, src_path)
876
src_path = pathjoin(dir, src_path)
949
878
rename(src_path, to_name)
950
879
temp_name[entry.id] = to_name
970
899
:type changeset: `Changeset`
971
900
:param dir: The directory to apply changes to
973
:param reverse: If true, apply changes in reverse
976
903
for entry in target_entries:
977
new_tree_path = entry.get_new_path(inventory, changeset, reverse)
904
new_tree_path = entry.get_new_path(inventory, changeset)
978
905
if new_tree_path is None:
980
new_path = os.path.join(dir, new_tree_path)
907
new_path = pathjoin(dir, new_tree_path)
981
908
old_path = changed_inventory.get(entry.id)
982
909
if bzrlib.osutils.lexists(new_path):
983
910
if conflict_handler.target_exists(entry, new_path, old_path) == \
986
if entry.is_creation(reverse):
987
entry.apply(new_path, conflict_handler, reverse)
913
if entry.is_creation():
914
entry.apply(new_path, conflict_handler)
988
915
changed_inventory[entry.id] = new_tree_path
989
916
elif entry.needs_rename():
990
if entry.is_deletion(reverse):
917
if entry.is_deletion():
992
919
if old_path is None:
1222
1160
warning("entry {%s} no longer present, can't be updated",
1225
path = os.path.join(dir, inventory[entry.id])
1226
entry.apply(path, conflict_handler, reverse)
1163
path = pathjoin(dir, inventory[entry.id])
1164
entry.apply(path, conflict_handler)
1228
1166
# Apply renames in stages, to minimize conflicts:
1229
1167
# Only files whose name or parent change are interesting, because their
1230
1168
# target name may exist in the source tree. If a directory's name changes,
1231
1169
# that doesn't make its children interesting.
1232
(source_entries, target_entries) = get_rename_entries(changeset, inventory,
1170
(source_entries, target_entries) = get_rename_entries(changeset, inventory)
1235
1172
changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
1236
temp_dir, conflict_handler,
1173
temp_dir, conflict_handler)
1239
1175
rename_to_new_create(changed_inventory, target_entries, inventory,
1240
changeset, dir, conflict_handler, reverse)
1176
changeset, dir, conflict_handler)
1241
1177
os.rmdir(temp_dir)
1242
1178
return changed_inventory
1245
def apply_changeset_tree(cset, tree, reverse=False):
1181
def apply_changeset_tree(cset, tree):
1246
1182
r_inventory = {}
1247
1183
for entry in tree.source_inventory().itervalues():
1248
1184
inventory[entry.id] = entry.path
1249
new_inventory = apply_changeset(cset, r_inventory, tree.basedir,
1185
new_inventory = apply_changeset(cset, r_inventory, tree.basedir)
1251
1186
new_entries, remove_entries = \
1252
get_inventory_change(inventory, new_inventory, cset, reverse)
1187
get_inventory_change(inventory, new_inventory, cset)
1253
1188
tree.update_source_inventory(new_entries, remove_entries)
1256
def get_inventory_change(inventory, new_inventory, cset, reverse=False):
1191
def get_inventory_change(inventory, new_inventory, cset):
1257
1192
new_entries = {}
1258
1193
remove_entries = []
1259
1194
for entry in cset.entries.itervalues():
1279
1214
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():
1403
1217
class UnsupportedFiletype(Exception):
1404
1218
def __init__(self, kind, full_path):