PS1 : show git branch when not in dotfiles.

This commit is contained in:
Dmitry Voronin 2023-12-04 13:32:44 +03:00
parent 8bd6946470
commit b7d4c77088
2 changed files with 36 additions and 3 deletions

View file

@ -87,6 +87,37 @@ _git_current_branch()
git branch --show-current 2> /dev/null
}
# Show origin's url.
_git_origin_url()
{
git remote get-url origin
}
# Get this dotfiles url.
_git_dotfiles_url()
{
echo 'https://git.voronind.com/voronind/linux.git'
}
# Check if current git repo is this dotfiles.
_git_is_dotfiles()
{
# [[ "$(_git_origin_url)" = "$(_git_dotfiles_url)" ]]
local dir="${PWD}"
while [[ "${dir}" != "" ]]; do
if [[ -d "${dir}/.git" ]]; then
if [[ "${dir}" = "${HOME}" ]]; then
return 0
else
return 1
fi
fi
dir="${dir%/*}"
done
}
# autocomplete.
_completion_loader git &> /dev/null
__git_complete gps _git_push &> /dev/null

View file

@ -44,9 +44,11 @@ __prompt_command()
# PS1+="\${PWD}"
# Add git branch if available.
if ! _git_is_dotfiles; then
local git_branch="$(_git_current_branch)"
if [[ "${git_branch}" != "" ]]; then
[[ "${git_branch}" =~ (master|main) ]] || PS1+=" ${color_bblue}@${git_branch}"
PS1+=" ${color_bblue}@${git_branch}"
fi
fi
# set error red