-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdot_functions
61 lines (49 loc) · 1.46 KB
/
dot_functions
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
# ==============================================================================
# git
function git-pull() {
git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote" 2> /dev/null; done
git pull --all
}
function git-prune() {
git fetch --prune
for branch in $(git for-each-ref --format '%(refname) %(upstream:track)' refs/heads | awk '$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}'); do
git branch -D $branch
done
for branch in $(git branch -vv | cut -c 3- | awk '$3 !~/\[origin/ { print $1 }'); do
git branch -D $branch
done
}
function git-track() {
branch=$(git rev-parse --abbrev-ref HEAD)
git branch --set-upstream-to=origin/$branch $branch
}
# ==============================================================================
# Miscellaneous functions
# Generate a random string ('A-Za-z0-9') with the given length
#
# Usage:
# $ ./random 32
#
function random() {
chars='A-Za-z0-9'
length=${1:-32}
cat /dev/urandom | env LC_ALL=C tr -dc $chars | fold -w $length | head -n 1
}
# Convert a decimal number to a byte unit
#
# Usage:
# echo 1234567890 | byte-me
#
function byte-me() {
local list="bytes,KB,MB,GB,TB,PB,EB,ZB,YB"
local p=1
local data=$(cat)
local v=$(echo "scale=2; $data / 1" | bc)
local i=$(echo $v / 1024 | bc)
while [ ! $i = "0" ]; do
let p=p+1
v=$(echo "scale=2; $v / 1024" | bc)
i=$(echo $v / 1024 | bc)
done
echo $v$(echo $list | cut -f$p -d,)
}