-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathucl
More file actions
executable file
·204 lines (164 loc) · 5.47 KB
/
Copy pathucl
File metadata and controls
executable file
·204 lines (164 loc) · 5.47 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
#!/usr/bin/env bash
# Environment setup
# -----------------------------------------------------------------------------
set -o pipefail
[[ ${DEBUG-} ]] && set -o xtrace
SCRIPT_DIR="$(cd "${BASH_SOURCE[0]%/*}" || exit 1; pwd)"
[[ ":${PATH}:" != *:"${SCRIPT_DIR}":* ]] && export PATH="${SCRIPT_DIR}:${PATH}"
source "${SCRIPT_DIR}/../../bash_modules/terminal.sh"
source "${SCRIPT_DIR}/../../bash_modules/user-input.sh"
[[ -z ${BASH_MODULES_DIR-} ]] && echo "ERROR: terminal.sh module missing" && exit 1
function print_usage() {
cat <<EOF
Usage: $(basename "$0") [special-instructions]
Update CHANGELOG.md using AI based on current branch changes.
Dependencies:
git Git version control
agy AI CLI tool
Arguments:
special-instructions Optional special instructions for the AI prompt
This script will:
1. Determine the default branch (main or master)
2. Get the current branch name
3. Generate a diff between default and current branch
4. Read or create a CHANGELOG.md file
5. Use AI to update the changelog based on the diff
EOF
}
if [[ "${1}" == "-h" || "${1}" == "--help" ]]; then
print_usage
exit 0
fi
if [[ $# -eq 0 ]]; then
special_instructions="None"
else
special_instructions="${*}"
fi
function ctrlc_trap() {
log_newline
log_warning "Script interrupted. Exiting."
exit 130
}
trap ctrlc_trap SIGINT
# Title and Dependency Checks
# -----------------------------------------------------------------------------
log_title "[u]pdate [c]hange [l]og"
# dependencies=(git claude)
dependencies=(git agy)
for cmd in "${dependencies[@]}"; do
if ! command -v "${cmd}" >/dev/null; then
log_error "ERROR: Missing dependency - '${cmd}'"
exit 1
fi
done
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
log_error "ERROR: Not inside a git repository"
exit 1
fi
# Get branch information
# -----------------------------------------------------------------------------
if git show-ref --quiet refs/heads/main; then
default_branch="main"
elif git show-ref --quiet refs/heads/master; then
default_branch="master"
else
log_error "ERROR: Could not determine default branch (neither main nor master found)"
exit 1
fi
log_message " Default branch: ${default_branch}"
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [[ $? -ne 0 ]]; then
log_error "ERROR: Failed to determine current branch"
exit 1
fi
log_message " Current branch: ${current_branch}"
if [[ "${current_branch}" == "${default_branch}" ]]; then
log_error "ERROR: Current branch is the default branch. Please switch to a feature branch."
exit 1
fi
log_message " Special Instructions: '${special_instructions}'"
# Generate diff between default and current branch
# -----------------------------------------------------------------------------
log_message "Generating diff between ${default_branch} and ${current_branch}..."
branch_diff=$(git diff "${default_branch}...${current_branch}")
if [[ $? -ne 0 ]]; then
log_error "ERROR: Failed to generate diff between branches"
exit 1
fi
if [[ -z "${branch_diff}" ]]; then
log_warning "No differences found between ${default_branch} and ${current_branch}"
exit 0
fi
# Handle CHANGELOG.md
# -----------------------------------------------------------------------------
changelog_path="${PWD}/CHANGELOG.md"
if [[ ! -f "${changelog_path}" ]]; then
log_message "Creating new CHANGELOG.md..."
if ! touch "${changelog_path}"; then
log_error "ERROR: Failed to create CHANGELOG.md"
exit 1
fi
fi
current_changelog=$(cat "${changelog_path}" 2>/dev/null)
# Update Changelog using AI
# -----------------------------------------------------------------------------
log_message "Generating updated changelog..."
# Build special instructions section if provided
special_instructions_section=""
if [[ "${special_instructions}" != "None" ]]; then
special_instructions_section="$(cat <<EOF
## Special Instructions
_If the following instructions tell you to only include a specific directory, ensure you only add information to the CHANGELOG for the given directory._
${special_instructions}
EOF
)"
fi
# Generate prompt using heredoc
prompt="$(cat <<EOF
$(cat "${SCRIPT_DIR}/ucl-prompt.md")${special_instructions_section}
## Information
Current date: $(date +%Y-%m-%d)
### Existing CHANGELOG.md
Note: If the change log is empty, you will need to start a new one.
\`\`\`md
${current_changelog}
\`\`\`
### Git Diff
\`\`\`txt
${branch_diff}
\`\`\`
EOF
)"
# if ! updated_changelog=$(claude --model haiku --print "${prompt}"); then
if ! updated_changelog=$(agy --print "${prompt}"); then
log_error "ERROR: AI chat command failed"
exit 1
fi
if [[ -z "${updated_changelog}" ]]; then
log_error "ERROR: Generated changelog is empty"
exit 1
fi
log_newline
preview_lines=30
echo "${updated_changelog}" | head -n ${preview_lines}
if [[ "$(echo "${updated_changelog}" | wc -l)" -gt ${preview_lines} ]]; then
log_message "... (showing only the first ${preview_lines} lines)"
fi
log_newline
log_line
if [[ "${updated_changelog}" == "${current_changelog}" ]]; then
log_warning "No changes to add to the changelog. No update needed."
exit 0
fi
if ! press_enter_to_continue "Press Enter to save the updated CHANGELOG.md, any other key to abort."; then
log_warning "Update aborted"
exit 1
fi
log_message "Saving updated CHANGELOG.md..."
if ! echo "${updated_changelog}" > "${changelog_path}"; then
log_error "ERROR: Failed to save CHANGELOG.md"
exit 1
fi
GIT_PAGER='' git diff "${changelog_path}"
log_success "CHANGELOG.md has been successfully updated!"
log_line