-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick-commit
More file actions
executable file
·82 lines (70 loc) · 2.15 KB
/
quick-commit
File metadata and controls
executable file
·82 lines (70 loc) · 2.15 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
#!/usr/bin/env bash
# quick-commit — stage, auto-commit with a good message, push
# Usage: quick-commit [message]
# With message: uses your message
# Without: generates one from the diff
set -euo pipefail
# Ensure we're in a git repo
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || { echo "ERROR: not in a git repo" >&2; exit 1; }
# Stage all changes
git add -A
# Check there's something to commit
if git diff --cached --quiet; then
echo "Nothing to commit — working tree clean"
exit 0
fi
# Get or generate commit message
if [[ $# -gt 0 ]]; then
MSG="$*"
else
# Generate from diff summary
STAT=$(git diff --cached --stat)
FILES_CHANGED=$(git diff --cached --name-only)
NUM_FILES=$(echo "$FILES_CHANGED" | wc -l)
# Detect patterns
NEW_FILES=$(git diff --cached --diff-filter=A --name-only)
DELETED_FILES=$(git diff --cached --diff-filter=D --name-only)
MODIFIED_FILES=$(git diff --cached --diff-filter=M --name-only)
# Build a sensible message
PARTS=()
if [[ -n "$NEW_FILES" ]]; then
NEW_COUNT=$(echo "$NEW_FILES" | wc -l)
if [[ $NEW_COUNT -eq 1 ]]; then
PARTS+=("add $(echo "$NEW_FILES" | head -1)")
else
PARTS+=("add $NEW_COUNT new files")
fi
fi
if [[ -n "$DELETED_FILES" ]]; then
DEL_COUNT=$(echo "$DELETED_FILES" | wc -l)
if [[ $DEL_COUNT -eq 1 ]]; then
PARTS+=("remove $(echo "$DELETED_FILES" | head -1)")
else
PARTS+=("remove $DEL_COUNT files")
fi
fi
if [[ -n "$MODIFIED_FILES" ]]; then
MOD_COUNT=$(echo "$MODIFIED_FILES" | wc -l)
if [[ $MOD_COUNT -eq 1 ]]; then
PARTS+=("update $(echo "$MODIFIED_FILES" | head -1)")
elif [[ $MOD_COUNT -le 3 ]]; then
PARTS+=("update $(echo "$MODIFIED_FILES" | tr '\n' ', ' | sed 's/,$//')")
else
PARTS+=("update $MOD_COUNT files")
fi
fi
if [[ ${#PARTS[@]} -eq 0 ]]; then
MSG="update"
else
MSG=$(IFS='; '; echo "${PARTS[*]}")
fi
fi
# Commit
git commit -m "$MSG"
# Push if upstream exists
if git rev-parse --abbrev-ref --symbolic-full-name @{upstream} >/dev/null 2>&1; then
git push
echo "✓ Committed and pushed: $MSG"
else
echo "✓ Committed (no upstream to push): $MSG"
fi