51
class ChangeExecFlag(object):
47
class ChangeUnixPermissions(object):
52
48
"""This is two-way change, suitable for file modification, creation,
54
def __init__(self, old_exec_flag, new_exec_flag):
55
self.old_exec_flag = old_exec_flag
56
self.new_exec_flag = new_exec_flag
50
def __init__(self, old_mode, new_mode):
51
self.old_mode = old_mode
52
self.new_mode = new_mode
58
54
def apply(self, filename, conflict_handler, reverse=False):
60
from_exec_flag = self.old_exec_flag
61
to_exec_flag = self.new_exec_flag
56
from_mode = self.old_mode
57
to_mode = self.new_mode
63
from_exec_flag = self.new_exec_flag
64
to_exec_flag = self.old_exec_flag
59
from_mode = self.new_mode
60
to_mode = self.old_mode
66
current_exec_flag = bool(os.stat(filename).st_mode & 0111)
62
current_mode = os.stat(filename).st_mode &0777
68
64
if e.errno == errno.ENOENT:
69
if conflict_handler.missing_for_exec_flag(filename) == "skip":
65
if conflict_handler.missing_for_chmod(filename) == "skip":
72
current_exec_flag = from_exec_flag
68
current_mode = from_mode
74
if from_exec_flag is not None and current_exec_flag != from_exec_flag:
75
if conflict_handler.wrong_old_exec_flag(filename,
76
from_exec_flag, current_exec_flag) != "continue":
70
if from_mode is not None and current_mode != from_mode:
71
if conflict_handler.wrong_old_perms(filename, from_mode,
72
current_mode) != "continue":
79
if to_exec_flag is not None:
80
current_mode = os.stat(filename).st_mode
84
to_mode = current_mode | (0100 & ~umask)
85
# Enable x-bit for others only if they can read it.
86
if current_mode & 0004:
87
to_mode |= 0001 & ~umask
88
if current_mode & 0040:
89
to_mode |= 0010 & ~umask
91
to_mode = current_mode & ~0111
75
if to_mode is not None:
93
77
os.chmod(filename, to_mode)
95
79
if e.errno == errno.ENOENT:
96
conflict_handler.missing_for_exec_flag(filename)
80
conflict_handler.missing_for_chmod(filename)
98
82
def __eq__(self, other):
99
return (isinstance(other, ChangeExecFlag) and
100
self.old_exec_flag == other.old_exec_flag and
101
self.new_exec_flag == other.new_exec_flag)
83
if not isinstance(other, ChangeUnixPermissions):
85
elif self.old_mode != other.old_mode:
87
elif self.new_mode != other.new_mode:
103
92
def __ne__(self, other):
104
93
return not (self == other)
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
225
def reversed(sequence):
306
226
max = len(sequence) - 1
307
227
for i in range(len(sequence)):
432
339
def __ne__(self, other):
433
340
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
342
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,
343
new_file = filename+".new"
344
base_file = self.base.readonly_path(self.file_id)
345
other_file = self.other.readonly_path(self.file_id)
352
status = patch.diff3(new_file, filename, base, other)
354
os.chmod(new_file, os.stat(filename).st_mode)
355
rename(new_file, filename)
359
def get_lines(filename):
360
my_file = file(base, "rb")
361
lines = my_file.readlines()
363
base_lines = get_lines(base)
364
other_lines = get_lines(other)
365
conflict_handler.merge_conflict(new_file, filename, base_lines,
1025
914
Exception.__init__(self, "Conflict applying changes to %s" % this_path)
1026
915
self.this_path = this_path
917
class MergePermissionConflict(Exception):
918
def __init__(self, this_path, base_path, other_path):
919
this_perms = os.stat(this_path).st_mode & 0755
920
base_perms = os.stat(base_path).st_mode & 0755
921
other_perms = os.stat(other_path).st_mode & 0755
922
msg = """Conflicting permission for %s
926
""" % (this_path, this_perms, base_perms, other_perms)
927
self.this_path = this_path
928
self.base_path = base_path
929
self.other_path = other_path
930
Exception.__init__(self, msg)
1028
932
class WrongOldContents(Exception):
1029
933
def __init__(self, filename):
1030
934
msg = "Contents mismatch deleting %s" % filename
1031
935
self.filename = filename
1032
936
Exception.__init__(self, msg)
1034
class WrongOldExecFlag(Exception):
1035
def __init__(self, filename, old_exec_flag, new_exec_flag):
1036
msg = "Executable flag missmatch on %s:\n" \
1037
"Expected %s, got %s." % (filename, old_exec_flag, new_exec_flag)
938
class WrongOldPermissions(Exception):
939
def __init__(self, filename, old_perms, new_perms):
940
msg = "Permission missmatch on %s:\n" \
941
"Expected 0%o, got 0%o." % (filename, old_perms, new_perms)
1038
942
self.filename = filename
1039
943
Exception.__init__(self, msg)
1083
987
msg = "Conflicting contents for new file %s" % (filename)
1084
988
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
991
class MissingForMerge(Exception):
1098
992
def __init__(self, filename):
1131
1025
os.unlink(new_file)
1132
1026
raise MergeConflict(this_path)
1028
def permission_conflict(self, this_path, base_path, other_path):
1029
raise MergePermissionConflict(this_path, base_path, other_path)
1134
1031
def wrong_old_contents(self, filename, expected_contents):
1135
1032
raise WrongOldContents(filename)
1137
1034
def rem_contents_conflict(self, filename, this_contents, base_contents):
1138
1035
raise RemoveContentsConflict(filename)
1140
def wrong_old_exec_flag(self, filename, old_exec_flag, new_exec_flag):
1141
raise WrongOldExecFlag(filename, old_exec_flag, new_exec_flag)
1037
def wrong_old_perms(self, filename, old_perms, new_perms):
1038
raise WrongOldPermissions(filename, old_perms, new_perms)
1143
1040
def rmdir_non_empty(self, filename):
1144
1041
raise DeletingNonEmptyDirectory(filename)
1149
1046
def patch_target_missing(self, filename, contents):
1150
1047
raise PatchTargetMissing(filename)
1152
def missing_for_exec_flag(self, filename):
1153
raise MissingForExecFlag(filename)
1049
def missing_for_chmod(self, filename):
1050
raise MissingPermsFile(filename)
1155
1052
def missing_for_rm(self, filename, change):
1156
1053
raise MissingForRm(filename)
1158
def missing_for_rename(self, filename, to_path):
1159
raise MissingForRename(filename, to_path)
1055
def missing_for_rename(self, filename):
1056
raise MissingForRename(filename)
1161
1058
def missing_for_merge(self, file_id, other_path):
1162
1059
raise MissingForMerge(other_path)
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." \
1279
class UnsuppportedFiletype(Exception):
1280
def __init__(self, full_path, stat_result):
1281
msg = "The file \"%s\" is not a supported filetype." % full_path
1393
1282
Exception.__init__(self, msg)
1394
1283
self.full_path = full_path
1284
self.stat_result = stat_result
1397
1286
def generate_changeset(tree_a, tree_b, interesting_ids=None):
1398
1287
return ChangesetGenerator(tree_a, tree_b, interesting_ids)()
1401
1289
class ChangesetGenerator(object):
1402
1290
def __init__(self, tree_a, tree_b, interesting_ids=None):
1403
1291
object.__init__(self)
1496
1384
if cs_entry is None:
1499
cs_entry.metadata_change = self.make_exec_flag_change(id)
1501
1386
if id in self.tree_a and id in self.tree_b:
1502
1387
a_sha1 = self.tree_a.get_file_sha1(id)
1503
1388
b_sha1 = self.tree_b.get_file_sha1(id)
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
full_path_a = self.tree_a.readonly_path(id)
1393
full_path_b = self.tree_b.readonly_path(id)
1394
stat_a = self.lstat(full_path_a)
1395
stat_b = self.lstat(full_path_b)
1397
cs_entry.metadata_change = self.make_mode_change(stat_a, stat_b)
1398
cs_entry.contents_change = self.make_contents_change(full_path_a,
1508
1402
return cs_entry
1510
def make_exec_flag_change(self, file_id):
1511
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)
1518
if exec_flag_a == exec_flag_b:
1520
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)
1404
def make_mode_change(self, stat_a, stat_b):
1406
if stat_a is not None and not stat.S_ISLNK(stat_a.st_mode):
1407
mode_a = stat_a.st_mode & 0777
1409
if stat_b is not None and not stat.S_ISLNK(stat_b.st_mode):
1410
mode_b = stat_b.st_mode & 0777
1411
if mode_a == mode_b:
1413
return ChangeUnixPermissions(mode_a, mode_b)
1415
def make_contents_change(self, full_path_a, stat_a, full_path_b, stat_b):
1416
if stat_a is None and stat_b is None:
1418
if None not in (stat_a, stat_b) and stat.S_ISDIR(stat_a.st_mode) and\
1419
stat.S_ISDIR(stat_b.st_mode):
1421
if None not in (stat_a, stat_b) and stat.S_ISREG(stat_a.st_mode) and\
1422
stat.S_ISREG(stat_b.st_mode):
1423
if stat_a.st_ino == stat_b.st_ino and \
1424
stat_a.st_dev == stat_b.st_dev:
1427
a_contents = self.get_contents(stat_a, full_path_a)
1428
b_contents = self.get_contents(stat_b, full_path_b)
1525
1429
if a_contents == b_contents:
1527
1431
return ReplaceContents(a_contents, b_contents)
1433
def get_contents(self, stat_result, full_path):
1434
if stat_result is None:
1436
elif stat.S_ISREG(stat_result.st_mode):
1437
return FileCreate(file(full_path, "rb").read())
1438
elif stat.S_ISDIR(stat_result.st_mode):
1440
elif stat.S_ISLNK(stat_result.st_mode):
1441
return SymlinkCreate(os.readlink(full_path))
1443
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))
1445
def lstat(self, full_path):
1447
if full_path is not None:
1449
stat_result = os.lstat(full_path)
1451
if e.errno != errno.ENOENT:
1545
1456
def full_path(entry, tree):
1546
return os.path.join(tree.basedir, entry.path)
1457
return os.path.join(tree.root, entry.path)
1548
1459
def new_delete_entry(entry, tree, inventory, delete):
1549
1460
if entry.path == "":