Minor refactors to Git status and branch functions in git-prompt.sh

- `getGitStatusSetting`: use local variables, improve regex pattern, use return status codes instead of echo strings
- `getSimpleGitBranch`: better error handling, use local variables
- Simplified conditional checks
This commit is contained in:
David Refoua
2025-07-16 04:16:47 +03:30
parent 1d51c4f402
commit c6bfd6f276

36
vendor/git-prompt.sh vendored
View File

@ -1,24 +1,32 @@
function getGitStatusSetting() { function getGitStatusSetting() {
gitStatusSetting=$(git --no-pager config -l 2>/dev/null) local gitConfig
if [[ -n ${gitStatusSetting} ]] && [[ ${gitStatusSetting} =~ cmder.status=false ]] || [[ ${gitStatusSetting} =~ cmder.shstatus=false ]] # Get all git config entries for the current repository without pager
gitConfig=$(git --no-pager config -l 2>/dev/null) || return 0 # treat failure as enabled
# Check if git status for Cmder is disabled
if [[ $gitConfig =~ (^|$'\n')cmder\.status=false($|$'\n') ]] || \
[[ $gitConfig =~ (^|$'\n')cmder\.shstatus=false($|$'\n') ]]
then then
echo false return 1 # disabled
else
echo true
fi fi
return 0
} }
# Prints current branch or detached HEAD short commit hash
function getSimpleGitBranch() { function getSimpleGitBranch() {
gitDir=$(git rev-parse --git-dir 2>/dev/null) local gitDir
if [ -z "$gitDir" ]; then gitDir=$(git rev-parse --git-dir 2>/dev/null) || return 0
return 0
fi
headContent=$(< "$gitDir/HEAD") local headFile="$gitDir/HEAD"
if [[ "$headContent" == "ref: refs/heads/"* ]] [ -f "$headFile" ] || return 0
local headContent
headContent=$(< "$headFile")
if [[ "$headContent" =~ ^ref:\ refs/heads/(.+)$ ]]
then then
echo " (${headContent:16})" echo " (${BASH_REMATCH[1]})"
else else
echo " (HEAD detached at ${headContent:0:7})" echo " (HEAD detached at ${headContent:0:7})"
fi fi
@ -33,7 +41,7 @@ fi
if test -f ~/.config/git/git-prompt.sh if test -f ~/.config/git/git-prompt.sh
then then
if [[ $(getGitStatusSetting) == true ]] if getGitStatusSetting
then then
. ~/.config/git/git-prompt.sh . ~/.config/git/git-prompt.sh
fi fi
@ -55,7 +63,7 @@ else
if test -f "$COMPLETION_PATH/git-prompt.sh" if test -f "$COMPLETION_PATH/git-prompt.sh"
then then
. "$COMPLETION_PATH/git-completion.bash" . "$COMPLETION_PATH/git-completion.bash"
if [[ $(getGitStatusSetting) == true ]] if getGitStatusSetting
then then
. "$COMPLETION_PATH/git-prompt.sh" . "$COMPLETION_PATH/git-prompt.sh"
PS1="$PS1"'\[\033[36m\]' # change color to cyan PS1="$PS1"'\[\033[36m\]' # change color to cyan