-
Notifications
You must be signed in to change notification settings - Fork 2
/
.lazyGit.sh
executable file
·304 lines (278 loc) · 8.9 KB
/
.lazyGit.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/bash
pull() {
# ------------- Quick Pull -----------------
if [[ $(git status --porcelain=v1 2>/dev/null | wc -l) -gt 0 ]]; then
echo "Untracked changes: $(git update-index --refresh)"
echo "commit[1] or stash[2] changes"
printf '%s ' "Please select an option 1[commit local changes] 2[stash local changes] "
read cSOption
case $cSOption in
1)
git add .
printf '%s ' "Please provide your git commit message"
read gitcmsg
git commit --allow-empty -a -m $gitcmsg
pullRemoteBranch
;;
2)
printf '%s ' "Please provide your git stash message"
read gitcmsg
git stash push -m $gitcmsg
pullRemoteBranch
;;
esac
else
pullRemoteBranch
fi
}
pullRemoteBranch() {
echo "select option for pulling new commits from a remote server"
echo "1. Pull Current Branch: $(git rev-parse --abbrev-ref HEAD)"
echo "2. Pull from Existing Other Branch"
echo "3. Pull specific commits [cherry-pick commits]"
printf '%s ' "Please select an option: "
read pullOption
case $pullOption in
1 | c | C)
git pull origin $(git rev-parse --abbrev-ref HEAD)
return
;;
2 | e | E)
echo "Loading all local and remote branches..."
local selectedBranch=$(git branch -a | tr -s '*' ' ' | tr -d "[:blank:]" | cut -d/ -f3 | sort -u | select_from_list)
local STATUS=$?
# Check if user selected something
if [ $STATUS == 0 ]; then
echo "Branch selected by user:" $selectedBranch
if [[ $selectedBranch=$(git rev-parse --abbrev-ref HEAD) ]]; then
git pull origin $(git rev-parse --abbrev-ref HEAD)
else
git pull origin $selectedBranch
fi
else
echo "Cancelled!"
fi
return
;;
3 | cp | CP)
cherrypick
return
;;
*) echo "Please answer 1/c/C, 2/e/E or 3/cp/CP." ;;
esac
}
push() {
# ------------- Quick Push -----------------
git add .
git commit --allow-empty -a -m "$*"
# give user to select/create branch
echo "select option for publishing new local commits on a remote server"
echo "1. Current Branch: $(git rev-parse --abbrev-ref HEAD)"
echo "2. Existing Branch"
echo "3. New Branch"
printf '%s ' "Please select an option "
read branchOption
case $branchOption in
1 | c | C)
git push -u origin $(git rev-parse --abbrev-ref HEAD)
return
;;
2 | e | E)
echo "Loading all local and remote branches..."
selectedBranch=$(git branch -a | tr -s '*' ' ' | tr -d "[:blank:]" | cut -d/ -f3 | sort -u | select_from_list)
local STATUS=$?
# Check if user selected something
if [ $STATUS == 0 ]; then
echo "Branch selected by user:" $selectedBranch
if [[ $selectedBranch=$(git rev-parse --abbrev-ref HEAD) ]]; then
git push -u origin $(git rev-parse --abbrev-ref HEAD)
else
commitedCodeBranch=$(git rev-parse --abbrev-ref HEAD)
git checkout $selectedBranch
git merge $commitedCodeBranch
git push -u origin $selectedBranch
fi
else
echo "Cancelled!"
fi
return
;;
3 | n | N)
printf '%s ' "Enter branch name: "
read newBranch
git checkout -b $newBranch
git push -u origin $newBranch
return
;;
*) echo "Please answer 1, 2 or 3." ;;
esac
}
cherrypick() {
# ------------- Quick cherry-pick -----------------
printf '%s ' "Please provide space-seprated commit ids ex. ("34cea36zzz" "18cc3c8zzz" "e8637dfzzz")"
read commitID
declare -a commitsArray=$commitID
local arraylength=${#commitsArray[@]}
declare cherryPickBranch=cherryPick-$(git rev-parse --abbrev-ref HEAD)-$(date "+%Y.%m.%d-%H.%M.%S")
git checkout -b $cherryPickBranch
# use for loop to read all values of commits
for ((i = ${arraylength}; i > 0; i--)); do
echo $i " / " ${arraylength} " : " ${commitsArray[$i - 1]}
local rawHash=${commitsArray[$i - 1]}
local hash=${rawHash:0:7}
printf "cherry picking ${hash} ...\n"
local read_status=$(git cherry-pick -x "${hash}" 2>&1)
local empty_message="The previous cherry-pick is now empty"
local error_message="error: cherry-pick is not possible because you have unmerged files"
local online_edited="edited online with Bitbucket"
if [[ $read_status = *"$online_edited"* ]]; then
echo $read_status
exit 0
elif [[ $read_status = *"$error_message"* ]]; then
echo $read_status
exit 0
elif [[ $read_status = *"$empty_message"* ]]; then
echo yes
git commit --allow-empty -m "${hash} empty"
else
echo no
if [ $? -eq 0 ]; then
echo "$hash - cherry-picked"
else
echo "There are conflicts to resolve!"
exit 0
fi
fi
done
git push -u origin $cherryPickBranch
}
show() {
selectedCommit=$(git log -n 10 --oneline --pretty="format:%h:%s:%ce:%ci" | select_from_list)
printf "Modified files in the selected commit: \n"
git diff-tree --no-commit-id --name-only -r $(echo "$selectedCommit" | cut -d: -f1)
}
init() {
printf '%s ' "Initialize the local directory as a Git repository? [Y/n]"
read gitSetup
case $gitSetup in
[Yy]*)
git init
if [[ $(git config user.name) ]]; then
echo "Git configurations found!"
echo "Configured Git Name: $(git config user.name)"
echo "Configured Git Email: $(git config user.email)"
printf '%s '"Wanted to change? [Y/n]"
read gitConfigChange
case $gitConfigChange in
[Yy]*)
config
;;
[Nn]*) ;;
esac
else
echo "No git configuartion on this system"
config
fi
printf '%s ' "Push to existing Git Repositary. Repo URL? [ex: git@bitbucket.org:USER/REPO.git]"
read gitRepo
git remote add origin $gitRepo
git remote -v
git add .
git commit --allow-empty -a -m "$*"
git push -u origin master
return
;;
[Nn]*) return ;;
*) echo "Please answer yes or no." ;;
esac
}
config() {
printf '%s ' "Global(system level) or Local(project level)? [G/l]"
read gitConfigSetup
case $gitConfigSetup in
[Gg]*)
git config --global credential.helper store
printf '%s ' "Enter your git name: "
read gitName
git config --global --add user.name $gitName
printf '%s ' "Enter your git email: "
read gitEmail
git config --global --add user.email $gitEmail
return
;;
[Ll]*)
git config --local credential.helper store
printf '%s ' "Enter your git name: "
read gitName
git config --local --add user.name $gitName
printf '%s '"Enter your git email: "
read gitEmail
git config --local --add user.email $gitEmail
return
;;
esac
}
select_from_list() {
prompt="Please select an item:"
local -a options=()
if [ -z "$1" ]; then
# Get options from PIPE
input=$(cat /dev/stdin)
while read line; do
options+=("$line")
done <<<"$input"
else
# Get options from command line
for var in "$@"; do
options+=("$var")
done
fi
# Close stdin
# 0<&-
# open /dev/tty as stdin
exec 0</dev/tty
PS3="$prompt "
select opt in "${options[@]}" "Quit"; do
if ((REPLY == 1 + ${#options[@]})); then
exit 1
elif ((REPLY > 0 && REPLY <= ${#options[@]})); then
break
else
echo "Invalid option. Try another one."
fi
done
echo $opt
}
function lazygit() {
if [ -d .git ]; then
declare opt
declare OPTARG
declare OPTIND
while getopts ":u:ds" opt; do
case $opt in
d)
echo "pull was triggered, Parameter: $OPTARG" >&2
pull
;;
u)
echo "push was triggered, Parameter: $OPTARG" >&2
push $OPTARG
;;
s)
echo "show was triggered, Parameter: $OPTARG" >&2
show
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 0
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 0
;;
esac
done
else
init $*
fi
}