Skip to content

Commit

Permalink
Handle deletion of uncommitted news fragments
Browse files Browse the repository at this point in the history
Before this commit, all the news fragments needed to be committed into
git, or the fragments removal after building the news file would crash.

In my workflow, I add missing fragments before building the news file
because I'm extracting author names from the git log, and towncrier
crashes at the end of the build process.

Signed-off-by: Aurélien Bompard <[email protected]>
  • Loading branch information
abompard committed Jun 27, 2024
1 parent b49cca6 commit 5ee3f19
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
20 changes: 17 additions & 3 deletions src/towncrier/_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@

import os

from subprocess import STDOUT, call, check_output
from subprocess import STDOUT, CalledProcessError, call, check_output


def remove_files(fragment_filenames: list[str]) -> None:
if fragment_filenames:
call(["git", "rm", "--quiet"] + fragment_filenames)
if not fragment_filenames:
return

# Filter out files that are unknown to git
try:
git_fragments = check_output(
["git", "ls-files"] + fragment_filenames, encoding="utf-8"
).split("\n")
git_fragments = [os.path.abspath(f) for f in git_fragments if os.path.isfile(f)]
except CalledProcessError:
git_fragments = []

call(["git", "rm", "--quiet", "--force"] + git_fragments)
unknown_fragments = set(fragment_filenames) - set(git_fragments)
for unknown_fragment in unknown_fragments:
os.remove(unknown_fragment)


def stage_newsfile(directory: str, filename: str) -> None:
Expand Down
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/357.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
It can now handle news fragments which are not part of the git repository. For example uncommited or unstagged files.
61 changes: 57 additions & 4 deletions src/towncrier/test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from datetime import date
from pathlib import Path
from subprocess import call
from textwrap import dedent
from unittest.mock import patch

Expand Down Expand Up @@ -747,8 +748,8 @@ def do_build_once_with(version, fragment_file, fragment):
"--yes",
],
)
# not git repository, manually remove fragment file
Path(f"newsfragments/{fragment_file}").unlink()
# fragment files unknown to git are removed even without a git repo
# Path(f"newsfragments/{fragment_file}").unlink()
return result

results = []
Expand Down Expand Up @@ -845,8 +846,8 @@ def do_build_once_with(version, fragment_file, fragment):
],
catch_exceptions=False,
)
# not git repository, manually remove fragment file
Path(f"newsfragments/{fragment_file}").unlink()
# fragment files unknown to git are removed even without a git repo
# Path(f"newsfragments/{fragment_file}").unlink()
return result

results = []
Expand Down Expand Up @@ -1530,3 +1531,55 @@ def test_orphans_in_non_showcontent_markdown(self, runner):

self.assertEqual(0, result.exit_code, result.output)
self.assertEqual(expected_output, result.output)

@with_git_project()
def test_uncommitted_files(self, runner, commit):
"""
At build time, it will delete any fragment file regardless of its stage,
included files that are not part of the git reporsitory,
or are just staged or modified.
"""
# 123 is committed, 124 is modified, 125 is just added, 126 is unknown

with open("foo/newsfragments/123.feature", "w") as f:
f.write("Adds levitation")
with open("foo/newsfragments/124.feature", "w") as f:
f.write("Extends levitation")

commit()

with open("foo/newsfragments/125.feature", "w") as f:
f.write("Baz levitation")
with open("foo/newsfragments/126.feature", "w") as f:
f.write("Fix (literal) crash")

with open("foo/newsfragments/124.feature", "a") as f:
f.write(" for an hour")
call(["git", "add", "foo/newsfragments/125.feature"])

result = runner.invoke(_main, ["--date", "01-01-2001", "--yes"])

self.assertEqual(0, result.exit_code)
for fragment in ("123", "124", "125", "126"):
self.assertFalse(os.path.isfile(f"foo/newsfragments/{fragment}.feature"))

path = "NEWS.rst"
self.assertTrue(os.path.isfile(path))
news_contents = open(path).read()
self.assertEqual(
news_contents,
dedent(
"""\
Foo 1.2.3 (01-01-2001)
======================
Features
--------
- Adds levitation (#123)
- Extends levitation for an hour (#124)
- Baz levitation (#125)
- Fix (literal) crash (#126)
"""
),
)

0 comments on commit 5ee3f19

Please sign in to comment.