summaryrefslogtreecommitdiff
path: root/bashrc
blob: 65890e43dce153ed45ef64c903a6de424d974b1e (plain)
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
body() {
    IFS= read -r header
    printf '%s\n' "$header"
    "$@"
}

col() {
	awk "{ print \$$1; }"
}

mp4() {
	ffmpeg -i "$1" -vcodec copy -acodec ac3 "${1%.*}.mp4"
}

safedir() {
	echo "$@ $(pwd)" | md5sum | cut -b-32
}

diffWith() {
	diffCmd=$@
	diffFile="/tmp/diffwith-$(safedir $diffCmd).patch"
	$diffCmd > $diffFile
	dos2unix -q $diffFile
	( $EDITOR $diffFile ; rm -f $diffFile ) &
}

showimage() {
	(
		local pkg
		pkg=$1
		pkg="${pkg:=*/*}"
		cd $(portageq envvar PORTAGE_TMPDIR)/portage/$pkg-*/image || return 1
		find . -not -type d -print0 | xargs -0r ls -lvhF --color=auto
		du -shc */*
	)
}

cov() {
	local cov=/tmp/cov.$$
	local all=$cov/all
	local key=$cov/key
	local html=$cov/html
	local htmlfin=bin/cov
	local gcov=$(build $@ -qand1 variant=coverage | grep -w compile | \
			sed -n "s/.*\/gcc-\([0-9.]\+\)\/.*/gcov-\1/p" | head -n1)
	if [[ -z $gcov ]] ; then
		echo "Couldn't figure a gcov version"
		return 1
	fi
	gcov=$(find ${PATH//:/ } -name "$gcov*" 2> /dev/null | sort -Vr | head -n1)
	if [[ ! -x $gcov ]] ; then
		echo "Couldn't find a suitable gcov"
		return 1
	fi
	echo "Using $gcov"

	echo "Building $@"
	mkdir -p $cov
	gcov=$gcov cov=$cov build testing.launcher=~/dev/util/diff.cov $@ -q variant=coverage || return 2

	echo "Generating HTML"
	mkdir -p $html
	genhtml bin/*.info -o $html -s -k -t . --demangle-cpp --sort --function-coverage -q --num-spaces=2 || return 5
	rsync -a --delete-after $html/ $htmlfin/
	rm -rf $cov

	xdotool search --name LCOV windowactivate --sync key F5 windowactivate $(xdotool getactivewindow) || \
		xdg-open "file://$(realpath $htmlfin)/index.html"
	echo "Done"
}

sizes() {
	MODE="variant=release strip=on"
	TARGETS=$(build $MODE $@ -and1 | grep '^\(gcc\|clang-linux\)\.link' | cut -d ' ' -f 2)
	if [[ -z $TARGETS ]] ; then return ; fi
	build $MODE -d0 $@
	find $TARGETS -printf "./%p\t%s\n" | \
		sed "s|/bin/.*/\([^/]\+\s\)|\t\1|; s|^$(pwd)/||" | \
		sort -bk1,2 | \
		column -N module,binary,size -R module,binary,size -t -s $'\t'
}

bloatysizes() {
	MODE="variant=release strip=off debug-symbols=on testing.execute=off"
	TARGETS=$(build $MODE $@ -and1 | grep '^\(gcc\|clang-linux\)\.link' | cut -d ' ' -f 2)
	TARGET="$(echo ${TARGETS} | awk '{ print $NF }')"
	if [[ -z $TARGETS ]] ; then return ; fi
	build $MODE -d1 $@
	ls -lh $TARGET
	bloaty -s vm --domain=vm -d symbols -C full -n 0 $TARGET | tee /var/tmp/bloaty
}

CORES=$(grep core\ id /proc/cpuinfo | wc -l)
CPUS=$(grep core\ id /proc/cpuinfo -c)
if [ -n "$DISTCC_HOSTS" ] ; then
	TASKS=$(echo $DISTCC_HOSTS | tr ' ' '\n' | cut -d / -f2 | paste -s -d+  | bc)
elif [ -x /usr/bin/distcc ] ; then
	TASKS=$(distcc -j)
else
	TASKS=$((($CORES + $CPUS) / 2))
fi
export FEATURES="ccache"
alias cdan="cd ~/work/web/analytics/src"
alias cdsrc="cd ~/work/web/smssite/src"
alias cdutl="cd ~/work/web/smssite/utils"
alias cdj="cd ~/work/jsBase"
alias cdt="cd ~/work/templates"
alias cdw="cd ~/work/web"
alias cdc="cd ~/work/config"
alias cdr='cd $(git root)'
alias :e="gvim"
alias d="cd ~/dev"
alias p="cd ~/dev/portage"
alias ChuckNorris="sudo"
alias l="/bin/ls -NlhvF --color"
alias grep="/bin/grep -I --color=auto --exclude-dir=node_modules --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.svn --exclude-dir=bin --exclude=tags --exclude-dir=docs/html --exclude-dir=target --exclude-dir=.idea --exclude=*.iml"
alias emerge-state='watch "( ps -C sandbox -o stime,cmd ; tail /var/log/emerge.log /var/tmp/portage/*/*/temp/build.log 2> /dev/null )"'
alias df="/bin/df -hT"
title() {
	echo -ne "\033]0;$@\007"
}
vg() {
  launcher=/tmp/$$.sh
  cat << EOF > $launcher
valgrind \
		--show-leak-kinds=all \
		--suppressions=$HOME/dev/valgrind.suppressions \
		--leak-check=full \
  --log-file=/var/tmp/vg-\$(basename \$1).log \
  \$@
EOF
  chmod +x $launcher
  build testing.launcher=$launcher $@
  rm -f $launcher
}

vgc() {
  launcher=/tmp/$$.sh
  cat << EOF > $launcher
valgrind \
  --tool=callgrind \
	--trace-children=no \
  --callgrind-out-file=/var/tmp/callgrind-\$(basename \$1).out \
  \$@
EOF
  chmod +x $launcher
  build testing.launcher=$launcher variant=release debug-symbols=on $@
  rm -f $launcher
}

str() {
	launcher=/tmp/$$.sh
		cat << EOF > $launcher
strace -s400 \
	-o /var/tmp/str-\$(basename \$1).out \
	\$@
EOF
	chmod +x $launcher
	build testing.launcher=$launcher $@
	rm -f $launcher
}

reset()
{
	clear
	printf '\e[3J'
}

bisect() {
	MAILINGLISTS_INSTALL_BASE=$(git root)/../mailinglists ANALYTICS_INSTALL_BASE=$(git root)/analytics OCTAL_INSTALL_BASE=$(git root)/../smssite OCTAL_BASE=$(git root) CCACHE_BASEDIR=$(git root) git bisect run nice -n5 b2 -l300 -j$TASKS $@
}
build() {
	title "Build in progress"
	MAILINGLISTS_INSTALL_BASE=$(git root)/../mailinglists ANALYTICS_INSTALL_BASE=$(git root)/analytics OCTAL_INSTALL_BASE=$(git root)/../smssite OCTAL_BASE=$(git root) CCACHE_BASEDIR=$(git root) time -f"...in %e seconds" nice -n5 b2 -l300 -j$TASKS $@
	rtn=$?
	if [ $rtn -eq 0 ]; then
		title "✔ ${PWD/#$HOME/\~} $@"
	else
		title "✘ ${PWD/#$HOME/\~} $@"
	fi
	return $rtn
}
onchange() {
	$@
	while inotifywait -qr . --exclude '\<bin\>|/tags$|\.sw.$|\<target\>|/\.git/|\.gcov$' -e modify ; do
		reset
		$@
	done
}
autobuild() {
	onchange build $@
}
autocov() {
	onchange cov . 0
}
# CVS
cvsd() {
	diffWith "cvs diff -wubB --show-c-function $@"
}
# SVN
svnd() {
	diffWith "svn diff --force --no-diff-deleted -x -b $@"
}
# Git
gitd() {
	diffWith "git diff --no-prefix --relative -w -D -M --patience $@"
}
gitdc() {
	gitd "--cached $@"
}
showdiff() {
	local feat=${1-HEAD} target=${2-origin/master}
	diffWith "git diff $target...$feat -w -D -M --patience --find-copies-harder"
}
style_reset() {
	echo -ne "\001\e[0m\002"
}
style_set() {
	echo -ne "\001\e[${1};${2}m\002"
}
style_message() {
	echo -ne " $(style_set $1 $2)$3$(style_reset)"
}
git_prompt() {
  local branchname=$(git branch --show-current 2> /dev/null)
	if [[ -n $branchname ]]; then
		local detail=$(git branch --list $branchname -v 2> /dev/null)
		local status=$(git status --porcelain 2> /dev/null)
		local style=00
		local color=92
		if [[ $detail =~ ahead\ [0-9] ]]; then
			if [[ $detail =~ behind\ [0-9] ]]; then
				style=05
			else
				style=03
			fi
		elif [[ $detail =~ behind\ [0-9] ]]; then
			style=04
		fi
		if [[ -n $status ]]; then
			color=96
		fi
		style_message $style $color $branchname
	fi
}
bjam_prompt() {
	if [[ -f Jamfile.jam || -f Jamroot.jam ]]; then
		local updates=$(bjam -nd1 | sed -n 's/^\.\.\.updated \([0-9]\+\) targets\.\.\.$/\1/p')
		if [[ -n $updates ]]; then
			style_message 01 31 $updates
		fi
	fi
}
result_prompt() {
	case $1 in
		0)
			style_message 00 32 			;;
		"")
			style_message 01 34 $
			;;
		*)
			style_message 01 91 			;;
	esac
}
prompts() {
	local res=$?
	git_prompt
	result_prompt $res
}
gppdmb() {
	PARENT=${1:-master}
	git checkout $PARENT && git pull --prune && git delete-merged-branches
}
createAllBranches() {
	for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do git branch --track "${branch#*/*/}" "$branch"; done
}
filterAllBranches() {
	git filter-branch --subdirectory-filter $1 --prune-empty --tag-name-filter cat -d /tmp/$$ -- $(git branch | cut -b 3-)
}
alias gst='git status'
# Hg
hgd() {
	diffWith "hg diff -p -b $@"
}

if [ "$(awk '$5=="/" {print $1}' </proc/1/mountinfo)" != "$(awk '$5=="/" {print $1}' </proc/$$/mountinfo)" ]; then
	# chroot
	if [ -e /etc/redhat-release ]; then
		ROOT=$(cat /etc/redhat-release)
	elif [ -e /etc/gentoo-release ]; then
		ROOT=$(cat /etc/gentoo-release)
	else
		ROOT="unknown chroot"
	fi
	PS1='\[\033[01;32m\]$ROOT \[\033[01;34m\]\w \$\[\033[00m\] '
	PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/\~} ($ROOT)\007"'
elif [ -z "$SSH_CONNECTION" ] ; then
	# local
	PS1='\[\033[01;34m\]\w \$\[\033[00m\] '
	PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/\~}\007"'
else
	# ssh
	PS1='\[\033[01;32m\]\h \[\033[01;34m\]\w \$\[\033[00m\] '
	PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/\~} ($HOSTNAME)\007"'
fi
PATH=$PATH:~/files/scripts/

export SHMUX_SSH_OPTS=-x
export XAUTHORITY=~/.Xauthority
export HISTFILESIZE=15000
export HISTSIZE=${HISTFILESIZE}
export HISTCONTROL=ignoredups:ignorespace
shopt -s histappend
export LESSCHARSET=utf-8
if [[ -n $DISPLAY ]]; then
	export EDITOR="gvim -f"
else
	export EDITOR="vim"
fi
shopt -s cdspell
shopt -s checkwinsize
shopt -s no_empty_cmd_completion

[ -x /usr/bin/fuck ] && eval $(thefuck --alias)

if [ -x /usr/bin/hh ] ; then
	export HSTR_CONFIG=hicolor,raw-history-view
	export PROMPT_COMMAND="history -a; history -n; ${PROMPT_COMMAND}"
	if [[ $- =~ .*i.* ]]; then bind '"\C-r": "\C-a hh -- \C-j"'; fi
fi