Skip to content

Commit 59bb397

Browse files
committed
feat: ✨ Add encoding option
1 parent b9bad9c commit 59bb397

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/git_changelog/cli.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,14 @@ def get_parser() -> argparse.ArgumentParser:
355355
dest="omit_empty_versions",
356356
help="Omit empty versions from the output. Default: unset (false).",
357357
)
358+
parser.add_argument(
359+
"-e",
360+
"--encoding",
361+
action="store",
362+
dest="encoding",
363+
help="Specify the encoding to use when reading and writing files. Default: utf-8.",
364+
default="utf-8",
365+
)
358366
parser.add_argument(
359367
"-Z",
360368
"--no-zerover",
@@ -541,6 +549,7 @@ def build_and_render(
541549
filter_commits: str | None = None,
542550
jinja_context: dict[str, Any] | None = None,
543551
versioning: Literal["pep440", "semver"] = "semver",
552+
encoding: str = "utf-8",
544553
) -> tuple[Changelog, str]:
545554
"""Build a changelog and render it.
546555
@@ -627,7 +636,7 @@ def build_and_render(
627636
# render new entries in-place
628637
if in_place:
629638
# read current changelog lines
630-
with open(output) as changelog_file: # type: ignore[arg-type]
639+
with open(output, encoding=encoding) as changelog_file: # type: ignore[arg-type]
631640
lines = changelog_file.read().splitlines()
632641

633642
# prepare version regex and marker line
@@ -671,7 +680,7 @@ def build_and_render(
671680
lines[marker : marker + marker2 + 2] = [rendered]
672681

673682
# write back updated changelog lines
674-
with open(output, "w") as changelog_file: # type: ignore[arg-type]
683+
with open(output, "w", encoding=encoding) as changelog_file: # type: ignore[arg-type]
675684
changelog_file.write("\n".join(lines).rstrip("\n") + "\n")
676685

677686
# overwrite output file
@@ -682,7 +691,7 @@ def build_and_render(
682691
if output is sys.stdout:
683692
sys.stdout.write(rendered)
684693
else:
685-
with open(output, "w") as stream: # type: ignore[arg-type]
694+
with open(output, "w", encoding=encoding) as stream: # type: ignore[arg-type]
686695
stream.write(rendered)
687696

688697
return changelog, rendered
@@ -692,6 +701,7 @@ def get_release_notes(
692701
input_file: str | Path = "CHANGELOG.md",
693702
version_regex: str = DEFAULT_VERSION_REGEX,
694703
marker_line: str = DEFAULT_MARKER_LINE,
704+
encoding: str = "utf-8",
695705
) -> str:
696706
"""Get release notes from existing changelog.
697707
@@ -708,7 +718,7 @@ def get_release_notes(
708718
release_notes = []
709719
found_marker = False
710720
found_version = False
711-
with open(input_file) as changelog:
721+
with open(input_file, encoding=encoding) as changelog:
712722
for line in changelog:
713723
line = line.strip() # noqa: PLW2901
714724
if not found_marker:
@@ -731,6 +741,7 @@ def output_release_notes(
731741
version_regex: str = DEFAULT_VERSION_REGEX,
732742
marker_line: str = DEFAULT_MARKER_LINE,
733743
output_file: str | TextIO | None = None,
744+
encoding: str = "utf-8",
734745
) -> None:
735746
"""Print release notes from existing changelog.
736747
@@ -743,11 +754,11 @@ def output_release_notes(
743754
output_file: Where to print/write the release notes.
744755
"""
745756
output_file = output_file or sys.stdout
746-
release_notes = get_release_notes(input_file, version_regex, marker_line)
757+
release_notes = get_release_notes(input_file, version_regex, marker_line, encoding=encoding)
747758
try:
748759
output_file.write(release_notes) # type: ignore[union-attr]
749760
except AttributeError:
750-
with open(output_file, "w") as file: # type: ignore[arg-type]
761+
with open(output_file, "w", encoding=encoding) as file: # type: ignore[arg-type]
751762
file.write(release_notes)
752763

753764

@@ -770,6 +781,7 @@ def main(args: list[str] | None = None) -> int:
770781
version_regex=settings["version_regex"],
771782
marker_line=settings["marker_line"],
772783
output_file=None, # force writing to stdout
784+
encoding=settings["encoding"],
773785
)
774786
return 0
775787

0 commit comments

Comments
 (0)