-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions
More file actions
235 lines (212 loc) · 6.53 KB
/
functions
File metadata and controls
235 lines (212 loc) · 6.53 KB
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
#!/bin/bash
## git
gdifff () { git diff --color "$@" | diff-so-fancy; }
prune () (
set -euo pipefail
remote_repo="${remote_repo:-origin}"
branch_name="$(git rev-parse --abbrev-ref HEAD)"
if [[ "${branch_name}" =~ ^(master|develop|main|trunk)$ ]]; then
git_action='pull --prune --autostash'
else
git_action='fetch --prune'
fi
echo $(tput setaf 6)git $git_action $remote_repo$(tput sgr0)
git $git_action $remote_repo
)
prune-rebase () (
prune
# Find the 'master' branch by trial/error
for m_branch in master main trunk; do
if git branch -a | grep -q "origin/${m_branch}\$"; then
git rebase --autostash "origin/${m_branch}"
return
fi
done
# Print error message
echo >&2 "Failed find a branch to rebase."
return 1
)
push () (
set -euo pipefail
remote_repo="${remote_repo:-origin}"
branch_name="$(git rev-parse --abbrev-ref HEAD)"
if [ "${1:-}" == "force" ]; then
[[ "${branch_name}" =~ ^(master|develop|main|trunk)$ ]] &&
echo "Cannot force-push to master / develop / main / trunk" >&2 &&
false
cmd="git push $remote_repo --force-with-lease"
color_cmd="git push $remote_repo $(tput setaf 1)--force-with-lease $(tput setaf 2)$branch_name$(tput sgr0)"
else
cmd="git push $remote_repo"
color_cmd="git push $remote_repo $(tput setaf 2)$branch_name$(tput sgr0)"
fi
echo "$color_cmd"
read -p "[type 42 to continue] " THE_answer
[[ "${THE_answer}" = "42" ]]
$cmd $branch_name
)
delete-merged () {
local current_branch="$(git branch --show-current)"
if [[ "$current_branch" =~ ^(master|main)$ ]]; then
local merged_branches="$(git branch --merged | grep -v "^. $current_branch\$" | sed 's:^ *::')"
if [[ -z "$merged_branches" ]]; then
echo >&2 "No more merged branch. Do nothing"
else
echo >&2 "Deleting these branches?"
echo "$merged_branches"
local THE_answer
read -p "[type 42 to continue] " THE_answer
[[ "${THE_answer}" = "42" ]] || return
local b
for b in $merged_branches; do
echo $(tput setaf 2)git branch -d "$b"$(tput sgr0)
git branch -d "$b"
done
fi
else
echo >&2 "Branch not on master/main. Do nothing"
fi
}
## subl
sf. () { f. $@ | tee >(tr '\n' '\0' | xargs -0 subl); }
snf. () { f. $@ | tee >(tr '\n' '\0' | xargs -0 subl --new-window); }
sack () { ack -l $@ | tee >(tr '\n' '\0' | xargs -0 subl); }
snack () { ack -l $@ | tee >(tr '\n' '\0' | xargs -0 subl --new-window); }
sgdiff () { git diff --name-only $@ | tee >(cd "$(git rev-parse --show-toplevel)"; tr '\n' '\0' | xargs -0 subl); }
sngdiff () { git diff --name-only $@ | tee >(cd "$(git rev-parse --show-toplevel)"; tr '\n' '\0' | xargs -0 subl --new-window); }
# unique and preserve input order stackoverflow.com/q/1444406
uniq2 () {
awk '!seen[$0]++' ${@}
}
count () {
awk '{ count[$0]++ } END { for (line in count) print count[line], line }' | sort -n
}
# awk print column
awkp () {
awk -F "${FS:- }" -v column="$1" '{print $column}' -
}
# sum up the $1 column
sumup () {
awk -F "${FS:- }" -v column="$1" '{a += $column} END {print a}' -
}
average () {
awk -F "${FS:- }" -v column="$1" '{a += $column} END {print a/NR}' -
}
# Calculate delta (intervals) between rows of numbers
delta () {
awk -F "${FS:- }" -v column="$1" 'BEGIN {last=0} {print $column-last; last=$column}' -
}
# Prefix timestamp on stdin
timestamp () {
ruby -ne 'BEGIN{startt=Time.now; trap("SIGINT"){}}; printf "%d(%.3f) %s", Time.now, Time.now-startt, $_' -
}
# A very simple implementation of watch
watch () {
while date >&2; do
eval "$@"
sleep "${sleep:-15}"
done
}
# pbedit
pbedit () {
# Edit clipboard using vim, then save it back
# If stdin is pipe, use contents from stdin
# If stdout is pipe, pbpaste to stdout
local temporary_buffer="$(mktemp)"
if [[ -p /dev/stdin ]]; then
cat -
else
pbpaste
fi > "$temporary_buffer"
(
exec < /dev/tty
exec &> /dev/tty
# do no modify the EOF newline
vim -c 'set nofixendofline' "$temporary_buffer"
)
pbcopy < "$temporary_buffer"
rm "$temporary_buffer"
# If stdout is a pipe
[[ -p /dev/stdout ]] && pbpaste
}
pbedit-subl () {
# Edit clipboard using subl, then save it back
# If stdin is pipe, use contents from stdin
# If stdout is pipe, pbpaste to stdout
if [[ -p /dev/stdin ]]; then
cat -
else
pbpaste
fi | subl - | perl -pe 'chomp if eof' | pbcopy
# If stdout is a pipe
[[ -p /dev/stdout ]] && pbpaste
}
# diff contents from contents in pasteboard
diff-pb () (
cd "$(mktemp -d)"
echo >&2 "Copy contents A"
pb0_md5="$(pbpaste | md5)" # pb before command, don't use.
# Loop till pb change
while [[ "$(pbpaste | md5 )" = "$pb0_md5" ]]; do sleep 0.5; done
pbpaste > A
pb1_md5="$(cat A | md5)"
echo >&2 "A copied. Now copy contents B"
# Loop till pb change
while [[ "$(pbpaste | md5 )" = "$pb1_md5" ]]; do sleep 0.5; done
pbpaste > B
git diff --no-index A B
)
# Convenient Functions that manipulate READLINE_LINE
# This requires bash 4.x+ (/usr/local/bin/bash)
if grep -q -e '^4' -e '^5' <<<$BASH_VERSION ; then
__copyline () {
# A nice hack to copy current READLINE into pasteboard
# Ref: stackoverflow.com/questions/14177700
printf %s "$READLINE_LINE" | pbcopy
}
__itermcopyline () {
# A nice iTerm2 version __copyline
# This works only with iTerm2, but it copies remote shells.
# Not binding to shortcuts
printf "\e]1337;Copy=:%s\x07" "$(printf %s "$READLINE_LINE" | base64)"
}
__evalline () {
# Replace the current READLINE buffer with its evaluation.
local readline_line_buffer
readline_line_buffer=$( eval "$READLINE_LINE" | tr '\n' ' ' )
[[ -n "$readline_line_buffer" ]] && READLINE_LINE="${readline_line_buffer}"
}
__insertpb () {
# Insert pasteboard as one line
local delimiter="${1:- }"
READLINE_LINE="${READLINE_LINE:0:READLINE_POINT}$(pbpaste | tr '\n' "$delimiter")${READLINE_LINE:READLINE_POINT}"
}
__insertpbd () {
# Insert pasteboard as one line with customized delimiter
local delimiter
read -n1 delimiter
__insertpb "$delimiter"
}
__historyfgrep () {
history | fgrep "$READLINE_LINE" | tail -5
}
# 'C-x c' -> copyline
# 'C-x e' -> evalline
# 'C-x p' -> insertpb
# 'C-x P' -> insertpbd
# 'C-x h' -> historyfgrep
if [[ "$(uname)" = "Darwin" ]]; then
bind -x '"\C-xc":__copyline'
else
bind -x '"\C-xc":__itermcopyline'
fi
bind -x '"\C-xe":__evalline'
bind -x '"\C-xp":__insertpb'
bind -x '"\C-xP":__insertpbd'
bind -x '"\C-xh":__historyfgrep'
fi
# Himitsu ;-)
nohist () {
unset HISTFILE
echo HISTFILE unset
}