Skip to content

Instantly share code, notes, and snippets.

@mauritsvanrees
Last active October 1, 2018 10:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mauritsvanrees/92c40bc16ceaf3c375d81c995b4552c4 to your computer and use it in GitHub Desktop.
Save mauritsvanrees/92c40bc16ceaf3c375d81c995b4552c4 to your computer and use it in GitHub Desktop.
Make initial changes in a git project to use towncrier instead of manually editing CHANGES.rst
#!/bin/sh
# Latest public version:
# https://gist.github.com/mauritsvanrees/92c40bc16ceaf3c375d81c995b4552c4
# Goal:
# Make initial changes in a git project to use towncrier
# instead of manually editing CHANGES.rst.
# Create a standard pyproject.toml file.
TOML="pyproject.toml"
if ! test -e $TOML; then
cat > $TOML <<TOML_END
[tool.towncrier]
filename = "CHANGES.rst"
directory = "news/"
title_format = "{version} ({project_date})"
underlines = ["-", ""]
[[tool.towncrier.type]]
directory = "breaking"
name = "Breaking changes:"
showcontent = true
[[tool.towncrier.type]]
directory = "feature"
name = "New features:"
showcontent = true
[[tool.towncrier.type]]
directory = "bugfix"
name = "Bug fixes:"
showcontent = true
TOML_END
git add $TOML
echo "### ADDED $TOML ###"
echo
fi
# Check .gitattributes.
FILE=".gitattributes"
if test -e $FILE; then
echo "$FILE exists"
# Check that "CHANGES.rst merge=union" is the only contents.
MERGE_MARKER="CHANGES.rst merge=union"
if ! test $(grep -c "$MERGE_MARKER" $FILE) -eq 1; then
echo "It does not contain $MERGE_MARKER, so I leave it alone."
else
echo "It contains $MERGE_MARKER"
if test $(grep -c -v "$MERGE_MARKER" $FILE) -eq 0; then
echo "It is is the only line."
git rm $FILE
echo "### $FILE has been REMOVED from git ###"
else
echo "It is NOT the only line."
tmpfile=$(mktemp)
sed "/$MERGE_MARKER/d" $FILE > $tmpfile
mv $tmpfile $FILE
git add $FILE
echo "### REMOVED $MERGE_MARKER from $FILE"
fi
echo
fi
fi
# Make sure a news directory exists.
if test -e news && ! test -d news; then
echo "ERROR: news exists, but is not a directory."
exit 1
fi
if ! test -e news; then
mkdir news
echo "### ADDED news directory ###"
fi
# Make sure a marker file exists to keep the news directory in git,
# even when otherwise empty.
FILE="news/.gitkeep"
if test -e $FILE && ! test -f $FILE; then
echo "ERROR: $FILE exists, but is not a file."
exit 1
fi
if ! test -e $FILE; then
echo > $FILE
git add $FILE
echo "### ADDED dummy $FILE to keep news directory in git. ###"
fi
# Update MANIFEST.in
if test -e MANIFEST.in; then
# It seems good to include pyproject.toml.
if test $(grep -c $TOML MANIFEST.in) -eq 0; then
echo "include $TOML" >> MANIFEST.in
git add MANIFEST.in
echo "### INCLUDED $TOML in MANIFEST.in ###"
fi
# But the news directory should be ignored.
if test $(grep -c news MANIFEST.in) -eq 0; then
echo "recursive-exclude news *" >> MANIFEST.in
# check-manifest still complains that the news dir is not in the dist.
echo "exclude news" >> MANIFEST.in
git add MANIFEST.in
echo "### Recursively EXCLUDED news in MANIFEST.in ###"
fi
fi
CHANGES="CHANGES.rst"
UNRELEASED="(unreleased)"
TOWNCRIER_MARKER=".. towncrier release notes start"
TOWNCRIER_COMMENT_FILE=$(mktemp)
cat > $TOWNCRIER_COMMENT_FILE <<COMMENT_MARKER
.. You should *NOT* be adding new change log entries to this file.
You should create a file in the news directory instead.
For helpful instructions, please see:
https://github.com/plone/plone.releaser/blob/master/ADD-A-NEWS-ITEM.rst
$TOWNCRIER_MARKER
COMMENT_MARKER
# cat $TOWNCRIER_COMMENT_FILE
if test $(grep -c "$TOWNCRIER_MARKER" $CHANGES) -eq 0; then
echo
echo "Missing marker for towncrier in $CHANGES: $TOWNCRIER_MARKER"
echo $TOWNCRIER_COMMENT
if test $(grep -c "$UNRELEASED" $CHANGES) -eq 0; then
echo "Did not find $UNRELEASED in $CHANGES."
echo "### Please EDIT $CHANGES yourself and add this: ###"
echo
echo "----------------------------------------"
cat $TOWNCRIER_COMMENT_FILE
echo "----------------------------------------"
else
# get line number where 'unreleased' is. Possibly more.
LINE=$(grep -n "$UNRELEASED" $CHANGES | cut -d : -f 1 | head -n 1)
# Prepend the wanted text on that line.
TEMP=$(mktemp)
head -n $(($LINE -1)) $CHANGES > $TEMP
cat $TOWNCRIER_COMMENT_FILE >> $TEMP
tail -n +$LINE $CHANGES >> $TEMP
mv $TEMP $CHANGES
git add $CHANGES
echo "### ADDED marker for towncrier in $CHANGES: $TOWNCRIER_MARKER ###"
fi
fi
cat <<MARKER
We have done all automatic fixes.
TODO:
- Remove the unreleased version header from CHANGES.rst.
- Move the unreleased changes to files in the news directory.
Suggested commit:
git commit -m "Initialized towncrier.
[ci skip]"
MARKER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment