Skip to content

Commit cd40573

Browse files
committed
git-commit-push-scriptsh updated
1 parent fcc1312 commit cd40573

File tree

1 file changed

+155
-39
lines changed

1 file changed

+155
-39
lines changed

β€Žgit-commit-push-script.shβ€Ž

Lines changed: 155 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,123 @@ MAX_DIFF_CHARS=2000 # Truncate diff to prevent long processing
66
TIMEOUT_SECONDS=10 # Max time to wait for LLM response
77
MAX_COMMIT_LENGTH=50 # Max characters for commit message
88

9-
# Spinner animation function
9+
# Colors
10+
RED='\033[0;31m'
11+
GREEN='\033[0;32m'
12+
YELLOW='\033[0;33m'
13+
BLUE='\033[0;34m'
14+
PURPLE='\033[0;35m'
15+
CYAN='\033[0;36m'
16+
WHITE='\033[1;37m'
17+
GRAY='\033[0;90m'
18+
BOLD='\033[1m'
19+
DIM='\033[2m'
20+
NC='\033[0m' # No Color
21+
22+
# Animated spinner with colors
1023
spinner() {
1124
local pid=$1
12-
local delay=0.1
13-
local spinstr='⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'
25+
local delay=0.08
26+
local frames=('⣾' '⣽' '⣻' 'Ⓙ' '⑿' '⣟' '⣯' '⣷')
27+
local colors=("$CYAN" "$BLUE" "$PURPLE" "$CYAN" "$BLUE" "$PURPLE" "$CYAN" "$BLUE")
28+
local i=0
29+
local elapsed=0
30+
1431
while ps -p $pid > /dev/null 2>&1; do
15-
for (( i=0; i<${#spinstr}; i++ )); do
16-
printf "\r${spinstr:$i:1} Generating commit message..."
17-
sleep $delay
18-
if ! ps -p $pid > /dev/null 2>&1; then
19-
break
20-
fi
21-
done
32+
printf "\r${colors[$i]}${frames[$i]}${NC} ${WHITE}Generating commit message${NC}${GRAY}...${NC} ${DIM}(${elapsed}s)${NC} "
33+
sleep $delay
34+
i=$(( (i + 1) % ${#frames[@]} ))
35+
elapsed=$(echo "scale=1; $elapsed + $delay" | bc)
36+
if ! ps -p $pid > /dev/null 2>&1; then
37+
break
38+
fi
2239
done
23-
printf "\rβœ“ Done! \n"
40+
printf "\r${GREEN}βœ“${NC} ${WHITE}Done!${NC} \n"
41+
}
42+
43+
# Status messages
44+
print_header() {
45+
clear
46+
echo ""
47+
echo -e "${PURPLE}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${CYAN}β•— ${PURPLE}β–ˆβ–ˆ${CYAN}β•— ${PURPLE}β–ˆβ–ˆ${CYAN}β•—${PURPLE}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${CYAN}β•— ${PURPLE}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${CYAN}β•— ${NC}"
48+
echo -e "${PURPLE}β–ˆβ–ˆ${CYAN}╔══${PURPLE}β–ˆβ–ˆ${CYAN}β•—${PURPLE}β–ˆβ–ˆ${CYAN}β•‘ ${PURPLE}β–ˆβ–ˆ${CYAN}β•‘β•šβ•β•${PURPLE}β–ˆβ–ˆ${CYAN}╔══╝${PURPLE}β–ˆβ–ˆ${CYAN}╔═══${PURPLE}β–ˆβ–ˆ${CYAN}β•—${NC}"
49+
echo -e "${PURPLE}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${CYAN}β•‘${PURPLE}β–ˆβ–ˆ${CYAN}β•‘ ${PURPLE}β–ˆβ–ˆ${CYAN}β•‘ ${PURPLE}β–ˆβ–ˆ${CYAN}β•‘ ${PURPLE}β–ˆβ–ˆ${CYAN}β•‘ ${PURPLE}β–ˆβ–ˆ${CYAN}β•‘${NC}"
50+
echo -e "${PURPLE}β–ˆβ–ˆ${CYAN}╔══${PURPLE}β–ˆβ–ˆ${CYAN}β•‘${PURPLE}β–ˆβ–ˆ${CYAN}β•‘ ${PURPLE}β–ˆβ–ˆ${CYAN}β•‘ ${PURPLE}β–ˆβ–ˆ${CYAN}β•‘ ${PURPLE}β–ˆβ–ˆ${CYAN}β•‘ ${PURPLE}β–ˆβ–ˆ${CYAN}β•‘${NC}"
51+
echo -e "${PURPLE}β–ˆβ–ˆ${CYAN}β•‘ ${PURPLE}β–ˆβ–ˆ${CYAN}β•‘β•š${PURPLE}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${CYAN}╔╝ ${PURPLE}β–ˆβ–ˆ${CYAN}β•‘ β•š${PURPLE}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${CYAN}╔╝${NC}"
52+
echo -e "${CYAN}β•šβ•β• β•šβ•β• β•šβ•β•β•β•β•β• β•šβ•β• β•šβ•β•β•β•β•β• ${NC}"
53+
echo ""
54+
echo -e "${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${YELLOW}β•— ${GREEN}β–ˆβ–ˆ${YELLOW}β•— ${GREEN}β–ˆβ–ˆ${YELLOW}β•—${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${YELLOW}β•—${GREEN}β–ˆβ–ˆ${YELLOW}β•— ${GREEN}β–ˆβ–ˆ${YELLOW}β•—${NC}"
55+
echo -e "${GREEN}β–ˆβ–ˆ${YELLOW}╔══${GREEN}β–ˆβ–ˆ${YELLOW}β•—${GREEN}β–ˆβ–ˆ${YELLOW}β•‘ ${GREEN}β–ˆβ–ˆ${YELLOW}β•‘${GREEN}β–ˆβ–ˆ${YELLOW}╔════╝${GREEN}β–ˆβ–ˆ${YELLOW}β•‘ ${GREEN}β–ˆβ–ˆ${YELLOW}β•‘${NC}"
56+
echo -e "${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${YELLOW}╔╝${GREEN}β–ˆβ–ˆ${YELLOW}β•‘ ${GREEN}β–ˆβ–ˆ${YELLOW}β•‘${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${YELLOW}β•—${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${YELLOW}β•‘${NC}"
57+
echo -e "${GREEN}β–ˆβ–ˆ${YELLOW}╔═══╝ ${GREEN}β–ˆβ–ˆ${YELLOW}β•‘ ${GREEN}β–ˆβ–ˆ${YELLOW}β•‘β•šβ•β•β•β•${GREEN}β–ˆβ–ˆ${YELLOW}β•‘${GREEN}β–ˆβ–ˆ${YELLOW}╔══${GREEN}β–ˆβ–ˆ${YELLOW}β•‘${NC}"
58+
echo -e "${GREEN}β–ˆβ–ˆ${YELLOW}β•‘ β•š${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${YELLOW}╔╝${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${YELLOW}β•‘${GREEN}β–ˆβ–ˆ${YELLOW}β•‘ ${GREEN}β–ˆβ–ˆ${YELLOW}β•‘${NC}"
59+
echo -e "${YELLOW}β•šβ•β• β•šβ•β•β•β•β•β• β•šβ•β•β•β•β•β•β•β•šβ•β• β•šβ•β•${NC}"
60+
echo ""
61+
echo -e "${DIM}${WHITE} AI-powered git commit messages${NC}"
62+
echo -e "${GRAY}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
63+
echo ""
64+
}
65+
66+
print_step() {
67+
echo -e "${CYAN}β–Έ${NC} $1"
68+
}
69+
70+
print_success() {
71+
echo -e "${GREEN}βœ“${NC} $1"
2472
}
2573

74+
print_warning() {
75+
echo -e "${YELLOW}⚠${NC} $1"
76+
}
77+
78+
print_error() {
79+
echo -e "${RED}βœ—${NC} $1"
80+
}
81+
82+
print_info() {
83+
echo -e "${GRAY} $1${NC}"
84+
}
85+
86+
# Print header
87+
print_header
88+
2689
# Stage all changes
90+
print_step "Staging changes..."
2791
git add -A
92+
print_success "Changes staged"
2893

2994
# Get the branch name
3095
base_branch=$(git rev-parse --abbrev-ref HEAD)
31-
echo "Current branch: $base_branch"
96+
print_info "Branch: ${CYAN}$base_branch${NC}"
3297

3398
# Get default branch or main branch
3499
default_branch=$(git rev-parse --abbrev-ref origin/HEAD | sed 's@^origin/@@')
35-
echo "Default branch: $default_branch"
100+
print_info "Default: ${GRAY}$default_branch${NC}"
36101

37102
# Extract Jira ticket number from current directory
38103
ticket=$(echo $base_branch | grep -o -E '([A-Za-z]+-[0-9]{3,}|[A-Za-z]+-[0-9]{3,})')
104+
if [ -n "$ticket" ]; then
105+
print_info "Ticket: ${YELLOW}$ticket${NC}"
106+
fi
107+
echo ""
39108

40109
# Get changed files for fallback message
41110
changed_files=$(git diff --name-only origin/$default_branch | head -3)
42111
first_file=$(echo "$changed_files" | head -1)
43112
file_count=$(git diff --name-only origin/$default_branch | wc -l | tr -d ' ')
44113

114+
# Show changed files
115+
print_step "Files changed: ${WHITE}$file_count${NC}"
116+
echo "$changed_files" | while read file; do
117+
if [ -n "$file" ]; then
118+
print_info " ${BLUE}$file${NC}"
119+
fi
120+
done
121+
if [ "$file_count" -gt 3 ]; then
122+
print_info " ${GRAY}...and $((file_count - 3)) more${NC}"
123+
fi
124+
echo ""
125+
45126
# Generate fallback message based on changes
46127
if [ "$file_count" -eq 1 ]; then
47128
fallback_message="${first_file} updated"
@@ -57,7 +138,7 @@ diff=$(git diff origin/$default_branch | head -c $MAX_DIFF_CHARS)
57138
# Skip LLM if diff is too large (use fallback)
58139
diff_size=$(git diff origin/$default_branch | wc -c | tr -d ' ')
59140
if [ "$diff_size" -gt 10000 ]; then
60-
echo "Large diff detected ($diff_size chars). Using fallback message."
141+
print_warning "Large diff detected (${diff_size} chars). Using fallback."
61142
commit_message="$fallback_message"
62143
else
63144
# Default model
@@ -68,6 +149,7 @@ else
68149
$(echo "$diff" | head -50)"
69150

70151
# Run model with timeout and spinner
152+
print_step "Asking AI for commit message..."
71153
echo "$PROMPT" | timeout $TIMEOUT_SECONDS ollama run "$MODEL" --verbose 2>/dev/null | head -1 > /tmp/commit_msg.txt &
72154
LLM_PID=$!
73155
spinner $LLM_PID
@@ -78,7 +160,7 @@ $(echo "$diff" | head -50)"
78160

79161
# Check if timeout occurred or empty response
80162
if [ $exit_code -eq 124 ] || [ -z "$commit_message" ]; then
81-
echo "LLM timeout or empty response. Using fallback message."
163+
print_warning "LLM timeout. Using fallback message."
82164
commit_message="$fallback_message"
83165
fi
84166
fi
@@ -91,69 +173,103 @@ if [ ${#commit_message} -gt $MAX_COMMIT_LENGTH ]; then
91173
commit_message=$(echo "$commit_message" | cut -c1-$MAX_COMMIT_LENGTH | sed 's/[[:space:]][^[:space:]]*$//')
92174
fi
93175

94-
echo "Commit message: $commit_message"
95-
96176
# Final fallback check
97177
if [ -z "$commit_message" ] || [ "$commit_message" == "null" ]; then
98178
commit_message="$fallback_message"
99-
echo "Using fallback: $commit_message"
100179
fi
101180

181+
echo ""
182+
echo -e "${PURPLE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
183+
echo -e "${BOLD}${WHITE} πŸ“ Commit Message${NC}"
184+
echo -e "${PURPLE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
185+
if [ -n "$ticket" ]; then
186+
echo -e " ${YELLOW}$ticket${NC} ${WHITE}$commit_message${NC}"
187+
else
188+
echo -e " ${WHITE}$commit_message${NC}"
189+
fi
190+
echo -e "${PURPLE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
191+
echo ""
192+
102193
# Set the environment variables
103194
export COMMIT_MESSAGE="$commit_message"
104195
export TICKET="$ticket"
105196

106197
# Prepare and execute commit command, remove -S to commit without signing
198+
print_step "Committing changes..."
107199
if [ -z "$ticket" ]; then
108-
expect <<'EOF'
200+
expect <<'EOF' > /dev/null 2>&1
109201
spawn git commit -S -m "$env(COMMIT_MESSAGE)"
110202
expect "Enter passphrase for \"/Users/wscholl/.ssh/id_ed25519\":"
111203
send "$env(GIT_SSH_PASSPHRASE)\r"
112204
expect eof
113205
EOF
114206
else
115-
expect <<'EOF'
207+
expect <<'EOF' > /dev/null 2>&1
116208
spawn git commit -S -m "$env(TICKET) $env(COMMIT_MESSAGE)"
117209
expect "Enter passphrase for \"/Users/wscholl/.ssh/id_ed25519\":"
118210
send "$env(GIT_SSH_PASSPHRASE)\r"
119211
expect eof
120212
EOF
121213
fi
214+
print_success "Committed successfully"
122215

123216
# Check if the branch exists on the remote
124217
remote_branch=$(git ls-remote --heads origin $base_branch)
125218

126219
# Function: pull_push_after_failed_push - If push fails, attempt to pull and push again
127220
pull_push_after_failed_push() {
128-
echo "Push failed. Attempting to pull and push again."
129-
git fetch origin $base_branch
130-
git pull
131-
git push --force
221+
print_warning "Push failed. Attempting to pull and push again..."
222+
git fetch origin $base_branch > /dev/null 2>&1
223+
git pull > /dev/null 2>&1
224+
git push --force > /dev/null 2>&1
132225
}
133226

134227
# Check if the branch exists on the remote
135228
if [ -z "$remote_branch" ]; then
136229
# If the branch does not exist on the remote, create it
137-
echo "Branch '$base_branch' does not exist on remote. Creating it."
138-
# Push the local branch to the remote, setting the upstream branch
230+
print_step "Creating remote branch..."
139231
set -e
140-
git push --set-upstream origin $base_branch
232+
git push --set-upstream origin $base_branch > /dev/null 2>&1
141233

142-
# Check if the push was successful, if previous command is not a failure, execute the function to handle a failed push
234+
# Check if the push was successful
143235
if [ $? -ne 0 ]; then
144236
pull_push_after_failed_push
145237
fi
146-
else # Branch exists on the remote, push changes to the remote branch
147-
echo "Branch '$base_branch' exists on remote."
148-
# Pull the latest changes from the remote branch
149-
echo "Pulling latest changes from remote branch..."
150-
git fetch origin $base_branch
151-
git pull
152-
echo "Pushing changes to remote $base_branch branch..."
153-
git push
154-
155-
# Check if the push wasn't successful, execute the function to handle a failed push
238+
print_success "Branch created and pushed"
239+
else
240+
# Branch exists on the remote, push changes
241+
print_step "Pushing to remote..."
242+
git fetch origin $base_branch > /dev/null 2>&1
243+
git pull > /dev/null 2>&1
244+
git push > /dev/null 2>&1
245+
246+
# Check if the push wasn't successful
156247
if [ $? -ne 0 ]; then
157248
pull_push_after_failed_push
158249
fi
159-
fi
250+
print_success "Pushed successfully"
251+
fi
252+
253+
# Animated success banner
254+
show_pushed_banner() {
255+
echo ""
256+
echo -e "${GRAY}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
257+
sleep 0.1
258+
echo -e "${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${WHITE}β•— ${GREEN}β–ˆβ–ˆ${WHITE}β•— ${GREEN}β–ˆβ–ˆ${WHITE}β•—${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${WHITE}β•—${GREEN}β–ˆβ–ˆ${WHITE}β•— ${GREEN}β–ˆβ–ˆ${WHITE}β•—${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${WHITE}β•—${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${WHITE}β•— ${GREEN}β–ˆβ–ˆ${WHITE}β•—${NC}"
259+
sleep 0.05
260+
echo -e "${GREEN}β–ˆβ–ˆ${WHITE}╔══${GREEN}β–ˆβ–ˆ${WHITE}β•—${GREEN}β–ˆβ–ˆ${WHITE}β•‘ ${GREEN}β–ˆβ–ˆ${WHITE}β•‘${GREEN}β–ˆβ–ˆ${WHITE}╔════╝${GREEN}β–ˆβ–ˆ${WHITE}β•‘ ${GREEN}β–ˆβ–ˆ${WHITE}β•‘${GREEN}β–ˆβ–ˆ${WHITE}╔════╝${GREEN}β–ˆβ–ˆ${WHITE}╔══${GREEN}β–ˆβ–ˆ${WHITE}β•—${GREEN}β–ˆβ–ˆ${WHITE}β•‘${NC}"
261+
sleep 0.05
262+
echo -e "${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${WHITE}╔╝${GREEN}β–ˆβ–ˆ${WHITE}β•‘ ${GREEN}β–ˆβ–ˆ${WHITE}β•‘${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${WHITE}β•—${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${WHITE}β•‘${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${WHITE}β•— ${GREEN}β–ˆβ–ˆ${WHITE}β•‘ ${GREEN}β–ˆβ–ˆ${WHITE}β•‘${GREEN}β–ˆβ–ˆ${WHITE}β•‘${NC}"
263+
sleep 0.05
264+
echo -e "${GREEN}β–ˆβ–ˆ${WHITE}╔═══╝ ${GREEN}β–ˆβ–ˆ${WHITE}β•‘ ${GREEN}β–ˆβ–ˆ${WHITE}β•‘β•šβ•β•β•β•${GREEN}β–ˆβ–ˆ${WHITE}β•‘${GREEN}β–ˆβ–ˆ${WHITE}╔══${GREEN}β–ˆβ–ˆ${WHITE}β•‘${GREEN}β–ˆβ–ˆ${WHITE}╔══╝ ${GREEN}β–ˆβ–ˆ${WHITE}β•‘ ${GREEN}β–ˆβ–ˆ${WHITE}β•‘β•šβ•β•${NC}"
265+
sleep 0.05
266+
echo -e "${GREEN}β–ˆβ–ˆ${WHITE}β•‘ β•š${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${WHITE}╔╝${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${WHITE}β•‘${GREEN}β–ˆβ–ˆ${WHITE}β•‘ ${GREEN}β–ˆβ–ˆ${WHITE}β•‘${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${WHITE}β•—${GREEN}β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ${WHITE}╔╝${GREEN}β–ˆβ–ˆ${WHITE}β•—${NC}"
267+
sleep 0.05
268+
echo -e "${WHITE}β•šβ•β• β•šβ•β•β•β•β•β• β•šβ•β•β•β•β•β•β•β•šβ•β• β•šβ•β•β•šβ•β•β•β•β•β•β•β•šβ•β•β•β•β•β• β•šβ•β•${NC}"
269+
echo -e "${GRAY}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
270+
echo ""
271+
echo -e " ${DIM}πŸš€ ${WHITE}Changes are now live on ${CYAN}$base_branch${NC}"
272+
echo ""
273+
}
274+
275+
show_pushed_banner

0 commit comments

Comments
Β (0)