Thibault Maekelbergh

📝 Auto-generating a changelog using only bash and git

I quickly cobbled this together and again it was fun to see how powerful bash is.

Very simple changelog generation based on the git log command to get the log between two tags. It will automatically get the current tag and the one before it and output all the commits in their short form.

You could very easily integrate this in your CI pipeline. My use case was generating a changelog on every new npm version which creates a tag and then pushes to a private repository. Note that I am filtering out the commits containing "Upgrade to:", leave the grep command out if you don't want to exclude commits.

bash
#!/usr/bin/env bash
set -e
function check_changelog() {
if [[ ! -f ./CHANGELOG ]]; then
touch CHANGELOG
fi
}
function get_changelog() {
local currentTag previousTag prevChangelogContents
currentTag=$(git describe --abbrev=0 --tags "$(git describe --abbrev=0)"^)
previousTag=$(git describe --abbrev=0)
prevChangelogContents=$(cat ./CHANGELOG)
{
echo "## $currentTag";
echo "";
git log-short --no-merges "$currentTag...$previousTag" | grep -v "Upgrade to";
echo "";
} > CHANGELOG
echo "$prevChangelogContents" >> CHANGELOG
}
function main() {
check_changelog
get_changelog
}
main

And then in your CI pipeline (using Bitbucket pipelines here):

yaml
...
step:
script:
- npm version minor -m "Upgrade to %s [skip ci]"
- ./generate-changelog.sh && cat ./CHANGELOG
- git add ./CHANGELOG && git commit --amend --no-edit
- git push && git push --tags
- npm publish

Demo repo: https://github.com/thibmaek/demo-collections/tree/master/pure-bash-git-changelog