-
Notifications
You must be signed in to change notification settings - Fork 5
/
commit.sh
executable file
·230 lines (201 loc) · 6.41 KB
/
commit.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/bash
## /*
# @usage commit <message> [-aA]
#
# @description
# Commits already-staged work to a branch with a few extra benefits. The branch name
# is prepended to the commit message so that all commits are easily associated with
# their branch. The commit summary message is also automatically suppressed.
#
# Non-staged work can be staged via the available options, which are described below.
# description@
#
# @options
# -a Automatically stage modified and deleted files before committing.
# -A Automatically stage ALL tracked/untracked files before committing.
# --no-branch-name Do not automatically prepend the commit message with the current branch name
# --branch-name Prepend the commit message with the current branch name, regardless of user overrides settings.
# --no-branch-number Do not automatically prepend the commit message with the current branch number
# --branch-number (takes precedence over --branch-name) Prepend the commit message with the current branch number, regardless of user overrides settings.
# options@
#
# @notes
# - The options for this command must come AFTER the message since the -m
# option is automatically passed to git commit during processing.
# - If there are untracked files in the working tree and the user passes the -a
# option, he/she will be prompted to add the untracked files as well.
# notes@
#
# @examples
# 1) commit "I know I added some untracked files, so I'll pass the right option" -A
# examples@
#
# @dependencies
# clear-screen.sh
# functions/0100.bad_usage.sh
# functions/5000.branch_exists.sh
# functions/5000.parse_git_branch.sh
# functions/5000.parse_git_status.sh
# functions/5000.set_remote.sh
# dependencies@
#
# @file commit.sh
## */
$loadfuncs
echo ${X}
numArgs=$#
local_PREPEND_BRANCHNAME_TO_COMMIT_MESSAGES=$PREPEND_BRANCHNAME_TO_COMMIT_MESSAGES;
local_PREPEND_BRANCHNUMBER_TO_COMMIT_MESSAGES=$PREPEND_BRANCHNUMBER_TO_COMMIT_MESSAGES;
# parse arguments
if (( numArgs > 0 && numArgs < 4 )); then
until [ -z "$1" ]; do
[ "$1" == "--branch-name" ] && local_PREPEND_BRANCHNAME_TO_COMMIT_MESSAGES=true
[ "$1" == "--no-branch-name" ] && local_PREPEND_BRANCHNAME_TO_COMMIT_MESSAGES=false
[ "$1" == "--branch-number" ] && local_PREPEND_BRANCHNUMBER_TO_COMMIT_MESSAGES=true
[ "$1" == "--no-branch-number" ] && local_PREPEND_BRANCHNUMBER_TO_COMMIT_MESSAGES=false
{ [ "$1" == "-a" ] || [ "$1" == "-A" ]; } && flag=$1
! echo "$1" | egrep -q "^-" && msg="$1"
shift 1
done
else
__bad_usage commit "Invalid number of parameters."
exit 1
fi
# conditions that should cause the script to halt immediately:
# make sure SOMETHING is staged if user doesn't specify flag
if ! __parse_git_status staged && [ ! $flag ]; then
echo
echo ${E}" You haven't staged any changes to commit! Aborting... "${X}
exit 1
fi
startingBranch=$(__parse_git_branch)
if [ -z "$startingBranch" ]; then
echo ${E}" Unable to determine current branch. "${X}
exit 1
fi
echo
echo ${H1}${H1HL}
echo " Committing changes to branch: ${H1B}\`${startingBranch}\`${H1} "
echo ${H1HL}${X}
echo
echo
echo "Checking status..."
echo ${O}${H2HL}
echo "$ git status"
git status
echo ${O}${H2HL}${X}
echo "PREPEND_BRANCHNUMBER_TO_COMMIT_MESSAGES: ${PREPEND_BRANCHNUMBER_TO_COMMIT_MESSAGES}"
echo "local_PREPEND_BRANCHNUMBER_TO_COMMIT_MESSAGES: ${local_PREPEND_BRANCHNUMBER_TO_COMMIT_MESSAGES}"
# check to see if user wants to add all modified/deleted files
if [ $flag ]; then
case $flag in
"-a")
if __parse_git_status untracked; then
echo
echo
echo ${Q}"Would you like to run ${A}git add -A${Q} to add untracked files as well? y (n)"${X}
read yn
if [ "$yn" == "y" ] || [ "$yn" == "Y" ]; then
echo
echo
echo "Adding all modified and untracked files..."
echo ${O}${H2HL}
echo "$ git add -A"
git add -A
gitAddResult=$?
echo ${O}${H2HL}${X}
if [ $gitAddResult -gt 0 ]; then
echo
echo ${W}" The command to add ALL tracked and untracked files failed (see above). "
echo " It is unlikely that your desired outcome will result from this commit. "${X}
echo ${Q}" Do you still want to continue with the ${A}commit${Q}? y (n)"${X}
read yn
if [ "$yn" != "y" ] && [ "$yn" != "Y" ]; then
echo
echo "It's probably for the best. Aborting..."
exit 1
fi
fi
fi
fi
;;
"-A")
flag=
echo
echo
echo "Adding all modified and untracked files..."
echo ${O}${H2HL}
echo "$ git add -A"
git add -A
gitAddResult=$?
echo ${O}${H2HL}${X}
if [ $gitAddResult -gt 0 ]; then
echo
echo ${W}" The command to add ALL tracked and untracked files failed (see above). "
echo " It is unlikely that your desired outcome will result from this commit. "${X}
echo ${Q}" Do you still want to continue with the ${A}commit${Q}? y (n)"${X}
read yn
if [ "$yn" != "y" ] && [ "$yn" != "Y" ]; then
echo
echo "It's probably for the best. Aborting..."
exit 1
fi
fi
;;
*)
__bad_usage commit "Invalid parameter ($flag)."
exit 1
;;
esac
fi
echo
echo
echo "Committing and displaying branch changes..."
echo ${O}${H2HL}
doubleDashIndex=$(echo ${startingBranch} | sed -E -n "s/(--|__).*//p" | wc -c)
echo "double dash index: $doubleDashIndex"
# decrement by 1 so we don't also include the '-' in the commit message
((doubleDashIndex--))
if (($doubleDashIndex < 0)); then
doubleDashIndex=0
fi
dashIndex=$(echo ${startingBranch} | sed -n "s/-.*//p" | wc -c)
echo "dash index: $dashIndex"
if (( $dashIndex > $doubleDashIndex || $dashIndex < 0)); then
dashIndex=0
fi
# find the offset between the two to get just the text between them
ticketRange=$doubleDashIndex-$dashIndex;
# make sure the range isn't huge
if (( $ticketRange > 7 || $ticketRange < 0 )); then
ticketRange=7
fi
ticketNumber="(${startingBranch:$dashIndex:$ticketRange}) "
if test $local_PREPEND_BRANCHNUMBER_TO_COMMIT_MESSAGES = false; then
ticketNumber=""
fi
if [ "$ticketNumber" == "() " ] || [ "$ticketNumber" == "" ]; then
ticketNumber="";
if test $local_PREPEND_BRANCHNAME_TO_COMMIT_MESSAGES = true; then
ticketNumber="(${startingBranch}) ";
fi
fi
echo "$ git commit -q -m \"${ticketNumber}$msg\" $flag"
git commit -q -m "${ticketNumber}$msg" $flag
echo ${O}
echo
echo "$ git diff-tree --stat HEAD"
git diff-tree --stat HEAD
echo ${O}${H2HL}${X}
echo
echo
echo "Checking status..."
echo ${O}${H2HL}
echo "$ git status"
git status
echo ${O}${H2HL}${X}
echo
# wrap up...
"${gitscripts_path}"push.sh "$startingBranch"
__clear
exit