-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-changelog.bash
executable file
·150 lines (116 loc) · 3.71 KB
/
update-changelog.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
#
# Automate the process of updating the CHANGELOG.md file, based on the latest commit
# messages from the dotfiles submodule.
#
# Version: v1.1.2
# License: MIT License
# Copyright (c) 2024-2025 Hunter T. (StrangeRanger)
#
########################################################################################
####[ Global Variables ]################################################################
readonly C_REF_BRANCH="origin/main"
readonly C_CHANGELOG="CHANGELOG.md"
readonly C_TMP_CHANGELOG="CHANGELOG.tmp"
readonly C_SUBMODULE_PATH="submodules/dotfiles"
C_REF_BRANCH_COMMIT=$(git rev-parse "$C_REF_BRANCH":submodules/dotfiles)
C_DATE=$(date +%Y-%m-%d)
readonly C_REF_BRANCH_COMMIT 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 ]############################################################################
echo "${C_INFO}Initializing submodule..."
git submodule update --init "$C_SUBMODULE_PATH"
###
### Checkout the latest commit of the submodule 'dotfiles' in the reference branch.
###
echo "${C_INFO}Checking out the latest commit of the submodule 'dotfiles' in the" \
"reference branch..."
git -C "$C_SUBMODULE_PATH" checkout "$C_REF_BRANCH_COMMIT"
###
### Extract latest commit messages.
###
echo "${C_INFO}Updating the submodule..."
git submodule update --remote "$C_SUBMODULE_PATH"
cd "$C_SUBMODULE_PATH" || {
echo "${C_ERROR}Failed to change directory to the dotfiles submodule"
exit 1
}
echo "${C_INFO}Fetching latest commits in current branch..."
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
# Regex to capture type, optional info, and optional message.
if [[ $commit =~ ^(added|changed|removed|fixed|other)(\([^\)]*\))?:\ (.+)$ ]]; then
type="${BASH_REMATCH[1]^}"
info="${BASH_REMATCH[2]}"
message="${BASH_REMATCH[3]}"
## Debugging output.
echo "Type: $type"
echo "Info: $info"
echo "Message: $message"
## Skip commits that only affect the dotfiles repository.
if [[ $info == "(chezmoi)" || $info == "(dotfiles)" ]]; then
skip=true
fi
else
echo "Commit Message: '$commit'"
skip=true
fi
if [[ $skip == true ]]; then
echo "Add to CHANGELOG: false"
echo "---" # Debug separator.
continue
fi
echo "Add to CHANGELOG: true"
# Append commit to the appropriate section.
sections["$type"]+="- $commit"$'\n'
echo "---" # Debug separator.
done <<< "$C_COMMITS"
###
### Add new entries to the changelog.
###
echo "${C_INFO}Updating the changelog..."
for type in "${!sections[@]}"; do
{
echo "### $type"
echo ""
echo "${sections[$type]}"
} >> "$C_TMP_CHANGELOG"
done
awk -v new_entry="$(cat $C_TMP_CHANGELOG)" '
/^## Unreleased$/ {
print;
print "";
print new_entry;
next
}
{ print }
' "$C_CHANGELOG" > "${C_CHANGELOG}.tmp"
mv "${C_CHANGELOG}.tmp" "$C_CHANGELOG"
echo "${C_INFO}Cleaning up..."
rm "$C_TMP_CHANGELOG"