Skip to content

Commit bdca412

Browse files
authored
[clang] Auto-detect which newline style to use for cxx_dr_status.html (#132045)
Aaron reported that `make_cxx_dr_status` replaces all newlines in `cxx_dr_status.html`, which makes for a huge diff. On Windows, we can't be compatible with all `autocrlf` modes at once, so this patch adds autodetection of newline style using the existing file, if one is present (which should be the case for all reasonable use cases).
1 parent 4e3440d commit bdca412

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

clang/www/make_cxx_dr_status

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,22 @@ out_html.append('''\
318318
</html>
319319
''')
320320

321-
out_file = open(output, 'w')
321+
# Make an effort to remain consistent with the existing file.
322+
# We can't pick one newline style and use it on Windows,
323+
# because we can't be compatible with all 'autocrlf' modes at once.
324+
def detect_newline_style(file_path):
325+
if not os.path.exists(file_path):
326+
return '\n'
327+
f = open(file_path)
328+
f.readline()
329+
if f.newlines is None:
330+
return '\n'
331+
if isinstance(f.newlines, str):
332+
return f.newlines
333+
newline = f.newlines[0]
334+
print(f"Existing '{file_path}' has inconsistent newlines; picking '{newline.encode('unicode_escape').decode('utf-8')}'")
335+
return newline
336+
337+
out_file = open(output, 'w', newline=detect_newline_style(output))
322338
out_file.write(''.join(out_html))
323339
out_file.close()

0 commit comments

Comments
 (0)