-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgb-git
executable file
·343 lines (326 loc) · 11.2 KB
/
gb-git
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
342
343
#!/usr/bin/env bash
## By Davoud Arsalani
## https://github.com/davoudarsalani/scripts
## https://github.com/davoudarsalani/scripts/blob/master/gb-git
## https://raw.githubusercontent.com/davoudarsalani/scripts/master/gb-git
## https://davoudarsalani.ir
# NOTE assigning dest="${1:-.}" and using git -C "$dest" instead of git -C "${1:-.}" did not work
## %x09 = tab (https://stackoverflow.com/questions/1441010/the-shortest-possible-output-from-git-log-containing-author-and-date)
## (https://git-scm.com/docs/pretty-formats)
function if_git { ## {{{
git -C "${1:-.}" rev-parse --is-inside-work-tree &>/dev/null && printf 'true\n' || printf 'false\n'
}
## }}}
function git_diff_all { ## {{{
## NOTE nearly share the same options for diff as JUMP_2
git -C "${1:-.}" diff --unified=0 --color=always --src-prefix=1st: --dst-prefix=2nd: | less -R
## --unified=0 makes git show only the modified lines
## --color-words show modified words side-by-side
}
## }}}
function git_diff_specific { ## {{{
## NOTE nearly share the same options for diff as JUMP_2
git -C "${1:-.}" diff --unified=0 --color=always --src-prefix=1st: --dst-prefix=2nd: -- "$2" | sed 1,4d ## $2 is item name
## --unified=0 makes git show only the modified lines
## --color-words show modified words side-by-side
}
## }}}
function git_log { ## {{{
## NOTE nearly share the same options for log as JUMP_1
## %G?: show "G" for a good (valid) signature, "B" for a bad signature, "U" for a good signature with unknown validity and "N" for no signature
git -C "${1:-.}" \
log --color --abbrev-commit --full-history --all --graph \
--format='%C(always,blue)%h %C(always,magenta)%G? %C(always,bold black)%>(15,trunc)%cr %C(always,green)%<(4,trunc)%cn %C(always,green)%d %C(always,reset)%s'
}
## }}}
function git_reflog { ## {{{
## NOTE nearly share the same options for log as JUMP_1
git -C "${1:-.}" \
reflog --color --abbrev-commit --full-history \
--format='%C(always,yellow)%h %C(always,magenta)%G? %C(always,bold black)%>(15,trunc)%cr %C(always,green)%<(4,trunc)%cn %C(always,green)%d %C(always,reset)%s'
}
## }}}
function git_show { ## {{{ show changes for a commit
git -C "${1:-.}" show --unified=0 --color=always "$2" ## $2 is commit hash
## --unified=0 makes git show only the modified lines
## --color-words show modified words side-by-side
}
## }}}
function git_add_all { ## {{{
git -C "${1:-.}" add -A
}
## }}}
function git_add_specific_or_pattern { ## {{{
git -C "${1:-.}" add "$2" ## $2 is item/pattern
}
## }}}
function git_branch_create { ## {{{
git -C "${1:-.}" branch "$2" ## $2 is branch
}
## }}}
function git_branch_delete_all { ## {{{
git -C "${1:-.}" branch -d "${@:2}" ## ${@:2} is branches
}
## }}}
function git_branch_delete_specific { ## {{{
git -C "${1:-.}" branch -d "$2" ## $2 is branch
}
## }}}
function git_branch_force_delete_all { ## {{{
git -C "${1:-.}" branch -D "${@:2}" ## ${@:2} is branches
}
## }}}
function git_branch_force_delete_specific { ## {{{
git -C "${1:-.}" branch -D "$2" ## $2 is branch
}
## }}}
function git_branch_rename { ## {{{
git -C "${1:-.}" branch -m "$2" "$3" ## $2 and $3 are current and new name of branch
}
## }}}
function git_branch_switch { ## {{{
git -C "${1:-.}" switch "$2" ## $2 is branch
}
## }}}
function git_branches { ## {{{
git -C "${1:-.}" branch -a --color=always | \grep -v '/HEAD\s' | sed 's/^ \+//' | sort
}
## }}}
function git_branches_exclude_remote { ## {{{
git -C "${1:-.}" branch | sed 's/^ \+//' | sort
}
## }}}
function git_commit_in_editor { ## {{{
git -C "${1:-.}" commit
}
## }}}
function git_commit_amend_in_vim { ## {{{
git -C "${1:-.}" commit --amend ## opens editor
}
## }}}
function git_commit_amend_auto { ## {{{ without having to open vim
## https://github.com/clokep/dotfiles/blob/main/.gitconfig
git -C "${1:-.}" commit --amend -C HEAD ## opens editor
}
## }}}
function git_commit_amend_with_message { ## {{{
git -C "${1:-.}" commit --amend -m "$2" ## $2 is message
}
## }}}
function git_commit_with_message { ## {{{
git -C "${1:-.}" commit -m "$2" ## $2 is message
}
## }}}
function git_commits_ahead { ## {{{
local commits_ahead
commits_ahead="$(git -C "${1:-.}" branch -v | \grep 'ahead' | \grep -ioP '(?<=\[).*?(?=\])' | awk '{print $2}')"
## ^^ previously: git -C "${1:-.}" status -s | sed '1q;d' | \grep -i 'ahead' | awk '{print $NF}' | sed 's/[^0-9]//g'
[ "$commits_ahead" ] && printf '%s\n' "$commits_ahead" || printf '0\n'
}
## }}}
function git_commits_behind_ahead { ## {{{ getting the total number of "different" commits between local and remote (https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git):
local current_branch
current_branch="$(git_current_branch "${1:-.}")"
git -C "${1:-.}" rev-list HEAD...origin/"$current_branch" --count
}
## }}}
function git_commits_count { ## {{{
git -C "${1:-.}" rev-list HEAD --count
}
## }}}
function git_commits_count_specific { ## {{{
git -C "${1:-.}" rev-list HEAD --count "$2" ## $2 is item name
}
## }}}
function git_config_edit { ## {{{
git -C "${1:-.}" config -e
}
## }}}
function git_config_see { ## {{{
git config --list ## no need to -C "${1:-.}"
}
## }}}
function git_current_branch { ## {{{
git -C "${1:-.}" branch | sed -n '/\* /s///p'
## OR 1: git -C "${1:-.}" branch -a 2>/dev/null | sed '/^[^*]/d;s/* \(.*\)/\1/')"
## 2: git -C "${1:-.}" rev-parse --abbrev-ref HEAD ## FIXME only returns HEAD instead of (HEAD detached at 56bb92a) when reverted to a commit
}
## }}}
function git_empty_commit { ## {{{
git -C "${1:-.}" commit --allow-empty -n ## opens editor
}
## }}}
function git_empty_commit_with_message { ## {{{
git -C "${1:-.}" commit --allow-empty -n -m "$2" ## $2 is message
}
## }}}
function git_garbage_clean { ## {{{
git -C "${1:-.}" gc --prune=now --aggressive
}
## }}}
function git_hash_head { ## {{{
git -C "${1:-.}" log -1 --format=%h
## PREVIOUSLY: git -C "${1:-.}" rev-parse --short --verify HEAD
}
## }}}
function git_hash_last_commit { ## {{{
git -C "${1:-.}" log -1 --format=%h --all
}
## }}}
function git_if_behind { ## {{{
source "$HOME"/main/scripts/gb-color
local commits_ahead commits_behind commits_behind_ahead temp_path
action_now 'updating remote'
commits_ahead="$(git_commits_ahead "${1:-.}")"
if [ "$proxy" == 'true' ]; then
git_remote_update_proxy "${1:-.}"
else
git_remote_update_noproxy "${1:-.}"
fi
commits_behind_ahead="$(git_commits_behind_ahead "${1:-.}")"
## getting commits_behind
(( commits_behind="commits_behind_ahead - commits_ahead" ))
(( commits_behind > 0 )) && {
temp_path="$(printf '%s\n' "${1:-.}" | sed "s|$HOME|\~|")" ## NOTE do NOT replace | with / in sed
red "local is $commits_behind commits behind remote."
red "try: git -C $temp_path pull"
exit 37
}
}
## }}}
function git_last_commit { ## {{{ returns last commit in repo
## usage:
## git_last_commit 'repo_path' ## returns date/time of last commit in repo
## git_last_commit 'repo_path' 'item' ## returns date/time item was last committed
git -C "${1:-.}" log -1 --format=%ci "${2:-.}" ## . in "${2:-.}" does nothing but preventing errors if there is no $2 passed
}
## }}}
function git_pull_noproxy { ## {{{
git -C "${1:-.}" pull
}
## }}}
function git_pull_proxy { ## {{{
torsocks git -C "${1:-.}" pull
}
## }}}
function git_push_failed { ## {{{
source "$HOME"/main/scripts/gb-color
red 'push failed.'
red "try: git -C ${1:-.} push --set-upstream origin master" ## --set-upstream or -u
}
## }}}
function git_push_noproxy { ## {{{
git -C "${1:-.}" push || git_push_failed
}
## }}}
function git_push_proxy { ## {{{
torsocks git -C "${1:-.}" push || git_push_failed
}
## }}}
function git_relative_head { ## {{{
git -C "${1:-.}" log -1 --format=%cr | sed 's/minute/min/;s/hour/hr/;s/ /-/g'
}
## }}}
function git_relative_last_commit { ## {{{
git -C "${1:-.}" log -1 --format=%cr --all | sed 's/minute/min/;s/hour/hr/;s/ /-/g'
}
## }}}
function git_remote_update_noproxy { ## {{{
git -C "${1:-.}" remote -v update
}
## }}}
function git_remote_update_proxy { ## {{{
torsocks git -C "${1:-.}" remote -v update
}
## }}}
function git_remotes { ## {{{
local rmt
rmt="$(git -C "${1:-.}" remote -v)"
[ "$rmt" ] && printf '%s\n' "$rmt"
}
## }}}
function git_remove { ## {{{
git -C "${1:-.}" rm -r --cached "$2" ## $2 is pattern
}
## }}}
function git_reset_hard { ## {{{
git -C "${1:-.}" reset --hard "$2" ## $2 is commit hash
}
## }}}
function git_reset_mixed { ## {{{
git -C "${1:-.}" reset --mixed "$2" ## $2 is commit hash
}
## }}}
function git_reset_soft { ## {{{
git -C "${1:-.}" reset --soft "$2" ## $2 is commit hash
}
## }}}
function git_reset_temp { ## {{{
## this reverts to a commit hash in form of a temporary branch
## named 'HEAD detached at HASH' and switches to it.
## The branch will be automatically deleted after switching back to master [or any other branch !]
git -C "${1:-.}" checkout "$2" ## $2 is commit hash
}
## }}}
function git_restore_all { ## {{{
git -C "${1:-.}" restore .
## PREVIOUSLY: git -C "${1:-.}" checkout -- '*' ## NOTE do NOT remove ''
}
## }}}
function git_restore_specific_or_pattern { ## {{{
git -C "${1:-.}" restore "$2" ## $2 is item/pattern
## PREVIOUSLY: git -C "${1:-.}" checkout -- "$2" ## $2 is item/pattern
}
## }}}
function git_source_file_from_a_commit { ## {{{
## Restore a specific revision of the file (as put by https://www.git-tower.com/learn/git/commands/git-restore)
## this function copies a file from a commit (as it was then) to the current commit
## this is useful when we know a time when a file worked well
## and we want to have it now again in the same condition as it was in that commit
git -C "${1:-.}" restore --source "$2" "$3" ## $2 and $3 are commit hash and file
}
## }}}
function git_status { ## {{{
local stts
stts="$(git -C "${1:-.}" status -s)"
[ "$stts" ] && printf '%s\n' "$stts"
}
## }}}
function git_tag_create { ## {{{
git -C "${1:-.}" tag "$2" ## $2 is tag
}
## }}}
function git_tag_create_with_message { ## {{{
git -C "${1:-.}" tag -a "$2" -m "$3" ## $2 is tag, "$3 is message"
}
## }}}
function git_tag_create_with_message_for_specific_commit { ## {{{
git -C "${1:-.}" tag -a "$2" -m "$3" "$4" ## $2 is tag, "$3 is message", "$4" is commit hash
}
## }}}
function git_tag_delete_all { ## {{{
git -C "${1:-.}" tag -d "${@:2}" ## ${@:2} is tags
}
## }}}
function git_tag_delete_specific { ## {{{
git -C "${1:-.}" tag -d "$2" ## $2 is tag
}
## }}}
function git_tag_show_all { ## {{{
git -C "${1:-.}" tag
}
## }}}
function git_tag_show_specific { ## {{{
git -C "${1:-.}" show "$2" ## $2 is tag
}
## }}}
function git_unstage_all { ## {{{
git -C "${1:-.}" restore --staged .
## PREVIOUSLY: git -C "${1:-.}" reset HEAD --
}
## }}}
function git_unstage_specific_or_pattern { ## {{{
git -C "${1:-.}" restore --staged "$2" ## $2 is item/pattern
## PREVIOUSLY: git -C "${1:-.}" reset HEAD -- "$2" ## $2 is item/pattern
}
## }}}