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():
35
class PatchApply(object):
36
"""Patch application as a kind of content change"""
37
def __init__(self, contents):
40
:param contents: The text of the patch to apply
41
:type contents: str"""
42
self.contents = contents
44
def __eq__(self, other):
45
if not isinstance(other, PatchApply):
47
elif self.contents != other.contents:
52
def __ne__(self, other):
53
return not (self == other)
55
def apply(self, filename, conflict_handler, reverse=False):
56
"""Applies the patch to the specified file.
58
:param filename: the file to apply the patch to
60
:param reverse: If true, apply the patch in reverse
63
input_name = filename+".orig"
65
os.rename(filename, input_name)
67
if e.errno != errno.ENOENT:
69
if conflict_handler.patch_target_missing(filename, self.contents)\
72
os.rename(filename, input_name)
75
status = patch.patch(self.contents, input_name, filename,
77
os.chmod(filename, os.stat(input_name).st_mode)
81
conflict_handler.failed_hunks(filename)
40
84
class ChangeUnixPermissions(object):
41
85
"""This is two-way change, suitable for file modification, creation,
671
714
if from_dir == to_dir:
672
715
dir = os.path.dirname(id_map[self.id])
674
mutter("path, new_path: %r %r" % (self.path, self.new_path))
675
717
parent_entry = changeset.entries[parent]
676
718
dir = parent_entry.get_new_path(id_map, changeset, reverse)
677
719
if from_name == to_name:
784
826
my_sort(target_entries, shortest_to_longest)
785
827
return (source_entries, target_entries)
787
def rename_to_temp_delete(source_entries, inventory, dir, temp_dir,
788
conflict_handler, reverse):
829
def rename_to_temp_delete(source_entries, inventory, dir, conflict_handler,
789
831
"""Delete and rename entries as appropriate. Entries are renamed to temp
790
names. A map of id -> temp name (or None, for deletions) is returned.
832
names. A map of id -> temp name is returned.
792
834
:param source_entries: The entries to rename and delete
793
835
:type source_entries: List of `ChangesetEntry`
800
842
:return: a mapping of id to temporary name
801
843
:rtype: Dictionary
845
temp_dir = os.path.join(dir, "temp")
804
847
for i in range(len(source_entries)):
805
848
entry = source_entries[i]
806
849
if entry.is_deletion(reverse):
807
850
path = os.path.join(dir, inventory[entry.id])
808
851
entry.apply(path, conflict_handler, reverse)
809
temp_name[entry.id] = None
812
to_name = os.path.join(temp_dir, str(i))
854
to_name = temp_dir+"/"+str(i)
813
855
src_path = inventory.get(entry.id)
814
856
if src_path is not None:
815
857
src_path = os.path.join(dir, src_path)
828
def rename_to_new_create(changed_inventory, target_entries, inventory,
829
changeset, dir, conflict_handler, reverse):
870
def rename_to_new_create(temp_name, target_entries, inventory, changeset, dir,
871
conflict_handler, reverse):
830
872
"""Rename entries with temp names to their final names, create new files.
832
:param changed_inventory: A mapping of id to temporary name
833
:type changed_inventory: Dictionary
874
:param temp_name: A mapping of id to temporary name
875
:type temp_name: Dictionary
834
876
:param target_entries: The entries to apply changes to
835
877
:type target_entries: List of `ChangesetEntry`
836
878
:param changeset: The changeset to apply
841
883
:type reverse: bool
843
885
for entry in target_entries:
844
new_tree_path = entry.get_new_path(inventory, changeset, reverse)
845
if new_tree_path is None:
886
new_path = entry.get_new_path(inventory, changeset, reverse)
847
new_path = os.path.join(dir, new_tree_path)
848
old_path = changed_inventory.get(entry.id)
889
new_path = os.path.join(dir, new_path)
890
old_path = temp_name.get(entry.id)
849
891
if os.path.exists(new_path):
850
892
if conflict_handler.target_exists(entry, new_path, old_path) == \
853
895
if entry.is_creation(reverse):
854
896
entry.apply(new_path, conflict_handler, reverse)
855
changed_inventory[entry.id] = new_tree_path
857
898
if old_path is None:
860
901
os.rename(old_path, new_path)
861
changed_inventory[entry.id] = new_tree_path
862
902
except OSError, e:
863
903
raise Exception ("%s is missing" % new_path)
966
1006
Exception.__init__(self, msg)
967
1007
self.filename = filename
969
class NewContentsConflict(Exception):
970
def __init__(self, filename):
971
msg = "Conflicting contents for new file %s" % (filename)
972
Exception.__init__(self, msg)
975
class MissingForMerge(Exception):
976
def __init__(self, filename):
977
msg = "The file %s was modified, but does not exist in this tree"\
979
Exception.__init__(self, msg)
982
1009
class ExceptionConflictHandler(object):
983
1010
def __init__(self, dir):
1039
1066
def missing_for_rename(self, filename):
1040
1067
raise MissingForRename(filename)
1042
def missing_for_merge(self, file_id, inventory):
1043
raise MissingForMerge(inventory.other.get_path(file_id))
1045
def new_contents_conflict(self, filename, other_contents):
1046
raise NewContentsConflict(filename)
1048
1069
def finalize():
1066
1087
if conflict_handler is None:
1067
1088
conflict_handler = ExceptionConflictHandler(dir)
1068
temp_dir = os.path.join(dir, "bzr-tree-change")
1072
if e.errno == errno.EEXIST:
1076
if e.errno == errno.ENOTEMPTY:
1077
raise OldFailedTreeOp()
1089
temp_dir = dir+"/temp"
1082
1092
#apply changes that don't affect filenames
1083
1093
for entry in changeset.entries.itervalues():
1092
1102
(source_entries, target_entries) = get_rename_entries(changeset, inventory,
1095
changed_inventory = rename_to_temp_delete(source_entries, inventory, dir,
1096
temp_dir, conflict_handler,
1105
temp_name = rename_to_temp_delete(source_entries, inventory, dir,
1106
conflict_handler, reverse)
1099
rename_to_new_create(changed_inventory, target_entries, inventory,
1100
changeset, dir, conflict_handler, reverse)
1108
rename_to_new_create(temp_name, target_entries, inventory, changeset, dir,
1109
conflict_handler, reverse)
1101
1110
os.rmdir(temp_dir)
1102
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
1105
1122
def apply_changeset_tree(cset, tree, reverse=False):
1116
1133
def get_inventory_change(inventory, new_inventory, cset, reverse=False):
1117
1134
new_entries = {}
1118
1135
remove_entries = []
1136
r_inventory = invert_dict(inventory)
1137
r_new_inventory = invert_dict(new_inventory)
1119
1138
for entry in cset.entries.itervalues():
1120
1139
if entry.needs_rename():
1121
new_path = entry.get_new_path(inventory, cset)
1122
if new_path is None:
1123
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)
1125
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
1126
1147
return new_entries, remove_entries
1334
1355
parent = inventory[dirname]
1335
1356
return parent.id
1337
def get_path(self, entry, tree):
1358
def get_paths(self, entry, tree):
1338
1359
if entry is None:
1339
1360
return (None, None)
1361
full_path = tree.readonly_path(entry.id)
1340
1362
if entry.path == ".":
1363
return ("", full_path)
1364
return (entry.path, full_path)
1344
1366
def make_basic_entry(self, id, only_interesting):
1345
1367
entry_a = self.r_inventory_a.get(id)
1346
1368
entry_b = self.r_inventory_b.get(id)
1347
1369
if only_interesting and not self.is_interesting(entry_a, entry_b):
1370
return (None, None, None)
1349
1371
parent = self.get_entry_parent(entry_a, self.inventory_a)
1350
path = self.get_path(entry_a, self.tree_a)
1372
(path, full_path_a) = self.get_paths(entry_a, self.tree_a)
1351
1373
cs_entry = ChangesetEntry(id, parent, path)
1352
1374
new_parent = self.get_entry_parent(entry_b, self.inventory_b)
1355
new_path = self.get_path(entry_b, self.tree_b)
1377
(new_path, full_path_b) = self.get_paths(entry_b, self.tree_b)
1357
1379
cs_entry.new_path = new_path
1358
1380
cs_entry.new_parent = new_parent
1381
return (cs_entry, full_path_a, full_path_b)
1361
1383
def is_interesting(self, entry_a, entry_b):
1362
1384
if entry_a is not None:
1370
1392
def make_boring_entry(self, id):
1371
cs_entry = self.make_basic_entry(id, only_interesting=False)
1393
(cs_entry, full_path_a, full_path_b) = \
1394
self.make_basic_entry(id, only_interesting=False)
1372
1395
if cs_entry.is_creation_or_deletion():
1373
1396
return self.make_entry(id, only_interesting=False)
1378
1401
def make_entry(self, id, only_interesting=True):
1379
cs_entry = self.make_basic_entry(id, only_interesting)
1402
(cs_entry, full_path_a, full_path_b) = \
1403
self.make_basic_entry(id, only_interesting)
1381
1405
if cs_entry is None:
1383
if id in self.tree_a and id in self.tree_b:
1384
a_sha1 = self.tree_a.get_file_sha1(id)
1385
b_sha1 = self.tree_b.get_file_sha1(id)
1386
if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1389
full_path_a = self.tree_a.readonly_path(id)
1390
full_path_b = self.tree_b.readonly_path(id)
1391
1408
stat_a = self.lstat(full_path_a)
1392
1409
stat_b = self.lstat(full_path_b)
1393
1410
if stat_b is None:
1423
1440
if stat_a.st_ino == stat_b.st_ino and \
1424
1441
stat_a.st_dev == stat_b.st_dev:
1443
if file(full_path_a, "rb").read() == \
1444
file(full_path_b, "rb").read():
1447
patch_contents = patch.diff(full_path_a,
1448
file(full_path_b, "rb").read())
1449
if patch_contents is None:
1451
return PatchApply(patch_contents)
1427
1453
a_contents = self.get_contents(stat_a, full_path_a)
1428
1454
b_contents = self.get_contents(stat_b, full_path_b)
1491
1517
return self.inventory.get(id)
1493
1519
def get_name(self, id):
1494
path = self.get_path(id)
1498
return os.path.basename(path)
1520
return os.path.basename(self.get_path(id))
1500
1522
def get_dir(self, id):
1501
1523
path = self.get_path(id)
1506
1526
return os.path.dirname(path)
1508
1528
def get_parent(self, id):
1509
if self.get_path(id) is None:
1511
1529
directory = self.get_dir(id)
1512
1530
if directory == '.':
1513
1531
directory = './.'