44
34
newdict[value] = key
38
class PatchApply(object):
39
"""Patch application as a kind of content change"""
40
def __init__(self, contents):
43
:param contents: The text of the patch to apply
44
:type contents: str"""
45
self.contents = contents
47
def __eq__(self, other):
48
if not isinstance(other, PatchApply):
50
elif self.contents != other.contents:
55
def __ne__(self, other):
56
return not (self == other)
58
def apply(self, filename, conflict_handler, reverse=False):
59
"""Applies the patch to the specified file.
61
:param filename: the file to apply the patch to
63
:param reverse: If true, apply the patch in reverse
66
input_name = filename+".orig"
68
os.rename(filename, input_name)
70
if e.errno != errno.ENOENT:
72
if conflict_handler.patch_target_missing(filename, self.contents)\
75
os.rename(filename, input_name)
78
status = patch.patch(self.contents, input_name, filename,
80
os.chmod(filename, os.stat(input_name).st_mode)
84
conflict_handler.failed_hunks(filename)
48
87
class ChangeUnixPermissions(object):
49
88
"""This is two-way change, suitable for file modification, creation,
328
368
class Diff3Merge(object):
329
def __init__(self, file_id, base, other):
330
self.file_id = file_id
369
def __init__(self, base_file, other_file):
370
self.base_file = base_file
371
self.other_file = other_file
334
373
def __eq__(self, other):
335
374
if not isinstance(other, Diff3Merge):
337
return (self.base == other.base and
338
self.other == other.other and self.file_id == other.file_id)
376
return (self.base_file == other.base_file and
377
self.other_file == other.other_file)
340
379
def __ne__(self, other):
341
380
return not (self == other)
343
382
def apply(self, filename, conflict_handler, reverse=False):
344
new_file = filename+".new"
345
base_file = self.base.readonly_path(self.file_id)
346
other_file = self.other.readonly_path(self.file_id)
383
new_file = filename+".new"
385
base = self.base_file
386
other = self.other_file
388
base = self.other_file
389
other = self.base_file
353
390
status = patch.diff3(new_file, filename, base, other)
355
392
os.chmod(new_file, os.stat(filename).st_mode)
356
rename(new_file, filename)
393
os.rename(new_file, filename)
359
396
assert(status == 1)
360
def get_lines(filename):
361
my_file = file(base, "rb")
362
lines = my_file.readlines()
364
base_lines = get_lines(base)
365
other_lines = get_lines(other)
366
conflict_handler.merge_conflict(new_file, filename, base_lines,
397
conflict_handler.merge_conflict(new_file, filename, base, other)
1019
1044
def rename_conflict(self, id, this_name, base_name, other_name):
1020
1045
raise RenameConflict(id, this_name, base_name, other_name)
1022
def move_conflict(self, id, this_dir, base_dir, other_dir):
1047
def move_conflict(self, id, inventory):
1048
this_dir = inventory.this.get_dir(id)
1049
base_dir = inventory.base.get_dir(id)
1050
other_dir = inventory.other.get_dir(id)
1023
1051
raise MoveConflict(id, this_dir, base_dir, other_dir)
1025
def merge_conflict(self, new_file, this_path, base_lines, other_lines):
1053
def merge_conflict(self, new_file, this_path, base_path, other_path):
1026
1054
os.unlink(new_file)
1027
1055
raise MergeConflict(this_path)
1056
1084
def missing_for_rename(self, filename):
1057
1085
raise MissingForRename(filename)
1059
def missing_for_merge(self, file_id, other_path):
1060
raise MissingForMerge(other_path)
1087
def missing_for_merge(self, file_id, inventory):
1088
raise MissingForMerge(inventory.other.get_path(file_id))
1062
1090
def new_contents_conflict(self, filename, other_contents):
1063
1091
raise NewContentsConflict(filename)
1068
1096
def apply_changeset(changeset, inventory, dir, conflict_handler=None,
1284
1312
self.full_path = full_path
1285
1313
self.stat_result = stat_result
1287
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1288
return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
1315
def generate_changeset(tree_a, tree_b, inventory_a=None, inventory_b=None):
1316
return ChangesetGenerator(tree_a, tree_b, inventory_a, inventory_b)()
1290
1318
class ChangesetGenerator(object):
1291
def __init__(self, tree_a, tree_b, interesting_ids=None):
1319
def __init__(self, tree_a, tree_b, inventory_a=None, inventory_b=None):
1292
1320
object.__init__(self)
1293
1321
self.tree_a = tree_a
1294
1322
self.tree_b = tree_b
1295
self._interesting_ids = interesting_ids
1323
if inventory_a is not None:
1324
self.inventory_a = inventory_a
1326
self.inventory_a = tree_a.inventory()
1327
if inventory_b is not None:
1328
self.inventory_b = inventory_b
1330
self.inventory_b = tree_b.inventory()
1331
self.r_inventory_a = self.reverse_inventory(self.inventory_a)
1332
self.r_inventory_b = self.reverse_inventory(self.inventory_b)
1297
def iter_both_tree_ids(self):
1298
for file_id in self.tree_a:
1300
for file_id in self.tree_b:
1301
if file_id not in self.tree_a:
1334
def reverse_inventory(self, inventory):
1336
for entry in inventory.itervalues():
1337
if entry.id is None:
1339
r_inventory[entry.id] = entry
1304
1342
def __call__(self):
1305
1343
cset = Changeset()
1306
for file_id in self.iter_both_tree_ids():
1307
cs_entry = self.make_entry(file_id)
1344
for entry in self.inventory_a.itervalues():
1345
if entry.id is None:
1347
cs_entry = self.make_entry(entry.id)
1308
1348
if cs_entry is not None and not cs_entry.is_boring():
1309
1349
cset.add_entry(cs_entry)
1351
for entry in self.inventory_b.itervalues():
1352
if entry.id is None:
1354
if not self.r_inventory_a.has_key(entry.id):
1355
cs_entry = self.make_entry(entry.id)
1356
if cs_entry is not None and not cs_entry.is_boring():
1357
cset.add_entry(cs_entry)
1311
1358
for entry in list(cset.entries.itervalues()):
1312
1359
if entry.parent != entry.new_parent:
1313
1360
if not cset.entries.has_key(entry.parent) and\
1321
1368
cset.add_entry(parent_entry)
1324
def iter_inventory(self, tree):
1325
for file_id in tree:
1326
yield self.get_entry(file_id, tree)
1328
def get_entry(self, file_id, tree):
1329
if not tree.has_or_had_id(file_id):
1331
return tree.tree.inventory[file_id]
1333
def get_entry_parent(self, entry):
1336
return entry.parent_id
1338
def get_path(self, file_id, tree):
1339
if not tree.has_or_had_id(file_id):
1341
path = tree.id2path(file_id)
1347
def make_basic_entry(self, file_id, only_interesting):
1348
entry_a = self.get_entry(file_id, self.tree_a)
1349
entry_b = self.get_entry(file_id, self.tree_b)
1371
def get_entry_parent(self, entry, inventory):
1374
if entry.path == "./.":
1376
dirname = os.path.dirname(entry.path)
1379
parent = inventory[dirname]
1382
def get_paths(self, entry, tree):
1385
full_path = tree.readonly_path(entry.id)
1386
if entry.path == ".":
1387
return ("", full_path)
1388
return (entry.path, full_path)
1390
def make_basic_entry(self, id, only_interesting):
1391
entry_a = self.r_inventory_a.get(id)
1392
entry_b = self.r_inventory_b.get(id)
1350
1393
if only_interesting and not self.is_interesting(entry_a, entry_b):
1352
parent = self.get_entry_parent(entry_a)
1353
path = self.get_path(file_id, self.tree_a)
1354
cs_entry = ChangesetEntry(file_id, parent, path)
1355
new_parent = self.get_entry_parent(entry_b)
1357
new_path = self.get_path(file_id, self.tree_b)
1394
return (None, None, None)
1395
parent = self.get_entry_parent(entry_a, self.inventory_a)
1396
(path, full_path_a) = self.get_paths(entry_a, self.tree_a)
1397
cs_entry = ChangesetEntry(id, parent, path)
1398
new_parent = self.get_entry_parent(entry_b, self.inventory_b)
1401
(new_path, full_path_b) = self.get_paths(entry_b, self.tree_b)
1359
1403
cs_entry.new_path = new_path
1360
1404
cs_entry.new_parent = new_parent
1405
return (cs_entry, full_path_a, full_path_b)
1363
1407
def is_interesting(self, entry_a, entry_b):
1364
if self._interesting_ids is None:
1366
1408
if entry_a is not None:
1367
file_id = entry_a.file_id
1368
elif entry_b is not None:
1369
file_id = entry_b.file_id
1372
return file_id in self._interesting_ids
1409
if entry_a.interesting:
1411
if entry_b is not None:
1412
if entry_b.interesting:
1374
1416
def make_boring_entry(self, id):
1375
cs_entry = self.make_basic_entry(id, only_interesting=False)
1417
(cs_entry, full_path_a, full_path_b) = \
1418
self.make_basic_entry(id, only_interesting=False)
1376
1419
if cs_entry.is_creation_or_deletion():
1377
1420
return self.make_entry(id, only_interesting=False)
1382
1425
def make_entry(self, id, only_interesting=True):
1383
cs_entry = self.make_basic_entry(id, only_interesting)
1426
(cs_entry, full_path_a, full_path_b) = \
1427
self.make_basic_entry(id, only_interesting)
1385
1429
if cs_entry is None:
1388
full_path_a = self.tree_a.readonly_path(id)
1389
full_path_b = self.tree_b.readonly_path(id)
1390
1432
stat_a = self.lstat(full_path_a)
1391
1433
stat_b = self.lstat(full_path_b)
1435
cs_entry.new_parent = None
1436
cs_entry.new_path = None
1393
1438
cs_entry.metadata_change = self.make_mode_change(stat_a, stat_b)
1395
if id in self.tree_a and id in self.tree_b:
1396
a_sha1 = self.tree_a.get_file_sha1(id)
1397
b_sha1 = self.tree_b.get_file_sha1(id)
1398
if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1401
1439
cs_entry.contents_change = self.make_contents_change(full_path_a,
1426
1464
if stat_a.st_ino == stat_b.st_ino and \
1427
1465
stat_a.st_dev == stat_b.st_dev:
1467
if file(full_path_a, "rb").read() == \
1468
file(full_path_b, "rb").read():
1471
patch_contents = patch.diff(full_path_a,
1472
file(full_path_b, "rb").read())
1473
if patch_contents is None:
1475
return PatchApply(patch_contents)
1430
1477
a_contents = self.get_contents(stat_a, full_path_a)
1431
1478
b_contents = self.get_contents(stat_b, full_path_b)