From 03c525d890bc9ef4b7841d0fde21adc53299f496 Mon Sep 17 00:00:00 2001 From: Nicole O'Connor Date: Thu, 20 Jul 2023 14:13:27 -0700 Subject: [PATCH] git module; adds rprompt string and auto-inits git-lfs if needed --- modules/git.zsh | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 modules/git.zsh diff --git a/modules/git.zsh b/modules/git.zsh new file mode 100644 index 0000000..af8f61a --- /dev/null +++ b/modules/git.zsh @@ -0,0 +1,34 @@ +(( $+commands[git] )) || return + +if (( $+commands[git-lfs] )); then + # ensures git-lfs is functional if installed + git config --get filter.lfs.process &> /dev/null || git lfs install +fi + +function git_prompt() { + git rev-parse --is-inside-work-tree &> /dev/null + __git_prompt__is_git_dir=$? + + if [[ $__git_prompt__is_git_dir -eq 0 ]]; then + commithash=$(git log --pretty=format:"%h" -n 1) + fullbranchname=$(git name-rev --name-only ${commithash}) + branchtype=$(echo ${fullbranchname} | cut -d"/" -f1) + + if [[ "${branchtype}" == "refs" ]]; then + branchname=$(echo ${fullbranchname} | cut -d"/" -f3) + elif [[ "${branchtype}" == "remotes" ]]; then + branchname=$(echo ${commithash} | cut -d"/" -f2-) + elif [[ "${branchtype}" == "${fullbranchname}" ]]; then + # git did it for us + branchname=${fullbranchname} + else + branchname=${fullbranchname} + # output a warning to stderr, convenience for maintenance + echo "warning: unrecognized branch type ${branchtype}" >&2 + fi + + print "%{$fg[yellow]%}${branchname}" + fi +} + +rprompt_functions+=git_prompt \ No newline at end of file