@@ -58,7 +58,19 @@ def file_checksum(filename):
58
58
with open (filename , 'rb' ) as file_handle :
59
59
return hashlib .sha1 (file_handle .read ()).hexdigest ()
60
60
61
- def check_group (group_name , files , master_file_picker , emit_error ):
61
+ def accept_prefix (line1 , line2 ):
62
+ suffix = line2 .removeprefix (line1 )
63
+ return not suffix or suffix .lstrip ().startswith ("//" )
64
+
65
+ def equivalent_lines (lines1 , lines2 ):
66
+ if len (lines1 ) != len (lines2 ):
67
+ return False
68
+ for line1 , line2 in zip (lines1 , lines2 ):
69
+ if not accept_prefix (line1 , line2 ) and not accept_prefix (line2 , line1 ):
70
+ return False
71
+ return True
72
+
73
+ def check_group (group_name , files , master_file_picker , emit_error , accept_prefix ):
62
74
extant_files = [f for f in files if path .isfile (f )]
63
75
if len (extant_files ) == 0 :
64
76
emit_error (__file__ , 0 , "No files found from group '" + group_name + "'." )
@@ -70,11 +82,23 @@ def check_group(group_name, files, master_file_picker, emit_error):
70
82
return
71
83
72
84
checksums = {file_checksum (f ) for f in extant_files }
73
-
74
- if len (checksums ) == 1 and len ( extant_files ) == len ( files ) :
85
+ same_lengths = len ( extant_files ) == len ( files )
86
+ if len (checksums ) == 1 and same_lengths :
75
87
# All files are present and identical.
76
88
return
77
89
90
+ # In this case we also consider files indentical, if
91
+ # (1) The group only containts two files.
92
+ # (2) The lines of one file are the same as the lines of another file
93
+ # modulo comments.
94
+ if accept_prefix and same_lengths and len (extant_files ) == 2 :
95
+ with open (extant_files [0 ], 'r' ) as f1 :
96
+ file1_lines = [l .strip ('\n \r ' ) for l in f1 .readlines ()]
97
+ with open (extant_files [1 ], 'r' ) as f2 :
98
+ file2_lines = [l .strip ('\n \r ' ) for l in f2 .readlines ()]
99
+ if equivalent_lines (file1_lines , file2_lines ):
100
+ return
101
+
78
102
master_file = master_file_picker (extant_files )
79
103
if master_file is None :
80
104
emit_error (__file__ , 0 ,
@@ -139,9 +163,10 @@ def sync_identical_files(emit_error):
139
163
raise Exception ("Bad command line or file not found" )
140
164
chdir_repo_root ()
141
165
load_if_exists ('.' , 'config/identical-files.json' )
142
- file_groups .update (csharp_test_files ())
166
+ for group_name , files in csharp_test_files ().items ():
167
+ check_group (group_name , files , master_file_picker , emit_error , True )
143
168
for group_name , files in file_groups .items ():
144
- check_group (group_name , files , master_file_picker , emit_error )
169
+ check_group (group_name , files , master_file_picker , emit_error , False )
145
170
146
171
def main ():
147
172
sync_identical_files (emit_local_error )
0 commit comments