diff --git a/update-changelog.bash b/update-changelog.bash index c68247a..0a1e26d 100755 --- a/update-changelog.bash +++ b/update-changelog.bash @@ -1,8 +1,9 @@ #!/bin/bash # -# ... +# Automate the process of updating the CHANGELOG.md file, based on the latest commit +# messages from the dotfiles submodule. # -# Version: v1.0.0-rc.1 +# Version: v1.0.0 # License: MIT License # Copyright (c) 2024 Hunter T. (StrangeRanger) # @@ -10,10 +11,22 @@ ####[ Global Variables ]################################################################ -C_CHANGELOG="CHANGELOG.md" +readonly C_CHANGELOG="CHANGELOG.md" +readonly C_TMP_CHANGELOG="CHANGELOG.tmp" +readonly C_SUBMODULE_PATH="submodules/dotfiles" + C_DATE=$(date +%Y-%m-%d) -C_TMP_CHANGELOG="CHANGELOG.tmp" -readonly C_CHANGELOG C_DATE C_TMP_CHANGELOG +readonly C_DATE + +## ANSI color codes. +C_BLUE="$(printf '\033[0;34m')" +C_RED="$(printf '\033[1;31m')" +C_NC="$(printf '\033[0m')" +readonly C_BLUE C_RED C_NC + +## Shorthanded variables for colorized output. +readonly C_ERROR="${C_RED}ERROR:${C_NC} " +readonly C_INFO="${C_BLUE}==>${C_NC} " declare -A sections @@ -21,15 +34,42 @@ declare -A sections ####[ Main ]############################################################################ -## Extract latest commit messages from submodule. +### +### Extract latest commit messages. +### + +echo "${C_INFO}Updating submodule..." git submodule update --remote -cd submodules/dotfiles/ || exit 1 -COMMITS=$(git log $(git rev-parse HEAD@{1})..HEAD --pretty=format:"%s") -cd - || exit 1 -# Prepare new changelog entry. -echo "## $C_DATE" > "$C_TMP_CHANGELOG" -echo "" >> "$C_TMP_CHANGELOG" +cd "$C_SUBMODULE_PATH" || { + echo "${C_ERROR}Failed to change directory to '$C_SUBMODULE_PATH'" + exit 1 +} + +echo "${C_INFO}Fetching latest commits..." +C_COMMITS=$(git log "$(git rev-parse HEAD@"{1}")..HEAD" --pretty=format:"%s") + +if [[ -z $C_COMMITS ]]; then + echo "${C_ERROR}Could not determine previous commit(s)" + exit 1 +fi + +cd - || { + echo "${C_ERROR}Failed to change directory back to project's root directory" + exit 1 +} + +echo "${C_INFO}Prepping new changelog entry..." +{ + echo "## $C_DATE" + echo "" +} > "$C_TMP_CHANGELOG" + +### +### Parse commit messages and append to the appropriate section. +### + +echo "${C_INFO}Parsing commit messages..." while IFS= read -r commit; do skip=false @@ -45,14 +85,10 @@ while IFS= read -r commit; do echo "Info: $info" echo "Message: $message" - # Define keywords to skip based on type. - case "$type" in - Added|Changed|Removed|Fixed) - if [[ "$info" == "(chezmoi)" || "$info" == "(dotfiles)" ]]; then - skip=true - fi - ;; - esac + ## Skip commits that only affect the dotfiles repository. + if [[ "$info" == "(chezmoi)" || "$info" == "(dotfiles)" ]]; then + skip=true + fi fi if [ "$skip" = true ]; then @@ -60,18 +96,24 @@ while IFS= read -r commit; do continue fi - # Append to the appropriate section. + # Append commit to the appropriate section. sections["$type"]+="- $commit"$'\n' echo "---" # Debug separator. -done <<< "$COMMITS" +done <<< "$C_COMMITS" + +### +### Add new entries to the changelog. +### +echo "${C_INFO}Updating the changelog..." for type in "${!sections[@]}"; do - echo "### $type" >> "$C_TMP_CHANGELOG" - echo "" >> "$C_TMP_CHANGELOG" - echo "${sections[$type]}" >> "$C_TMP_CHANGELOG" + { + echo "### $type" + echo "" + echo "${sections[$type]}" + } >> "$C_TMP_CHANGELOG" done -# Insert the new entry under ## Unreleased awk -v new_entry="$(cat $C_TMP_CHANGELOG)" ' /^## Unreleased$/ { print; @@ -82,5 +124,7 @@ awk -v new_entry="$(cat $C_TMP_CHANGELOG)" ' { print } ' "$C_CHANGELOG" > "${C_CHANGELOG}.tmp" -rm "$C_TMP_CHANGELOG" mv "${C_CHANGELOG}.tmp" "$C_CHANGELOG" + +echo "${C_INFO}Cleaning up..." +rm "$C_TMP_CHANGELOG" diff --git a/update-submodule.bash b/update-submodule.bash deleted file mode 100755 index a36baf9..0000000 --- a/update-submodule.bash +++ /dev/null @@ -1,95 +0,0 @@ -#!/usr/bin/env bash -# -# Automate the process of updating the submodule. -# -# Version: v1.0.0 -# License: MIT License -# Copyright (c) 2024 Hunter T. (StrangeRanger) -# -######################################################################################## -####[ Global Variables ]################################################################ - - -readonly C_SUBMODULE_PATH="submodules/dotfiles" - -## ANSI color codes. -C_YELLOW="$(printf '\033[1;33m')" -C_GREEN="$(printf '\033[0;32m')" -C_BLUE="$(printf '\033[0;34m')" -C_CYAN="$(printf '\033[0;36m')" -C_RED="$(printf '\033[1;31m')" -C_NC="$(printf '\033[0m')" -readonly C_YELLOW C_GREEN C_BLUE C_CYAN C_RED C_NC - -## Shorthanded variables for colorized output. -readonly C_WARNING="${C_YELLOW}==>${C_NC} " -readonly C_SUCCESS="${C_GREEN}==>${C_NC} " -readonly C_ERROR="${C_RED}ERROR:${C_NC} " -readonly C_INFO="${C_BLUE}==>${C_NC} " -readonly C_NOTE="${C_CYAN}==>${C_NC} " - - -####[ Functions ]####################################################################### - - -#### -# Check the status of the submodule and perform the necessary actions. -# -# RETURNS: -# - 0: The submodule is up to date. -# - 1: The submodule is out of date. -# - 2: The submodule is not initialized. -# - 3: The submodule has merge conflicts. -submodule_status() { - local git_status - git_status="$(git submodule status "$C_SUBMODULE_PATH")" - git_status=${git_status:0:1} - - case "$git_status" in - +) - echo "${C_NOTE}The submodule at '$C_SUBMODULE_PATH' is out of date" - echo "${C_INFO}Updating submodule..." - git submodule update --remote "$C_SUBMODULE_PATH" || { - echo "${C_ERROR}Failed to update submodule '$C_SUBMODULE_PATH'" - exit 1 - } - return 1 - ;; - -) - echo "${C_WARNING}The submodule at '$C_SUBMODULE_PATH' is not initialized" - echo "${C_INFO}Initializing submodule..." - git submodule update --init "$C_SUBMODULE_PATH" || { - echo "${C_ERROR}Failed to initialize submodule '$C_SUBMODULE_PATH'" - exit 1 - } - return 2 - ;; - U) - echo "${C_ERROR}The submodule at '$C_SUBMODULE_PATH' has merge conflicts" - return 3 - ;; - *) - echo "${C_SUCCESS}The submodule at '$C_SUBMODULE_PATH' is up to date" - return 0 - ;; - esac -} - - -####[ Main ]############################################################################ - - -if [[ ! -d $C_SUBMODULE_PATH ]]; then - echo "${C_ERROR}Submodule '$C_SUBMODULE_PATH' not found" - exit 1 -fi - -while true; do - submodule_status - case $? in - 0) break ;; - 1) continue ;; - 2) continue ;; - 3) exit 1 ;; - esac -done