Merge branch 'master' of https://github.com/aks/bash-git-prompt into aks-master

Conflicts:
	git-prompt-colors.sh
	gitprompt.sh
master
Martin Gondermann 11 years ago
commit f6eba356b0
  1. 77
      git-prompt-colors.sh
  2. 122
      gitprompt.sh
  3. 6
      prompt-colors.sh

@ -1,38 +1,45 @@
# These are the color definitions used by gitprompt.sh # These are the color definitions used by gitprompt.sh
local Time12a="\$(date +%H:%M)" define_git_prompt_colors() {
local PathShort="\w" Time12a="\$(date +%H:%M)"
PathShort="\w"
# These are the color definitions used by gitprompt.sh
GIT_PROMPT_PREFIX="[" # start of the git info string
GIT_PROMPT_SUFFIX="]" # the end of the git info string
GIT_PROMPT_SEPARATOR="|" # separates each item
GIT_PROMPT_BRANCH="${Magenta}" # the git branch that is active in the current directory
GIT_PROMPT_STAGED="${Red}" # the number of staged files/directories
GIT_PROMPT_CONFLICTS="${Red}" # the number of files in conflict
GIT_PROMPT_CHANGED="${Blue}" # the number of changed files
GIT_PROMPT_REMOTE=" " # the remote branch name (if any) and the symbols for ahead and behind
GIT_PROMPT_UNTRACKED="${Cyan}" # the number of untracked files/dirs
GIT_PROMPT_STASHED="${BoldBlue}" # the number of stashed files/dir
GIT_PROMPT_CLEAN="${BoldGreen}" # a colored flag indicating a "clean" repo
# For the command indicator, the placeholder _LAST_COMMAND_STATE_
# will be replaced with the exit code of the last command
# e.g.
# GIT_PROMPT_COMMAND_OK="${Green}✔-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of 0
# GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of other than 0
GIT_PROMPT_COMMAND_OK="${Green}" # indicator if the last command returned with an exit code of 0
GIT_PROMPT_COMMAND_FAIL="${Red}" # indicator if the last command returned with an exit code of other than 0
GIT_PROMPT_START_USER="${Yellow}${PathShort}${ResetColor}"
GIT_PROMPT_START_ROOT="${Yellow}${PathShort}${ResetColor}"
GIT_PROMPT_END_USER=" \n${White}${Time12a}${ResetColor} $ "
GIT_PROMPT_END_ROOT=" \n${White}${Time12a}${ResetColor} # "
# Please do not add colors to these symbols
GIT_PROMPT_SYMBOLS_AHEAD="↑·" # The symbol for "n versions ahead of origin"
GIT_PROMPT_SYMBOLS_BEHIND="↓·" # The symbol for "n versions behind of origin"
GIT_PROMPT_SYMBOLS_PREHASH=":" # Written before hash of commit, if no name could be found
}
if [[ -z "$GIT_PROMPT_SEPARATOR" || -z "$GIT_PROMPT_COMMAND_OK" ]]; then
define_git_prompt_colors
fi
# These are the color definitions used by gitprompt.sh
GIT_PROMPT_PREFIX="[" # start of the git info string
GIT_PROMPT_SUFFIX="]" # the end of the git info string
GIT_PROMPT_SEPARATOR="|" # separates each item
GIT_PROMPT_BRANCH="${Magenta}" # the git branch that is active in the current directory
GIT_PROMPT_STAGED="${Red}" # the number of staged files/directories
GIT_PROMPT_CONFLICTS="${Red}" # the number of files in conflict
GIT_PROMPT_CHANGED="${Blue}" # the number of changed files
GIT_PROMPT_REMOTE=" " # the remote branch name (if any) and the symbols for ahead and behind
GIT_PROMPT_UNTRACKED="${Cyan}" # the number of untracked files/dirs
GIT_PROMPT_STASHED="${BoldBlue}" # the number of stashed files/dir
GIT_PROMPT_CLEAN="${BoldGreen}" # a colored flag indicating a "clean" repo
# For the command indicator, the placeholder _LAST_COMMAND_STATE_
# will be replaced with the exit code of the last command
# e.g.
# GIT_PROMPT_COMMAND_OK="${Green}✔-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of 0
# GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of other than 0
GIT_PROMPT_COMMAND_OK="${Green}" # indicator if the last command returned with an exit code of 0
GIT_PROMPT_COMMAND_FAIL="${Red}" # indicator if the last command returned with an exit code of other than 0
GIT_PROMPT_START_USER="${Yellow}${PathShort}${ResetColor}"
GIT_PROMPT_START_ROOT="${Yellow}${PathShort}${ResetColor}"
GIT_PROMPT_END_USER=" \n${White}${Time12a}${ResetColor} $ "
GIT_PROMPT_END_ROOT=" \n${White}${Time12a}${ResetColor} # "
# Please do not add colors to these symbols
GIT_PROMPT_SYMBOLS_AHEAD="↑·" # The symbol for "n versions ahead of origin"
GIT_PROMPT_SYMBOLS_BEHIND="↓·" # The symbol for "n versions behind of origin"
GIT_PROMPT_SYMBOLS_PREHASH=":" # Written before hash of commit, if no name could be found

@ -11,19 +11,20 @@ function git_prompt_dir()
{ {
# assume the gitstatus.py is in the same directory as this script # assume the gitstatus.py is in the same directory as this script
# code thanks to http://stackoverflow.com/questions/59895 # code thanks to http://stackoverflow.com/questions/59895
if [ -z "${__GIT_PROMPT_DIR}" ]; then if [ -z "$__GIT_PROMPT_DIR" ]; then
local SOURCE="${BASH_SOURCE[0]}" local SOURCE="${BASH_SOURCE[0]}"
while [ -h "${SOURCE}" ]; do while [ -h "$SOURCE" ]; do
local DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )" local DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "${SOURCE}")" SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="${DIR}/${SOURCE}" [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done done
__GIT_PROMPT_DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )" __GIT_PROMPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
fi fi
} }
# gp_set_file_var ENVAR SOMEFILE # gp_set_file_var ENVAR SOMEFILE
# #
# If ENVAR is set, check that it's value exists as a readable file. Otherwise,
# Set ENVAR to the path to SOMEFILE, based on $HOME, $__GIT_PROMPT_DIR, and the # Set ENVAR to the path to SOMEFILE, based on $HOME, $__GIT_PROMPT_DIR, and the
# directory of the current script. The SOMEFILE can be prefixed with '.', or # directory of the current script. The SOMEFILE can be prefixed with '.', or
# not. # not.
@ -33,7 +34,13 @@ function git_prompt_dir()
function gp_set_file_var() { function gp_set_file_var() {
local envar="$1" local envar="$1"
local file="$2" local file="$2"
if eval "test -z \"\$$envar\"" ; then if eval "[[ -n \"\$$envar\" && -r \"\$$envar\" ]]" ; then # is envar set to a readable file?
local basefile
eval "basefile=\"\`basename \\\"\$$envar\\\"\`\"" # assign basefile
if [[ "$basefile" = "$file" || "$basefile" = ".$file" ]]; then
return 0
fi
else # envar is not set, or it's set to a different file than requested
eval "$envar=" # set empty envar eval "$envar=" # set empty envar
gp_maybe_set_envar_to_path "$envar" "$HOME/.$file" "$HOME/$file" "$HOME/lib/$file" && return 0 gp_maybe_set_envar_to_path "$envar" "$HOME/.$file" "$HOME/$file" "$HOME/lib/$file" && return 0
git_prompt_dir git_prompt_dir
@ -72,34 +79,28 @@ function git_prompt_config()
# git-prompt-colors.sh -- sets the GIT_PROMPT color scheme, using names from prompt-colors.sh # git-prompt-colors.sh -- sets the GIT_PROMPT color scheme, using names from prompt-colors.sh
if gp_set_file_var __PROMPT_COLORS_SH prompt-colors.sh ; then if gp_set_file_var __PROMPT_COLORS_SH prompt-colors.sh ; then
if [[ -n "${__PROMPT_COLORS_SH}" ]]; then source "$__PROMPT_COLORS_SH" # outsource the color defs
source "${__PROMPT_COLORS_SH}" # outsource the color defs else
else echo 1>&2 "Cannot find prompt-colors.sh!"
echo 1>&2 "Cannot find prompt-colors.sh!"
fi
fi fi
# source the user's ~/.git-prompt-colors.sh file, or the one that should be # source the user's ~/.git-prompt-colors.sh file, or the one that should be
# sitting in the same directory as this script # sitting in the same directory as this script
if gp_set_file_var __GIT_PROMPT_COLORS_FILE git-prompt-colors.sh ; then if gp_set_file_var __GIT_PROMPT_COLORS_FILE git-prompt-colors.sh ; then
source "$__GIT_PROMPT_COLORS_FILE"
# if the envar is defined, source the file for custom colors else
if [[ -n "${__GIT_PROMPT_COLORS_FILE}" ]]; then echo 1>&2 "Cannot find git-prompt-colors.sh!"
source "${__GIT_PROMPT_COLORS_FILE}"
else
echo 1>&2 "Cannot find git-prompt-colors.sh!"
fi
fi fi
if [ "x${GIT_PROMPT_SHOW_LAST_COMMAND_INDICATOR}" == "x1" ]; then if [ "x$GIT_PROMPT_SHOW_LAST_COMMAND_INDICATOR" == "x1" ]; then
if [ $GIT_PROMPT_LAST_COMMAND_STATE = 0 ]; then if [ $GIT_PROMPT_LAST_COMMAND_STATE = 0 ]; then
LAST_COMMAND_INDICATOR="${GIT_PROMPT_COMMAND_OK}"; LAST_COMMAND_INDICATOR="$GIT_PROMPT_COMMAND_OK";
else else
LAST_COMMAND_INDICATOR="${GIT_PROMPT_COMMAND_FAIL}"; LAST_COMMAND_INDICATOR="$GIT_PROMPT_COMMAND_FAIL";
fi fi
LAST_COMMAND_INDICATOR="${LAST_COMMAND_INDICATOR/_LAST_COMMAND_STATE_/${GIT_PROMPT_LAST_COMMAND_STATE}}" LAST_COMMAND_INDICATOR="${LAST_COMMAND_INDICATOR/_LAST_COMMAND_STATE_/$GIT_PROMPT_LAST_COMMAND_STATE}"
fi fi
# Do this only once to define PROMPT_START and PROMPT_END # Do this only once to define PROMPT_START and PROMPT_END
@ -112,44 +113,44 @@ function git_prompt_config()
# local Time12a="(\@))" # local Time12a="(\@))"
local PathShort="\w" local PathShort="\w"
if [[ -z "${GIT_PROMPT_START}" ]] ; then if [[ -z "$GIT_PROMPT_START" ]] ; then
if $_isroot; then if $_isroot; then
PROMPT_START="$GIT_PROMPT_START_ROOT" PROMPT_START="$GIT_PROMPT_START_ROOT"
else else
PROMPT_START="$GIT_PROMPT_START_USER" PROMPT_START="$GIT_PROMPT_START_USER"
fi fi
else else
PROMPT_START="${GIT_PROMPT_START}" PROMPT_START="$GIT_PROMPT_START"
fi fi
if [[ -z "${GIT_PROMPT_END}" ]] ; then if [[ -z "$GIT_PROMPT_END" ]] ; then
if $_isroot; then if $_isroot; then
PROMPT_END="$GIT_PROMPT_END_ROOT" PROMPT_END="$GIT_PROMPT_END_ROOT"
else else
PROMPT_END="$GIT_PROMPT_END_USER" PROMPT_END="$GIT_PROMPT_END_USER"
fi fi
else else
PROMPT_END="${GIT_PROMPT_END}" PROMPT_END="$GIT_PROMPT_END"
fi fi
fi fi
# set GIT_PROMPT_LEADING_SPACE to 0 if you want to have no leading space in front of the GIT prompt # set GIT_PROMPT_LEADING_SPACE to 0 if you want to have no leading space in front of the GIT prompt
if [ "x${GIT_PROMPT_LEADING_SPACE}" == "x0" ]; then if [[ "$GIT_PROMPT_LEADING_SPACE" = 0 ]]; then
PROMPT_LEADING_SPACE="" PROMPT_LEADING_SPACE=""
else else
PROMPT_LEADING_SPACE=" " PROMPT_LEADING_SPACE=" "
fi fi
if [ "x${GIT_PROMPT_ONLY_IN_REPO}" == "x1" ]; then if [[ "$GIT_PROMPT_ONLY_IN_REPO" = 1 ]]; then
EMPTY_PROMPT=$OLD_GITPROMPT EMPTY_PROMPT="$OLD_GITPROMPT"
else else
if [[ -n "${VIRTUAL_ENV}" ]]; then local ps="$LAST_COMMAND_INDICATOR"
EMPTY_PROMPT="${LAST_COMMAND_INDICATOR}(${Blue}$(basename "${VIRTUAL_ENV}")${ResetColor}) ${PROMPT_START}$($prompt_callback)${PROMPT_END}" if [[ -n "$VIRTUAL_ENV" ]]; then
elif [[ -n "${CONDA_DEFAULT_ENV}" ]]; then ps="$ps($Blue$(basename \"$VIRTUAL_ENV\")$ResetColor) "
EMPTY_PROMPT="${LAST_COMMAND_INDICATOR}(${Blue}$(basename "${CONDA_DEFAULT_ENV}")${ResetColor}) ${PROMPT_START}$($prompt_callback)${PROMPT_END}" elif [[ -n "$CONDA_DEFAULT_ENV" ]]; then
else ps="$ps($Blue$(basename \"$CONDA_DEFAULT_ENV\")$ResetColor) "
EMPTY_PROMPT="${LAST_COMMAND_INDICATOR}${PROMPT_START}$($prompt_callback)${PROMPT_END}"
fi fi
EMPTY_PROMPT="$ps$PROMPT_START$($prompt_callback)$PROMPT_END"
fi fi
# fetch remote revisions every other $GIT_PROMPT_FETCH_TIMEOUT (default 5) minutes # fetch remote revisions every other $GIT_PROMPT_FETCH_TIMEOUT (default 5) minutes
@ -159,6 +160,7 @@ function git_prompt_config()
if ! gp_maybe_set_envar_to_path __GIT_STATUS_CMD "$__GIT_PROMPT_DIR/gitstatus.sh" "$__GIT_PROMPT_DIR/gitstatus.py" ; then if ! gp_maybe_set_envar_to_path __GIT_STATUS_CMD "$__GIT_PROMPT_DIR/gitstatus.sh" "$__GIT_PROMPT_DIR/gitstatus.py" ; then
echo 1>&2 "Cannot find gitstatus.sh or gitstatus.py!" echo 1>&2 "Cannot find gitstatus.sh or gitstatus.py!"
fi fi
# __GIT_STATUS_CMD defined
fi fi
} }
@ -173,21 +175,21 @@ function setGitPrompt() {
git_prompt_config git_prompt_config
local repo=`git rev-parse --show-toplevel 2> /dev/null` local repo=`git rev-parse --show-toplevel 2> /dev/null`
if [[ ! -e "${repo}" ]]; then if [[ ! -e "$repo" ]]; then
PS1="${EMPTY_PROMPT}" PS1="$EMPTY_PROMPT"
return return
fi fi
local FETCH_REMOTE_STATUS=1 local FETCH_REMOTE_STATUS=1
if [[ "x${GIT_PROMPT_FETCH_REMOTE_STATUS}" == "x0" ]]; then if [[ "$GIT_PROMPT_FETCH_REMOTE_STATUS" = 0 ]]; then
FETCH_REMOTE_STATUS=0 FETCH_REMOTE_STATUS=0
fi fi
if [[ -e "${repo}/.bash-git-rc" ]]; then if [[ -e "$repo/.bash-git-rc" ]]; then
source "${repo}/.bash-git-rc" source "$repo/.bash-git-rc"
fi fi
if [ "x${FETCH_REMOTE_STATUS}" == "x1" ]; then if [ "$FETCH_REMOTE_STATUS" = 1 ]; then
checkUpstream checkUpstream
fi fi
@ -198,9 +200,9 @@ function checkUpstream() {
local GIT_PROMPT_FETCH_TIMEOUT local GIT_PROMPT_FETCH_TIMEOUT
git_prompt_config git_prompt_config
local FETCH_HEAD="${repo}/.git/FETCH_HEAD" local FETCH_HEAD="$repo/.git/FETCH_HEAD"
# Fech repo if local is stale for more than $GIT_FETCH_TIMEOUT minutes # Fech repo if local is stale for more than $GIT_FETCH_TIMEOUT minutes
if [[ ! -e "${FETCH_HEAD}" || -e `find "${FETCH_HEAD}" -mmin +${GIT_PROMPT_FETCH_TIMEOUT}` ]] if [[ ! -e "$FETCH_HEAD" || -e `find "$FETCH_HEAD" -mmin +$GIT_PROMPT_FETCH_TIMEOUT` ]]
then then
if [[ -n $(git remote show) ]]; then if [[ -n $(git remote show) ]]; then
( (
@ -224,7 +226,7 @@ function updatePrompt() {
git_prompt_config git_prompt_config
local -a GitStatus local -a GitStatus
GitStatus=($("${__GIT_STATUS_CMD}" 2>/dev/null)) GitStatus=($("$__GIT_STATUS_CMD" 2>/dev/null))
local GIT_BRANCH=${GitStatus[0]} local GIT_BRANCH=${GitStatus[0]}
local GIT_REMOTE=${GitStatus[1]} local GIT_REMOTE=${GitStatus[1]}
@ -238,7 +240,7 @@ function updatePrompt() {
local GIT_STASHED=${GitStatus[6]} local GIT_STASHED=${GitStatus[6]}
local GIT_CLEAN=${GitStatus[7]} local GIT_CLEAN=${GitStatus[7]}
if [[ -n "${GitStatus}" ]]; then if [[ -n "$GitStatus" ]]; then
local STATUS="${PROMPT_LEADING_SPACE}${GIT_PROMPT_PREFIX}${GIT_PROMPT_BRANCH}${GIT_BRANCH}${ResetColor}" local STATUS="${PROMPT_LEADING_SPACE}${GIT_PROMPT_PREFIX}${GIT_PROMPT_BRANCH}${GIT_BRANCH}${ResetColor}"
# __add_status KIND VALEXPR INSERT # __add_status KIND VALEXPR INSERT
@ -247,26 +249,26 @@ function updatePrompt() {
__chk_gitvar_status() { __chk_gitvar_status() {
local v local v
if [[ "x$2" == "x-n" ]] ; then if [[ "x$2" == "x-n" ]] ; then
v="$2 \"\${GIT_$1}\"" v="$2 \"\$GIT_$1\""
else else
v="\${GIT_$1} $2" v="\$GIT_$1 $2"
fi fi
if eval "test $v" ; then if eval "test $v" ; then
if [[ $# -lt 2 || "$3" != '-' ]]; then if [[ $# -lt 2 || "$3" != '-' ]]; then
__add_status "\${GIT_PROMPT_$1}\${GIT_$1}\${ResetColor}" __add_status "\$GIT_PROMPT_$1\$GIT_$1\$ResetColor"
else else
__add_status "\${GIT_PROMPT_$1}\${ResetColor}" __add_status "\$GIT_PROMPT_$1\$ResetColor"
fi fi
fi fi
} }
__add_gitvar_status() { __add_gitvar_status() {
__add_status "\${GIT_PROMPT_$1}\${GIT_$1}\${ResetColor}" __add_status "\$GIT_PROMPT_$1\$GIT_$1\$ResetColor"
} }
# __add_status SOMETEXT # __add_status SOMETEXT
__add_status() { __add_status() {
eval "STATUS=\"${STATUS}$1\"" eval "STATUS=\"$STATUS$1\""
} }
__chk_gitvar_status 'REMOTE' '-n' __chk_gitvar_status 'REMOTE' '-n'
@ -277,19 +279,19 @@ function updatePrompt() {
__chk_gitvar_status 'UNTRACKED' '-ne 0' __chk_gitvar_status 'UNTRACKED' '-ne 0'
__chk_gitvar_status 'STASHED' '-ne 0' __chk_gitvar_status 'STASHED' '-ne 0'
__chk_gitvar_status 'CLEAN' '-eq 1' - __chk_gitvar_status 'CLEAN' '-eq 1' -
__add_status "${ResetColor}$GIT_PROMPT_SUFFIX" __add_status "$ResetColor$GIT_PROMPT_SUFFIX"
PS1="${LAST_COMMAND_INDICATOR}${PROMPT_START}$($prompt_callback)${STATUS}${PROMPT_END}" PS1="$LAST_COMMAND_INDICATOR$PROMPT_START$($prompt_callback)$STATUS$PROMPT_END"
if [[ -n "${VIRTUAL_ENV}" ]]; then if [[ -n "$VIRTUAL_ENV" ]]; then
PS1="(${Blue}$(basename ${VIRTUAL_ENV})${ResetColor}) ${PS1}" PS1="($Blue$(basename \"$VIRTUAL_ENV\")$ResetColor) $PS1"
fi fi
if [[ -n "${CONDA_DEFAULT_ENV}" ]]; then if [[ -n "$CONDA_DEFAULT_ENV" ]]; then
PS1="(${Blue}$(basename ${CONDA_DEFAULT_ENV})${ResetColor}) ${PS1}" PS1="($Blue$(basename \"$CONDA_DEFAULT_ENV\")$ResetColor) $PS1"
fi fi
else else
PS1="${EMPTY_PROMPT}" PS1="$EMPTY_PROMPT"
fi fi
} }
@ -326,7 +328,7 @@ function gp_install_prompt {
esac esac
fi fi
if [ "x${GIT_PROMPT_SHOW_LAST_COMMAND_INDICATOR}" == "x1" ]; then if [ "$GIT_PROMPT_SHOW_LAST_COMMAND_INDICATOR" = 1 ]; then
local setLastCommandStateEntry="setLastCommandState" local setLastCommandStateEntry="setLastCommandState"
case ";$PROMPT_COMMAND;" in case ";$PROMPT_COMMAND;" in
*";$setLastCommandStateEntry;"*) *";$setLastCommandStateEntry;"*)

@ -76,7 +76,11 @@ define_color_names() {
_def_color ResetColor 0 0 _def_color ResetColor 0 0
} }
define_color_names
# do the color definitions only once
if [[ ${#ColorNames[*]} = 0 || -z "$IntenseBlack" || -z "$ResetColor" ]]; then
define_color_names
fi
# end of prompt-colors.sh # end of prompt-colors.sh
# vim: set ai sw=2 # vim: set ai sw=2

Loading…
Cancel
Save