-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast_commit_push.sh
232 lines (213 loc) · 5.16 KB
/
fast_commit_push.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
231
232
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[0;37m'
RESET='\033[0m'
direct=false
message=""
if [ $# -gt 2 ] || [ $# -eq 0 ]; then
echo -e "Usage : gfc [-d : direct] <commit_string>"
exit 1
fi
for i in "$@"; do
case $i in
-d)
direct=true
;;
*)
if [ -z "$message" ]; then
message="$i"
fi
;;
esac
done
if [ -z "$message" ]; then
echo -e $RED"You should enter commit message to first argument." $RESET
exit 1
fi
added=$(git status --porcelain | grep -E '(^ A|^A)' | cut -c 4-)
modified=$(git status --porcelain | grep -E '(^ M|^M)' | cut -c 4-)
deleted=$(git status --porcelain | grep -E '(^ D|^D)' | cut -c 4-)
untracked=$(git status --porcelain | grep -E '^\?\?' | cut -c 4-)
if $direct; then
echo -e $GREEN"@___Will be committed___@"
for file in $added; do
echo "$file"
done
for file in $modified; do
echo "$file"
done
for file in $untracked; do
echo "$file"
done
for file in $deleted; do
echo "$file"
done
echo -e "@-----------------------@\n" $RESET
echo -e $PURPLE"Committing and pushing directly..\n"$YELLOW
cd $(git rev-parse --show-toplevel)
git add .
cd - | echo -n ""
echo -e -n $YELLOW"Commit : "$WHITE
git commit -m "$message" | sed -n '2p'
echo -e -n $YELLOW"\nPush : "$WHITE
git push $(git remote) $(git branch | grep \* | awk '{ print $2 }')
exit 0
fi
#check git status
clear
while true; do
echo -e $GREEN"@___Added___@"
for file in $added; do
echo "$file"
done
echo -e "@-----------@\n" $RESET
echo -e $RED"@___Modified___@"
for file in $modified; do
echo "$file"
done
echo -e "@--------------@\n" $RESET
echo -e $RED"@___Untracked___@"
for file in $untracked; do
echo "$file"
done
echo -e "@---------------@\n" $RESET
echo -e $RED"@___Deleted___@"
for file in $deleted; do
echo "$file"
done
echo -e "@--------------@\n" $RESET
echo -e $YELLOW"Want to update all changes to current branch? (y/n)"$WHITE
read answer
answer="$(echo "${answer}" | tr '[:upper:]' '[:lower:]')"
if [ "$answer" == "y" ]; then
break
elif [ "$answer" == "n" ]; then
echo -e $YELLOW"Exiting..."
exit 0
else
echo $YELLOW"Invalid input. Please enter 'y' or 'n'." $RESET
fi
done
#check commit message
message=$1
while true; do
clear
echo -e $WHITE"Branch : \"$(git branch | grep \* | awk '{ print $2 }')\""
echo -e $WHITE"Message : \"$message\""
echo -e $YELLOW"is it right? (y/n/q to quit)"$WHITE
read answer
answer="$(echo "${answer}" | tr '[:upper:]' '[:lower:]')"
if [ "$answer" == "y" ]; then
clear
break
elif [ "$answer" == "n" ]; then
clear
echo -e $YELLOW"Input commit message you want to change."$WHITE
read message
elif [ "$answer" == "q" ]; then
clear
echo -e $YELLOW"Exiting..."$WHITE
exit 0
else
echo "Invalid input. please enter y or n."
fi
done
#move to git root directory
cd $(git rev-parse --show-toplevel)
#add all changes
git add .
cd -
#check user's decision
while true; do
staged=$(git status --porcelain | grep -E '(^A|^M|^D)' | tr '\n' ' ')
unstaged=$(git status --porcelain | grep -Ev '(^A|^M|^D)' | tr '\n' ' ')
echo -e $GREEN"@________Staged________@"
modified=false
added=false
deleted=false
untracked=false
for file in $staged; do
if [ $file == 'M' ] && ! $modified; then
modified=true
echo "M"
continue
fi
if [ $file == 'A' ] && ! $added; then
added=true
echo "A"
continue
fi
if [ $file == 'D' ] && ! $deleted; then
deleted=true
echo "D"
continue
fi
if [ $file != 'M' ] && [ $file != 'A' ] && [ $file != 'D' ]; then
echo "$file"
fi
done
echo -e "@-----------------------@\n" $RESET
echo -e $RED"@_______Not Staged______@"
modified=false
added=false
deleted=false
untracked=false
for file in $unstaged; do
if [ $file == 'M' ] && ! $modified; then
modified=true
echo "M"
continue
fi
if [ $file == '??' ] && ! $untracked; then
untracked=true
echo "??"
continue
fi
if [ $file == 'D' ] && ! $deleted; then
deleted=true
echo "D"
continue
fi
if [ $file != 'M' ] && [ $file != '??' ] && [ $file != 'D' ]; then
echo "$file"
fi
done
echo -e "@-----------------------@\n" $RESET
echo -e $YELLOW"do you want to push these updates?\n(y / q / [filename] : to unstage / a [filename] : to stage / c : undo / s : stop with commit)"$WHITE
read -e answer
answer="$(echo "${answer}" | tr '[:upper:]' '[:lower:]')"
if [ "$answer" == "y" ]; then
clear
break
elif [ "$answer" == "q" ]; then
clear
echo -e $YELLOW"Cancelling commit..."$WHITE
git reset | echo -n ""
exit 0;
elif [ $(echo "$answer" | awk '{ print $1 }') == "a" ]; then
git add $(echo "$answer" | awk '{ print $2 }') | grep noting
clear
elif [ "$answer" == "c" ]; then
git add $prev | grep noting
clear
elif [ "$answer" == "s" ]; then
clear
echo -e $YELLOW"Stopping with staged commit..."
git commit -m "$message" | sed -n '2p'
exit 0
else
prev=$answer
git restore --staged $answer
clear
fi
done
echo -e -n $YELLOW"Commit : "$WHITE
git commit -m "$message" | sed -n '2p'
#get current working branch
echo -e -n $YELLOW"Push : "$WHITE
git push $(git remote | sed -n '1p') $(git branch | grep \* | awk '{ print $2 }')