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)
47
87
class ChangeUnixPermissions(object):
48
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)
1022
1044
def rename_conflict(self, id, this_name, base_name, other_name):
1023
1045
raise RenameConflict(id, this_name, base_name, other_name)
1025
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)
1026
1051
raise MoveConflict(id, this_dir, base_dir, other_dir)
1028
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):
1029
1054
os.unlink(new_file)
1030
1055
raise MergeConflict(this_path)
1059
1084
def missing_for_rename(self, filename):
1060
1085
raise MissingForRename(filename)
1062
def missing_for_merge(self, file_id, other_path):
1063
raise MissingForMerge(other_path)
1087
def missing_for_merge(self, file_id, inventory):
1088
raise MissingForMerge(inventory.other.get_path(file_id))
1065
1090
def new_contents_conflict(self, filename, other_contents):
1066
1091
raise NewContentsConflict(filename)
1287
1312
self.full_path = full_path
1288
1313
self.stat_result = stat_result
1290
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1291
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)()
1293
1318
class ChangesetGenerator(object):
1294
def __init__(self, tree_a, tree_b, interesting_ids=None):
1319
def __init__(self, tree_a, tree_b, inventory_a=None, inventory_b=None):
1295
1320
object.__init__(self)
1296
1321
self.tree_a = tree_a
1297
1322
self.tree_b = tree_b
1298
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)
1300
def iter_both_tree_ids(self):
1301
for file_id in self.tree_a:
1303
for file_id in self.tree_b:
1304
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
1307
1342
def __call__(self):
1308
1343
cset = Changeset()
1309
for file_id in self.iter_both_tree_ids():
1310
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)
1311
1348
if cs_entry is not None and not cs_entry.is_boring():
1312
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)
1314
1358
for entry in list(cset.entries.itervalues()):
1315
1359
if entry.parent != entry.new_parent:
1316
1360
if not cset.entries.has_key(entry.parent) and\
1324
1368
cset.add_entry(parent_entry)
1327
def iter_inventory(self, tree):
1328
for file_id in tree:
1329
yield self.get_entry(file_id, tree)
1331
def get_entry(self, file_id, tree):
1332
if file_id not in tree:
1334
return tree.tree.inventory[file_id]
1336
def get_entry_parent(self, entry):
1339
return entry.parent_id
1341
def get_path(self, file_id, tree):
1342
if not tree.has_id(file_id):
1344
path = tree.id2path(file_id)
1350
def make_basic_entry(self, file_id, only_interesting):
1351
entry_a = self.get_entry(file_id, self.tree_a)
1352
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)
1353
1393
if only_interesting and not self.is_interesting(entry_a, entry_b):
1355
parent = self.get_entry_parent(entry_a)
1356
path = self.get_path(file_id, self.tree_a)
1357
cs_entry = ChangesetEntry(file_id, parent, path)
1358
new_parent = self.get_entry_parent(entry_b)
1360
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)
1362
1403
cs_entry.new_path = new_path
1363
1404
cs_entry.new_parent = new_parent
1405
return (cs_entry, full_path_a, full_path_b)
1366
1407
def is_interesting(self, entry_a, entry_b):
1367
if self._interesting_ids is None:
1369
1408
if entry_a is not None:
1370
file_id = entry_a.file_id
1371
elif entry_b is not None:
1372
file_id = entry_b.file_id
1375
return file_id in self._interesting_ids
1409
if entry_a.interesting:
1411
if entry_b is not None:
1412
if entry_b.interesting:
1377
1416
def make_boring_entry(self, id):
1378
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)
1379
1419
if cs_entry.is_creation_or_deletion():
1380
1420
return self.make_entry(id, only_interesting=False)
1385
1425
def make_entry(self, id, only_interesting=True):
1386
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)
1388
1429
if cs_entry is None:
1390
if id in self.tree_a and id in self.tree_b:
1391
a_sha1 = self.tree_a.get_file_sha1(id)
1392
b_sha1 = self.tree_b.get_file_sha1(id)
1393
if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1396
full_path_a = self.tree_a.readonly_path(id)
1397
full_path_b = self.tree_b.readonly_path(id)
1398
1432
stat_a = self.lstat(full_path_a)
1399
1433
stat_b = self.lstat(full_path_b)
1400
1434
if stat_b is None:
1430
1464
if stat_a.st_ino == stat_b.st_ino and \
1431
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)
1434
1477
a_contents = self.get_contents(stat_a, full_path_a)
1435
1478
b_contents = self.get_contents(stat_b, full_path_b)