From d799b06547a32bd6825a35a1161e2d264700dafd Mon Sep 17 00:00:00 2001 From: Alicia Sykes Date: Mon, 15 Aug 2022 23:26:29 +0100 Subject: [PATCH] Adds a git alias to jump to root of repo, gj --- zsh/aliases/git.zsh | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/zsh/aliases/git.zsh b/zsh/aliases/git.zsh index 6b67045..a2bced4 100644 --- a/zsh/aliases/git.zsh +++ b/zsh/aliases/git.zsh @@ -61,9 +61,32 @@ function gplfs() { git lfs push origin "$(git_current_branch)" --all } -# Navigate back to project root (where .git is) alias gx='cd $(git rev-parse --show-toplevel)' +# Navigate back to project root (where .git is) +function jump-to-git-root { + local _root_dir="$(git rev-parse --show-toplevel 2>/dev/null)" + if [[ $? -gt 0 ]]; then + >&2 echo '\033[1;93m Not a Git repo\033[0m' + exit 1 + fi + local _pwd=$(pwd) + if [[ $_pwd = $_root_dir ]]; then + # Handle submodules + _root_dir="$(git -C $(dirname $_pwd) rev-parse --show-toplevel 2>/dev/null)" + if [[ $? -gt 0 ]]; then + echo "\033[0;96m Already at Git repo root.\033[0m" + return 0 + fi + fi + # Make `cd -` work. + OLDPWD=$_pwd + echo "\033[0;96m Git repo root: $_root_dir\033[0m" + cd $_root_dir +} + +alias gj='jump-to-git-root' + # Shorthand clone (e.g. $ clone lissy93/dotfiles) function clone { default_service='github.com' # Used if full URL isn't specified