Bash script to generate a changelog from git commits between two tags, with error handling and formatted output for release notes.
#!/bin/bash
# Script to generate a changelog from git commits between two specific tags
# Function to show correct script usage
show_usage() {
echo "Usage: $0 <start_tag> <end_tag>"
echo "Example: $0 v1.0.0 v1.1.0"
exit 1
}
# Function to check if a tag exists in the git repository
check_tag() {
local tag=$1
if ! git rev-parse --verify "$tag" >/dev/null 2>&1; then
echo "Error: the tag '$tag' does not exist in the git repository." >&2
exit 2
fi
}
# Check that git is installed
if ! command -v git >/dev/null 2>&1; then
echo "Error: git is not installed or not in PATH." >&2
exit 3
fi
# Check number of arguments
if [ "$#" -ne 2 ]; then
show_usage
fi
START_TAG=$1
END_TAG=$2
# Check that tags exist
check_tag "$START_TAG"
check_tag "$END_TAG"
# Check tag order (START_TAG must be ancestor of END_TAG)
# Get commit hashes of tags
HASH_START=$(git rev-list -n 1 "$START_TAG")
HASH_END=$(git rev-list -n 1 "$END_TAG")
# Verify if START_TAG is ancestor of END_TAG
if ! git merge-base --is-ancestor "$HASH_START" "$HASH_END"; then
echo "Error: the start tag '$START_TAG' is not an ancestor of the end tag '$END_TAG'." >&2
exit 4
fi
# Generate changelog
echo "Changelog between tags '$START_TAG' and '$END_TAG':"
git log --pretty=format:"- %h %s" "$START_TAG".."$END_TAG" | tac
# tac reverses the order to show from oldest to newest
exit 0Git: required to access commit history and tags.
Bash: script interpreter, compatible with Unix-like systems.
User input: two git tags (e.g., v1.0.0 and v1.1.0) to define the commit range.
Error checking: verifies that tags exist, git is installed, and the range is valid.
Output: prints the changelog with commits ordered from newest to oldest.
Git configuration: the script assumes the git repository is in the current or specified directory.
Software dependencies: git installed and accessible from the command line.