-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_commit_template.sh
More file actions
executable file
·110 lines (96 loc) · 3.37 KB
/
Copy pathcreate_commit_template.sh
File metadata and controls
executable file
·110 lines (96 loc) · 3.37 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
#!/usr/bin/env bash
# Get the directory of the current script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. ./load_color_codes.def
. ./ensure_git_directory.sh
. ./validate_commit_message.sh
# Function to create the commit message template
create_commit_template ()
{
echo "Ensuring the script is running in a Git repository..."
if ! ensure_git_directory; then
echo -e "${red}Exiting script due to missing Git repository.${reset}"
return 1
fi
echo -e "${green}This is a valid Git repository.${reset}"
local -n template_ref=$1 # Use nameref to modify the variable passed as an argument
# Create a temporary file for the commit message template
template_ref=$(mktemp)
cat <<EOF >"$template_ref"
# Write your commit message above this line.
#
# ######################################################################
#
# Linus Torvalds' Git Commit Message Style:
#
# 1. Subject Line (50 characters max):
# - Use a concise summary of the changes made.
# - Write in the imperative mood (e.g., "Fix," "Add," "Update").
# - Avoid using a period (.) at the end of the subject line.
#
# 2. Separate Subject Line and Body with a Blank Line:
# - The blank line improves readability.
#
# 3. Body (72 characters max per line):
# - Use paragraphs or bullet points to explain:
# * What was changed.
# * Why the change was made.
# * Avoid details of *how* unless it's non-obvious.
# - Wrap lines to 72 characters for better readability in logs.
#
# 4. Use Blank Lines Around Lists:
# - Separate bullet lists or paragraphs with blank lines for clarity.
#
# 5. Signed-off-by Line:
# - Add the line 'Signed-off-by: Your Name <your.email@example.com>' at
# the bottom. This certifies that you authored the change or have
# the right to submit it under the project’s license.
#
# Example Commit Message:
#
# feat: improve validation workflow
# feat: Improve validation workflow
# fix: Resolve crash on startup
# docs: Update README with usage instructions
#
# Refactor the validation process to include additional checks for empty
# subject lines and overly long body lines. This ensures compliance with
# the 50/72 rule and improves commit message quality.
#
# - Added subject line length validation.
# - Trimmed trailing spaces and blank lines.
# - Improved user feedback for violations.
#
# Signed-off-by: Jane Doe <jane.doe@example.com>
#
# Below is the status and diff of changes to help you craft the message:
EOF
# Append git status and diff
{
echo
echo ######################################################################
echo
echo "Status:"
git status
echo
echo ######################################################################
echo
echo "Changes:"
git diff --staged
} | sed 's/^/# /' >>"$template_ref"
# Open the template in vim interactively
if [ -t 1 ]; then
vim "$template_ref"
else
vim "$template_ref" < /dev/tty
fi
# Validate the commit message
validate_commit_message "$template_ref"
}
# Source footer if it exists
if [ -f "$SCRIPT_DIR/bash_footer.template.live" ]; then
source "$SCRIPT_DIR/bash_footer.template.live"
else
echo -e "${red}Footer template missing. Skipping...${reset}"
echo -e "Please ensure 'bash_footer.template.live' exists in the same directory."
fi