-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-release-note.sh
More file actions
executable file
·327 lines (277 loc) · 9.91 KB
/
generate-release-note.sh
File metadata and controls
executable file
·327 lines (277 loc) · 9.91 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PS1_SCRIPT="$SCRIPT_DIR/generate-release-note.ps1"
SCRIPT_NAME="$(basename "$0")"
usage() {
cat >&2 <<EOF
Usage: $SCRIPT_NAME [HASH1] [HASH2] [-o OUTPUT_FILE] [-a AGENT] [-m MAX_DIFF_LENGTH]
Generate release notes from git changes:
- If two hashes given: generate notes for changes between HASH1 and HASH2
- If one hash given: generate notes for all changes from beginning to HASH1
- If no hash given: generate notes for all commits in the repository
Examples:
$SCRIPT_NAME # All commits
$SCRIPT_NAME abc1234 # From beginning to abc1234
$SCRIPT_NAME abc1234 def5678 # Between abc1234 and def5678
$SCRIPT_NAME abc1234 def5678 -o release.md # Save to file
$SCRIPT_NAME v1.0.0 v2.0.0 -a copilot # Between tags
EOF
}
if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
usage
exit 0
fi
# Detect platform - use native bash on macOS/Linux, PowerShell on Windows
USE_NATIVE=0
case "$(uname -s)" in
Darwin|Linux)
USE_NATIVE=1
;;
CYGWIN*|MINGW*|MSYS*)
USE_NATIVE=0
;;
esac
HASH1=""
HASH2=""
OUTPUT_FILE=""
AGENT_CMD="${AGENT:-agent}"
MAX_DIFF_LENGTH=20000
# Parse positional hash arguments
if [ $# -gt 0 ] && [ "${1#-}" = "$1" ]; then
HASH1="$1"
shift
fi
if [ $# -gt 0 ] && [ "${1#-}" = "$1" ]; then
HASH2="$1"
shift
fi
while [ $# -gt 0 ]; do
case "$1" in
-o) OUTPUT_FILE="$2"; shift 2 ;;
-a) AGENT_CMD="$2"; shift 2 ;;
-m) MAX_DIFF_LENGTH="$2"; shift 2 ;;
*) echo "Unknown option: $1" >&2; usage; exit 1 ;;
esac
done
# ============================================================================
# NATIVE BASH IMPLEMENTATION (macOS/Linux)
# ============================================================================
if [ $USE_NATIVE -eq 1 ]; then
# --- Validate prerequisites ---
if ! command -v git >/dev/null 2>&1; then
echo "Error: git is not installed or not in PATH." >&2
exit 127
fi
if ! command -v "$AGENT_CMD" >/dev/null 2>&1; then
echo "Error: '$AGENT_CMD' command not found in PATH." >&2
exit 127
fi
# --- Ensure we're in a git repo ---
if ! git rev-parse --show-toplevel >/dev/null 2>&1; then
echo "Error: Not inside a git repository." >&2
exit 1
fi
# --- Resolve range mode ---
if [ -n "$HASH1" ] && [ -n "$HASH2" ]; then
# Both hashes provided
if ! RESOLVED1=$(git rev-parse --verify "$HASH1" 2>&1); then
echo "Error: Invalid commit reference: $HASH1" >&2
exit 1
fi
if ! RESOLVED2=$(git rev-parse --verify "$HASH2" 2>&1); then
echo "Error: Invalid commit reference: $HASH2" >&2
exit 1
fi
RANGE="$HASH1..$HASH2"
SOURCE_LABEL="changes from ${RESOLVED1:0:8} to ${RESOLVED2:0:8}"
elif [ -n "$HASH1" ]; then
# Only one hash provided - from beginning to this hash
if ! RESOLVED1=$(git rev-parse --verify "$HASH1" 2>&1); then
echo "Error: Invalid commit reference: $HASH1" >&2
exit 1
fi
FIRST_COMMIT=$(git rev-list --max-parents=0 HEAD 2>/dev/null | head -1)
if [ -z "$FIRST_COMMIT" ]; then
echo "Error: No commits found in repository" >&2
exit 1
fi
RANGE="$FIRST_COMMIT..$HASH1"
SOURCE_LABEL="changes from beginning to ${RESOLVED1:0:8}"
else
# No hash provided - all commits
RANGE=""
SOURCE_LABEL="all commits in repository"
fi
# --- Gather context ---
echo "Collecting $SOURCE_LABEL ..." >&2
if [ -n "$RANGE" ]; then
COMMIT_LOG=$(git log --oneline "$RANGE" 2>/dev/null || echo "(no commits in range)")
STAT=$(git diff "$RANGE" --stat 2>/dev/null || echo "(no changes)")
DIFF=$(git diff "$RANGE" 2>/dev/null || echo "(no diff available)")
FILE_LIST=$(git diff "$RANGE" --name-status 2>/dev/null | while IFS=$'\t' read -r status file; do
case "$status" in
A) echo "added: $file" ;;
M) echo "modified: $file" ;;
D) echo "deleted: $file" ;;
R*) echo "renamed: $file" ;;
C*) echo "copied: $file" ;;
*) echo "$status: $file" ;;
esac
done)
else
COMMIT_LOG=$(git log --oneline 2>/dev/null || echo "(no commits yet)")
FIRST_COMMIT=$(git rev-list --max-parents=0 HEAD 2>/dev/null | head -1)
if [ -n "$FIRST_COMMIT" ]; then
STAT=$(git diff --stat "$FIRST_COMMIT" HEAD 2>/dev/null || echo "(no changes)")
DIFF=$(git diff "$FIRST_COMMIT" HEAD 2>/dev/null || echo "(no diff available)")
FILE_LIST=$(git diff "$FIRST_COMMIT" HEAD --name-status 2>/dev/null | while IFS=$'\t' read -r status file; do
case "$status" in
A) echo "added: $file" ;;
M) echo "modified: $file" ;;
D) echo "deleted: $file" ;;
R*) echo "renamed: $file" ;;
C*) echo "copied: $file" ;;
*) echo "$status: $file" ;;
esac
done)
else
STAT="(no commits)"
DIFF=""
FILE_LIST=""
fi
fi
BRANCH=$(git branch --show-current 2>/dev/null || echo "(detached HEAD)")
REPO_NAME=$(basename "$(git rev-parse --show-toplevel)")
# Truncate diff if needed
if [ ${#DIFF} -gt $MAX_DIFF_LENGTH ]; then
DIFF="${DIFF:0:$MAX_DIFF_LENGTH}"$'\n'"... [diff truncated at $MAX_DIFF_LENGTH chars] ..."
fi
# --- Build prompt ---
PROMPT=$(cat <<EOF
Generate release notes from the following git repository changes.
Format the output as markdown with the following structure:
# Release Title (e.g., "v2.0.0 - Major Performance Update")
## Summary
A brief overview paragraph of the key changes and improvements.
## New Features
- Feature 1
- Feature 2
## Bug Fixes
- Fix 1
- Fix 2
## Improvements
- Improvement 1
- Improvement 2
## Breaking Changes (if any)
- Breaking change 1
## Technical Details (optional)
Additional technical information if relevant.
Rules:
- Include a descriptive release title as H1 (suggest version number if tags are present, or a descriptive name)
- Be concise and user-friendly
- Group related changes together
- Highlight breaking changes prominently
- Use clear, descriptive bullet points
- Focus on user-facing changes, not implementation details
- Output ONLY the release notes in markdown format, nothing else — no explanation, no quotes, no markdown fences
# Context
- Repository: $REPO_NAME
- Branch: $BRANCH
- Source: $SOURCE_LABEL
## Commit Log
$COMMIT_LOG
## Changed Files
$FILE_LIST
## Diff Summary
$STAT
## Full Diff
$DIFF
EOF
)
# --- Call agent ---
echo "Generating release notes via $AGENT_CMD ..." >&2
echo "" >&2
# Handle different agent command formats
if [[ "$AGENT_CMD" == *"copilot"* ]]; then
if ! RAW_OUTPUT=$("$AGENT_CMD" -p "$PROMPT" 2>&1); then
echo "Error: Agent call failed." >&2
exit 1
fi
# Clean copilot output: remove stats, tool execution output, and headers
MESSAGE=$(echo "$RAW_OUTPUT" | awk '
BEGIN { in_message = 0; message = ""; }
# Skip usage stats and tool execution lines
/^Total usage est:|^API time spent:|^Total session time:|^Total code changes:|^Breakdown by AI model:|^ claude-|^ gpt-|^●|^ \$|^ └/ { next; }
# Skip empty lines before the message starts
/^[[:space:]]*$/ && in_message == 0 { next; }
# Once we hit content (markdown heading), start collecting
/^#/ { in_message = 1; }
in_message == 1 {
if (message != "") message = message "\n";
message = message $0;
}
END { print message; }
')
else
# Default format for agent.cmd and similar
if ! MESSAGE=$(echo "$PROMPT" | "$AGENT_CMD" -p --trust); then
echo "Error: Agent call failed." >&2
exit 1
fi
fi
# --- Output ---
echo "--- Release Notes ---" >&2
echo "" >&2
echo "$MESSAGE" >&2
if [ -n "$OUTPUT_FILE" ]; then
echo "$MESSAGE" > "$OUTPUT_FILE"
echo "" >&2
echo "Saved to $OUTPUT_FILE" >&2
else
# Try to copy to clipboard (macOS/Linux)
if command -v pbcopy >/dev/null 2>&1; then
echo "$MESSAGE" | pbcopy
echo "" >&2
echo "Copied to clipboard." >&2
elif command -v xclip >/dev/null 2>&1; then
echo "$MESSAGE" | xclip -selection clipboard
echo "" >&2
echo "Copied to clipboard." >&2
fi
fi
exit 0
fi
# ============================================================================
# POWERSHELL FALLBACK (Windows)
# ============================================================================
if [ ! -f "$PS1_SCRIPT" ]; then
echo "Error: $PS1_SCRIPT not found." >&2
exit 1
fi
# Convert Cygwin/MSYS paths to Windows paths for PowerShell
to_win_path() {
if command -v cygpath >/dev/null 2>&1; then
cygpath -w "$1"
else
echo "$1"
fi
}
POWERSHELL=""
for cmd in pwsh powershell.exe powershell; do
if command -v "$cmd" >/dev/null 2>&1; then
POWERSHELL="$cmd"
break
fi
done
if [ -z "$POWERSHELL" ]; then
echo "Error: PowerShell not found (tried pwsh, powershell.exe, powershell)." >&2
exit 1
fi
ARGS=(-ExecutionPolicy Bypass -File "$(to_win_path "$PS1_SCRIPT")")
[ -n "$HASH1" ] && ARGS+=("$HASH1")
[ -n "$HASH2" ] && ARGS+=("$HASH2")
[ -n "$OUTPUT_FILE" ] && ARGS+=(-OutputFile "$(to_win_path "$OUTPUT_FILE")")
[ -n "$AGENT_CMD" ] && [ "$AGENT_CMD" != "agent" ] && ARGS+=(-Agent "$AGENT_CMD")
[ -n "$MAX_DIFF_LENGTH" ] && ARGS+=(-MaxDiffLength "$MAX_DIFF_LENGTH")
"$POWERSHELL" "${ARGS[@]}"