Skip to content

Commit c6bfd6f

Browse files
committed
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
1 parent 1d51c4f commit c6bfd6f

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

vendor/git-prompt.sh

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,32 @@
11
function getGitStatusSetting() {
2-
gitStatusSetting=$(git --no-pager config -l 2>/dev/null)
2+
local gitConfig
33

4-
if [[ -n ${gitStatusSetting} ]] && [[ ${gitStatusSetting} =~ cmder.status=false ]] || [[ ${gitStatusSetting} =~ cmder.shstatus=false ]]
4+
# Get all git config entries for the current repository without pager
5+
gitConfig=$(git --no-pager config -l 2>/dev/null) || return 0 # treat failure as enabled
6+
7+
# Check if git status for Cmder is disabled
8+
if [[ $gitConfig =~ (^|$'\n')cmder\.status=false($|$'\n') ]] || \
9+
[[ $gitConfig =~ (^|$'\n')cmder\.shstatus=false($|$'\n') ]]
510
then
6-
echo false
7-
else
8-
echo true
11+
return 1 # disabled
912
fi
13+
14+
return 0
1015
}
1116

17+
# Prints current branch or detached HEAD short commit hash
1218
function getSimpleGitBranch() {
13-
gitDir=$(git rev-parse --git-dir 2>/dev/null)
14-
if [ -z "$gitDir" ]; then
15-
return 0
16-
fi
19+
local gitDir
20+
gitDir=$(git rev-parse --git-dir 2>/dev/null) || return 0
21+
22+
local headFile="$gitDir/HEAD"
23+
[ -f "$headFile" ] || return 0
1724

18-
headContent=$(< "$gitDir/HEAD")
19-
if [[ "$headContent" == "ref: refs/heads/"* ]]
25+
local headContent
26+
headContent=$(< "$headFile")
27+
if [[ "$headContent" =~ ^ref:\ refs/heads/(.+)$ ]]
2028
then
21-
echo " (${headContent:16})"
29+
echo " (${BASH_REMATCH[1]})"
2230
else
2331
echo " (HEAD detached at ${headContent:0:7})"
2432
fi
@@ -33,7 +41,7 @@ fi
3341

3442
if test -f ~/.config/git/git-prompt.sh
3543
then
36-
if [[ $(getGitStatusSetting) == true ]]
44+
if getGitStatusSetting
3745
then
3846
. ~/.config/git/git-prompt.sh
3947
fi
@@ -55,7 +63,7 @@ else
5563
if test -f "$COMPLETION_PATH/git-prompt.sh"
5664
then
5765
. "$COMPLETION_PATH/git-completion.bash"
58-
if [[ $(getGitStatusSetting) == true ]]
66+
if getGitStatusSetting
5967
then
6068
. "$COMPLETION_PATH/git-prompt.sh"
6169
PS1="$PS1"'\[\033[36m\]' # change color to cyan

0 commit comments

Comments
 (0)