42
44
newdict[value] = key
47
class ChangeUnixPermissions(object):
48
class ChangeExecFlag(object):
48
49
"""This is two-way change, suitable for file modification, creation,
50
def __init__(self, old_mode, new_mode):
51
self.old_mode = old_mode
52
self.new_mode = new_mode
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
54
55
def apply(self, filename, conflict_handler, reverse=False):
56
from_mode = self.old_mode
57
to_mode = self.new_mode
57
from_exec_flag = self.old_exec_flag
58
to_exec_flag = self.new_exec_flag
59
from_mode = self.new_mode
60
to_mode = self.old_mode
60
from_exec_flag = self.new_exec_flag
61
to_exec_flag = self.old_exec_flag
62
current_mode = os.stat(filename).st_mode &0777
63
current_exec_flag = bool(os.stat(filename).st_mode & 0111)
64
65
if e.errno == errno.ENOENT:
65
if conflict_handler.missing_for_chmod(filename) == "skip":
66
if conflict_handler.missing_for_exec_flag(filename) == "skip":
68
current_mode = from_mode
69
current_exec_flag = from_exec_flag
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":
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":
75
if to_mode is not None:
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
77
90
os.chmod(filename, to_mode)
79
92
if e.errno == errno.ENOENT:
80
conflict_handler.missing_for_chmod(filename)
93
conflict_handler.missing_for_exec_flag(filename)
82
95
def __eq__(self, other):
83
if not isinstance(other, ChangeUnixPermissions):
85
elif self.old_mode != other.old_mode:
87
elif self.new_mode != other.new_mode:
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)
92
100
def __ne__(self, other):
93
101
return not (self == other)
95
104
def dir_create(filename, conflict_handler, reverse):
96
105
"""Creates the directory, or deletes it if reverse is true. Intended to be
97
106
used with ReplaceContents.
353
360
status = patch.diff3(new_file, filename, base, other)
355
362
os.chmod(new_file, os.stat(filename).st_mode)
356
os.rename(new_file, filename)
363
rename(new_file, filename)
359
366
assert(status == 1)
360
367
def get_lines(filename):
361
my_file = file(base, "rb")
368
my_file = file(filename, "rb")
362
369
lines = my_file.readlines()
364
372
base_lines = get_lines(base)
365
373
other_lines = get_lines(other)
366
374
conflict_handler.merge_conflict(new_file, filename, base_lines,
915
923
Exception.__init__(self, "Conflict applying changes to %s" % this_path)
916
924
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)
933
926
class WrongOldContents(Exception):
934
927
def __init__(self, filename):
935
928
msg = "Contents mismatch deleting %s" % filename
936
929
self.filename = filename
937
930
Exception.__init__(self, msg)
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)
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)
943
936
self.filename = filename
944
937
Exception.__init__(self, msg)
1029
1019
os.unlink(new_file)
1030
1020
raise MergeConflict(this_path)
1032
def permission_conflict(self, this_path, base_path, other_path):
1033
raise MergePermissionConflict(this_path, base_path, other_path)
1035
1022
def wrong_old_contents(self, filename, expected_contents):
1036
1023
raise WrongOldContents(filename)
1038
1025
def rem_contents_conflict(self, filename, this_contents, base_contents):
1039
1026
raise RemoveContentsConflict(filename)
1041
def wrong_old_perms(self, filename, old_perms, new_perms):
1042
raise WrongOldPermissions(filename, old_perms, new_perms)
1028
def wrong_old_exec_flag(self, filename, old_exec_flag, new_exec_flag):
1029
raise WrongOldExecFlag(filename, old_exec_flag, new_exec_flag)
1044
1031
def rmdir_non_empty(self, filename):
1045
1032
raise DeletingNonEmptyDirectory(filename)
1050
1037
def patch_target_missing(self, filename, contents):
1051
1038
raise PatchTargetMissing(filename)
1053
def missing_for_chmod(self, filename):
1054
raise MissingPermsFile(filename)
1040
def missing_for_exec_flag(self, filename):
1041
raise MissingForExecFlag(filename)
1056
1043
def missing_for_rm(self, filename, change):
1057
1044
raise MissingForRm(filename)
1267
1254
return new_meta
1268
1255
elif new_meta is None:
1269
1256
return old_meta
1270
elif isinstance(old_meta, ChangeUnixPermissions) and \
1271
isinstance(new_meta, ChangeUnixPermissions):
1272
return ChangeUnixPermissions(old_meta.old_mode, new_meta.new_mode)
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)
1274
1261
return ApplySequence(old_meta, new_meta)
1388
1376
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)
1390
1386
if id in self.tree_a and id in self.tree_b:
1391
1387
a_sha1 = self.tree_a.get_file_sha1(id)
1392
1388
b_sha1 = self.tree_b.get_file_sha1(id)
1393
1389
if None not in (a_sha1, b_sha1) and a_sha1 == b_sha1:
1394
1390
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.new_parent = None
1402
cs_entry.new_path = None
1404
cs_entry.metadata_change = self.make_mode_change(stat_a, stat_b)
1405
1392
cs_entry.contents_change = self.make_contents_change(full_path_a,
1409
1396
return cs_entry
1411
def make_mode_change(self, stat_a, stat_b):
1398
def make_exec_flag_change(self, stat_a, stat_b):
1399
exec_flag_a = exec_flag_b = None
1413
1400
if stat_a is not None and not stat.S_ISLNK(stat_a.st_mode):
1414
mode_a = stat_a.st_mode & 0777
1401
exec_flag_a = bool(stat_a.st_mode & 0111)
1416
1402
if stat_b is not None and not stat.S_ISLNK(stat_b.st_mode):
1417
mode_b = stat_b.st_mode & 0777
1418
if mode_a == mode_b:
1403
exec_flag_b = bool(stat_b.st_mode & 0111)
1404
if exec_flag_a == exec_flag_b:
1420
return ChangeUnixPermissions(mode_a, mode_b)
1406
return ChangeExecFlag(exec_flag_a, exec_flag_b)
1422
1408
def make_contents_change(self, full_path_a, stat_a, full_path_b, stat_b):
1423
1409
if stat_a is None and stat_b is None: