433
392
return not (self == other)
435
394
def dump_file(self, temp_dir, name, tree):
436
out_path = os.path.join(temp_dir, name)
395
out_path = pathjoin(temp_dir, name)
437
396
out_file = file(out_path, "wb")
438
397
in_file = tree.get_file(self.file_id)
439
398
for line in in_file:
440
399
out_file.write(line)
443
def apply(self, filename, conflict_handler, reverse=False):
402
def apply(self, filename, conflict_handler):
444
403
import bzrlib.patch
445
temp_dir = mkdtemp(prefix="bzr-")
404
temp_dir = mkdtemp(prefix="bzr-", dir=os.path.dirname(filename))
447
new_file = filename+".new"
406
new_file = os.path.join(temp_dir, filename)
448
407
base_file = self.dump_file(temp_dir, "base", self.base)
449
408
other_file = self.dump_file(temp_dir, "other", self.other)
456
411
status = bzrlib.patch.diff3(new_file, filename, base, other)
458
413
os.chmod(new_file, os.stat(filename).st_mode)
739
def summarize_name(self, reverse=False):
706
def summarize_name(self):
740
707
"""Produce a one-line summary of the filename. Indicates renames as
741
708
old => new, indicates creation as None => new, indicates deletion as
744
:param changeset: The changeset to get paths from
745
:type changeset: `Changeset`
746
:param reverse: If true, reverse the names in the output
750
713
orig_path = self.get_cset_path(False)
751
714
mod_path = self.get_cset_path(True)
752
if orig_path is not None:
715
if orig_path and orig_path.startswith('./'):
753
716
orig_path = orig_path[2:]
754
if mod_path is not None:
717
if mod_path and mod_path.startswith('./'):
755
718
mod_path = mod_path[2:]
756
719
if orig_path == mod_path:
760
return "%s => %s" % (orig_path, mod_path)
762
return "%s => %s" % (mod_path, orig_path)
765
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):
766
725
"""Determine the full pathname to rename to
768
727
:param id_map: The map of ids to filenames for the tree
769
728
:type id_map: Dictionary
770
729
:param changeset: The changeset to get data from
771
730
:type changeset: `Changeset`
772
:param reverse: If true, we're applying the changeset in reverse
776
733
mutter("Finding new path for %s", self.summarize_name())
780
from_dir = self.new_dir
782
from_name = self.new_name
784
parent = self.new_parent
785
to_dir = self.new_dir
787
to_name = self.new_name
788
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
790
740
if to_name is None:
793
743
if parent == NULL_ID or parent is None:
795
745
raise SourceRootHasName(self, to_name)
798
if from_dir == to_dir:
748
parent_entry = changeset.entries.get(parent)
749
if parent_entry is None:
799
750
dir = os.path.dirname(id_map[self.id])
801
752
mutter("path, new_path: %r %r", self.path, self.new_path)
802
parent_entry = changeset.entries[parent]
803
dir = parent_entry.get_new_path(id_map, changeset, reverse)
753
dir = parent_entry.get_new_path(id_map, changeset)
804
754
if from_name == to_name:
805
755
name = os.path.basename(id_map[self.id])
808
758
assert(from_name is None or from_name == os.path.basename(id_map[self.id]))
809
return os.path.join(dir, name)
759
return pathjoin(dir, name)
811
761
def is_boring(self):
812
762
"""Determines whether the entry does nothing
828
def apply(self, filename, conflict_handler, reverse=False):
778
def apply(self, filename, conflict_handler):
829
779
"""Applies the file content and/or metadata changes.
831
781
:param filename: the filename of the entry
832
782
:type filename: str
833
:param reverse: If true, apply the changes in reverse
836
if self.is_deletion(reverse) and self.metadata_change is not None:
837
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)
838
786
if self.contents_change is not None:
839
self.contents_change.apply(filename, conflict_handler, reverse)
840
if not self.is_deletion(reverse) and self.metadata_change is not None:
841
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)
843
792
class IDPresent(Exception):
844
793
def __init__(self, id):
858
808
raise IDPresent(entry.id)
859
809
self.entries[entry.id] = entry
861
def my_sort(sequence, key, reverse=False):
862
"""A sort function that supports supplying a key for comparison
864
:param sequence: The sequence to sort
865
:param key: A callable object that returns the values to be compared
866
:param reverse: If true, sort in reverse order
869
def cmp_by_key(entry_a, entry_b):
874
return cmp(key(entry_a), key(entry_b))
875
sequence.sort(cmp_by_key)
877
def get_rename_entries(changeset, inventory, reverse):
812
def get_rename_entries(changeset, inventory):
878
813
"""Return a list of entries that will be renamed. Entries are sorted from
879
814
longest to shortest source path and from shortest to longest target path.
900
my_sort(source_entries, longest_to_shortest, reverse=True)
833
source_entries.sort(None, longest_to_shortest, True)
902
835
target_entries = source_entries[:]
903
836
# These are done from shortest to longest path, to avoid creating a
904
837
# child before its parent has been created/renamed
905
838
def shortest_to_longest(entry):
906
path = entry.get_new_path(inventory, changeset, reverse)
839
path = entry.get_new_path(inventory, changeset)
911
my_sort(target_entries, shortest_to_longest)
844
target_entries.sort(None, shortest_to_longest)
912
845
return (source_entries, target_entries)
914
848
def rename_to_temp_delete(source_entries, inventory, dir, temp_dir,
915
conflict_handler, reverse):
916
850
"""Delete and rename entries as appropriate. Entries are renamed to temp
917
851
names. A map of id -> temp name (or None, for deletions) is returned.
922
856
:type inventory: Dictionary
923
857
:param dir: The directory to apply changes to
925
:param reverse: Apply changes in reverse
927
859
:return: a mapping of id to temporary name
928
860
:rtype: Dictionary
931
863
for i in range(len(source_entries)):
932
864
entry = source_entries[i]
933
if entry.is_deletion(reverse):
934
path = os.path.join(dir, inventory[entry.id])
935
entry.apply(path, conflict_handler, reverse)
865
if entry.is_deletion():
866
path = pathjoin(dir, inventory[entry.id])
867
entry.apply(path, conflict_handler)
936
868
temp_name[entry.id] = None
938
870
elif entry.needs_rename():
939
to_name = os.path.join(temp_dir, str(i))
871
if entry.is_creation():
873
to_name = pathjoin(temp_dir, str(i))
940
874
src_path = inventory.get(entry.id)
941
875
if src_path is not None:
942
src_path = os.path.join(dir, src_path)
876
src_path = pathjoin(dir, src_path)
944
878
rename(src_path, to_name)
945
879
temp_name[entry.id] = to_name
965
899
:type changeset: `Changeset`
966
900
:param dir: The directory to apply changes to
968
:param reverse: If true, apply changes in reverse
971
903
for entry in target_entries:
972
new_tree_path = entry.get_new_path(inventory, changeset, reverse)
904
new_tree_path = entry.get_new_path(inventory, changeset)
973
905
if new_tree_path is None:
975
new_path = os.path.join(dir, new_tree_path)
907
new_path = pathjoin(dir, new_tree_path)
976
908
old_path = changed_inventory.get(entry.id)
977
909
if bzrlib.osutils.lexists(new_path):
978
910
if conflict_handler.target_exists(entry, new_path, old_path) == \
981
if entry.is_creation(reverse):
982
entry.apply(new_path, conflict_handler, reverse)
913
if entry.is_creation():
914
entry.apply(new_path, conflict_handler)
983
915
changed_inventory[entry.id] = new_tree_path
984
916
elif entry.needs_rename():
917
if entry.is_deletion():
985
919
if old_path is None:
922
mutter('rename %s to final name %s', old_path, new_path)
988
923
rename(old_path, new_path)
989
924
changed_inventory[entry.id] = new_tree_path
990
925
except OSError, e:
991
raise Exception ("%s is missing" % new_path)
926
raise BzrCheckError('failed to rename %s to %s for changeset entry %s: %s'
927
% (old_path, new_path, entry, e))
993
930
class TargetExists(Exception):
994
931
def __init__(self, entry, target):
1213
1160
warning("entry {%s} no longer present, can't be updated",
1216
path = os.path.join(dir, inventory[entry.id])
1217
entry.apply(path, conflict_handler, reverse)
1163
path = pathjoin(dir, inventory[entry.id])
1164
entry.apply(path, conflict_handler)
1219
1166
# Apply renames in stages, to minimize conflicts:
1220
1167
# Only files whose name or parent change are interesting, because their
1221
1168
# target name may exist in the source tree. If a directory's name changes,
1222
1169
# that doesn't make its children interesting.
1223
(source_entries, target_entries) = get_rename_entries(changeset, inventory,
1170
(source_entries, target_entries) = get_rename_entries(changeset, inventory)
1226
1172
changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
1227
temp_dir, conflict_handler,
1173
temp_dir, conflict_handler)
1230
1175
rename_to_new_create(changed_inventory, target_entries, inventory,
1231
changeset, dir, conflict_handler, reverse)
1176
changeset, dir, conflict_handler)
1232
1177
os.rmdir(temp_dir)
1233
1178
return changed_inventory
1236
def apply_changeset_tree(cset, tree, reverse=False):
1238
for entry in tree.source_inventory().itervalues():
1239
inventory[entry.id] = entry.path
1240
new_inventory = apply_changeset(cset, r_inventory, tree.basedir,
1242
new_entries, remove_entries = \
1243
get_inventory_change(inventory, new_inventory, cset, reverse)
1244
tree.update_source_inventory(new_entries, remove_entries)
1247
def get_inventory_change(inventory, new_inventory, cset, reverse=False):
1250
for entry in cset.entries.itervalues():
1251
if entry.needs_rename():
1252
new_path = entry.get_new_path(inventory, cset)
1253
if new_path is None:
1254
remove_entries.append(entry.id)
1256
new_entries[new_path] = entry.id
1257
return new_entries, remove_entries
1260
1181
def print_changeset(cset):
1261
1182
"""Print all non-boring changeset entries
1270
1191
print entry.summarize_name(cset)
1272
class CompositionFailure(Exception):
1273
def __init__(self, old_entry, new_entry, problem):
1274
msg = "Unable to conpose entries.\n %s" % problem
1275
Exception.__init__(self, msg)
1277
class IDMismatch(CompositionFailure):
1278
def __init__(self, old_entry, new_entry):
1279
problem = "Attempt to compose entries with different ids: %s and %s" %\
1280
(old_entry.id, new_entry.id)
1281
CompositionFailure.__init__(self, old_entry, new_entry, problem)
1283
def compose_changesets(old_cset, new_cset):
1284
"""Combine two changesets into one. This works well for exact patching.
1285
Otherwise, not so well.
1287
:param old_cset: The first changeset that would be applied
1288
:type old_cset: `Changeset`
1289
:param new_cset: The second changeset that would be applied
1290
:type new_cset: `Changeset`
1291
:return: A changeset that combines the changes in both changesets
1294
composed = Changeset()
1295
for old_entry in old_cset.entries.itervalues():
1296
new_entry = new_cset.entries.get(old_entry.id)
1297
if new_entry is None:
1298
composed.add_entry(old_entry)
1300
composed_entry = compose_entries(old_entry, new_entry)
1301
if composed_entry.parent is not None or\
1302
composed_entry.new_parent is not None:
1303
composed.add_entry(composed_entry)
1304
for new_entry in new_cset.entries.itervalues():
1305
if not old_cset.entries.has_key(new_entry.id):
1306
composed.add_entry(new_entry)
1309
def compose_entries(old_entry, new_entry):
1310
"""Combine two entries into one.
1312
:param old_entry: The first entry that would be applied
1313
:type old_entry: ChangesetEntry
1314
:param old_entry: The second entry that would be applied
1315
:type old_entry: ChangesetEntry
1316
:return: A changeset entry combining both entries
1317
:rtype: `ChangesetEntry`
1319
if old_entry.id != new_entry.id:
1320
raise IDMismatch(old_entry, new_entry)
1321
output = ChangesetEntry(old_entry.id, old_entry.parent, old_entry.path)
1323
if (old_entry.parent != old_entry.new_parent or
1324
new_entry.parent != new_entry.new_parent):
1325
output.new_parent = new_entry.new_parent
1327
if (old_entry.path != old_entry.new_path or
1328
new_entry.path != new_entry.new_path):
1329
output.new_path = new_entry.new_path
1331
output.contents_change = compose_contents(old_entry, new_entry)
1332
output.metadata_change = compose_metadata(old_entry, new_entry)
1335
def compose_contents(old_entry, new_entry):
1336
"""Combine the contents of two changeset entries. Entries are combined
1337
intelligently where possible, but the fallback behavior returns an
1340
:param old_entry: The first entry that would be applied
1341
:type old_entry: `ChangesetEntry`
1342
:param new_entry: The second entry that would be applied
1343
:type new_entry: `ChangesetEntry`
1344
:return: A combined contents change
1345
:rtype: anything supporting the apply(reverse=False) method
1347
old_contents = old_entry.contents_change
1348
new_contents = new_entry.contents_change
1349
if old_entry.contents_change is None:
1350
return new_entry.contents_change
1351
elif new_entry.contents_change is None:
1352
return old_entry.contents_change
1353
elif isinstance(old_contents, ReplaceContents) and \
1354
isinstance(new_contents, ReplaceContents):
1355
if old_contents.old_contents == new_contents.new_contents:
1358
return ReplaceContents(old_contents.old_contents,
1359
new_contents.new_contents)
1360
elif isinstance(old_contents, ApplySequence):
1361
output = ApplySequence(old_contents.changes)
1362
if isinstance(new_contents, ApplySequence):
1363
output.changes.extend(new_contents.changes)
1365
output.changes.append(new_contents)
1367
elif isinstance(new_contents, ApplySequence):
1368
output = ApplySequence((old_contents.changes,))
1369
output.extend(new_contents.changes)
1372
return ApplySequence((old_contents, new_contents))
1374
def compose_metadata(old_entry, new_entry):
1375
old_meta = old_entry.metadata_change
1376
new_meta = new_entry.metadata_change
1377
if old_meta is None:
1379
elif new_meta is None:
1381
elif (isinstance(old_meta, ChangeExecFlag) and
1382
isinstance(new_meta, ChangeExecFlag)):
1383
return ChangeExecFlag(old_meta.old_exec_flag, new_meta.new_exec_flag)
1385
return ApplySequence(old_meta, new_meta)
1388
def changeset_is_null(changeset):
1389
for entry in changeset.entries.itervalues():
1390
if not entry.is_boring():
1394
1194
class UnsupportedFiletype(Exception):
1395
1195
def __init__(self, kind, full_path):