46
class PatchApply(object):
47
"""Patch application as a kind of content change"""
48
def __init__(self, contents):
51
:param contents: The text of the patch to apply
52
:type contents: str"""
53
self.contents = contents
55
def __eq__(self, other):
56
if not isinstance(other, PatchApply):
58
elif self.contents != other.contents:
63
def __ne__(self, other):
64
return not (self == other)
66
def apply(self, filename, conflict_handler, reverse=False):
67
"""Applies the patch to the specified file.
69
:param filename: the file to apply the patch to
71
:param reverse: If true, apply the patch in reverse
74
input_name = filename+".orig"
76
os.rename(filename, input_name)
78
if e.errno != errno.ENOENT:
80
if conflict_handler.patch_target_missing(filename, self.contents)\
83
os.rename(filename, input_name)
86
status = patch.patch(self.contents, input_name, filename,
88
os.chmod(filename, os.stat(input_name).st_mode)
92
conflict_handler.failed_hunks(filename)
95
47
class ChangeUnixPermissions(object):
96
48
"""This is two-way change, suitable for file modification, creation,
376
328
class Diff3Merge(object):
377
def __init__(self, base_file, other_file):
378
self.base_file = base_file
379
self.other_file = other_file
329
def __init__(self, file_id, base, other):
330
self.file_id = file_id
381
334
def __eq__(self, other):
382
335
if not isinstance(other, Diff3Merge):
384
return (self.base_file == other.base_file and
385
self.other_file == other.other_file)
337
return (self.base == other.base and
338
self.other == other.other and self.file_id == other.file_id)
387
340
def __ne__(self, other):
388
341
return not (self == other)
390
343
def apply(self, filename, conflict_handler, reverse=False):
391
new_file = filename+".new"
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)
393
base = self.base_file
394
other = self.other_file
396
base = self.other_file
397
other = self.base_file
398
353
status = patch.diff3(new_file, filename, base, other)
400
355
os.chmod(new_file, os.stat(filename).st_mode)
404
359
assert(status == 1)
405
conflict_handler.merge_conflict(new_file, filename, base, other)
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,
1060
1022
def rename_conflict(self, id, this_name, base_name, other_name):
1061
1023
raise RenameConflict(id, this_name, base_name, other_name)
1063
def move_conflict(self, id, inventory):
1064
this_dir = inventory.this.get_dir(id)
1065
base_dir = inventory.base.get_dir(id)
1066
other_dir = inventory.other.get_dir(id)
1025
def move_conflict(self, id, this_dir, base_dir, other_dir):
1067
1026
raise MoveConflict(id, this_dir, base_dir, other_dir)
1069
def merge_conflict(self, new_file, this_path, base_path, other_path):
1028
def merge_conflict(self, new_file, this_path, base_lines, other_lines):
1070
1029
os.unlink(new_file)
1071
1030
raise MergeConflict(this_path)
1100
1059
def missing_for_rename(self, filename):
1101
1060
raise MissingForRename(filename)
1103
def missing_for_merge(self, file_id, inventory):
1104
raise MissingForMerge(inventory.other.get_path(file_id))
1062
def missing_for_merge(self, file_id, other_path):
1063
raise MissingForMerge(other_path)
1106
1065
def new_contents_conflict(self, filename, other_contents):
1107
1066
raise NewContentsConflict(filename)
1328
1287
self.full_path = full_path
1329
1288
self.stat_result = stat_result
1331
def generate_changeset(tree_a, tree_b, inventory_a=None, inventory_b=None):
1332
return ChangesetGenerator(tree_a, tree_b, inventory_a, inventory_b)()
1290
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1291
return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
1334
1293
class ChangesetGenerator(object):
1335
def __init__(self, tree_a, tree_b, inventory_a=None, inventory_b=None):
1294
def __init__(self, tree_a, tree_b, interesting_ids=None):
1336
1295
object.__init__(self)
1337
1296
self.tree_a = tree_a
1338
1297
self.tree_b = tree_b
1339
if inventory_a is not None:
1340
self.inventory_a = inventory_a
1342
self.inventory_a = tree_a.inventory()
1343
if inventory_b is not None:
1344
self.inventory_b = inventory_b
1346
self.inventory_b = tree_b.inventory()
1347
self.r_inventory_a = self.reverse_inventory(self.inventory_a)
1348
self.r_inventory_b = self.reverse_inventory(self.inventory_b)
1298
self._interesting_ids = interesting_ids
1350
def reverse_inventory(self, inventory):
1352
for entry in inventory.itervalues():
1353
if entry.id is None:
1355
r_inventory[entry.id] = entry
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:
1358
1307
def __call__(self):
1359
1308
cset = Changeset()
1360
for entry in self.inventory_a.itervalues():
1361
if entry.id is None:
1363
cs_entry = self.make_entry(entry.id)
1309
for file_id in self.iter_both_tree_ids():
1310
cs_entry = self.make_entry(file_id)
1364
1311
if cs_entry is not None and not cs_entry.is_boring():
1365
1312
cset.add_entry(cs_entry)
1367
for entry in self.inventory_b.itervalues():
1368
if entry.id is None:
1370
if not self.r_inventory_a.has_key(entry.id):
1371
cs_entry = self.make_entry(entry.id)
1372
if cs_entry is not None and not cs_entry.is_boring():
1373
cset.add_entry(cs_entry)
1374
1314
for entry in list(cset.entries.itervalues()):
1375
1315
if entry.parent != entry.new_parent:
1376
1316
if not cset.entries.has_key(entry.parent) and\
1384
1324
cset.add_entry(parent_entry)
1387
def get_entry_parent(self, entry, inventory):
1390
if entry.path == "./.":
1392
dirname = os.path.dirname(entry.path)
1395
parent = inventory[dirname]
1398
def get_path(self, entry, tree):
1401
if entry.path == ".":
1405
def make_basic_entry(self, id, only_interesting):
1406
entry_a = self.r_inventory_a.get(id)
1407
entry_b = self.r_inventory_b.get(id)
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)
1408
1353
if only_interesting and not self.is_interesting(entry_a, entry_b):
1410
parent = self.get_entry_parent(entry_a, self.inventory_a)
1411
path = self.get_path(entry_a, self.tree_a)
1412
cs_entry = ChangesetEntry(id, parent, path)
1413
new_parent = self.get_entry_parent(entry_b, self.inventory_b)
1416
new_path = self.get_path(entry_b, self.tree_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)
1418
1362
cs_entry.new_path = new_path
1419
1363
cs_entry.new_parent = new_parent
1420
1364
return cs_entry
1422
1366
def is_interesting(self, entry_a, entry_b):
1367
if self._interesting_ids is None:
1423
1369
if entry_a is not None:
1424
if entry_a.interesting:
1426
if entry_b is not None:
1427
if entry_b.interesting:
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
1431
1377
def make_boring_entry(self, id):
1432
1378
cs_entry = self.make_basic_entry(id, only_interesting=False)
1484
1430
if stat_a.st_ino == stat_b.st_ino and \
1485
1431
stat_a.st_dev == stat_b.st_dev:
1487
if file(full_path_a, "rb").read() == \
1488
file(full_path_b, "rb").read():
1491
patch_contents = patch.diff(full_path_a,
1492
file(full_path_b, "rb").read())
1493
if patch_contents is None:
1495
return PatchApply(patch_contents)
1497
1434
a_contents = self.get_contents(stat_a, full_path_a)
1498
1435
b_contents = self.get_contents(stat_b, full_path_b)