239
class TreeFileCreate(object):
240
"""Create or delete a file (for use with ReplaceContents)"""
241
def __init__(self, tree, file_id):
244
:param contents: The contents of the file to write
248
self.file_id = file_id
251
return "TreeFileCreate(%s)" % self.file_id
253
def __eq__(self, other):
254
if not isinstance(other, TreeFileCreate):
256
return self.tree.get_file_sha1(self.file_id) == \
257
other.tree.get_file_sha1(other.file_id)
259
def __ne__(self, other):
260
return not (self == other)
262
def write_file(self, filename):
263
outfile = file(filename, "wb")
264
for line in self.tree.get_file(self.file_id):
267
def same_text(self, filename):
268
in_file = file(filename, "rb")
269
return sha_file(in_file) == self.tree.get_file_sha1(self.file_id)
271
def __call__(self, filename, conflict_handler, reverse):
272
"""Create or delete a file
274
:param filename: The name of the file to create
276
:param reverse: Delete the file instead of creating it
281
self.write_file(filename)
283
if e.errno == errno.ENOENT:
284
if conflict_handler.missing_parent(filename)=="continue":
285
self.write_file(filename)
291
if not self.same_text(filename):
292
direction = conflict_handler.wrong_old_contents(filename,
293
self.tree.get_file(self.file_id).read())
294
if direction != "continue":
298
if e.errno != errno.ENOENT:
300
if conflict_handler.missing_for_rm(filename, undo) == "skip":
305
233
def reversed(sequence):
306
234
max = len(sequence) - 1
307
235
for i in range(len(sequence)):
432
347
def __ne__(self, other):
433
348
return not (self == other)
435
def dump_file(self, temp_dir, name, tree):
436
out_path = os.path.join(temp_dir, name)
437
out_file = file(out_path, "wb")
438
in_file = tree.get_file(self.file_id)
443
350
def apply(self, filename, conflict_handler, reverse=False):
444
temp_dir = mkdtemp(prefix="bzr-")
446
new_file = filename+".new"
447
base_file = self.dump_file(temp_dir, "base", self.base)
448
other_file = self.dump_file(temp_dir, "other", self.other)
455
status = patch.diff3(new_file, filename, base, other)
457
os.chmod(new_file, os.stat(filename).st_mode)
458
rename(new_file, filename)
462
def get_lines(filename):
463
my_file = file(filename, "rb")
464
lines = my_file.readlines()
467
base_lines = get_lines(base)
468
other_lines = get_lines(other)
469
conflict_handler.merge_conflict(new_file, filename, base_lines,
351
new_file = filename+".new"
352
base_file = self.base.readonly_path(self.file_id)
353
other_file = self.other.readonly_path(self.file_id)
360
status = patch.diff3(new_file, filename, base, other)
362
os.chmod(new_file, os.stat(filename).st_mode)
363
rename(new_file, filename)
367
def get_lines(filename):
368
my_file = file(filename, "rb")
369
lines = my_file.readlines()
372
base_lines = get_lines(base)
373
other_lines = get_lines(other)
374
conflict_handler.merge_conflict(new_file, filename, base_lines,
1083
981
msg = "Conflicting contents for new file %s" % (filename)
1084
982
Exception.__init__(self, msg)
1086
class WeaveMergeConflict(Exception):
1087
def __init__(self, filename):
1088
msg = "Conflicting contents for file %s" % (filename)
1089
Exception.__init__(self, msg)
1091
class ThreewayContentsConflict(Exception):
1092
def __init__(self, filename):
1093
msg = "Conflicting contents for file %s" % (filename)
1094
Exception.__init__(self, msg)
1097
985
class MissingForMerge(Exception):
1098
986
def __init__(self, filename):
1155
1043
def missing_for_rm(self, filename, change):
1156
1044
raise MissingForRm(filename)
1158
def missing_for_rename(self, filename, to_path):
1159
raise MissingForRename(filename, to_path)
1046
def missing_for_rename(self, filename):
1047
raise MissingForRename(filename)
1161
1049
def missing_for_merge(self, file_id, other_path):
1162
1050
raise MissingForMerge(other_path)
1164
1052
def new_contents_conflict(self, filename, other_contents):
1165
1053
raise NewContentsConflict(filename)
1167
def weave_merge_conflict(self, filename, weave, other_i, out_file):
1168
raise WeaveMergeConflict(filename)
1170
def threeway_contents_conflict(self, filename, this_contents,
1171
base_contents, other_contents):
1172
raise ThreewayContentsConflict(filename)
1174
1055
def finalize(self):
1232
1113
r_inventory = {}
1233
1114
for entry in tree.source_inventory().itervalues():
1234
1115
inventory[entry.id] = entry.path
1235
new_inventory = apply_changeset(cset, r_inventory, tree.basedir,
1116
new_inventory = apply_changeset(cset, r_inventory, tree.root,
1236
1117
reverse=reverse)
1237
1118
new_entries, remove_entries = \
1238
1119
get_inventory_change(inventory, new_inventory, cset, reverse)
1389
class UnsupportedFiletype(Exception):
1390
def __init__(self, kind, full_path):
1391
msg = "The file \"%s\" is a %s, which is not a supported filetype." \
1270
class UnsuppportedFiletype(Exception):
1271
def __init__(self, full_path, stat_result):
1272
msg = "The file \"%s\" is not a supported filetype." % full_path
1393
1273
Exception.__init__(self, msg)
1394
1274
self.full_path = full_path
1275
self.stat_result = stat_result
1397
1277
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1398
1278
return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
1504
1389
if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1505
1390
return cs_entry
1507
cs_entry.contents_change = self.make_contents_change(id)
1392
cs_entry.contents_change = self.make_contents_change(full_path_a,
1508
1396
return cs_entry
1510
def make_exec_flag_change(self, file_id):
1398
def make_exec_flag_change(self, stat_a, stat_b):
1511
1399
exec_flag_a = exec_flag_b = None
1512
if file_id in self.tree_a and self.tree_a.kind(file_id) == "file":
1513
exec_flag_a = self.tree_a.is_executable(file_id)
1515
if file_id in self.tree_b and self.tree_b.kind(file_id) == "file":
1516
exec_flag_b = self.tree_b.is_executable(file_id)
1400
if stat_a is not None and not stat.S_ISLNK(stat_a.st_mode):
1401
exec_flag_a = bool(stat_a.st_mode & 0111)
1402
if stat_b is not None and not stat.S_ISLNK(stat_b.st_mode):
1403
exec_flag_b = bool(stat_b.st_mode & 0111)
1518
1404
if exec_flag_a == exec_flag_b:
1520
1406
return ChangeExecFlag(exec_flag_a, exec_flag_b)
1522
def make_contents_change(self, file_id):
1523
a_contents = get_contents(self.tree_a, file_id)
1524
b_contents = get_contents(self.tree_b, file_id)
1408
def make_contents_change(self, full_path_a, stat_a, full_path_b, stat_b):
1409
if stat_a is None and stat_b is None:
1411
if None not in (stat_a, stat_b) and stat.S_ISDIR(stat_a.st_mode) and\
1412
stat.S_ISDIR(stat_b.st_mode):
1414
if None not in (stat_a, stat_b) and stat.S_ISREG(stat_a.st_mode) and\
1415
stat.S_ISREG(stat_b.st_mode):
1416
if stat_a.st_ino == stat_b.st_ino and \
1417
stat_a.st_dev == stat_b.st_dev:
1420
a_contents = self.get_contents(stat_a, full_path_a)
1421
b_contents = self.get_contents(stat_b, full_path_b)
1525
1422
if a_contents == b_contents:
1527
1424
return ReplaceContents(a_contents, b_contents)
1426
def get_contents(self, stat_result, full_path):
1427
if stat_result is None:
1429
elif stat.S_ISREG(stat_result.st_mode):
1430
return FileCreate(file(full_path, "rb").read())
1431
elif stat.S_ISDIR(stat_result.st_mode):
1433
elif stat.S_ISLNK(stat_result.st_mode):
1434
return SymlinkCreate(os.readlink(full_path))
1436
raise UnsupportedFiletype(full_path, stat_result)
1530
def get_contents(tree, file_id):
1531
"""Return the appropriate contents to create a copy of file_id from tree"""
1532
if file_id not in tree:
1534
kind = tree.kind(file_id)
1536
return TreeFileCreate(tree, file_id)
1537
elif kind in ("directory", "root_directory"):
1539
elif kind == "symlink":
1540
return SymlinkCreate(tree.get_symlink_target(file_id))
1542
raise UnsupportedFiletype(kind, tree.id2path(file_id))
1438
def lstat(self, full_path):
1440
if full_path is not None:
1442
stat_result = os.lstat(full_path)
1444
if e.errno != errno.ENOENT:
1545
1449
def full_path(entry, tree):
1546
return os.path.join(tree.basedir, entry.path)
1450
return os.path.join(tree.root, entry.path)
1548
1452
def new_delete_entry(entry, tree, inventory, delete):
1549
1453
if entry.path == "":