mirror of
https://github.com/cmderdev/cmder.git
synced 2025-07-17 21:19:41 +08:00
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:
36
vendor/git-prompt.sh
vendored
36
vendor/git-prompt.sh
vendored
@ -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
|
||||||
|
Reference in New Issue
Block a user