#!/usr/bin/env bash get_tag() { local rev="$1" local lastTag="$2" local tag='' IFS='-' read -ra tag <<< "$(git describe --tags "${rev}")" if [[ -z "${tag[1]}" ]]; then echo -n "${tag[0]}" else echo -n "${lastTag}" fi } get_tag_date() { local tag="$1" local date='' date="$(git log -1 --pretty=tformat:%ad --date=short "${tag}")" echo -n "${date}" } check_git_tag() { local rev="$1" if git describe --tags "${rev}" &> /dev/null; then local tag='' tag="$(get_tag "${rev}" "${lastTag}")" # echo "Last Tag: ${lastTag} Current: ${tag}" if [[ "${lastTag}" != "${tag}" ]]; then local date='' date="$(get_tag_date "${tag}")" echo '' # Add a blank line echo "## [${tag}] - ${date}" lastTag="${tag}" fi fi } get_changelog_notes() { local rev="$1" if git notes --ref=changelog list "${rev}" &> /dev/null; then local line='' line="$(git notes --ref=changelog show "${rev}")" echo "- ${line}" fi } get_changelog_notes_by_section() { local rev="$1" local section="$2" if git notes --ref=changelog list "${rev}" &> /dev/null; then local line='' line="$(git notes --ref=changelog show "${rev}")" if [[ "${line}" =~ ^${section} ]]; then #echo "${rev} - ${line}" echo "- ${line}" fi fi } generate_header() { echo "# Changelog" echo 'All notable changes to this project will be documented in this file.' echo "" # Blank line intentional echo 'The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)' printf '\n## %s\n' "[Unreleased]" } main() { local lastTag='' generate_header sections=('\[Added\]' '\[Changed\]' '\[Deprecated\]' '\[Removed\]' '\[Fixed\]' '\[Security\]') for rev in $(git rev-list HEAD); do for section in "${sections[@]}"; do check_git_tag "${rev}" get_changelog_notes_by_section "${rev}" "${section}" done done exit 0 } main "$@"