Bash Script to Generate a Changelog from Git Commits Between Two Specific Tags

Bash script to generate a changelog from git commits between two tags, with error handling and formatted output for release notes.

Brief Introduction

Bash Script to Generate a Changelog from Git Commits Between Two Specific Tags (bash)
DESCRIPTION:
This Bash script automatically generates a changelog by reading commit messages between two git tags specified by the user. The script is robust, handles common errors, and produces formatted output ready to be used in documentation or release notes.

ADDITIONAL NOTES:
The script must be run inside a git repository directory. If the tags do not exist or are reversed, the script reports an error. The output is a simple changelog based on commit messages.

EXPLANATIONS:
- The 'check_tag' function verifies that the provided tags exist in the git repository to avoid execution errors.
- The 'git log' command with the '--pretty=format:%h %s' option extracts the abbreviated hash and commit message for a compact view.
- The order of tags is checked to ensure the starting tag precedes the ending tag.
- The output is formatted with clear headers to facilitate reading and integration into release documents.

Example Code: bash

#!/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 0
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Git: 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.

Similar Topics

Script to batch rename files in a folder
Bash script to batch rename files in a folder by adding a custom prefix and sequential numbers, ideal for organizing files efficiently.
Script to download files with curl and check the result
Bash script to download files using curl, verify success, and handle errors with clear messages for reliable command-line file transfers.
Batch Image Converter with ImageMagick from bash
Batch Image Converter bash script using ImageMagick to resize and convert images in bulk, supporting multiple formats and maintaining proportions.
Bash Script to Search Text in Multiple Files and Show Context
Bash script to search text in multiple files with context lines, enabling quick and recursive searches in specified directories using grep.
Script to compare two folders and show file differences
Bash script to compare two folders, highlighting unique and modified files using standard Unix commands like diff, find, sort, and comm.
Simple contact book saved in CSV file managed by bash
Bash script for managing a simple contact book saved in a CSV file, allowing adding, searching, displaying, and deleting contacts via a text menu.
Multiple downloader reading a list of URLs from file
Simple bash script to sequentially download multiple files from URLs listed in a text file using wget, automating batch downloads efficiently.
Script to rename photos based on modification date
Bash script to rename photos by modification date, adding counters to avoid duplicates, for easy chronological photo organization on Linux or macOS.