-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcm
More file actions
executable file
·167 lines (134 loc) · 4.22 KB
/
gcm
File metadata and controls
executable file
·167 lines (134 loc) · 4.22 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
#!/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"
source "${SCRIPT_DIR}/../../bash_modules/config.sh"
source "${SCRIPT_DIR}/../../bash_modules/ai.sh"
[[ -z ${BASH_MODULES_DIR-} ]] && echo "ERROR: terminal.sh module missing" && exit 1
function print_usage() {
cat <<EOF
Usage: $(basename "$0") [-y|--yes] [special-instructions]
Generate a commit message using AI for the currently staged changes.
Dependencies:
git Git version control
aichat AI chat tool
Options:
-y, --yes Skip confirmation prompts and auto-commit/push
Environment Variables:
CONFIRM Set to 'true' to automatically commit and push without user confirmation
Arguments:
special-instructions Optional special instructions for the AI prompt
This script will:
1. Check if there are staged git changes
2. Generate a commit message using AI
3. Allow you to confirm and use the generated message (unless -y or CONFIRM=true)
Examples:
$(basename "$0") # Interactive mode with confirmations
$(basename "$0") -y # Auto-confirm commit and push
$(basename "$0") -y fix typo # Auto-confirm with special instructions
CONFIRM=true $(basename "$0") # Auto-confirm using environment variable
EOF
}
if [[ "${1}" == "-h" || "${1}" == "--help" ]]; then
print_usage
exit 0
fi
# Parse arguments for -y/--yes flag
auto_confirm=false
args=()
for arg in "$@"; do
if [[ "${arg}" == "-y" || "${arg}" == "--yes" ]]; then
auto_confirm=true
else
args+=("${arg}")
fi
done
if [[ ${#args[@]} -eq 0 ]]; then
special_instructions="None"
else
special_instructions="${args[*]}"
fi
function ctrlc_trap() {
log_newline
log_warning "Script interrupted. Exiting."
exit 130
}
trap ctrlc_trap SIGINT
# Title and Dependency Checks
# -----------------------------------------------------------------------------
log_title "[g]it [c]ommit [m]essage generator"
git status
dependencies=(git aichat)
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
staged_changes="$(git diff --staged)"
if [[ $? -ne 0 || -z "${staged_changes}" ]]; then
log_error "ERROR: No staged changes found. Use 'git add' to stage changes first."
exit 1
fi
# Commit Message Generation
# -----------------------------------------------------------------------------
log_message "Generating commit message..."
prompt="$(cat <<EOF
$(cat "${SCRIPT_DIR}/gcm-prompt.md")
## Special Instructions
${special_instructions}
## Git Diff
\`\`\`md
${staged_changes}
\`\`\`
EOF
)"
if ! commit_message=$(eval "$(ai_get_command aichat fast "${SCRIPT_DIR}/gcm-prompt.md" "${prompt}")"); then
log_error "ERROR: AI chat command failed"
exit 1
fi
if [[ -z "${commit_message}" ]]; then
log_error "ERROR: Generated commit message is empty"
exit 1
fi
log_newline
echo "${commit_message}"
log_newline
log_line
if [[ "${auto_confirm}" == "true" || "${CONFIRM:-false}" == "true" ]]; then
log_message "Auto-confirming commit"
else
if ! press_enter_to_continue "Press Enter to commit, any other key to abort."; then
log_warning "Commit aborted"
exit 1
fi
fi
log_message "❯ git commit -m '${commit_message}'"
if ! git commit -m "${commit_message}"; then
log_error "ERROR: Git commit failed"
exit 1
fi
log_line
if ! git remote get-url origin &>/dev/null; then
log_warning "No 'origin' remote found. Skipping push option."
exit 0
fi
if [[ "${auto_confirm}" == "true" || "${CONFIRM:-false}" == "true" ]]; then
log_message "Auto-confirming push"
else
if ! press_enter_to_continue "Press Enter to push, any other key to abort."; then
log_warning "Push aborted"
exit 1
fi
fi
log_message "❯ push"
"$(cd "${BASH_SOURCE[0]%/*}" || exit 1; pwd)/push"