@@ -102,11 +102,36 @@ def _substitute_path(path, regex_iter):
102102 continue
103103 if not content :
104104 raise UnicodeDecodeError ('Unable to decode with any encoding: %s' % path )
105+
106+ # list that will contain the comments
107+ comments = []
108+ replace_comments = (path .suffix == '.java' or path .suffix == '.cc' or path .suffix == '.h'
109+ or path .suffix == '.js' or path .suffix == '.cpp' or path .suffix == '.c' )
110+ if replace_comments :
111+ content = re .sub (
112+ r'(\/\*.*?\*/\n|\s\/\/[^\n]+)' ,
113+ lambda match : _replace_comments (comments , match .group (0 )),
114+ content ,
115+ flags = re .DOTALL )
116+ if path .suffix == '.py' or path .suffix == '.gn' :
117+ content = re .sub (
118+ # docstrings not supported
119+ r'\s\#[^\n]+' ,
120+ lambda match : _replace_comments (comments , match .group (0 )),
121+ content ,
122+ flags = re .DOTALL )
123+ replace_comments = True
124+
105125 file_subs = 0
106126 for regex_pair in regex_iter :
107127 content , sub_count = regex_pair .pattern .subn (regex_pair .replacement , content )
108128 file_subs += sub_count
109129 if file_subs > 0 :
130+ if replace_comments :
131+ # restore comments
132+ content = re .sub (PLACE_HOLDER + r'(\d+):' ,
133+ lambda match : _restore_comments (comments , match .group (1 )), content )
134+
110135 substituted_content = content .encode (encoding )
111136 input_file .seek (0 )
112137 input_file .write (content .encode (encoding ))
@@ -115,6 +140,19 @@ def _substitute_path(path, regex_iter):
115140 return (None , None )
116141
117142
143+ # use a randomized placeholder for comment replacements
144+ PLACE_HOLDER = ':C7yae7ozv:'
145+
146+
147+ def _replace_comments (comments , comment ):
148+ comments .append (comment )
149+ return PLACE_HOLDER + str (len (comments )) + ':'
150+
151+
152+ def _restore_comments (comments , index ):
153+ return comments [int (index ) - 1 ]
154+
155+
118156def _validate_file_index (index_file , resolved_tree , cache_index_files ):
119157 """
120158 Validation of file index and hashes against the source tree.
0 commit comments