Repository navigation
Expand file tree
/
Copy pathprepare_release_notes.sh
More file actions
240 lines (222 loc) · 8.84 KB
/
Copy pathprepare_release_notes.sh
File metadata and controls
240 lines (222 loc) · 8.84 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/bin/bash
#********************************************************************
# Script Name: prepare_release_notes.sh
# Description: Prepares a release-preparation notes file "a.md" from
# version.txt and the git history since the last tag, or
# the whole history when the project has never been tagged
# and this is its first release.
#
# Location: llm-shared/scripts/ (mutualized across projects).
#
# Parameters: $1 optional project directory. When omitted, the
# PRJ_DIR environment variable is used, and finally
# the current directory.
#
# Usage: bash <LLM_SHARED_DIR>/scripts/prepare_release_notes.sh
# (run from <PRJ_DIR>, or pass <PRJ_DIR> as $1)
#
# Reads: <PRJ_DIR>/version.txt (first word must be X.Y.Z-SNAPSHOT)
# Writes: <home>/a.md, where <home> is the project's review artifact
# home (.reviews unless .review-artifacts.ini declares another
# home), never the project root (see rules/artifact_files.md).
# The shared tools/artifact_home.py helper resolves the path,
# moving a legacy root a.md into the home once; the final Ok
# line prints the full path written.
#
# Exit codes:
# 1 - project directory not found or not usable, or its artifact home
# cannot be resolved
# 2 - version.txt not found
# 3 - version unreadable from version.txt
# 4 - version.txt version is not a -SNAPSHOT version
# 5 - retired: no git tag is now the first-release case, not an error.
# The number stays reserved so 6 keeps its meaning for callers.
# 6 - last git tag already matches the snapshot version
#********************************************************************
info() { echo "Info: $1"; }
task() { echo "Task: $1"; }
ok() { echo "Ok: $1"; }
warn() { echo "Warn: $1" >&2; }
fatal() { echo "Error: $1" >&2; exit "${2:-1}"; }
LLM_SHARED="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
# Print the artifact-home path of one file of the calling project, through
# the shared tools/artifact_home.py helper (llm-shared venv when present).
artifact_file() {
local root="$1" name="$2" py="" cand
for cand in "${LLM_SHARED}"/venvs/python_3*llm-shared*/Scripts/python.exe \
"${LLM_SHARED}"/venvs/python_3*llm-shared*/bin/python; do
if [[ -x "${cand}" ]]; then py="${cand}"; break; fi
done
[[ -n "${py}" ]] || py="$(command -v python3 || command -v python)"
[[ -n "${py}" ]] || return 1
PYTHONPATH="${LLM_SHARED}" "${py}" -m tools.artifact_home "${root}" "${name}" \
| tr -d '\r'
return "${PIPESTATUS[0]}"
}
# Parse a conventional-commit subject.
# Sets globals: ps_has_colon ps_type ps_scope ps_title
parse_subject() {
local subject="$1" header
if [[ "${subject}" != *:* ]]; then
ps_has_colon=0
ps_type=""
ps_scope=""
ps_title="${subject}"
return
fi
ps_has_colon=1
header="${subject%%:*}"
ps_title="${subject#*:}"
# trim leading whitespace from the title
ps_title="${ps_title#"${ps_title%%[![:space:]]*}"}"
if [[ "${header}" == *"("*")"* ]]; then
ps_type="${header%%(*}"
ps_scope="${header#*(}"
ps_scope="${ps_scope%%)*}"
else
ps_type="${header}"
ps_scope=""
fi
# normalise type: drop a trailing "!" breaking-change marker and spaces
ps_type="${ps_type%!}"
ps_type="${ps_type// /}"
}
main() {
# ===============================================
# RESOLVE PROJECT DIRECTORY
# ===============================================
# Priority: $1 argument, then PRJ_DIR env var, then current directory.
local prj="${1:-${PRJ_DIR:-$PWD}}"
[[ -d "${prj}" ]] || fatal "Project directory '${prj}' not found" 1
cd "${prj}" || fatal "Unable to enter project directory '${prj}'" 1
PRJ_DIR="$(pwd)"
local version_file="${PRJ_DIR}/version.txt"
local out_file
out_file="$(artifact_file "${PRJ_DIR}" a.md)" \
|| fatal "Cannot resolve the artifact home of '${PRJ_DIR}'" 1
info "Project directory '${PRJ_DIR}'"
[[ -f "${version_file}" ]] || fatal "version.txt not found at '${version_file}'" 2
# ===============================================
# SNAPSHOT VERSION FROM version.txt
# ===============================================
local snapshot_version
snapshot_version="$(head -n 1 "${version_file}" | sed 's/^[#[:space:]]*//' | awk '{print $1}')"
[[ -n "${snapshot_version}" ]] || fatal "Unable to read version from '${version_file}'" 3
if [[ "${snapshot_version}" != *-SNAPSHOT ]]; then
fatal "version.txt version '${snapshot_version}' is not a -SNAPSHOT version" 4
fi
local xyz="${snapshot_version%-SNAPSHOT}"
info "Snapshot version '${snapshot_version}', release version 'v${xyz}'"
# ===============================================
# LAST GIT TAG
# ===============================================
# The last tag is the lower bound of the release range. A project cutting
# its first release has none, and that is not an error: there is simply no
# earlier release to diff against, so the range is the whole history.
local raw_tag range range_label
raw_tag="$(git -C "${PRJ_DIR}" describe --tags --abbrev=0 2>/dev/null)"
if [[ -n "${raw_tag}" ]]; then
local tag_version="${raw_tag#v}"
info "Last git tag '${raw_tag}' (version '${tag_version}')"
if [[ "${tag_version}" == "${xyz}" ]]; then
fatal "Last git tag '${raw_tag}' already matches version '${xyz}': release notes already prepared" 6
fi
# `A..HEAD` excludes A itself, which is right: A is the previous release.
range="${raw_tag}..HEAD"
range_label="${raw_tag}"
else
info "No git tag found: first release, reading the whole history"
# `HEAD` alone, not `<root>..HEAD`: the root commit belongs to a first
# release, and a two-dot range would leave it out.
range="HEAD"
range_label="the first commit"
fi
# ===============================================
# COLLECT TITLES SINCE LAST TAG (oldest -> newest)
# ===============================================
task "Collecting commit titles since '${range_label}'"
local -a type_order=() # type group names, in first-appearance order
local -a title_type=() # parallel: type of each kept title
local -a title_line=() # parallel: formatted "- type(scope): title" line
local subject k known
while IFS= read -r subject; do
[[ -z "${subject}" ]] && continue
parse_subject "${subject}"
[[ "${ps_has_colon}" -eq 1 ]] || continue
[[ "${ps_type}" == "chore" ]] && continue
[[ -z "${ps_type}" ]] && continue
if [[ -n "${ps_scope}" ]]; then
title_line+=("- ${ps_type}(${ps_scope}): ${ps_title}")
else
title_line+=("- ${ps_type}: ${ps_title}")
fi
title_type+=("${ps_type}")
known=0
for k in "${type_order[@]}"; do
[[ "${k}" == "${ps_type}" ]] && known=1 && break
done
[[ ${known} -eq 0 ]] && type_order+=("${ps_type}")
done < <(git -C "${PRJ_DIR}" log --reverse --format='%s' "${range}")
if [[ ${#title_line[@]} -eq 0 ]]; then
warn "No conventional-commit titles found since '${range_label}' (chore commits excluded)"
else
info "Collected ${#title_line[@]} title(s) across ${#type_order[@]} type group(s)"
fi
# ===============================================
# WRITE a.md
# ===============================================
task "Writing release-preparation notes to '${out_file}'"
{
echo ""
echo "# v${xyz} Release Preparation Notes"
echo ""
sed 's/\r$//' "${version_file}"
echo ""
echo "## v${xyz} changelog"
echo ""
local ty i
for ty in "${type_order[@]}"; do
echo "${ty}"
echo ""
for i in "${!title_type[@]}"; do
[[ "${title_type[$i]}" == "${ty}" ]] && echo "${title_line[$i]}"
done
echo ""
done
echo "## v${xyz} commit list"
echo ""
echo "List of commit messages (type, scope, title and body, from oldest after last tag to newest HEAD)"
echo ""
local record body line
while IFS= read -r -d '' record; do
subject="${record%%$'\n'*}"
if [[ "${record}" == *$'\n'* ]]; then
body="${record#*$'\n'}"
else
body=""
fi
# strip CR and trailing blank lines from the body
body="${body//$'\r'/}"
body="${body%"${body##*[![:space:]]}"}"
parse_subject "${subject}"
if [[ "${ps_has_colon}" -eq 1 && -n "${ps_scope}" ]]; then
echo "- ${ps_type}(${ps_scope}): ${ps_title}"
elif [[ "${ps_has_colon}" -eq 1 ]]; then
echo "- ${ps_type}: ${ps_title}"
else
echo "- ${subject}"
fi
if [[ -n "${body}" ]]; then
while IFS= read -r line; do
if [[ -n "${line}" ]]; then
echo " ${line}"
else
echo ""
fi
done <<< "${body}"
fi
done < <(git -C "${PRJ_DIR}" log --reverse -z --format='%s%n%b' "${range}")
} > "${out_file}"
ok "Release-preparation notes written to '${out_file}'"
}
main "$@"