44
42
newdict[value] = key
48
class ChangeExecFlag(object):
47
class ChangeUnixPermissions(object):
49
48
"""This is two-way change, suitable for file modification, creation,
51
def __init__(self, old_exec_flag, new_exec_flag):
52
self.old_exec_flag = old_exec_flag
53
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
55
54
def apply(self, filename, conflict_handler, reverse=False):
57
from_exec_flag = self.old_exec_flag
58
to_exec_flag = self.new_exec_flag
56
from_mode = self.old_mode
57
to_mode = self.new_mode
60
from_exec_flag = self.new_exec_flag
61
to_exec_flag = self.old_exec_flag
59
from_mode = self.new_mode
60
to_mode = self.old_mode
63
current_exec_flag = bool(os.stat(filename).st_mode & 0111)
62
current_mode = os.stat(filename).st_mode &0777
65
64
if e.errno == errno.ENOENT:
66
if conflict_handler.missing_for_exec_flag(filename) == "skip":
65
if conflict_handler.missing_for_chmod(filename) == "skip":
69
current_exec_flag = from_exec_flag
68
current_mode = from_mode
71
if from_exec_flag is not None and current_exec_flag != from_exec_flag:
72
if conflict_handler.wrong_old_exec_flag(filename,
73
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":
76
if to_exec_flag is not None:
77
current_mode = os.stat(filename).st_mode
81
to_mode = current_mode | (0100 & ~umask)
82
# Enable x-bit for others only if they can read it.
83
if current_mode & 0004:
84
to_mode |= 0001 & ~umask
85
if current_mode & 0040:
86
to_mode |= 0010 & ~umask
88
to_mode = current_mode & ~0111
75
if to_mode is not None:
90
77
os.chmod(filename, to_mode)
92
79
if e.errno == errno.ENOENT:
93
conflict_handler.missing_for_exec_flag(filename)
80
conflict_handler.missing_for_chmod(filename)
95
82
def __eq__(self, other):
96
return (isinstance(other, ChangeExecFlag) and
97
self.old_exec_flag == other.old_exec_flag and
98
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:
100
92
def __ne__(self, other):
101
93
return not (self == other)
104
95
def dir_create(filename, conflict_handler, reverse):
105
96
"""Creates the directory, or deletes it if reverse is true. Intended to be
106
97
used with ReplaceContents.
360
353
status = patch.diff3(new_file, filename, base, other)
362
355
os.chmod(new_file, os.stat(filename).st_mode)
363
rename(new_file, filename)
356
os.rename(new_file, filename)
366
359
assert(status == 1)
367
360
def get_lines(filename):
368
my_file = file(filename, "rb")
361
my_file = file(base, "rb")
369
362
lines = my_file.readlines()
372
364
base_lines = get_lines(base)
373
365
other_lines = get_lines(other)
374
366
conflict_handler.merge_conflict(new_file, filename, base_lines,
923
915
Exception.__init__(self, "Conflict applying changes to %s" % this_path)
924
916
self.this_path = this_path
918
class MergePermissionConflict(Exception):
919
def __init__(self, this_path, base_path, other_path):
920
this_perms = os.stat(this_path).st_mode & 0755
921
base_perms = os.stat(base_path).st_mode & 0755
922
other_perms = os.stat(other_path).st_mode & 0755
923
msg = """Conflicting permission for %s
927
""" % (this_path, this_perms, base_perms, other_perms)
928
self.this_path = this_path
929
self.base_path = base_path
930
self.other_path = other_path
931
Exception.__init__(self, msg)
926
933
class WrongOldContents(Exception):
927
934
def __init__(self, filename):
928
935
msg = "Contents mismatch deleting %s" % filename
929
936
self.filename = filename
930
937
Exception.__init__(self, msg)
932
class WrongOldExecFlag(Exception):
933
def __init__(self, filename, old_exec_flag, new_exec_flag):
934
msg = "Executable flag missmatch on %s:\n" \
935
"Expected %s, got %s." % (filename, old_exec_flag, new_exec_flag)
939
class WrongOldPermissions(Exception):
940
def __init__(self, filename, old_perms, new_perms):
941
msg = "Permission missmatch on %s:\n" \
942
"Expected 0%o, got 0%o." % (filename, old_perms, new_perms)
936
943
self.filename = filename
937
944
Exception.__init__(self, msg)
1019
1029
os.unlink(new_file)
1020
1030
raise MergeConflict(this_path)
1032
def permission_conflict(self, this_path, base_path, other_path):
1033
raise MergePermissionConflict(this_path, base_path, other_path)
1022
1035
def wrong_old_contents(self, filename, expected_contents):
1023
1036
raise WrongOldContents(filename)
1025
1038
def rem_contents_conflict(self, filename, this_contents, base_contents):
1026
1039
raise RemoveContentsConflict(filename)
1028
def wrong_old_exec_flag(self, filename, old_exec_flag, new_exec_flag):
1029
raise WrongOldExecFlag(filename, old_exec_flag, new_exec_flag)
1041
def wrong_old_perms(self, filename, old_perms, new_perms):
1042
raise WrongOldPermissions(filename, old_perms, new_perms)
1031
1044
def rmdir_non_empty(self, filename):
1032
1045
raise DeletingNonEmptyDirectory(filename)
1037
1050
def patch_target_missing(self, filename, contents):
1038
1051
raise PatchTargetMissing(filename)
1040
def missing_for_exec_flag(self, filename):
1041
raise MissingForExecFlag(filename)
1053
def missing_for_chmod(self, filename):
1054
raise MissingPermsFile(filename)
1043
1056
def missing_for_rm(self, filename, change):
1044
1057
raise MissingForRm(filename)
1254
1267
return new_meta
1255
1268
elif new_meta is None:
1256
1269
return old_meta
1257
elif (isinstance(old_meta, ChangeExecFlag) and
1258
isinstance(new_meta, ChangeExecFlag)):
1259
return ChangeExecFlag(old_meta.old_exec_flag, new_meta.new_exec_flag)
1270
elif isinstance(old_meta, ChangeUnixPermissions) and \
1271
isinstance(new_meta, ChangeUnixPermissions):
1272
return ChangeUnixPermissions(old_meta.old_mode, new_meta.new_mode)
1261
1274
return ApplySequence(old_meta, new_meta)
1376
1388
if cs_entry is None:
1379
full_path_a = self.tree_a.readonly_path(id)
1380
full_path_b = self.tree_b.readonly_path(id)
1381
stat_a = self.lstat(full_path_a)
1382
stat_b = self.lstat(full_path_b)
1384
cs_entry.metadata_change = self.make_exec_flag_change(stat_a, stat_b)
1386
1390
if id in self.tree_a and id in self.tree_b:
1387
1391
a_sha1 = self.tree_a.get_file_sha1(id)
1388
1392
b_sha1 = self.tree_b.get_file_sha1(id)
1389
1393
if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1390
1394
return cs_entry
1396
full_path_a = self.tree_a.readonly_path(id)
1397
full_path_b = self.tree_b.readonly_path(id)
1398
stat_a = self.lstat(full_path_a)
1399
stat_b = self.lstat(full_path_b)
1401
cs_entry.metadata_change = self.make_mode_change(stat_a, stat_b)
1392
1402
cs_entry.contents_change = self.make_contents_change(full_path_a,
1396
1406
return cs_entry
1398
def make_exec_flag_change(self, stat_a, stat_b):
1399
exec_flag_a = exec_flag_b = None
1408
def make_mode_change(self, stat_a, stat_b):
1400
1410
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)
1411
mode_a = stat_a.st_mode & 0777
1402
1413
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)
1404
if exec_flag_a == exec_flag_b:
1414
mode_b = stat_b.st_mode & 0777
1415
if mode_a == mode_b:
1406
return ChangeExecFlag(exec_flag_a, exec_flag_b)
1417
return ChangeUnixPermissions(mode_a, mode_b)
1408
1419
def make_contents_change(self, full_path_a, stat_a, full_path_b, stat_b):
1409
1420
if stat_a is None and stat_b is None: