Last active
December 15, 2021 19:15
-
-
Save zessx/10777a44ff56304487e7970142ca0b65 to your computer and use it in GitHub Desktop.
Custom PS1 for git
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install for all users: | |
# $ wget https://gist.githubusercontent.com/zessx/10777a44ff56304487e7970142ca0b65/raw/bash-git-prompt.sh --no-check-certificate -P /etc/bash_completion.d | |
# Install for one user: | |
# $ wget https://gist.githubusercontent.com/zessx/10777a44ff56304487e7970142ca0b65/raw/bash-git-prompt.sh --no-check-certificate -P ~ | |
# $ echo "\n\n# Bash Git Prompt\nsource ~/bash-git-prompt.sh" >> ~/.bashrc | |
# $ . ~/.bashrc | |
# Settings: | |
BGP_BRANCH_SAFE=("develop" "dev") | |
BGP_BRANCH_UNSAFE=("master") | |
BGP_USER_UNSAFE=("root" "prod" "git") | |
function in_array() { | |
local e | |
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done | |
return 1 | |
} | |
function bgp_prompt() { | |
# Define colors | |
local RED="\[\e[31m\]" | |
local YELLOW="\[\e[33m\]" | |
local GREEN="\[\e[32m\]" | |
local RESET="\[$(tput sgr0)\]" | |
# Get username and host | |
local USER=$(whoami) | |
local USER_PS="$GREEN\u@\h$RESET" | |
if in_array "${USER}" "${BGP_USER_UNSAFE[@]}"; then | |
USER_PS="$RED\u@\h$RESET" | |
fi | |
# Get git status | |
local GIT_STATUS=$(git status --porcelain --ignore-submodules 2> /dev/null | wc -l) | |
local GIT_STATUS_PS="$GREEN=$RESET" | |
if [[ "0" != "$GIT_STATUS" ]]; then | |
GIT_STATUS_PS="$RED~$GIT_STATUS$RESET" | |
fi | |
# Get git branch | |
local GIT_BRANCH=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') | |
local GIT_BRANCH_PS=" $YELLOW($GIT_BRANCH)$RESET$GIT_STATUS_PS" | |
if in_array "${GIT_BRANCH}" "${BGP_BRANCH_SAFE[@]}"; then | |
GIT_BRANCH_PS=" $GREEN($GIT_BRANCH)$RESET$GIT_STATUS_PS" | |
elif in_array "${GIT_BRANCH}" "${BGP_BRANCH_UNSAFE[@]}"; then | |
GIT_BRANCH_PS=" $RED($GIT_BRANCH)$RESET$GIT_STATUS_PS" | |
elif [[ "" == "$GIT_BRANCH" ]]; then | |
GIT_BRANCH_PS="" | |
fi | |
PS1="${USER_PS} ${YELLOW}\w${RESET}${GIT_BRANCH_PS} \\$ " | |
} | |
PROMPT_COMMAND=bgp_prompt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment