-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_commit.sh
More file actions
executable file
·172 lines (142 loc) · 5.03 KB
/
Copy pathgit_commit.sh
File metadata and controls
executable file
·172 lines (142 loc) · 5.03 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env bash
# Get the directory of the current script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "$SCRIPT_DIR/load_color_codes.def"
. "$SCRIPT_DIR/backup_files.sh"
. "$SCRIPT_DIR/check_git_config.sh"
. "$SCRIPT_DIR/stage_files.sh"
. "$SCRIPT_DIR/create_commit_template.sh"
. "$SCRIPT_DIR/ensure_git_directory.sh"
# Function to handle git commit
git_commit() {
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}"
# Initialize the files array with command-line arguments
local files=("$@")
# Add files already staged in "git add" section
mapfile -t staged_files < <(git diff --cached --name-only)
# Combine command-line files and staged files, avoiding duplicates
for staged_file in "${staged_files[@]}"; do
if ! printf '%s\n' "${files[@]}" | grep -q -F -- "$staged_file"; then
files+=("$staged_file")
fi
done
# Auto-fix or warn about non-executable .sh files
for file in "${files[@]}"; do
if [[ "$file" == *.sh && -f "$file" && ! -x "$file" ]]; then
if [[ "$GIT_COMMIT_AUTOFIX_EXEC" == "1" ]]; then
chmod +x "$file"
echo -e "${green}Auto-fixed +x:${reset} $file"
else
echo -e "${yellow}Warning:${reset} '$file' is not executable. Set +x manually or rerun with GIT_COMMIT_AUTOFIX_EXEC=1"
fi
fi
done
# Debug output for collected files
echo "Files to back up and commit:"
printf " %s\n" "${files[@]}"
echo "Step 1: Backing up specified and staged files..."
backup_files "${files[@]}"
echo "Step 2: Checking Git configuration..."
check_git_config
echo "Step 3: Checking Git repository status..."
git status
echo
echo "Step 4: Pulling latest changes from the remote repository..."
git pull || {
echo "Error: Failed to pull changes."
exit 1
}
echo
echo "Step 5: Staging files for commit..."
stage_files "${files[@]}"
echo
echo "Step 6: Creating commit message template..."
local template
create_commit_template template # Pass template as a reference
echo "Step 7: Validating and applying commit message..."
# Remove comments, trailing spaces, and blank lines at the bottom
local commit_message
commit_message=$(sed -e '/^#/d' \
-e 's/[[:space:]]*$//' \
"$template" \
| awk 'NF {p=1} p' \
| tac \
| awk 'NF {p=1} p' \
| tac)
# Split commit message into subject and body
local subject_line body_lines
subject_line=$(echo "$commit_message" | sed -n '1p')
body_lines=$(echo "$commit_message" \
| sed -e '1d' \
| awk 'NF {p=1} p')
# Automatically enforce no period at the end of the subject line
if [[ "$subject_line" =~ \.$ ]]; then
echo "Warning: Subject line ends with a period. Removing the period."
subject_line=${subject_line%.}
fi
# Validate subject line length
if [[ ${#subject_line} -gt 50 ]]; then
echo "Warning: Subject line exceeds 50 characters (${#subject_line})."
fi
# Validate body line lengths
if echo "$body_lines" | grep -q '.\{73\}'; then
echo "Warning: One or more body lines exceed 72 characters."
fi
# Ensure the subject line and body are not empty
if [[ -z "$subject_line" ]]; then
echo "Error: Commit message must have a subject line."
rm -f "$template"
return 1
fi
if [[ -z "$body_lines" ]]; then
echo "Error: Commit message must have a body after the subject line."
rm -f "$template"
return 1
fi
# Reconstruct the commit message
commit_message="${subject_line}
${body_lines}"
echo "Commit message preview:"
echo "-----------------------"
echo "$commit_message"
echo "-----------------------"
read -p "Proceed with commit? (y/n): " confirm
if [[ "$confirm" != "y" ]]; then
echo "Aborting commit."
rm -f "$template"
exit 1
fi
# Perform the commit
echo "$commit_message" | git commit -F -
# Cleanup
rm -f "$template"
echo "Commit completed successfully."
echo
echo "Step 8: Pushing changes to the remote repository..."
git push || {
echo "Error: Failed to push changes."
exit 1
}
echo
echo "Step 9: Pulling latest changes after push to sync repository..."
git pull || {
echo "Error: Failed to pull changes."
exit 1
}
echo
echo "Step 10: Final repository status:"
git status
echo "Workflow completed successfully."
}
# 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