-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-bash-aliases.bash
328 lines (297 loc) · 8.18 KB
/
git-bash-aliases.bash
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
#!/bin/bash
# @name git-bash-aliases
# @brief Short aliases for commonly used git commands.
# @description
# These aliases allow you to quickly perform everyday commands with git.
# @description `git reset` the current branch to the one of the same name on `origin`
#
# Mnemonic: *git reset origin*
#
# For a local branch named `branch`, this script performs:
# ```
# git reset --hard origin/branch
# ```
#
# Arguments are forwarded to `git reset`.
#
# @example
# gro
#
# @exitcode 0 Successful.
# @exitcode 1 Unable to read current branch.
# @exitcode 2 Unable to read user input.
# @exitcode 3 Operation canceled by user.
gro() {
local -r RED=1
local -r BLUE=4
LOCAL_BRANCH="$(git branch --show-current 2>/dev/null)"
if [ $? -ne 0 ] ; then
>&2 echo "Failed to read current branch. Are you in a git repository with the HEAD pointing to a local branch?"
return 1
fi
ORIGIN_BRANCH="origin/${LOCAL_BRANCH}"
echo "This operation will $(tput setaf ${RED})hard-reset$(tput sgr0) the current branch to the origin:
$(tput setaf ${BLUE})Current branch$(tput sgr0): ${LOCAL_BRANCH}
$(tput setaf ${BLUE})Reset to$(tput sgr0): ${ORIGIN_BRANCH}"
if [[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]] ; then
echo "$(tput smso)$(tput setaf ${RED})PROCEED WITH CAUTION:$(tput sgr0)$(tput smso) The current worktree has uncommitted changes.$(tput sgr0)"
fi
PROMPT_MESSAGE="Do you wish to continue? [yN] "
read -n 1 -p "${PROMPT_MESSAGE}"
READ_ERROR="$?"
echo
if [ ${READ_ERROR} = 0 ] ; then
case ${REPLY} in
Y|y)
git reset --hard ${ORIGIN_BRANCH}
return 0
;;
*)
esac
else
>&2 echo "Reset operation canceled. Reason: Failed to read input; error code: $?."
return 2
fi
>&2 echo "Reset operation canceled. Reason: User input '${REPLY}'."
return 3
}
# @description `git log --oneline`
#
# @example
# glo origin/develop
# glo -2
# glo -3 develop
glo() {
git log --oneline $*
}
# @description `git rebase --interactive --autosquash`
#
#
# Mnemonic: *git rebase interactive autosquash*
#
# @example
# gr origin/develop
gria() {
git rebase --interactive --autosquash "$@"
}
# @description `git rebase --interactive --autosquash origin/develop`
#
# @example
# grod
grod() {
gria "origin/develop" "$@"
}
# @description `git rebase --interactive --autosquash origin/master`
#
# @example
# grom
grom() {
gria "origin/master" "$@"
}
# @description `git rebase --interactive --autosquash` with the current branch onto the branch of the same name on `origin`
#
# @example
# gro
gro() {
LOCAL_BRANCH=$(__get_local_branch)
[ $? -ne 0 ] && return 1
ORIGIN_BRANCH="origin/${LOCAL_BRANCH}"
gria "${ORIGIN_BRANCH}" "$@"
}
# @description `git diff origin/develop`
#
# @example
# gdod
gdod() {
git diff origin/develop "$@"
}
# @description `git diff --stat origin/develop`
#
# @example
# gdsod
gdsod() {
gdod --stat "$@"
}
# @description `git diff origin/develop`
#
# @example
# gdom
gdom() {
git diff origin/master "$@"
}
# @description `git diff --stat origin/master`
#
# @example
# gdsom
gdsom() {
gdom --stat "$@"
}
# @description `git diff` with the branch of the same name on `origin`
#
# @example
# gdo
#
# @exitcode 1 Unable to read current branch.
gdo() {
LOCAL_BRANCH=$(__get_local_branch)
[ $? -ne 0 ] && return 1
ORIGIN_BRANCH="origin/${LOCAL_BRANCH}"
git diff "${ORIGIN_BRANCH}" "$@"
}
# @description `git commit --fixup=$(git diff --staged --name-only HEAD)`
#
# @example gfl
#
# Mnemonic: *git fixup last*
gfl() {
git commit --fixup="$(
cd $(git rev-parse --show-toplevel)
git diff --staged --name-only | xargs git rev-list -1 HEAD --
)"
}
# @description `git diff --stat` with the branch of the same name on `origin`
#
# @example
# gdso
#
# @exitcode 1 Unable to read current branch.
gdso() {
gdo --stat "$@"
}
# @description `git pull --ff-only`
#
# @example
# gpf
gpf () {
git pull --ff-only "$@"
}
# @description Push the branch to origin and set the upstream.
#
# @example
# gposu -f
#
# @exitcode 1 Unable to read current branch.
gposu () {
LOCAL_BRANCH=$(__get_local_branch)
[ $? -ne 0 ] && return 1
git push --set-upstream origin "${LOCAL_BRANCH}" "$@"
}
# @description `git push --force-with-lease origin` with the current branch to the upstream branch.
#
# If the upstream branch is not set, this command may fail.
#
# @example
# gpof
#
# @exitcode 1 Unable to read current branch.
gpof () {
LOCAL_BRANCH=$(__get_local_branch)
[ $? -ne 0 ] && return 1
git push --force-with-lease origin "${LOCAL_BRANCH}" "$@"
}
# @description Download and reinstall bash completion for git.
#
# Periodically, git bash completions seem to stop working. One fix is to
# download the bash script from the public repo and copy it over the
# currently installed file. This command does this, replacing, the
# existing command.
#
# To fix an existing bash session after this command has been run:
#
# ```
# source ${HOME}/.bashrc
# ```
#
# @example
# reinstall-git-bash-completion
# source ${HOME}/.bashrc
reinstall-git-bash-completion () {
local git_version=$(git --version | sed 's/^.* \([0-9\\.]\+$\)/\1/g')
rm -f /tmp/git-completion.bash
wget https://raw.githubusercontent.com/git/git/v${git_version}/contrib/completion/git-completion.bash -O /tmp/git-completion.bash
sudo mv /tmp/git-completion.bash /etc/bash_completion.d/git-completion.bash
}
# @description Return the current branch of the worktree.
#
# @exitcode 0 Successful.
# @exitcode 1 Unable to read current branch.
__get_local_branch() {
LOCAL_BRANCH="$(git branch --show-current 2>/dev/null)"
if [ $? -ne 0 ] ; then
>&2 echo "Failed to read current branch. Are you in a git repository with the HEAD pointing to a local branch?"
return 1
fi
echo "${LOCAL_BRANCH}"
}
# @description Show a sorted list of objects in the Git database.
#
# @stdout A list of objects sorted by size in descending order
#
# @exitcode 0 Successful.
#
# @see https://stackoverflow.com/a/42544963/2001889
gba-list-objects() {
local -r batch_check='%(objecttype) %(objectname) %(objectsize) %(rest)'
git rev-list --objects --all \
| git cat-file --batch-check="${batch_check}" \
| sed -n 's/^blob //p' \
| sort --numeric-sort --key=2 \
| cut -c 1-12,41- \
| $(command -v gnumfmt || echo numfmt) \
--field=2 --to=iec-i --suffix=B \
--padding=7 --round=nearest
}
# @description Show git commit hash for the specified file path and hash.
#
# Optionally, specificy a hash for a specific version of the file.
#
# @arg $1 The path to the file, relative to the root of the repository.
# @arg $2 (Optional) The has for the version of interest.
#
# @example
# gba-find-commit path/to.file
# gba-find-commit path/to.file 21339ea0c4ce
#
# @exitcode 0 Successful.
# @exitcode 1 Incorrect number of arguments.
#
# @see https://stackoverflow.com/a/32611564/2001889
gba-find-commit() {
[[ $# -lt 1 ]] && (>&2 echo "Too few arguments ($# < 1)") && return 1 || true
[[ $# -gt 2 ]] && (>&2 echo "Too many arguments ($# > 2)") && return 1 || true
local -r fpath="$1"
[[ $# == 2 ]] && hash_filter=( grep -q "$2" ) || hash_filter=( cat )
local -r gitcmd="git ls-tree % -- \"${fpath}\" | ${hash_filter[@]} && echo %"
git log --all --pretty=format:%H -- "${fpath}" \
| xargs -n1 -I% bash -c "${gitcmd}"
}
# @description Show git log for the specified file path and hash.
#
# Optionally, specificy a hash for a specific version of the file.
#
# @arg $1 The path to the file, relative to the root of the repository.
# @arg $2 (Optional) The has for the version of interest.
#
# @example
# gba-show-commit path/to.file
# gba-show-commit path/to.file 21339ea0c4ce
#
# @exitcode 0 Successful.
# @exitcode 1 Incorrect number of arguments.
# @exitcode 2 Object not found.
gba-show-commit() {
local -r commit="$(gba-find-commit "$@")"
if [[ -z "${commit}" ]] ; then
(>&2 echo "Object $1 with hash $2 not found")
return 2
fi
git log -n1 "${commit}"
}
_completion_loader git || true
# @internal
__glo_complete() {
__git_complete_refs --cur="${COMP_WORDS[COMP_CWORD]}" --sfx=""
}
complete -F __glo_complete -o default glo
complete -F __glo_complete -o default gria
complete -F __glo_complete -o default gds