-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.function
470 lines (398 loc) · 12.1 KB
/
.function
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/bin/zsh
# helpers
function _git_get_branches() {
IFS=" "
git branch | grep -v "\*" | sed 's:^[ ]*::g'
}
function _git_branch_completion() {
local -a commands
IFS=$'\n'
commands=(`_git_get_branches`)
_describe 'commands' commands
}
function ldu() {
ls -d1 -- *(/) |\
while read in; do
echo "$PWD/$in" | sed 's: :\\\ :g'
done |\
xargs du $*
}
function dash(){
if [[ $2 != '' ]]; then
open dash://$1:{$2}
else
open dash://{$1}
fi
}
st() {
if [[ $1 = *"--long"* || $1 = *"-l"* || $1 = *"-v"* ]]; then
git status;
return 0;
fi
git status > /dev/null 2>&1
if [[ $? != 0 ]]; then
echo "No git repo found.";
return 1;
fi
local gstatus="`git -c color.status=always status --short`";
if [[ $gstatus != "" ]];then
echo -e "`echo $gstatus | sed 's/^\(.*\) \(.*\)/\\\\e[90m\[\1\\\\e[90m\]\\\\e[0m \2/g' | sed 's/\(.* \)\(.*\/\)\(.*$\)/\1\\\\e[90m\2\\\\e[0m\3/g'`";
fi
}
function stashy() {
local long_stash=$(\
git stash list |\
fzf --ansi\
--no-sort\
--print-query\
--preview-window up:50:noborder\
--preview "git -c color.ui=always stash show -p \`echo {} | ack -o \"stash@\\{[0-9]*\\}\"\`" |\
\grep -v "^[ \t]*$" \
)
local stash="$(echo "$long_stash" | ack -o "stash@\{[0-9]*\}")"
if [[ $stash = "" ]]; then
echo "Okay."
return
fi
echo -e " $long_stash"
git --no-pager -c color.ui=always stash show $stash
read response\?"Pop this stash? [y/n] "
case "$response" in
[yY][eE][sS]|[yY])
git stash pop $stash
;;
*)
;;
esac
}
function rh(){
git ls-remote --heads origin $(git rev-parse --abbrev-ref HEAD) | grep $(git rev-parse --abbrev-ref HEAD) | sed 's/refs\/heads\/'$(git this | sed 's:/:\\\/:g')'//g' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//';
}
function cauth(){
local search="$1"
local users="$(\
git log --since=$(gdate -d "4 months ago" +"%Y-%m-%d") --no-merges --pretty='format:%aN <%ae>' \
| awk -F ' ' '!seen[$1]++' \
| sort \
)"
if [[ $1 = *"--print"* ]]; then
echo "$users";
return 0;
fi
local user=$(echo "$users" | fzf -1 -q "${search:-}")
local coauth="Co-authored-by: $user"
echo -e "Copied to clipboard: \033[2m"
echo $coauth | tee >(pbcopy)
echo -e "\033[0m"
}
function isdown(){
curl $1 -s -f -o /dev/null && echo -e "\e[32m$1 is up!" || echo -e "\e[31m$1 is down...";
}
# fda - including hidden directories
fda() {
local dir
dir=$(find ${1:-.} -type d 2> /dev/null | fzf-tmux +m --preview "ls -a1 {}") && cd "$dir"
}
function calc(){
awk "BEGIN { print $* }";
}
function di() {
if [[ $@ == "" ]]; then
git diff HEAD
else
git diff $@
fi
}
compdef _git_branch_completion di
function fdi() {
local TARGET="head"
if [[ $@ != "" ]]; then
TARGET=$@
fi
git diff $TARGET -- `git diff $TARGET --name-only | fzf --preview "cat {}"`
}
function din(){
input=$1
if [[ $input == "" ]]; then
base_branch="$(git config repo.base-branch)";
default_branch="$([[ $base_branch != "" ]] && echo $base_branch || echo "main")";
git --no-pager diff $default_branch --name-only
else
git --no-pager diff $input --name-only
fi
}
compdef _git_branch_completion din
# GIT
function pu(){
git push -u origin $(git rev-parse --abbrev-ref HEAD);
}
function co() {
# if the --last argument is passed, checkout the last branch and exit
[[ $* = *"--last"* ]] && git checkout @{-1} && return 0;
local all_branches="$(git branch | grep -v "\*")";
local recent_branches="$(git reflog | grep checkout: | sed 's,^.*from \([\.A-z0-9_/\-]*\) to \([\.A-z0-9_/\-]*\),\2,g' | awk '!a[$0]++')"
local branch=""
if [[ $@ == "" ]]; then
branch="$(echo "$recent_branches" | fzf --height 25%)"
elif [[ $* = *"--all"* ]]; then
branch="$(echo "$all_branches" | sort -fdu | fzf --layout=reverse)"
else
local branches="$(echo $recent_branches | grep $@)"
local exact="$(echo $recent_branches | grep "^[ ]*$@$")"
local wcount=$(echo $branches | wc -l | sed 's/[[:space:]]//g')
if [[ $exact != "" ]]; then
branch=$@
elif [[ $wcount == 0 ]]; then
echo -e "\e[31mNo branches found with that name."
return 1;
elif [[ $wcount == 1 ]]; then
branch="$(echo $branches | head -n 1)"
else
branch="$(echo "$recent_branches" | fzf --height 15% -q "$@")"
fi
fi
branch="$(echo $branch | sed 's:^[ ]*::g')"
if [[ $branch != "" ]]; then
git checkout "$branch"
fi
}
compdef _git_branch_completion co
rco() {
if [[ "$(git remote -v 2>&1 1>/dev/null; echo $?)" -gt 0 ]]; then
echo "Not a git repo."
return
fi
git fetch --all
local branch=""
local all_remote_branches="$(git ls-remote --heads origin | grep -o 'refs/heads/.*' | sed 's:refs/heads/::g')"
if [[ $@ == "" ]]; then
branch="$(echo $all_remote_branches | fzf)"
else
branch="$(echo $all_remote_branches | fzf -q "$@")"
fi
if [[ $branch != "" ]]; then
if [[ $(git branch | grep $branch ) != "" ]]; then
git checkout $branch
return;
fi
read "response?Create a local copy of $(echo -e "\e[34m$branch\e[0m")? "
case "$response" in
[yY][eE][sS]|[yY])
git switch $branch
;;
*)
;;
esac
fi
}
function mg() {
if [[ $@ == "" ]]; then
local branch=$(echo `git branch | grep -v "\*" | fzf` | sed 's/[[:space:]]//g')
if [[ $branch == "" ]]; then
echo -e "\e[31mCancelling merge."
return 1;
fi
read response\?"Do you want to merge "`echo -e "\e[33m$branch\e[0m"`" into "`echo -e "\e[32m$(git rev-parse --abbrev-ref HEAD)\e[0m"`"? [y/n] "
case "$response" in
[yy][ee][ss]|[yy])
git merge $branch
;;
*)
tput cuu1;
printf "%${COLUMNS}s" "";
tput cuu1;
echo -e "\e[33m$branch\e[0m was not merged."
return 1;
;;
esac
else
git merge $@
fi
}
compdef _git_branch_completion mg
function nuke(){
# stty -echo -icanon time 0 min 0
echo "Branch: $(git rev-parse --abbrev-ref HEAD)"
echo "Repo: $(git remote -v -- | sed 's/^.*[email protected]://g' | sed 's/.git.*$//g' | tr -s " " | awk '!seen[$0]++')"
echo ""
# sleep 0.5 && stty sane
read response\?"Are you sure you want to nuke your changes? [no] "
case "$response" in
[yY][eE][sS])
git add . >& /dev/null;
git reset HEAD --hard >& /dev/null;
tput cuu1;
echo "Now at commit: \e[0;31m$(git rev-parse HEAD) ";
;;
*)
;;
esac
}
function mkbr(){
local INPUT=$1
if [[ $INPUT = "" ]]; then
echo -e "\e[31mSorry, you need to provide a branch name.";
return 1;
fi
git checkout -b ${INPUT}
if [[ $? -ne 0 ]]; then
echo -e "\e[31mSorry, something went wrong there";
return 1;
fi
read remote\?"Does the branch `echo -e "\e[32m$INPUT\e[0m"` already exist as a remote? [y/n] "
case "$remote" in
[yY][eE][sS]|[yY])
git fetch origin ${INPUT}
git branch -u origin/${INPUT} ${INPUT}
read sync\?"Would you like to sync `echo -e "\e[32m$INPUT\e[0m"` with `echo -e "\e[33morigin/$INPUT\e[0m"`? [y/n] "
case "$sync" in
[yY][eE][sS]|[yY])
git pull origin ${INPUT}
git reset $(rh) --hard
;;
*)
echo "Okay, then."
;;
esac
;;
*)
read push\?"Would you like to push `echo -e "\e[32m$INPUT\e[0m"` to origin and add `echo -e "\e[33morigin/$INPUT\e[0m"` as a remote? [y/n] "
case "$push" in
[yY][eE][sS]|[yY])
git push -u origin ${INPUT}
;;
*)
echo "Okay, then."
;;
esac
;;
esac
}
function dbr(){
local todelete=""
if [[ $@ == "--fuzzy" ]]; then
todelete=("${(@f)$(_git_get_no_remotes | fzf --multi --reverse)}")
else
todelete=$@
fi
for branch in $todelete; do
read response\?"Are you sure you would like to force delete branch $(echo "\033[31m${branch}\033[0m")? [y] "
case "$response" in
[nN][oO]|[nN])
echo "Branch left untouched."
;;
*)
git branch -D ${branch};
echo ""
;;
esac
done
}
function _git_get_merged() {
IFS=" "
git branch --merged | grep -v "main\|mobile\|staging\|\*" | sed 's:^[ ]*::g'
}
function _git_get_no_remotes() {
git fetch -p --all > /dev/null 2>&1
local remotes=$(git branch -r | grep -v HEAD | sed 's:^[ ]*origin/::g' | sort)
local locals=$(git branch | grep -v HEAD | sed 's:^[ ]*[\*]*[ ]*::g' | sort)
comm -23 <(echo $locals) <(echo $remotes)
}
function _dbr() {
local -a commands
IFS=$'\n'
commands=(--fuzzy `git branch`)
_describe 'commands' commands
}
# should be registering with: compdef _dbr dbr
compdef _dbr dbr
function dicount(){
base_branch="$(git config repo.base-branch)";
default_branch="$([[ $base_branch != "" ]] && echo $base_branch || echo "main")";
branch="$([[ $1 != "" ]] && echo $1 || echo $default_branch)";
local result=`git diff $branch --stat | tail -n 1 | tr , '\n' | sed 's/([+-])//g' | awk '$1=$1'`
echo $result | while read in; do
if [[ $in = *"changed"* ]]; then
echo -e "\e[33m~ $in\e[0m";
elif [[ $in = *"insertions"* ]]; then
echo -e "\e[32m+ $in\e[0m";
elif [[ $in = *"deletions"* ]]; then
echo -e "\e[31m- $in\e[0m";
fi
done
}
function lsearch() {
git log --grep=$1
}
function disearch(){
git diff $2 --name-only | grep $1 | while read in; do git diff $2 -- $in; done
}
gpr() {
local arg1=$1
local base=${arg1:-"main"}
local branch=$(git rev-parse --abbrev-ref HEAD)
hub pull-request -b $base --draft
}
compdef _git_branch_completion gpr
function fromroman () {
local max=0 sum i j
local -A conv
conv=(I 1 V 5 X 10 L 50 C 100 D 500 M 1000)
for j in ${(Oas::)1}; do
i=conv[$j]
if (( i >= max )); then
(( sum+=i ))
(( max=i ))
else
(( sum-=i ))
fi
done
echo $sum
}
function toroman() {
local -a conv
local number=$1 div rom num out
conv=(I 1 IV 4 V 5 IX 9 X 10 XL 40 L 50 XC 90 C 100 CD 400 D 500 CM 900 M 1000)
for num rom in ${(Oa)conv}; do
(( div = number / num, number = number % num ))
while (( div-- > 0 )); do
out+=$rom
done
done
echo $out
}
function icat() {
local maxWidth="$(tput cols)"
local width=
if [[ $@ == *"--half"* ]]; then
width=$[$(tput cols)/2]
elif [[ $@ == *"--quarter"* ]]; then
width=$[$(tput cols)/4]
elif [[ $@ =~ '--width=[0-9]*' ]]; then
width="$(echo $@ | sed -n 's/.*\(\-\-width\=\)\([0-9]*\)/\2/p')"
else
width=$maxWidth
fi
if [[ $width -gt $maxWidth ]]; then
width=$maxWidth;
fi
if [[ $@ == *"--pager"* ]]; then
imgcat -w $width --256 -H $1 | cat | $PAGER
else
imgcat -w $width --256 -H $1 | cat
fi
}
unalias z 2> /dev/null
z() {
[ $# -gt 0 ] && _z "$*" && return
cd "$(_z -l 2>&1 | fzf --height 40% --nth 2.. --reverse --inline-info +s --tac --query "${*##-* }" | sed 's/^[0-9,.]* *//')"
}
function totoilet()
{
local text=$*
toilet -d ~/.config/figlet/fonts/ -f $(ls ~/.config/figlet/fonts/ \
| sed 's:\..*::g' \
| sort \
| fzf --preview "toilet -d ~/.config/figlet/fonts/ -f {} \"$text\"") "$text" \
| tee >(pbcopy)
}