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 <aurelien@bompard.org>
  • Loading branch information
abompard committed Jun 27, 2024
1 parent b49cca6 commit ec709a8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 9 deletions.
39 changes: 34 additions & 5 deletions src/towncrier/_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,50 @@
from __future__ import annotations

import os
import sys

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


def run(args, **kwargs):
"""Run a command.
Use UTF-8 both when sys.stdout does not have .encoding (Python 2.7) and
when the attribute is present but set to None (explicitly piped output
and also some CI such as GitHub Actions).
"""
output = check_output(args, **kwargs)
if not isinstance(output, str):
encoding = getattr(sys.stdout, "encoding", None)
if encoding is None:
encoding = "utf8"
output = output.decode(encoding)
return output.strip()


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 = run(["git", "ls-files"] + fragment_filenames).split("\n")
git_fragments = [os.path.abspath(f) for f in git_fragments]
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:
call(["git", "add", os.path.join(directory, filename)])


def get_remote_branches(base_directory: str) -> list[str]:
output = check_output(
output = run(
["git", "branch", "-r"], cwd=base_directory, encoding="utf-8", stderr=STDOUT
)

Expand All @@ -28,7 +57,7 @@ def get_remote_branches(base_directory: str) -> list[str]:
def list_changed_files_compared_to_branch(
base_directory: str, compare_with: str
) -> list[str]:
output = check_output(
output = run(
["git", "diff", "--name-only", compare_with + "..."],
cwd=base_directory,
encoding="utf-8",
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 ec709a8

Please sign in to comment.