zsh: Move 'change directory into repo root' logic

Add an alias that switches directories into the repository root, instead
of having the normal `cd` command behave like that.

Sadly this is not possible (AFAIK) with a git alias as that will always
spawn a subshell.
This commit is contained in:
2022-04-23 16:59:44 +02:00
parent e97ade48e2
commit 8cd490ea0a
2 changed files with 10 additions and 20 deletions

View File

@@ -60,6 +60,7 @@ fi
alias ga='git add'
alias gap='git add -p'
alias gc='git commit'
alias gcd='cd "$(git rev-parse --show-toplevel)"'
alias gch='git checkout'
alias gd='git diff'
alias gds='git diff --staged'

View File

@@ -359,28 +359,17 @@ mvln() {
return $reg
}
## cd wrapper that when called without arguments, moves into the root of the
## current repo instead of HOME. (Except when already there)
## cd-wrapper that recognizes a trailing `ls` behind the path (When not properly
## pressing <CR>)
cd() {
if [[ $# -gt 0 ]]; then
# Call `ls` on paths that end on '/ls' and don't exist with the suffix.
# (When not properly pressing <CR>)
if [[ ! -e ${@[-1]} && -e ${@[-1]%%/ls} ]]; then
builtin cd "${@[1,-2]}" "${@[-1]%%ls}"
pwd
ls
else
builtin cd "$@"
fi
return
fi
local toplevel
toplevel="$(git rev-parse --show-toplevel 2>/dev/null)"
if (( $? )) || [[ $PWD = $toplevel ]]; then
builtin cd
# Call `ls` on paths that end on '/ls' and don't exist with the suffix.
# (When not properly pressing <CR>)
if [[ ! -e ${@[-1]} && -e ${@[-1]%%/ls} ]]; then
builtin cd "${@[1,-2]}" "${@[-1]%%ls}"
pwd
ls
else
builtin cd "$toplevel"
builtin cd "$@"
fi
}