From: Junio C Hamano Date: Mon, 1 Jul 2013 19:41:55 +0000 (-0700) Subject: Merge branch 'ed/color-prompt' X-Git-Tag: v1.8.4-rc0~91 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=46b045917c2786b26c80bfb118466fdf373c346a;p=thirdparty%2Fgit.git Merge branch 'ed/color-prompt' Code clean-up for in-prompt status script (in contrib/). * ed/color-prompt: git-prompt.sh: add missing information in comments git-prompt.sh: do not print duplicate clean color code t9903: remove redundant tests git-prompt.sh: refactor colored prompt code t9903: add tests for git-prompt pcmode --- 46b045917c2786b26c80bfb118466fdf373c346a diff --cc contrib/completion/git-prompt.sh index daed6a1d03,b3f39e82d5..a81ef5a482 --- a/contrib/completion/git-prompt.sh +++ b/contrib/completion/git-prompt.sh @@@ -13,18 -13,20 +13,20 @@@ # 3a) Change your PS1 to call __git_ps1 as # command-substitution: # Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' - # ZSH: PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ ' + # ZSH: setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ ' # the optional argument will be used as format string. - # 3b) Alternatively, for a slighly faster prompt, if you are - # using bash, __git_ps1 can be used for PROMPT_COMMAND -# 3b) Alternatively, __git_ps1 can be used for PROMPT_COMMAND in -# Bash or for precmd() in ZSH with two parameters,
 and
 -#        , which are strings you would put in $PS1 before
 -#        and after the status string generated by the git-prompt
 -#        machinery.  e.g.
++#    3b) Alternatively, for a slightly faster prompt, __git_ps1 can
++#        be used for PROMPT_COMMAND in Bash or for precmd() in Zsh
 +#        with two parameters, 
 and , which are strings
 +#        you would put in $PS1 before and after the status string
 +#        generated by the git-prompt machinery.  e.g.
  #        Bash: PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'
+ #          will show username, at-sign, host, colon, cwd, then
+ #          various status string, followed by dollar and SP, as
+ #          your prompt.
  #        ZSH:  precmd () { __git_ps1 "%n" ":%~$ " "|%s" }
- #        will show username, at-sign, host, colon, cwd, then
- #        various status string, followed by dollar and SP, as
- #        your prompt.
+ #          will show username, pipe, then various status string,
+ #          followed by colon, cwd, dollar and SP, as your prompt.
  #        Optionally, you can supply a third argument with a printf
  #        format string to finetune the output of the branch status
  #
@@@ -78,8 -81,33 +81,9 @@@
  #
  # If you would like a colored hint about the current dirty state, set
  # GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
- # the colored output of "git status -sb".
+ # the colored output of "git status -sb" and are available only when
+ # using __git_ps1 for PROMPT_COMMAND or precmd.
  
 -# __gitdir accepts 0 or 1 arguments (i.e., location)
 -# returns location of .git repo
 -__gitdir ()
 -{
 -	# Note: this function is duplicated in git-completion.bash
 -	# When updating it, make sure you update the other one to match.
 -	if [ -z "${1-}" ]; then
 -		if [ -n "${__git_dir-}" ]; then
 -			echo "$__git_dir"
 -		elif [ -n "${GIT_DIR-}" ]; then
 -			test -d "${GIT_DIR-}" || return 1
 -			echo "$GIT_DIR"
 -		elif [ -d .git ]; then
 -			echo .git
 -		else
 -			git rev-parse --git-dir 2>/dev/null
 -		fi
 -	elif [ -d "$1/.git" ]; then
 -		echo "$1/.git"
 -	else
 -		echo "$1"
 -	fi
 -}
 -
  # stores the divergence from upstream in $p
  # used by GIT_PS1_SHOWUPSTREAM
  __git_ps1_show_upstream ()
@@@ -401,74 -362,71 +371,75 @@@ __git_ps1 (
  					git describe --tags --exact-match HEAD ;;
  				esac 2>/dev/null)" ||
  
 -				b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
 -				b="unknown"
 +				b="$short_sha..."
  				b="($b)"
 -			}
 +			fi
  		fi
 +	fi
  
 -		if [ -n "$step" ] && [ -n "$total" ]; then
 -			r="$r $step/$total"
 -		fi
 +	if [ -n "$step" ] && [ -n "$total" ]; then
 +		r="$r $step/$total"
 +	fi
  
 -		local w=""
 -		local i=""
 -		local s=""
 -		local u=""
 -		local c=""
 -		local p=""
 +	local w=""
 +	local i=""
 +	local s=""
 +	local u=""
 +	local c=""
 +	local p=""
  
 -		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
 -			if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
 -				c="BARE:"
 +	if [ "true" = "$inside_gitdir" ]; then
 +		if [ "true" = "$bare_repo" ]; then
 +			c="BARE:"
 +		else
 +			b="GIT_DIR!"
 +		fi
 +	elif [ "true" = "$inside_worktree" ]; then
 +		if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&
 +		   [ "$(git config --bool bash.showDirtyState)" != "false" ]
 +		then
 +			git diff --no-ext-diff --quiet --exit-code || w="*"
 +			if [ -n "$short_sha" ]; then
 +				git diff-index --cached --quiet HEAD -- || i="+"
  			else
 -				b="GIT_DIR!"
 -			fi
 -		elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
 -			if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&
 -			   [ "$(git config --bool bash.showDirtyState)" != "false" ]
 -			then
 -				git diff --no-ext-diff --quiet --exit-code || w="*"
 -				if git rev-parse --quiet --verify HEAD >/dev/null; then
 -					git diff-index --cached --quiet HEAD -- || i="+"
 -				else
 -					i="#"
 -				fi
 -			fi
 -			if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
 -				git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
 +				i="#"
  			fi
 +		fi
 +		if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ] &&
 +		   [ -r "$g/refs/stash" ]; then
 +			s="$"
 +		fi
  
 -			if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ] &&
 -			   [ "$(git config --bool bash.showUntrackedFiles)" != "false" ] &&
 -			   [ -n "$(git ls-files --others --exclude-standard)" ]
 -			then
 -				u="%${ZSH_VERSION+%}"
 -			fi
 +		if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ] &&
 +		   [ "$(git config --bool bash.showUntrackedFiles)" != "false" ] &&
 +		   git ls-files --others --exclude-standard --error-unmatch -- '*' >/dev/null 2>/dev/null
 +		then
 +			u="%${ZSH_VERSION+%}"
 +		fi
  
 -			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
 -				__git_ps1_show_upstream
 -			fi
 +		if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
 +			__git_ps1_show_upstream
  		fi
 +	fi
  
 -		local z="${GIT_PS1_STATESEPARATOR-" "}"
 +	local z="${GIT_PS1_STATESEPARATOR-" "}"
+ 
 -		# NO color option unless in PROMPT_COMMAND mode
 -		if [ $pcmode = yes ] && [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
 -			__git_ps1_colorize_gitstring
 -		fi
++	# NO color option unless in PROMPT_COMMAND mode
++	if [ $pcmode = yes ] && [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
++		__git_ps1_colorize_gitstring
++	fi
+ 
 -		local f="$w$i$s$u"
 -		local gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
 +	local f="$w$i$s$u"
++	local gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
+ 
 -		if [ $pcmode = yes ]; then
 +	if [ $pcmode = yes ]; then
- 		local gitstring=
- 		if [ -n "${GIT_PS1_SHOWCOLORHINTS-}" ]; then
- 			__git_ps1_colorize_gitstring
- 		else
- 			gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
- 		fi
 +		if [[ -n ${ZSH_VERSION-} ]]; then
  			gitstring=$(printf -- "$printf_format" "$gitstring")
 -			PS1="$ps1pc_start$gitstring$ps1pc_end"
  		else
 -			printf -- "$printf_format" "$gitstring"
 +			printf -v gitstring -- "$printf_format" "$gitstring"
  		fi
 +		PS1="$ps1pc_start$gitstring$ps1pc_end"
 +	else
- 		# NO color option unless in PROMPT_COMMAND mode
- 		printf -- "$printf_format" "$c${b##refs/heads/}${f:+$z$f}$r$p"
++		printf -- "$printf_format" "$gitstring"
  	fi
  }
diff --cc t/t9903-bash-prompt.sh
index c05458cbe6,5cd138ed9a..3c3e4e8c38
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@@ -10,10 -10,15 +10,14 @@@ test_description='test git-specific bas
  . "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
  
  actual="$TRASH_DIRECTORY/actual"
+ c_red='\\[\\e[31m\\]'
+ c_green='\\[\\e[32m\\]'
+ c_lblue='\\[\\e[1;34m\\]'
+ c_clear='\\[\\e[0m\\]'
  
  test_expect_success 'setup for prompt tests' '
 -	mkdir -p subdir/subsubdir &&
  	git init otherrepo &&
 -	echo 1 > file &&
 +	echo 1 >file &&
  	git add file &&
  	test_tick &&
  	git commit -m initial &&