-
Notifications
You must be signed in to change notification settings - Fork 2
/
.bashrc
783 lines (692 loc) · 19.1 KB
/
.bashrc
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
if ip --help 2>&1 | grep -q netns \
&& [ "x$(ip netns identify $$)" != "x" ]; then
namespace="[$(ip netns identify $$)]"
fi
use_color=true
# Set colorful PS1 only on colorful terminals.
# dircolors --print-database uses its own built-in database
# instead of using /etc/DIR_COLORS. Try to use the external file
# first to take advantage of user additions. Use internal bash
# globbing instead of external grep binary.
PROMPT_COMMAND=__prompt_command
__prompt_command() {
time=$(date +'%b %e %R:%S %Z')
safe_term=${TERM//[^[:alnum:]]/?} # sanitize TERM
match_lhs=""
[[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)"
[[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(</etc/DIR_COLORS)"
[[ -z ${match_lhs} ]] \
&& type -P dircolors >/dev/null \
&& match_lhs=$(dircolors --print-database)
[[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true
if ${use_color} ; then
# Enable colors for ls, etc. Prefer ~/.dir_colors #64489
if type -P dircolors >/dev/null ; then
if [[ -f ~/.dir_colors ]] ; then
eval $(dircolors -b ~/.dir_colors)
elif [[ -f /etc/DIR_COLORS ]] ; then
eval $(dircolors -b /etc/DIR_COLORS)
fi
fi
PS1='${debian_chroot:+($debian_chroot)}'
if [[ ${EUID} == 0 ]] ; then
PS1+='\[\033[01;31m\]'
else
PS1+='\[\033[01;32m\]\u@'
fi
PS1+='\h\[\033[01;33m\]${namespace} '
PS1+='\[\033[01;90m\][${time}] '
PS1+='\[\033[01;34m\]\W '
# Colorize based on success/failure of previous execution
PS1+="\$([ \$? == 0 ] && echo '\[\e[01;32m\]' || echo '\[\e[01;31m\]')"
PS1+='\$\[\033[00m\] '
alias ls='ls --color=auto'
alias grep='grep --color=auto'
else
if [[ ${EUID} == 0 ]] ; then
# show root@ when we don't have colors
PS1='\u@\h \W \$ '
else
PS1='\u@\h \w \$ '
fi
fi
}
__demo_prompt(){
if ${use_color} ; then
PS1=""
if [ $# -ge 1 ]; then
if [[ ${EUID} == 0 ]] ; then
PS1+="\[\033[01;31m\]$@ "
else
PS1+="\[\033[01;33m\]$@ "
fi
fi
# Colorize based on success/failure of previous execution
PS1+="\$([ \$? == 0 ] && echo '\[\e[01;32m\]' || echo '\[\e[01;31m\]')"
PS1+='\$\[\033[00m\] '
else
if [[ ${EUID} == 0 ]] ; then
# show root@ when we don't have colors
PS1='\u@\h \W \$ '
else
PS1='\u@\h \w \$ '
fi
fi
}
# Try to keep environment pollution down, EPA loves us.
unset use_color safe_term match_lhs
# enable bash completion in interactive shells
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi
# Client-specific settings
if [ -e ~/.bashrc.k8s ]; then
source ~/.bashrc.k8s
fi
if [ -e ~/.bashrc.local ]; then
source ~/.bashrc.local
fi
# Personal Settings
export EDITOR=nvim
export GOTOOLCHAIN='path'
export ACK_PAGER_COLOR="${PAGER:-less -R}"
alias journalctl='journalctl --no-pager'
alias make='make --quiet -j $(getconf _NPROCESSORS_ONLN)'
alias ll="ls -lh"
alias ll="ls -lha"
alias nv="nano -v"
alias nt="nano -E"
alias lr="less -r"
alias :e="vim"
alias watch="watch "
alias wn="watch -n 0.5 "
alias tmux="tmux -2"
alias gi="git"
alias gti="git"
alias show="git show"
alias gitap="git ap"
alias gsu="git su"
alias grv="git remote -v"
alias mka="make kind-image-agent"
alias flatjson="gron"
# Disable terminal flow control
stty -ixon
branch_prune() {
git remote prune upstream | awk 'NR>2 { sub(/upstream\//, "", $3); print $3 }' | xargs git branch -D
git remote prune origin | awk 'NR>2 { sub(/upstream\//, "", $3); print $3 }' | xargs git branch -D
}
# Tmux Attach
ta() {
if [ $# -eq 0 ]; then
session=0;
else
session=$1
shift 1
fi
# Prevent nesting
if [ -n "$TMUX" ]; then
echo "Thwarted an attempt to nest tmux sessions."
return 1
fi
if ! tmux has-session -t ${session}; then
tmux new-session -d -s ${session};
fi
# Clear unattached sessions
idle=$(tmux ls 2>/dev/null \
| egrep "^.*\.[0-9]{14}.*[0-9]+\)$" \
| cut -f 1 -d:)
for old_session_id in ${idle}; do
tmux kill-session -t ${old_session_id}
done
clientid=$session+$(date +%Y%m%d%H%M%S)
tmux new-session -d -t ${session} -s ${clientid}
tmux attach-session -t ${clientid} $@
tmux kill-session -t ${clientid}
}
# Keep trying.
kt()
{
while true; do
$@;
sleep 1;
done
}
# Watch a file or directory for changes and trigger some action at that time.
#
# $1 = File to watch
# $2+ = Command and arguments
watchdo() {
local FILE=$1
shift
local reset='\e[00m'
local red='\e[01;31m'
local green='\e[01;32m'
local yellow='\e[01;33m'
echo -e "${yellow}Running '$@' on changes to $FILE ...${reset}"
while inotifywait -q -r -e move -e move_self $FILE; do
eval "$@";
if [ $? == 0 ] ; then
echo -e "${yellow}$@${reset}: ${green}✔${reset}"
else
echo -e "${yellow}$@${reset}: ${red}✘${reset}"
fi
done
}
# Watch a file or directory for changes and trigger builds when it is modified.
#
# $1 = File to watch
watchmake() {
watchdo $1 make
}
# Grep recursive.
gr()
{
if [ "$#" == "1" ]; then
grep -r $@ . \
| grep -vE "(cscope|tags|.git|testsuite|_build*)" \
| grep $@
elif [ "$#" -ge "2" ]; then
grep -r $@
else
echo $(grep)
fi
}
# Git send email with prompt.
gse()
{
if [ $(git diff | wc -l) -ne 0 ] \
|| [ $(git diff --cached | wc -l) -ne 0 ]; then
echo "There are uncommitted changes in the tree."
return
fi
echo "Have you run 'make check' yet?"
echo "What's the shape of the patch?"
echo "Which branch is this for?"
echo
read -r -p "Continue? [y/N] " response
case ${response} in
[yY])
git send-email $@
;;
*)
;;
esac
}
# Get the current git branch, or fail out.
git-get-branch()
{
(
set -e
git rev-parse --symbolic-full-name --abbrev-ref HEAD
)
}
# Git push upstream with prompts.
gpu()
{
local local_branch=$(git-get-branch)
local base_branch=$(echo ${local_branch} | sed 's/^core\///')
if ! echo ${local_branch} | grep "^core" 2>&1>/dev/null; then
echo "Branch \"${local_branch}\" is not core; stopping"
return
fi
echo "Patches between upstream and local branch:"
git log --oneline --reverse upstream/${base_branch}..core/${base_branch}
echo
echo "Have you run 'make check'?"
echo "Do the patches have their Acks?"
echo
read -r -p "exec 'git push upstream core/${base_branch}:${base_branch}'? [y/N] " response
case ${response} in
[yY])
git push upstream core/${base_branch}:${base_branch}
;;
*)
;;
esac
}
# Given current git branch foo.X, create and switch to branch foo.X+1.
gcn()
{
local branch=$(git-get-branch)
if echo ${branch} | grep -q '\.'; then
base=$(echo ${branch} | cut -s -d'.' -f -1)
version=$(echo ${branch} | cut -s -d'.' -f 2-)
new_branch=${base}.$(echo "${version} + 1" | bc -q)
else
if echo ${branch} | grep -q '_v'; then
new_branch=${branch}".1"
else
new_branch=${branch}"_v1.1"
fi
fi
git checkout -b ${new_branch}
}
#Given current git branch X, create and switch to branch 'X+$1'
gcb()
{
local branch=$(git-get-branch)
suffix=$1
if [ $# -ge 1 ]; then
git checkout -b "${branch}+${suffix}"
fi
}
git-bd()
{
(
set -e
local branch=$(git-get-branch)
git checkout main
git branch -D $branch
)
}
# Test until Fail.
tuf()
{
local iter=1
time while $@; do sleep 0.5 $((iter++)); done
echo "Iterated $iter times"
}
# Make with sparse and endianness checks.
kmake()
{
make C=1 CF="-Wsparse-all -D__CHECKER__ -D__CHECK_ENDIAN__ -Wbitwise -Werror" $@
}
# Git log one-liner
#
# $1 = Git commit ID
glo()
{
git log -1 --pretty=linux-fmt $1
}
# Print the log for a commit, with a 'Fixes: xxx ("yyy")' tag added inside.
#
# $1 = Git commit ID that introduced the bug
# $2 = Git commit ID to take the log from (default: HEAD -1)
git-fixes()
{
log_commit=-1
if [ $# -lt 1 ]; then
echo "Specify the git commit ID with the original bug." && return
fi
if [ $# -ge 2 ]; then
log_commit=$2
fi
# Place the tag immediately before the Signed-off-by lines.
git log --format=%B -n 1 ${log_commit} | sed '/^[^ ]*-by/Q'; \
git log -1 --pretty=fixes $1; \
git log --format=%B -n 1 ${log_commit} | sed -n '/^[^ ]*-by/,/$a/p'
}
# Amend the latest commit with a 'Fixes: xxx ("yyy")' tag.
#
# $1 = Git commit ID that introduced the bug
git-fixes-amend()
{
if [ $# -lt 1 ]; then
echo "Specify the git commit ID with the original bug." && return
fi
git-fixes $1 | git commit --amend -F -
}
munge-ovs-git-commit-subject()
{
if echo ${1} | grep -q openvswitch; then
echo ${1} | sed -e 's/openvswitch/datapath/' -e 's/\.*$/./'
else
echo "compat: ${1}" | sed 's/\.*$/./'
fi
}
# Print the log for a commit, indented to show it is a backport of an upstream
# commit. Prepend a subject derived from the original patch, and append an
# 'Upstream: xxx ("...")' tag.
#
# $1 = Git commit ID of original commit upstream
# $2 = Git commit ID to take the log from (default: HEAD -1)
git-upstream()
{
log_commit=-1
if [ $# -lt 1 ]; then
echo "Specify the git commit ID of the upstream patch." && return
fi
if [ $# -ge 2 ]; then
log_commit=$2
fi
if [ ${#1} -lt 20 ]; then
echo "Original commit \`$1' seems incorrect; specify full hash." \
&& return
fi
orig_commit=$(echo ${1} | cut -c1-12)
title=$(git log --format=%s -n 1 ${log_commit})
munge-ovs-git-commit-subject "${title}"
echo
echo "Upstream commit:"
echo " commit ${1}"
git log --format='Author: %aN <%aE>' -n 1 ${log_commit} | sed -e 's/^/ /g' -e 's/^\w$//g'
git log --format='Date: %ad' -n 1 ${log_commit} | sed -e 's/^/ /g' -e 's/^\w$//g'
echo ""
git log --format=%B -n 1 ${log_commit} | sed -e 's/^/ /g' -e 's/^\w$//g'
echo "Upstream: ${orig_commit} (\"${title}\")"
}
# Amend the latest commit with "Upstream commit: ..." pretty-printing and tags.
#
# $1 = Git commit ID of original commit upstream
git-upstream-amend()
{
if [ $# -lt 1 ]; then
echo "Specify the git commit ID of the upstream patch." && return
fi
git-upstream $1 | git commit --amend --reset-author -s -F -
}
# Print the log for a commit, with a 'Fixes: xxx ("yyy")' tag added inside.
#
# $1 = Git commit ID that introduced the bug
# $2 = Git commit ID to take the log from (default: HEAD -1)
git-backports()
{
log_commit=-1
if [ $# -lt 1 ]; then
echo "Specify the git commit ID with the original bug." && return
fi
if [ $# -ge 2 ]; then
log_commit=$2
fi
# Place the tag immediately before the Signed-off-by lines.
git log --format=%B -n 1 ${log_commit} | sed '/^Fixes/Q'; \
echo "From master commit $1."; echo; \
git log --format=%B -n 1 ${log_commit} | sed -n '/^Fixes/,/$a/p'
}
# Amend the latest commit with a 'From master commit xxx.' tag.
#
# $1 = Git commit ID originally merged upstream.
git-backports-amend()
{
if [ $# -lt 1 ]; then
echo "Specify the git commit ID originally merged upstream." && return
fi
git-backports $1 | git commit --amend -F -
}
# Cherry pick the specified commit from the 'local' remote tree. Adds the line
# "From master commit xxx." into the message above the "Fixes" tag.
#
# $1 = Git commit ID originally merged upstream.
gcp()
{
if [ $# -lt 1 ]; then
echo "Specify the git commit ID originally merged upstream." && return
fi
git fetch local
git cherry-pick $1
git-backports-amend $1
}
# Get the list of tags that contain the commit in a particular repository.
#
# $1 = Repository
# $2 = Git commit ID
gtc()
{
dir_prefix=~/git
repo=$1
commit=$2
if [ $# -lt 2 ]; then
echo "usage: gtc <repo> <commit>"
return 1;
fi
git_path=${dir_prefix}/${repo}
if [ ! -d ${git_path} ]; then
echo "path ${git_path} does not exist."
return 1;
fi
cd ${git_path}
git tag --contains ${commit} | sort -V
cd -
}
# Adds a specified string to all commits from the specified commit to HEAD.
#
# $1 - Git commit ID
# $2 - String to add as newline at end of commit message
git-amend-add()
{
commit=$1
string=$2
if [ $# -lt 2 ]; then
echo "usage: git-ack <commit> <string>"
return 1;
fi
git filter-branch -f --msg-filter "sed '$ a\\$string'" $commit..HEAD
}
# Adds signoff tags to all commits from the specified commit to HEAD.
#
# $1 - Git commit ID
git-sign()
{
commit=$1
user="$(git config user.name) <$(git config user.email)>"
if [ $# -lt 1 ]; then
echo "usage: git-sign <commit>"
return 1;
fi
git-amend-add $commit "Signed-off-by: $user"
}
# Adds "ack" tags to all commits from the specified commit to HEAD.
#
# $1 - Git commit ID
# $2 - Optional alternative ack string of the form "name <user@domain>"
git-ack()
{
commit=$1
user=${2:-"$(git config user.name) <$(git config user.email)>"}
if [ $# -lt 1 ]; then
echo "usage: git-ack <commit> [id]"
return 1;
fi
ack="Acked-by: $user"
if [ $# -gt 1 ]; then
echo "Acking with \"$ack\" (CTRL+C to abort)"
read -r -n 1
fi
git-amend-add $commit $ack
}
# Specialized version of "gtc" that searches using the Linux net-next tree,
# listing the first N version tags that contain the specific commit.
#
# $1 - Git commit ID
# $2 - Count of commits to check (default: 1)
gtl()
{
count=1
if [ $# -gt 1 ]; then
count=$2
fi
gtc net-next $1 | grep -v next | grep "^v" | head -n $count
}
# Fast forward changes to the given commit.
gff()
{
git merge --ff-only $1
}
# Fetch upstream changes from git.
gfu()
{
git fetch upstream
}
# Fetch changes from git origin.
gfo()
{
git fetch origin
}
# Git recent branches. Lists recent branches with recent commit details,
# ordered by most recent commit.
#
# Adapted from version by Daniel Mulholland, based on script by Jason Rudolph.
# https://gist.github.com/jasonrudolph/1810768
#
# $1 - Number of recent branches to list (default 10)
gb()
{
local n_branches=${1:-10}
git for-each-ref \
--color \
--sort=-committerdate \
--format="%(color:magenta)%(committerdate:relative)^%(color:bold cyan)%(refname:short)^%(color:reset)%(contents:subject)" \
refs/heads/ \
| head -"$n_branches" \
| column -t -s '^'
}
# Wait for N seconds, displaying a countdown timer
#
# $1 - Time in seconds to wait
countdown()
{
if [ $# -lt 1 ]; then
echo "usage: countdown <n_secs>"
return 1;
fi
secs=$1
while [ $secs -gt 0 ]; do
echo -ne "$secs\033[0K\r"
sleep 1
: $((secs--))
done
}
# Find a kernel based on a string and load it using kexec
#
# $1 - Kernel localversion string to specify kernel
load_kernel()
{
if [ $# -ne 1 ]; then
echo "Usage: load_kernel <kernel>"
echo;
echo "Available kernels:"
ls /boot/vmlinuz-* | cut -d'-' -f2- | sed 's/^/ /'
else
version=$1
initfs=$(ls /boot/init*${version}*)
if kexec -l /boot/vmlinuz-${version} --ramdisk=${initfs} \
--reuse-cmdline; then
echo "Loaded Linux-${version} Run 'kexec -e' to execute it."
fi
fi
}
# Shortcut for "git rc" alias (git rebase --continue ...)
gitrc()
{
git rc
}
# Drop the PS1 to a basic "$" to simplify shell output for copy/paste somewhere
demo()
{
export PROMPT_COMMAND="__demo_prompt $@"
}
# undemo reverses 'demo'.
undemo()
{
export PROMPT_COMMAND=__prompt_command
}
# count_failures_output looks for lines with the word "pass" in them in the
# file specified, and prints a count comparing this vs the total number of
# lines with a rate.
#
# $1 - file to use for counting the failures
count_failures_output()
{
out=$1
success=$(grep "pass" $out | wc -l)
total=$(cat $out | wc -l)
rate=$(awk "BEGIN { print int(${success} / ${total} * 100) }")
echo
echo "Successes: ${success}/${total}; rate: ${rate}%"
}
# count_failures repeatedly runs the command specified as arguments until the
# user interrupts the test. Upon interruption, it prints the number of times
# that the command succeeded/failed.
#
# $@ - command + args to run
count_failures()
{
local out=$(mktemp)
trap "rm $out"
trap "count_failures_output $out" SIGINT
while true; do
if "$@"; then
echo "pass" >> $out
else
echo "fail" >> $out
fi
done
}
# cleanup_docker will remove containers that have exited, and dangling images.
cleanup_docker()
{
docker rm -v $(docker ps -a -q -f status=exited)
docker rmi $(docker images -f "dangling=true" -q)
docker images --no-trunc | grep '<none>' | awk '{ print $3 }' \
| xargs -r docker rmi
docker system prune -a -f
}
# The greatest git grep you'll ever see
ggg()
{
git grep "$@" -- './*' ':^*svg' ':^*css*' ':^*js' ':^*toml' ':^*lock' ':^*svg' ':^examples/*'
}
# Edit files added in the latest Git Commit
egc() {
$EDITOR $(git ls HEAD) "$@"
}
# Git Rebase All
#
# No arguments: Rebase each branch.
# $1: filter branches by this expression.
gra()
{
DEFAULT_BRANCH=main
UPSTREAM=upstream
LOG="../rebase-failed.log"
FLAGS=("-v" "-e" "$DEFAULT_BRANCH")
if [ $# -ge 1 ]; then
FLAGS=("-e" "$1")
fi
echo > "$LOG"
for branch in $(git branch | grep "${FLAGS[@]}" | grep -ve '^[+*] '); do
git checkout "$branch";
if git rebase "$UPSTREAM/$DEFAULT_BRANCH"; then
git checkout "$DEFAULT_BRANCH"
git branch -D "$branch"
else
git rebase --abort
git clean -f -d
echo "$branch" >> "$LOG"
fi
done
}
# yaml2json converts yaml into json.
#
# $1: filepath to convert [optional]
# shellcheck disable=SC2120
yaml2json()
{
if [ $# -ge 1 ]; then
yq -o json < "$1"
else
yq -o json
fi
}
# Yaml Grep
#
# $1: filepath to grep [optional]
# $@: grep arguments
yg() {
if [ -e "$1" ]; then
path="$1"; shift
yaml2json "$path" | gron | grep "$@"
else
# shellcheck disable=SC2119
yaml2json | gron | grep "$@"
fi
}