-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommit
executable file
Β·187 lines (166 loc) Β· 5.65 KB
/
commit
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/bash
# Make the credentials available at the start of the script
y_regex="^([y|Y][eE][sS]|[y|Y])$"
email=$(grep -oP '^user_email:\s*\K.*' ~/.commitconfig)
username=$(grep -oP '^user_username:\s*\K.*' ~/.commitconfig)
# Check if the --help flag is used
if [[ "$*" == *"--help"* ]]; then
echo "Usage: commit [options] [files]"
echo
echo "Options:"
echo " --help Display this help message"
echo " --verbose Display verbose output"
echo
echo "Description:"
echo " This script helps you stage, commit, and push changes to a Git repository."
echo " It prompts for a commit message and performs the necessary Git commands."
echo
echo "Examples:"
echo " commit => Commit all changes (assumes git add .)"
echo " commit file1.txt file2.txt => Stage and commit specific files"
echo " commit --verbose file.txt => Commit changes with verbose output"
echo
exit 0
fi
# Check if the current directory is a Git repository
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
read -rp "The current directory is not a Git repository. Would you like to initialize it? (y/n): " initialize
if [[ "$initialize" =~ $y_regex ]]; then
git init
else
echo "Script terminated. Please initialize a Git repository to continue."
exit 1
fi
fi
#Prompt for user input
read -rp "Enter commit message: " message
echo
# Validate that the commit message is not empty
if [[ -z "$message" ]]; then
echo "Error: commit message cannot be empty"
exit 1
fi
# Validate the length of the commit message
if [[ ${#message} -gt 50 ]]; then
echo "Error: commit message exceeds the maximum length of 50 characters"
exit 1
fi
# Set git config for the current user
if ! git config --local user.name "$username"; then
echo "Error: unable to set git user name"
exit 1
fi
if ! git config --local user.email "$email"; then
echo "Error: unable to set git user email"
exit 1
fi
# Check if the --verbose flag is used
verbose_flag=""
for arg in "$@"; do
if [ "$arg" == "--verbose" ]; then
verbose_flag="--verbose"
break
fi
done
# Remove the --verbose flag from the arguments
new_args=()
for arg in "$@"; do
if [ "$arg" != "--verbose" ]; then
new_args+=("$arg")
fi
done
set -- "${new_args[@]}"
# Check if any files are specified
if [ $# -eq 0 ]; then
# No files specified, assume all changes should be staged and committed
if [[ -n "$verbose_flag" ]]; then
# Display staging output when verbose flag is used
echo "--- Staging Output ---"
if ! git add .; then
echo "--- Changes staged unsuccessfully ---"
exit 1
fi
echo "--- Changes staged successfully ---"
else
# Suppress output when verbose flag is not used
if ! git add . > /dev/null 2>&1; then
echo "Error: unable to add changes to the staging area"
echo "(Consider using the --verbose flag for more information)"
exit 1
fi
fi
# Check if any changes were added to the staging area
staged_files=$(git diff --cached --name-only)
if [[ -z "$staged_files" ]]; then
echo "No recent changes have been made to any files"
echo "(Consider using the --verbose flag for more information)"
exit 1
else
echo "Changes staged successfully:"
echo "$staged_files"
fi
else
# Stage specified files
if [[ -n "$verbose_flag" ]]; then
# Display staging output when verbose flag is used
echo "--- Staging Output ---"
if ! git add "$@"; then
echo "--- Changes staged unsuccessfully ---"
exit 1
fi
echo "--- Changes staged successfully ---"
else
# Suppress output when verbose flag is not used
if ! git add "$@" > /dev/null 2>&1; then
echo "Error: unable to add changes to the staging area"
echo "(Consider using the --verbose flag for more information)"
exit 1
fi
fi
# Check if any changes were added to the staging area
staged_files=$(git diff --cached --name-only -- "$@")
if [[ -z "$staged_files" ]]; then
echo "No recent changes have been made to the specified file(s)"
echo "(Consider using the --verbose flag for more information)"
exit 1
else
echo "Changes staged successfully:"
echo "$staged_files"
fi
fi
echo
# Commit the changes with the commit message
if [[ -n "$verbose_flag" ]]; then
# Display commit output when verbose flag is used
echo "--- Commit Output ---"
git commit "$verbose_flag" -m "$message"
echo "--- Changes committed successfully ---"
else
# Suppress output when verbose flag is not used
if ! git commit -m "$message" > /dev/null 2>&1; then
echo "Error: unable to commit changes"
echo "(Consider using the --verbose flag for more information)"
exit 1
fi
echo "Changes committed successfully"
fi
echo
# Set upstream to be done automatically by git incase the user
# opened a new branch and wants to push the branch
git config --local --add --bool push.autoSetupRemote true
# Push the changes to the remote repository
if [[ -n "$verbose_flag" ]]; then
# Display push output when verbose flag is used
echo "--- Push Output ---"
git push
echo "--- Changes pushed successfully ---"
else
# Suppress output when verbose flag is not used
if ! git push > /dev/null 2>&1; then
echo "Error: unable to push changes to the remote repository"
echo "(Consider using the --verbose flag for more information)"
exit 1
fi
echo "Changes pushed successfully"
fi
echo