-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-commit-shortcuts.zsh
More file actions
116 lines (104 loc) · 2.36 KB
/
git-commit-shortcuts.zsh
File metadata and controls
116 lines (104 loc) · 2.36 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
# git-commit-shortcuts.zsh
types_map=(
feat "✨ feat"
fix "🐛 fix"
docs "📝 docs"
style "💄 style"
refactor "♻️ refactor"
perf "⚡ perf"
test "✅ test"
chore "🔧 chore"
build "🏗️ build"
ci "🔁 ci"
revert "⏪ revert"
)
_get_type_label() {
local key=$1
local i=1
while [ $i -le ${#types_map[@]} ]; do
if [ "${types_map[$i]}" = "$key" ]; then
echo "${types_map[$i+1]}"
return 0
fi
((i+=2))
done
echo "🔖 $key"
}
_git_commit_with_icon() {
local type=$1
shift
local label=$(_get_type_label "$type")
local subject
if [ $# -gt 0 ]; then
subject="$*"
else
echo -n "Commit subject for ${label}: "
IFS= read -r subject
fi
# If no subject and stdin is a tty, abort
if [ -z "$subject" ] && [ -t 0 ]; then
echo "Aborted: empty subject."
return 1
fi
# Read piped stdin (if any) as body
local body=""
if [ ! -t 0 ]; then
body="$(cat -)"
fi
# Build commit message file without adding extra quotes
local full_subject="${label}: ${subject}"
local tmpfile
tmpfile="$(mktemp /tmp/gitmsg.XXXXXX)"
{
printf '%s\n' "$full_subject"
if [ -n "$body" ]; then
printf '\n%s' "$body"
fi
} >| "$tmpfile"
git add -A
if git commit -F "$tmpfile"; then
rm -f "$tmpfile"
return 0
else
echo "git commit failed."
rm -f "$tmpfile"
return 1
fi
}
_create_wrappers() {
local i=1
while [ $i -le ${#types_map[@]} ]; do
local key="${types_map[$i]}"
local fname="g${key//[^a-zA-Z0-9]/}"
eval "
$fname() {
_git_commit_with_icon '$key' \"\$@\"
}
"
((i+=2))
done
}
_create_wrappers
# Soft reset to HEAD~1 by default or specified commit
greset() {
local target="${1:-HEAD~1}"
echo "🔄 Resetting to $target (soft reset)..."
git reset --soft "$target"
echo "✅ Soft reset to $target complete"
echo " - Use 'git status' to see changes"
echo " - Use 'git restore --staged <file>' to unstage specific files"
echo " - Use 'git commit' to create a new commit with the changes"
}
ghelp() {
echo "Available git shortcuts:"
local i=1
while [ $i -le ${#types_map[@]} ]; do
printf " g%s\t→ %s\n" "${types_map[$i]}" "${types_map[$i+1]}"
((i+=2))
done
echo
echo "Usage examples:"
echo " gfeat add login button"
echo " gfix"
echo " echo \"Detailed body\" | gdocs update docs"
}