-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliases.sh
341 lines (269 loc) · 8.35 KB
/
aliases.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env bash
# Git Aliases
{ # this ensures the entire script is downloaded #
# helpers
# ----------------------------------------------------------------------------------
git_echo() {
command printf %s\\n "$*" 2>/dev/null
}
git_exit() {
# Catch errors, if any
if [ $? -ne 0 ]; then
echo
echo "==> ${1:-"Exiting, some unknown error occurred"}"
echo
fi
}
git_detect_remote() {
if [[ -z "$1" ]]; then
_ORIGIN="$(git remote)"
_LENGTH="$(git remote | wc -l)"
if [[ $_LENGTH -eq 1 ]]; then
git_echo "==> Detected Remote: $_ORIGIN"
else
git_echo "==> Multiple Remotes Detected:"
git_echo "$_ORIGIN"
exit 1
fi
else
_ORIGIN="$1"
git_echo "==> Requested Remote: $_ORIGIN"
fi
if [[ -z "$2" ]]; then
_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
git_echo "==> Detected Branch: $_BRANCH"
else
_BRANCH="$2"
git_echo "==> Requested Branch: $_BRANCH"
fi
}
git_commit_lint() {
local message_string="$1"
# Regular expression pattern
regex="^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style)(\([a-zA-Z0-9]+\))?: .*$"
if [[ $message_string =~ $regex ]]; then
echo ">> Your commit message adheres to the commit rules"
echo
else
echo
echo "Oops! Your commit message does not conform to the commit rules."
echo "Please ensure your commit message follows the conventional commits format:"
echo " type(scope): description"
echo "Where:"
echo " - 'type' could be one of: build, chore, ci, docs, feat, fix, perf, refactor, revert, style"
echo " - 'scope' is optional and can be anything specifying the place of the commit change"
echo " - 'description' is a short description of the change"
exit 1
fi
}
# aliases
# ----------------------------------------------------------------------------------
# git aliases description
git-aliases() { (
set -e #fail early
git_echo "git-aliases is at $(git-ver)"
) || git_exit "Failed to display git aliases version"; }
# gives current git version
git-ver() { (
set -e #fail early
git_echo "v1.2-beta"
) || git_exit "Failed to retrieve git version"; }
# long list of commit history
git-ll() { (
set -e #fail early
git log --abbrev-commit --decorate --pretty=format:"%C(yellow)%h %C(reset)-%C(red)%d %C(reset)%s %C(green)(%ar) %C(blue)[%an]" "$@"
) || git_exit "Failed to display commit history"; }
# stage changes & commit
git-it() { (
set -e #fail early
git add --all
git_commit_lint "$1"
git commit -m "$1"
) || git_exit "Failed to stage and commit changes"; }
# commit changes & push
git-up() { (
set -e #fail early
git-it "$1"
git-push "${@:2}"
) || git_exit "Failed to commit and push changes"; }
# modify last commit
git-amend() { (
set -e #fail early
git add --all
if [[ -n "$1" ]]; then
git_commit_lint "$1"
git commit --amend --message="$1"
else
git commit --amend --no-edit
fi
) || git_exit "Failed to amend the last commit"; }
# modify last commit with current time
git-amend-now() { (
set -e #fail early
git add --all
if [[ -n "$1" ]]; then
git_commit_lint "$1"
git commit --amend --reset-author --message="$1"
else
git commit --amend --reset-author --no-edit
fi
) || git_exit "Failed to amend the last commit with the current time"; }
# push changes to remote
git-push() { (
set -e #fail early
if [[ -z "$1" ]]; then local _ORIGIN; fi
if [[ -z "$2" ]]; then local _BRANCH; fi
git_detect_remote "${@:1:2}"
git push "${_ORIGIN}" "${_BRANCH}" "${@:3}"
) || git_exit "Failed to push changes"; }
# push changes forcefully to remote
git-pushf() { (
set -e #fail early
if [[ -z "$1" ]]; then local _ORIGIN; fi
if [[ -z "$2" ]]; then local _BRANCH; fi
git_detect_remote "${@:1:2}"
git push --force "${_ORIGIN}" "${_BRANCH}" "${@:3}"
) || git_exit "Failed to force push changes"; }
# pull changes from remote
git-pull() { (
set -e #fail early
if [[ -z "$1" ]]; then local _ORIGIN; fi
if [[ -z "$2" ]]; then local _BRANCH; fi
git_detect_remote "${@:1:2}"
git pull "${_ORIGIN}" "${_BRANCH}" "${@:3}"
) || git_exit "Failed to pull changes"; }
# update local code as per remote
git-pullf() { (
set -e #fail early
if [[ -z "$1" ]]; then local _ORIGIN; fi
if [[ -z "$2" ]]; then local _BRANCH; fi
git_detect_remote "${@:1:2}"
git fetch --all
git reset --hard "$_ORIGIN/$_BRANCH"
) || git_exit "Failed to forcibly update local code"; }
# remove unwanted reflogs
git-clean() { (
set -e #fail early
git gc --prune=now --aggressive
git_echo
git_echo "==> Git Repository Cleaned"
git_echo
) || git_exit "Failed to clean the git repository"; }
# clear the workspace
git-clear() { (
set -e #fail early
git reset --hard
git clean -df
git_echo
git_echo "==> Git Repository Cleared"
git_echo
) || git_exit "Failed to clear the git repository"; }
# sync everything
git-sync() { (
set -e #fail early
if [[ -z "$1" ]]; then local _ORIGIN; fi
if [[ -z "$2" ]]; then local _BRANCH; fi
git_detect_remote "${@:1:2}"
git add --all
git stash
git fetch --all
git checkout "$_BRANCH"
git reset --hard "$_ORIGIN/$_BRANCH"
git remote prune "$_ORIGIN"
git stash pop
git_echo
git_echo "==> Synced with '$_ORIGIN/$_BRANCH'"
git_echo
) || git_exit "Failed to sync with remote"; }
# fix for previous commit
git-fixit() { (
set -e #fail early
local _HASH="${1:-HEAD}"
_HASH="$(git rev-parse "$_HASH")"
git add --all
git commit --no-verify --fixup "$_HASH"
) || git_exit "Failed to create a fixup commit"; }
# fix commit & push changes
git-fixup() { (
set -e #fail early
git-fixit "$1"
git-push "${@:2}"
) || git_exit "Failed to fixup and push changes"; }
# rebase all commit automatically
git-rebase() { (
set -e #fail early
EDITOR=true git rebase --interactive --autosquash --autostash --rebase-merges --no-fork-point "$@"
) || git_exit "Failed to rebase commits"; }
# add merge
git-merge() { (
set -e #fail early
if [[ -z "$1" ]]; then
echo "Error: No branch specified."
exit 1
fi
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "$current_branch" == "$1" ]]; then
echo "Error: You cannot merge into the same branch you are currently on."
exit 1
fi
if [[ -n "$2" ]]; then
EDITOR=true git merge "$1" --no-ff -m "$2"
else
EDITOR=true git merge "$1" --no-ff --log
fi
) || git_exit "Failed to merge branch"; }
git-merge-to() { (
set -e # fail early
if [[ -z "$1" ]]; then
echo "Error: No branch specified."
exit 1
fi
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "$current_branch" == "$1" ]]; then
echo "Error: You cannot merge into the same branch you are currently on."
exit 1
fi
if [[ -n "$2" ]]; then
git checkout "$1"
EDITOR=true git merge "$current_branch" --no-ff -m "$2"
else
git checkout "$1"
EDITOR=true git merge "$current_branch" --no-ff --log
fi
# Checkout back to the original branch
git checkout "$current_branch"
) || git_exit "Failed to merge into target branch"; }
# reset commit
git-reset() { (
set -e #fail early
if [[ -n "$1" ]]; then
git reset --soft "$1"
else
git reset --soft HEAD^
fi
) || git_exit "Failed to reset commit"; }
# reset commit forcefully
git-resetf() { (
set -e #fail early
if [[ -n "$1" ]]; then
git reset --hard "$1"
else
git reset --hard HEAD^
fi
) || git_exit "Failed to force reset commit"; }
# miscellaneous
# ----------------------------------------------------------------------------------
__git_branches() {
if [ -d .git ]; then
echo "$(git branch -a | sed 's/^[* ]*//; s/remotes\///' | tr '\n' ' ' | xargs)"
else
echo ""
fi
}
if command -v git-ver &>/dev/null; then
complete -W "$(__git_branches)" git-merge
complete -W "$(__git_branches)" git-merge-to
complete -W "$(__git_branches)" git-reset
complete -W "$(__git_branches)" git-resetf
fi
} # this ensures the entire script is downloaded #