Skip to content

Commit

Permalink
Respect the existing newline style of changelog files
Browse files Browse the repository at this point in the history
Closes #14
  • Loading branch information
kurtmckee authored and Ned Batchelder committed Dec 14, 2020
1 parent 93ab544 commit c500bda
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 7 additions & 0 deletions changelog.d/20201213_140401_kurtmckee_newlines.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Changed
.......

- Respect the existing newline style of changelog files. (#14)

This means that a changelog file with Linux newlines on a Windows platform
will be updated with Linux newlines, not rewritten with Windows newlines.
12 changes: 10 additions & 2 deletions src/scriv/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,15 @@ def collect(
sections = order_dict(sections, [None] + config.categories)

changelog = Path(config.output_file)
newline = ""
if changelog.exists():
changelog_text = changelog.read_text()
with changelog.open("r") as f:
changelog_text = f.read()
if f.newlines: # .newlines may be None, str, or tuple
if isinstance(f.newlines, str):
newline = f.newlines
else:
newline = f.newlines[0]
text_before, text_after = cut_at_line(
changelog_text, config.insert_marker
)
Expand All @@ -150,7 +157,8 @@ def collect(
else:
new_header = ""
new_text = format_tools.format_sections(sections)
changelog.write_text(text_before + new_header + new_text + text_after)
with changelog.open("w", newline=newline or None) as f:
f.write(text_before + new_header + new_text + text_after)

if edit:
git_edit(changelog)
Expand Down

0 comments on commit c500bda

Please sign in to comment.