-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvcommit
More file actions
executable file
·434 lines (387 loc) · 15.3 KB
/
convcommit
File metadata and controls
executable file
·434 lines (387 loc) · 15.3 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/env bash
# @BP010: Release metadata
# @package: convcommit
# @build_type: bin
# @build_with: Mush v0.2.0 (2026-02-06 develop)
# @build_date: 2026-03-03T15:09:41Z
set -e
use() { return 0; }
extern() { return 0; }
legacy() { return 0; }
module() { return 0; }
public() { return 0; }
embed() { return 0; }
inject() { return 0; }
## BP004: Compile the entrypoint
module color
module init
module selector
usage() {
echo "Usage: convcommit [OPTIONS]"
echo ""
echo "Options:"
echo " -t, --type <type> Commit type (bypasses interactive selector)"
echo " -s, --scope <scope> Commit scope (bypasses interactive selector)"
echo " -m, --message <msg> Commit message (bypasses interactive selector)"
echo " -A, --add <file> Stage a specific file (repeatable)"
echo " -a, --all Stage all changes (git add .) before committing"
echo " -p, --push Push to remote after committing"
echo " --reset Regenerate .convcommit with latest defaults"
echo " --no-color Disable colored output"
echo " -h, --help Print this help and exit"
echo ""
echo "Non-interactive (pipe) usage:"
echo " printf 'F\\n\\nmy message\\n' | convcommit"
echo ""
echo "Direct flags usage:"
echo " convcommit --type fix --scope auth --message 'fix null pointer'"
echo " convcommit -t feat -s api -m 'add endpoint' -a -p"
echo " convcommit --add src/foo.sh --add README.md -t docs -m 'update docs' -p"
}
main() {
local commit_all
local push
local message
local commit_message
local commit_type
local commit_scope
local convcommit_file
local direct_type
local direct_scope
local direct_message
local add_files
while [ $# -gt 0 ]; do
case "$1" in
-*)
case "$1" in
-h|--help)
usage; exit 0 ;;
-V|--version)
echo "convcommit 0.1.0"; exit 0 ;;
-a|--all)
commit_all=true
;;
-p|--push)
push=true
;;
-A|--add)
add_files="${add_files} $2"; shift ;;
-t|--type)
direct_type="$2"; shift ;;
-s|--scope)
direct_scope="$2"; shift ;;
-m|--message)
direct_message="$2"; shift ;;
--reset)
rm -f ".convcommit"
;;
--no-color)
CONVCOMMIT_NO_COLOR=1
;;
*)
echo "Unknown option: $1" >&2
exit 1
;;
esac
;;
*)
break
;;
esac
shift
done || true
convcommit_color_init
# Pre-flight checks — only in interactive/commit mode (stdout is a TTY).
# Skipped when stdout is captured (msg=$(convcommit ...)) to allow message injection.
if [ -t 1 ]; then
if [ -n "$commit_all" ] || [ -n "$add_files" ]; then
if [ -z "$(git status --porcelain 2>/dev/null)" ]; then
printf "$(cc_red)error:$(cc_reset) nothing to commit, working tree clean\n" >&2
exit 1
fi
fi
if [ -n "$push" ]; then
if ! git remote | grep -q '.'; then
printf "$(cc_red)error:$(cc_reset) no remote configured\n" >&2
exit 1
fi
local behind
behind=$(git rev-list --count HEAD..@{u} 2>/dev/null || echo "0")
if [ "$behind" -gt 0 ]; then
printf "$(cc_red)error:$(cc_reset) branch is behind remote by $(cc_yellow)%s$(cc_reset) commit(s), run '$(cc_blue)git pull$(cc_reset)' first\n" "$behind" >&2
exit 1
fi
fi
fi
convcommit_file=".convcommit"
convcommit_init_file "${convcommit_file}"
commit_type=${direct_type:-$(convcommit_selector "$convcommit_file" "type" 4 10)}
commit_scope=${direct_scope:-$(convcommit_selector "$convcommit_file" "scope")}
commit_message=${direct_message:-$(convcommit_selector "$convcommit_file" "message")}
if [ -n "${commit_scope}" ]; then
message="${commit_type}(${commit_scope}): ${commit_message}"
else
message="${commit_type}: ${commit_message}"
fi
if [ -n "$add_files" ]; then
# shellcheck disable=SC2086
git add $add_files
git commit -m "${message}" && true
elif [ -n "$commit_all" ]; then
git add .
git commit -am "${message}" && true
else
echo "${message}"
fi
if [ -n "$push" ]; then
git config credential.helper 'cache --timeout=3600'
git push
fi
}
# Respects https://no-color.org and --no-color flag.
# Call convcommit_color_init once at startup (after arg parsing).
# Then use cc_* functions to get ANSI sequences; they return empty
# strings when color is disabled, so callers need no conditionals.
convcommit_color_init() {
if [ -n "${NO_COLOR:-}" ] || [ "${TERM:-}" = "dumb" ] || [ "${CONVCOMMIT_NO_COLOR:-0}" = "1" ]; then
CONVCOMMIT_COLOR=0
else
CONVCOMMIT_COLOR=1
fi
}
_cc() { [ "${CONVCOMMIT_COLOR:-1}" = "1" ] && printf '%b' "$1"; }
cc_reset() { _cc '\033[0m'; }
cc_bold() { _cc '\033[1m'; }
cc_dim() { _cc '\033[2m'; }
cc_green() { _cc '\033[38;2;166;227;161m'; } # #a6e3a1
cc_blue() { _cc '\033[38;2;137;180;250m'; } # #89b4fa
cc_yellow() { _cc '\033[38;2;249;226;175m'; } # #f9e2af
cc_mauve() { _cc '\033[38;2;203;166;247m'; } # #cba6f7
cc_red() { _cc '\033[38;2;243;139;168m'; } # #f38ba8
cc_teal() { _cc '\033[38;2;148;226;213m'; } # #94e2d5
cc_text() { _cc '\033[38;2;205;214;244m'; } # #cdd6f4
cc_gray() { _cc '\033[38;2;88;91;112m'; } # #585b70
convcommit_init_file() {
local convcommit_file="$1"
[ -f "${convcommit_file}" ] && return 0
# shellcheck disable=SC2129
echo "# convcommit - Conventional Commit message builder" >> "${convcommit_file}"
echo "# This file is read by the \`convcommit\` CLI tool to populate" >> "${convcommit_file}"
echo "# the interactive selector menus." >> "${convcommit_file}"
echo "# Commit this file to share the project's commit vocabulary with the team." >> "${convcommit_file}"
echo "#" >> "${convcommit_file}"
echo "# FORMAT" >> "${convcommit_file}"
echo "# type:<value> — commit type option (e.g. fix, feat, docs)" >> "${convcommit_file}"
echo "# scope:<value> — commit scope option" >> "${convcommit_file}"
echo "# message:<value> — commit message template" >> "${convcommit_file}"
echo "#" >> "${convcommit_file}"
echo "# SPECIAL PREFIXES" >> "${convcommit_file}"
echo "# ~<value> — marks the default selection" >> "${convcommit_file}"
echo "# _ — enables free-text manual input (press \".\")" >> "${convcommit_file}"
echo "# [X]<value> — forces key letter X for this entry (e.g. [B]build, [W]wip)" >> "${convcommit_file}"
echo "#" >> "${convcommit_file}"
echo "# HOW TO USE (interactive)" >> "${convcommit_file}"
echo "# Run \`convcommit\` in a git repo. A menu appears for type, scope, message." >> "${convcommit_file}"
echo "# Press the letter in brackets [A][B]... or [.] for free-text input." >> "${convcommit_file}"
echo "# Stage and push in one shot:" >> "${convcommit_file}"
echo "# convcommit -a -p" >> "${convcommit_file}"
echo "#" >> "${convcommit_file}"
echo "# HOW TO USE (direct flags — scripts, AI agents)" >> "${convcommit_file}"
echo "# Bypass the selector entirely with explicit flags:" >> "${convcommit_file}"
echo "# convcommit --type fix --scope auth --message \"fix null pointer\" --push" >> "${convcommit_file}"
echo "# convcommit -t feat -s api -m \"add endpoint\" -a -p" >> "${convcommit_file}"
echo "#" >> "${convcommit_file}"
echo "# SMART PATTERN — stage specific files and commit in one command" >> "${convcommit_file}"
echo "# Use --add instead of nested command substitution." >> "${convcommit_file}"
echo "# Anti-pattern (avoid):" >> "${convcommit_file}"
echo "# msg=\$(convcommit --type fix --message \"fix\") && git commit -m \"\$msg\" && git push" >> "${convcommit_file}"
echo "# Recommended:" >> "${convcommit_file}"
echo "# convcommit --add src/auth.sh --type fix --scope auth --message \"fix null pointer\" --push" >> "${convcommit_file}"
echo "# Stage multiple files:" >> "${convcommit_file}"
echo "# convcommit --add src/auth.sh --add tests/auth_test.sh -t test -s auth -m \"add tests\" -p" >> "${convcommit_file}"
echo "#" >> "${convcommit_file}"
echo "# HOW TO USE (pipe / non-interactive)" >> "${convcommit_file}"
echo "# Pipe selections as lines: one per stage (type, scope, message)." >> "${convcommit_file}"
echo "# Use the letter shown in the menu, or \".\" to trigger free-text input." >> "${convcommit_file}"
echo "# Examples:" >> "${convcommit_file}"
echo "# printf \"G\n.\nfix null pointer in login\n\" | convcommit" >> "${convcommit_file}"
echo "# printf \"F\n\nadd endpoint\n\" | convcommit -a -p" >> "${convcommit_file}"
echo "# Capture just the formatted message:" >> "${convcommit_file}"
echo "# msg=\$(printf \"G\n\nfix null pointer\n\" | convcommit)" >> "${convcommit_file}"
echo "#" >> "${convcommit_file}"
echo "# OTHER USEFUL FLAGS" >> "${convcommit_file}"
echo "# --reset Regenerate this file with the latest defaults" >> "${convcommit_file}"
echo "# --help Show all options" >> "${convcommit_file}"
echo "#" >> "${convcommit_file}"
echo "# INSTALLATION" >> "${convcommit_file}"
echo "# convcommit is a single bash file with no dependencies." >> "${convcommit_file}"
echo "# Install it locally in your project:" >> "${convcommit_file}"
echo "# curl -fsSL https://raw.githubusercontent.com/francescobianco/convcommit/refs/heads/main/bin/convcommit \\" >> "${convcommit_file}"
echo "# -o bin/convcommit && chmod +x bin/convcommit" >> "${convcommit_file}"
echo "# Or system-wide:" >> "${convcommit_file}"
echo "# curl -fsSL https://raw.githubusercontent.com/francescobianco/convcommit/refs/heads/main/bin/convcommit \\" >> "${convcommit_file}"
echo "# -o /usr/local/bin/convcommit && chmod +x /usr/local/bin/convcommit" >> "${convcommit_file}"
echo "type:[B]build" >> "${convcommit_file}"
echo "type:~chore" >> "${convcommit_file}"
echo "type:[D]docs" >> "${convcommit_file}"
echo "type:deps" >> "${convcommit_file}"
echo "type:feat" >> "${convcommit_file}"
echo "type:fix" >> "${convcommit_file}"
echo "type:ci" >> "${convcommit_file}"
echo "type:init" >> "${convcommit_file}"
echo "type:merge" >> "${convcommit_file}"
echo "type:perf" >> "${convcommit_file}"
echo "type:refactor" >> "${convcommit_file}"
echo "type:revert" >> "${convcommit_file}"
echo "type:security" >> "${convcommit_file}"
echo "type:style" >> "${convcommit_file}"
echo "type:test" >> "${convcommit_file}"
echo "type:[W]wip" >> "${convcommit_file}"
echo "scope:_" >> "${convcommit_file}"
echo "scope:~" >> "${convcommit_file}"
echo "message:_" >> "${convcommit_file}"
echo "message:~_" >> "${convcommit_file}"
}
convcommit_selector() {
local convcommit_file
local stage
local has_manual_input
local input
local key
local columns
local columns_width
local default_value
local field
local forced_letter
local marker
local stage_upper
local r b gy tx yw mp gn bd
convcommit_file="$1"
stage="$2"
columns="${3:-1}"
columns_width="${4:-80}"
# Capture color sequences once (empty strings when --no-color)
r="$(cc_reset)"
b="$(cc_blue)"
gy="$(cc_gray)"
tx="$(cc_text)"
yw="$(cc_yellow)"
mp="$(cc_mauve)"
gn="$(cc_green)"
bd="$(cc_bold)"
stage_upper=$(echo "$stage" | tr '[:lower:]' '[:upper:]')
printf "${gy}── ${r}${bd}${b}Select commit %s${r}${gy} ──────────────────────────────────────────${r}\n" "$stage_upper" >&2
index=64
column=0
default_value=
while read -r line; do
prefix=$(echo "${line}" | cut -d ':' -f 1)
value=$(echo "${line}" | cut -d ':' -f 2)
[ "${prefix}" != "${stage}" ] && continue
# Extract forced letter: [X]value syntax
forced_letter=
case "$value" in
\[?\]*)
forced_letter=$(echo "$value" | cut -c2 | tr '[:lower:]' '[:upper:]')
value=$(echo "$value" | cut -c4-)
;;
esac
if [ "${value#\~}" != "$value" ]; then
value="${value#\~}"
default_value="${value}"
fi
if [ "${value}" = "_" ]; then
has_manual_input=true
continue
fi
[ -z "${value}" ] && continue
if [ -n "$forced_letter" ]; then
index=$(printf '%d' "'${forced_letter}")
letter="$forced_letter"
else
index=$((index + 1))
letter=$(printf "%b" "\\$(printf '%03o' "$index")")
fi
marker="${gy} ${r}"
[ "${value}" = "${default_value}" ] && marker="${yw}★${r}"
column=$((column + 1))
printf "%s${gy}[${r}${b}%s${r}${gy}]${r} ${tx}%-${columns_width}s${r}" "$marker" "$letter" "$value" >&2
if [ "${column}" -eq "${columns}" ]; then
column=0
printf "\n" >&2
fi
done < "${convcommit_file}"
[ "${column}" -ne 0 ] && printf "\n" >&2
if [ "${index}" -gt 64 ]; then
input=${default_value}
[ "${default_value}" = "_" ] && default_value="[manual input]"
[ -n "${has_manual_input}" ] && printf " ${gy}[${r}${mp}.${r}${gy}]${r} ${gy}free text${r}\n" >&2
printf "${gy}Choose ${r}${bd}${b}%s${r}${gy} (default: ${r}${yw}%s${r}${gy}):${r} " "$stage" "${default_value:-[empty]}" >&2
if [ -t 0 ]; then
stty -icanon -echo
key=$(dd bs=1 count=1 2>/dev/null | tr '[:lower:]' '[:upper:]')
stty icanon echo
else
read -r key
key=$(echo "$key" | tr '[:lower:]' '[:upper:]')
fi
echo "" >&2
index=64
while read -r line; do
prefix=$(echo "${line}" | cut -d ':' -f 1)
value=$(echo "${line}" | cut -d ':' -f 2)
[ "${prefix}" != "${stage}" ] && continue
# Extract forced letter: [X]value syntax
forced_letter=
case "$value" in
\[?\]*)
forced_letter=$(echo "$value" | cut -c2 | tr '[:lower:]' '[:upper:]')
value=$(echo "$value" | cut -c4-)
;;
esac
[ "${value#\~}" != "$value" ] && value="${value#\~}"
[ "${value}" = "_" ] && continue
[ -z "${value}" ] && continue
if [ -n "$forced_letter" ]; then
index=$(printf '%d' "'${forced_letter}")
letter="$forced_letter"
else
index=$((index + 1))
letter=$(printf "%b" "\\$(printf '%03o' "$index")")
fi
[ "${key}" = "${letter}" ] && input="${value}"
done < "${convcommit_file}"
elif [ -n "${has_manual_input}" ]; then
key="."
fi
if [ "${key}" = "." ] || [ "${input}" = "_" ]; then
if [ -t 2 ]; then
tput cuu1 >&2
tput el >&2
fi
printf "${yw}⟩${r} ${gy}Type %s:${r} " "$stage" >&2
read -r input
echo "${stage}:${input}" | sed 's/\([^ =]\+\)=\([^ ]*\)/\1=?/g' >> "${convcommit_file}"
fi
if [ -t 2 ]; then
tput cuu1 >&2
tput el >&2
fi
printf "${gn}✓${r} ${b}%s${r} ${bd}${tx}%s${r}\n" "$stage" "${input:-[empty]}" >&2
echo "" >&2
if echo "${input}" | grep -qi '[a-z][a-z]*=?'; then
while echo "${input}" | grep -qi '[a-z][a-z]*=?'; do
field=$(echo "${input}" | grep -oi '[a-z][a-z]*=?' | head -n 1 | cut -d= -f1)
printf "${yw}⟩${r} ${gy}Insert value for '${r}${b}%s${r}${gy}':${r} " "$field" >&2
read -r value
# shellcheck disable=SC2001
input=$(echo "${input}" | sed "s/\($field\)=?/\1 $value/")
done
echo "" >&2
fi
# shellcheck disable=SC2001
input=$(echo "${input}" | sed 's/\([^ =]\+\)=\([^ ]*\)/\1 \2/g')
echo "${input}"
}
## BP005: Execute the entrypoint
main "$@"