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":
233
305
def reversed(sequence):
234
306
max = len(sequence) - 1
235
307
for i in range(len(sequence)):
347
431
def __ne__(self, other):
348
432
return not (self == other)
434
def dump_file(self, temp_dir, name, tree):
435
out_path = os.path.join(temp_dir, name)
436
out_file = file(out_path, "wb")
437
in_file = tree.get_file(self.file_id)
350
442
def apply(self, filename, conflict_handler, reverse=False):
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,
443
temp_dir = mkdtemp(prefix="bzr-")
445
new_file = filename+".new"
446
base_file = self.dump_file(temp_dir, "base", self.base)
447
other_file = self.dump_file(temp_dir, "other", self.other)
454
status = patch.diff3(new_file, filename, base, other)
456
os.chmod(new_file, os.stat(filename).st_mode)
457
rename(new_file, filename)
461
def get_lines(filename):
462
my_file = file(filename, "rb")
463
lines = my_file.readlines()
466
base_lines = get_lines(base)
467
other_lines = get_lines(other)
468
conflict_handler.merge_conflict(new_file, filename, base_lines,
1043
1149
def missing_for_rm(self, filename, change):
1044
1150
raise MissingForRm(filename)
1046
def missing_for_rename(self, filename):
1047
raise MissingForRename(filename)
1152
def missing_for_rename(self, filename, to_path):
1153
raise MissingForRename(filename, to_path)
1049
1155
def missing_for_merge(self, file_id, other_path):
1050
1156
raise MissingForMerge(other_path)
1113
1223
r_inventory = {}
1114
1224
for entry in tree.source_inventory().itervalues():
1115
1225
inventory[entry.id] = entry.path
1116
new_inventory = apply_changeset(cset, r_inventory, tree.root,
1226
new_inventory = apply_changeset(cset, r_inventory, tree.basedir,
1117
1227
reverse=reverse)
1118
1228
new_entries, remove_entries = \
1119
1229
get_inventory_change(inventory, new_inventory, cset, reverse)
1270
class UnsuppportedFiletype(Exception):
1271
def __init__(self, full_path, stat_result):
1272
msg = "The file \"%s\" is not a supported filetype." % full_path
1380
class UnsupportedFiletype(Exception):
1381
def __init__(self, kind, full_path):
1382
msg = "The file \"%s\" is a %s, which is not a supported filetype." \
1273
1384
Exception.__init__(self, msg)
1274
1385
self.full_path = full_path
1275
self.stat_result = stat_result
1277
1388
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1278
1389
return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
1389
1495
if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1390
1496
return cs_entry
1392
cs_entry.contents_change = self.make_contents_change(full_path_a,
1498
cs_entry.contents_change = self.make_contents_change(id)
1396
1499
return cs_entry
1398
def make_exec_flag_change(self, stat_a, stat_b):
1501
def make_exec_flag_change(self, file_id):
1399
1502
exec_flag_a = exec_flag_b = None
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)
1503
if file_id in self.tree_a and self.tree_a.kind(file_id) == "file":
1504
exec_flag_a = self.tree_a.is_executable(file_id)
1506
if file_id in self.tree_b and self.tree_b.kind(file_id) == "file":
1507
exec_flag_b = self.tree_b.is_executable(file_id)
1404
1509
if exec_flag_a == exec_flag_b:
1406
1511
return ChangeExecFlag(exec_flag_a, exec_flag_b)
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)
1513
def make_contents_change(self, file_id):
1514
a_contents = get_contents(self.tree_a, file_id)
1515
b_contents = get_contents(self.tree_b, file_id)
1422
1516
if a_contents == b_contents:
1424
1518
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)
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:
1521
def get_contents(tree, file_id):
1522
"""Return the appropriate contents to create a copy of file_id from tree"""
1523
if file_id not in tree:
1525
kind = tree.kind(file_id)
1527
return TreeFileCreate(tree, file_id)
1528
elif kind in ("directory", "root_directory"):
1530
elif kind == "symlink":
1531
return SymlinkCreate(tree.get_symlink_target(file_id))
1533
raise UnsupportedFiletype(kind, tree.id2path(file_id))
1449
1536
def full_path(entry, tree):
1450
return os.path.join(tree.root, entry.path)
1537
return os.path.join(tree.basedir, entry.path)
1452
1539
def new_delete_entry(entry, tree, inventory, delete):
1453
1540
if entry.path == "":