-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommitish
More file actions
executable file
·261 lines (224 loc) · 7.41 KB
/
commitish
File metadata and controls
executable file
·261 lines (224 loc) · 7.41 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
#!/usr/bin/env zsh
# commitish: A simple commitizen-like tool for git commits
# This script helps create standardized commit messages following conventional commits format
commitish() {
# Display help
show_help() {
cat << EOF
commitish: A simple commitizen-like tool for creating conventional git commits
Usage: commitish [OPTIONS] [INITIAL_TYPE]
Options:
-h, --help Show this help message and exit
-b, --body Include a commit body (multi-line description)
--no-verify Skip git hooks (passes --no-verify to git commit)
--ai Use AI to suggest commit details (auto-fills type, scope, message, body; requires _commitish_ai in PATH and OPENAI_API_KEY env var)
Examples:
commitish # Basic usage with interactive prompt
commitish fix # Start with 'fix' type pre-selected
commitish -b # Include a commit body
commitish fix -b # Start with 'fix' and include body
commitish --no-verify # Skip git hooks
commitish fix -b --no-verify # Combine options
Commit Types:
feat: A new feature
fix: A bug fix
docs: Documentation only changes
style: Changes that do not affect the meaning of the code
refactor: A code change that neither fixes a bug nor adds a feature
perf: A code change that improves performance
test: Adding missing tests or correcting existing tests
build: Changes that affect the build system or external dependencies
ci: Changes to CI configuration files and scripts
chore: Other changes that don't modify src or test files
revert: Reverts a previous commit
EOF
}
# Process command line arguments
local ask_for_body=false
local initial_query=""
local no_verify=false
local use_ai=false
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help)
show_help
return 0
;;
-b|--body)
ask_for_body=true
shift
;;
--no-verify)
no_verify=true
shift
;;
--ai)
use_ai=true
shift
;;
*)
if [[ -z "$initial_query" ]]; then
initial_query="$1"
else
echo "Error: Unknown parameter: $1"
show_help
return 1
fi
shift
;;
esac
done
# Check if fzf is installed
if ! command -v fzf &> /dev/null; then
echo "Error: fzf is required but not installed. Please install it first."
return 1
fi
# Check if we're in a git repository
if ! git rev-parse --is-inside-work-tree &> /dev/null; then
echo "Error: Not in a git repository."
return 1
fi
# Check if there are staged changes
if git diff --cached --quiet; then
echo "Warning: No staged changes found. Did you forget to 'git add' your changes?"
read "?Continue anyway? (Y/n): " continue_anyway
if [[ "$continue_anyway" =~ ^[Nn]$ ]]; then
return 1
fi
fi
# Define commit types according to conventional commits
commit_types=(
"feat: A new feature"
"fix: A bug fix"
"docs: Documentation only changes"
"style: Changes that do not affect the meaning of the code"
"refactor: A code change that neither fixes a bug nor adds a feature"
"perf: A code change that improves performance"
"test: Adding missing tests or correcting existing tests"
"build: Changes that affect the build system or external dependencies"
"ci: Changes to CI configuration files and scripts"
"chore: Other changes that don't modify src or test files"
"revert: Reverts a previous commit"
)
# Get AI suggestions if requested
local ai_type="" ai_scope="" ai_message="" ai_body=""
if $use_ai; then
if ! command -v _commitish_ai &> /dev/null; then
echo "Error: _commitish_ai command not found in PATH. Please install it to use --ai." >&2
return 1
fi
echo "Getting AI suggestions..."
local ai_result
if ai_result=$(_commitish_ai 2>&1); then
ai_type=$(echo "$ai_result" | jq -r '.type // empty')
ai_scope=$(echo "$ai_result" | jq -r '.scope // empty')
ai_message=$(echo "$ai_result" | jq -r '.message // empty')
ai_body=$(echo "$ai_result" | jq -r '.body // empty')
# Filter out 'none' values
if [[ "$ai_scope" == "none" ]]; then
ai_scope=""
fi
if [[ "$ai_body" == "none" ]]; then
ai_body=""
fi
else
echo "Warning: AI suggestion failed: $ai_result"
fi
fi
# Use fzf to select commit type
if [ -z "$initial_query" ] && [ -n "$ai_type" ]; then
initial_query="$ai_type"
fi
selected_type=$(printf "%s\n" "${commit_types[@]}" |
fzf --height=40% --layout=reverse --prompt="Select commit type: " --header="Use arrows to navigate, type to filter" --query="$initial_query")
# Exit if no type was selected
if [ -z "$selected_type" ]; then
echo "Commit aborted: No commit type selected"
return 1
fi
# Extract just the type part (before the ":")
type=$(echo "$selected_type" | cut -d':' -f1)
# Prompt for scope (optional) - prepopulate with AI suggestion
local scope_prompt="Scope (optional)"
if [ -n "$ai_scope" ]; then
scope_prompt="Scope (optional) [$ai_scope]"
fi
read "?${scope_prompt}: " scope
if [ -z "$scope" ] && [ -n "$ai_scope" ]; then
scope="$ai_scope"
fi
# Create the commit prefix
if [ -n "$scope" ]; then
prefix="${type}(${scope})"
else
prefix="${type}"
fi
# Prompt for commit message - prepopulate with AI suggestion
local msg_prompt="Commit message"
if [ -n "$ai_message" ]; then
msg_prompt="Commit message [$ai_message]"
fi
read "?${msg_prompt}: " message
if [ -z "$message" ] && [ -n "$ai_message" ]; then
message="$ai_message"
fi
if [ -z "$message" ]; then
echo "Commit aborted: Commit message is required"
return 1
fi
# Prompt for body if -b flag is passed, or if AI generated one
body=""
if $ask_for_body || ($use_ai && [ -n "$ai_body" ]); then
if $use_ai && [ -n "$ai_body" ]; then
# Use editor to let user modify the AI-generated body
local editor=(${=EDITOR:-vi})
local body_tmp
body_tmp=$(mktemp)
echo "$ai_body" > "$body_tmp"
$editor "$body_tmp"
body=$(cat "$body_tmp")
rm -f "$body_tmp"
# If user emptied the file, don't use a body
if [ -z "$body" ]; then
body=""
fi
else
echo "Commit body (press Ctrl+D when done):"
body=$(cat)
fi
fi
# Prompt for breaking changes (optional)
read "?Breaking changes? (y/N): " has_breaking_changes
if [[ "$has_breaking_changes" =~ ^[Yy]$ ]]; then
read "?Breaking changes description: " breaking_changes
if [ -n "$breaking_changes" ]; then
breaking="BREAKING CHANGE: ${breaking_changes}"
fi
fi
# Construct the commit message
commit_msg="${prefix}: ${message}"
# Add body if provided
if [ -n "$body" ]; then
commit_msg="${commit_msg}
${body}"
fi
# Add breaking changes if provided
if [ -n "$breaking" ]; then
commit_msg="${commit_msg}
${breaking}"
fi
# Prepare git commit command
commit_command="git commit -m \"$commit_msg\""
# Add --no-verify flag if requested
if $no_verify; then
commit_command="$commit_command --no-verify"
fi
# Perform the commit
echo -e "\nCommitting with message:\n---\n${commit_msg}\n---"
if $no_verify; then
echo "Skipping git hooks (--no-verify)"
fi
eval "$commit_command"
return $?
}
commitish $@