Skip to content

Commit

Permalink
Merge pull request #104 from StrangeRanger/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
StrangeRanger authored Dec 21, 2024
2 parents cb174be + 2c2db39 commit 303b80b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 122 deletions.
98 changes: 71 additions & 27 deletions update-changelog.bash
Original file line number Diff line number Diff line change
@@ -1,35 +1,75 @@
#!/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)
#
########################################################################################
####[ 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


####[ 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
Expand All @@ -45,33 +85,35 @@ 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
echo "---" # Debug separator.
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;
Expand All @@ -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"
95 changes: 0 additions & 95 deletions update-submodule.bash

This file was deleted.

0 comments on commit 303b80b

Please sign in to comment.