alias sa='ssh aisa -CX'
alias sb="ssh bellona -CX"
alias sd='ssh decibel -CX'
alias san='ssh anemoi -X'

alias osci='osc -A https://api.suse.de'
alias oscp='osc -A https://pmbs-api.links2linux.org'
alias oscss='osc service remoterun home:jirislaby slaby-scripts'

alias gde='git describe --contains --exact-match'
alias gpr='git pull --rebase'
alias gru='git remote update'
alias gd='git diff'
alias gdw='git diff --word-diff'
alias glm='git log --author=jslaby'
alias qd='quilt diff'
alias qdig='echo "vim `quilt next`" | xclip; dig_patch `quilt next`'
alias qpr='quilt push && ~/kernel-source/scripts/refresh_patch.sh'
alias ref='~/kernel-source/scripts/refresh_patch.sh'

alias tigl='tig -1000'
alias tign='tig korg/next_master..'
alias tigs='tig v4.12..'
alias ip='ip $IP_COLOR'

alias awrap='echo -ne "\x1b[?7h"'

IP_COLOR='--color=auto'
ip $IP_COLOR l &>/dev/null
if [ $? != 0 ]; then
	IP_COLOR='--color'
fi

function join_by() {
	local IFS="$1"
	shift
	echo "$*"
}

function show_patch {
	SHA="$1"
	HEAD="$2"
	declare -a PATCHES
	PATCHES=( $(git grep -l "Git-commit: $SHA" $HEAD -- 'patches.*') )
	if [ ${#PATCHES[*]} -eq 0 ]; then
		echo no patch found >&2
	elif [ ${#PATCHES[*]} -gt 1 ]; then
		echo more than one patch found: >&2
		echo `join_by $'\n' "${PATCHES[*]}"` >&2
	elif [ -n "$HEAD" ]; then
		git show ${PATCHES[0]} | view '+set ft=diff' -
	else
		${EDITOR:-vim} ${PATCHES[0]}
	fi
}

function vimp() {
	REV="$1"
	if [ -z "$REV" ]; then
		REV="@{u}"
	fi
	declare -a PATCHES
	PATCHES=( $(gd --name-only "$REV" -- 'patches.*') )
	PATCHES=( $(git grep -hE "`join_by '|' \"${PATCHES[@]}\"`" series.conf) )
#	| grep -v '^\s*#'`
	${EDITOR:-vim} "${PATCHES[@]}"
}

function cd_existing() {
	for DIR; do
		if [ -d "$DIR" ]; then
			cd "$DIR"
			break
		fi
	done
}

alias cdl='cd_existing ~/linux ~/linux.git ~/labs/linux.git'
alias cds='cd ~/tmp/stable-4.12'
alias cdsl='cd_existing ~/repos/slaby-scripts/ ~/labs/slaby-scripts/ ~/slaby-scripts/'
alias cdq='cd ~/repos/stable-queue/queue-4.13/'
alias cdx='cd ~/repos/xf86-video-intel/'
alias cdk='cd_existing ~/kernel-source ~/repos/suse/kernel-source/'
alias shm='cd_existing /dev/shm/jslaby/ /dev/shm/'

function stable_dry() {
	patch --dry-run -p1 -i ~/repos/stable-queue/queue-4.13/"$1"
}
