-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-cmg.sh
86 lines (69 loc) · 2.11 KB
/
git-cmg.sh
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
#!/bin/bash
# original repo code: https://github.com/realattila/clean_git_commit_message
commitTypes=(
"⭐ feature:"
"🛠️ fix:"
"🔃 refactor:"
"💫 style:"
"📄 document:"
"🧪 test:"
"🚀 release:"
"🗑️ remove:"
"🔼 upgrade:"
"📦 add:"
"🧹 chore:"
)
displayCommitTypes() {
echo "Choose a commit type:"
for i in "${!commitTypes[@]}"; do
echo "$((i + 1)). ${commitTypes[i]}"
done
}
promptUser() {
displayCommitTypes
read -p "Enter the number of the commit type: " selectedType
typeIndex=$((selectedType - 1))
if ((typeIndex < 0 || typeIndex >= ${#commitTypes[@]})); then
echo "Invalid selection. Please choose a valid number."
return
fi
selectedFormat="${commitTypes[typeIndex]}"
# Get optional scope
read -p "Enter an optional scope (leave empty to skip): " commitScope
if [ -n "$commitScope" ]; then
selectedFormat="${selectedFormat}(${commitScope}):"
fi
# Get commit description
read -p "Enter your commit message (description): " commitMessage
if [ -z "$commitMessage" ]; then
echo "Description is required."
return
fi
# Get optional body
read -p "Enter an optional body (leave empty to skip): " commitBody
# Get optional footer
read -p "Enter optional footer(s) (e.g., BREAKING CHANGE or references to issues; leave empty to skip): " commitFooter
# Check if the staging area is empty
if [[ -z $(git diff --cached --exit-code) ]]; then
git add .
fi
# Format the commit message
formattedCommitMessage="${selectedFormat} ${commitMessage}"
if [ -n "$commitBody" ]; then
formattedCommitMessage="${formattedCommitMessage}\n\n${commitBody}"
fi
if [ -n "$commitFooter" ]; then
formattedCommitMessage="${formattedCommitMessage}\n\n${commitFooter}"
fi
# Ensure proper handling of line breaks
formattedCommitMessage=$(echo -e "$formattedCommitMessage") # This will interpret \n correctly
# Perform the commit
git commit -m "$formattedCommitMessage"
if [ $? -ne 0 ]; then
echo "Error committing changes."
else
echo "Git commit successful:"
echo -e "$formattedCommitMessage"
fi
}
promptUser