Skip to content

Instantly share code, notes, and snippets.

@jballe
Last active February 28, 2019 10:00
Show Gist options
  • Save jballe/5a64daec43906e60999f811acfd7c02c to your computer and use it in GitHub Desktop.
Save jballe/5a64daec43906e60999f811acfd7c02c to your computer and use it in GitHub Desktop.
Generate changelog from git history
#!/bin/bash
CURRENTVER="v%build.number%" # This is teamcity replacement, could be param
LOGREGEX="[A-Z]+\-[0-9]+"
mkdir -p out
RESULTFILE=out/release-notes.txt
LASTTAG=$(git describe --tags --abbrev=0)
SINCE="v2.0.0" #"${SINCE:-${LASTTAG}}"
mapfile -t tags < <( git tag --contains $SINCE )
LAST="HEAD"
LASTTEXT=$CURRENTVER
STARTPOS=1
if [ "${LASTTAG}" = "${LASTTEXT}" ]; then
echo "Build number is same as last tag so we skip ther first entry"
STARTPOS=2
fi
echo "Generating change log since ${SINCE}, last tag was ${LASTTAG}"
echo "# Changelog" > "$RESULTFILE"
for (( idx=${#tags[@]}-$STARTPOS ; idx>=0 ; idx-- )) ;
do
tag="${tags[idx]}"
echo "Tag ${tag}"
echo "" >> "$RESULTFILE"
echo "## ${LASTTEXT}" >> "$RESULTFILE"
echo "" >> "$RESULTFILE"
git log "${tag}..${LAST}" --pretty=format:"%s %b" \
| grep -E $LOGREGEX \
| sed -n -r "s/^Merge pull request #[0-9]+ from [a-zA-Z0-9\/\-\-]* //p" \
>> "$RESULTFILE"
LAST="${tag}"
LASTTEXT="${tag}"
done
echo "" >> "$RESULTFILE"
echo "## ${SINCE}" >> "$RESULTFILE"
echo "(omitted)" >> "$RESULTFILE"
echo ""
echo "Done. The result is:"
echo ""
cat ${RESULTFILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment