Skip to content

Instantly share code, notes, and snippets.

@bnjmnt4n
Created November 14, 2016 01:30
Show Gist options
  • Save bnjmnt4n/2b3994e7c72ffd1037d7a8ba2964e17a to your computer and use it in GitHub Desktop.
Save bnjmnt4n/2b3994e7c72ffd1037d7a8ba2964e17a to your computer and use it in GitHub Desktop.
Simple script to generate a Markdown-friendly changelog.

This is a simple shell script to generate a Markdown-friendly list of commits for a changelog. The list of commits will be from the current HEAD to the previous tag (including lightweight tags), in reverse chronological order.

Usage

Simply run the script in a git repository:

$ ./changelog.sh
## HEAD

 * Commit 1.
 * Commit 2.
 * Commit 3.
 * Commit 4.

You can also save the contents to a file:

$ ./changelog.sh > changelog.md
#!/usr/bin/env sh
# Obtain the latest tag, include lightweight tags.
CURRENT=$(git describe --long --tags)
if [[ ${CURRENT} =~ (.*)-([0-9]+)-g[0-9a-z]{7}$ ]]; then
# If the current tag is the `HEAD`…
if [ ${BASH_REMATCH[2]} -eq "0" ]; then
# …compare the current tag and the previous tag.
CURRENT="${BASH_REMATCH[1]}"
TAG=$(git describe --abbrev=0 --tags "${BASH_REMATCH[1]}^")
# Otherwise, compare `HEAD` with the latest tag.
else
CURRENT="HEAD"
TAG="${BASH_REMATCH[1]}"
fi
# Log out the commits in a Markdown-friendly format.
echo "## ${CURRENT}"
echo ""
git log --pretty="format: * %s" "${TAG}..${CURRENT}"
echo ""
else
echo "Invalid git response." >&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment