Created
August 26, 2012 21:37
-
-
Save taha/3483751 to your computer and use it in GitHub Desktop.
Command prompt customisation to show git branch and status
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
RED="\[\033[0;31m\]" | |
YELLOW="\[\033[0;33m\]" | |
GREEN="\[\033[0;32m\]" | |
CYAN="\[\033[0;36m\]" | |
NO_COLOUR="\[\033[0m\]" | |
GIT_PROMPT_PREFIX="[git:" | |
GIT_PROMPT_SUFFIX="]$NO_COLOUR " | |
GIT_PROMPT_DIRTY="$RED+" | |
GIT_PROMPT_CLEAN="$GREEN" | |
function git_current_branch() | |
{ | |
echo "$(git branch 2>/dev/null | sed -n '/^\*/s/^\* //p')" | |
} | |
function git_parse_dirty() | |
{ | |
if [[ -n $(git status -s 2> /dev/null) ]]; then | |
echo "$GIT_PROMPT_DIRTY" | |
else | |
echo "$GIT_PROMPT_CLEAN" | |
fi | |
} | |
function git_prompt_info() | |
{ | |
if ! git rev-parse --git-dir > /dev/null 2>&1; then | |
return 0 | |
fi | |
echo "$(git_parse_dirty)$GIT_PROMPT_PREFIX$(git_current_branch)$GIT_PROMPT_SUFFIX" | |
} | |
function prompt_line() | |
{ | |
printf "%s: %s%s" "$CYAN\u" "$YELLOW\w $(git_prompt_info)$NO_COLOUR" "→ " | |
} | |
PROMPT_COMMAND='PS1="$(prompt_line)"' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Preview of the script above