Merge branch 'sg/bash-prompt'

* sg/bash-prompt:
  bash prompt: mention that PROMPT_COMMAND mode is faster
  bash prompt: avoid command substitution when finalizing gitstring
  bash prompt: avoid command substitution when checking for untracked files
  bash prompt: use bash builtins to check stash state
  bash prompt: use bash builtins to check for unborn branch for dirty state
  bash prompt: combine 'git rev-parse' for detached head
  bash prompt: combine 'git rev-parse' executions in the main code path
  bash prompt: use bash builtins to find out current branch
  bash prompt: use bash builtins to find out rebase state
  bash prompt: run 'git rev-parse --git-dir' directly instead of __gitdir()
  bash prompt: return early from __git_ps1() when not in a git repository
  bash prompt: print unique detached HEAD abbreviated object name
  bash prompt: add a test for symbolic link symbolic refs
  completion, bash prompt: move __gitdir() tests to completion test suite
  bash prompt: use 'write_script' helper in interactive rebase test
  bash prompt: fix redirection coding style in tests
This commit is contained in:
Junio C Hamano
2013-07-01 12:41:37 -07:00
4 changed files with 388 additions and 354 deletions

View File

@ -33,8 +33,6 @@ esac
# returns location of .git repo
__gitdir ()
{
# Note: this function is duplicated in git-prompt.sh
# When updating it, make sure you update the other one to match.
if [ -z "${1-}" ]; then
if [ -n "${__git_dir-}" ]; then
echo "$__git_dir"

View File

@ -15,11 +15,11 @@
# Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
# ZSH: PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
# the optional argument will be used as format string.
# 3b) Alternatively, if you are using bash, __git_ps1 can be
# used for PROMPT_COMMAND with two parameters, <pre> and
# <post>, 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 slighly faster prompt, if you are
# using bash, __git_ps1 can be used for PROMPT_COMMAND
# with two parameters, <pre> and <post>, 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" "\\\$ "'
# ZSH: precmd () { __git_ps1 "%n" ":%~$ " "|%s" }
# will show username, at-sign, host, colon, cwd, then
@ -80,30 +80,6 @@
# GIT_PS1_SHOWCOLORHINTS to a nonempty value. The colors are based on
# the colored output of "git status -sb".
# __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 ()
@ -335,21 +311,40 @@ __git_ps1 ()
;;
esac
local g="$(__gitdir)"
if [ -z "$g" ]; then
local repo_info rev_parse_exit_code
repo_info="$(git rev-parse --git-dir --is-inside-git-dir \
--is-bare-repository --is-inside-work-tree \
--short HEAD 2>/dev/null)"
rev_parse_exit_code="$?"
if [ -z "$repo_info" ]; then
if [ $pcmode = yes ]; then
#In PC mode PS1 always needs to be set
PS1="$ps1pc_start$ps1pc_end"
fi
else
return
fi
local short_sha
if [ "$rev_parse_exit_code" = "0" ]; then
short_sha="${repo_info##*$'\n'}"
repo_info="${repo_info%$'\n'*}"
fi
local inside_worktree="${repo_info##*$'\n'}"
repo_info="${repo_info%$'\n'*}"
local bare_repo="${repo_info##*$'\n'}"
repo_info="${repo_info%$'\n'*}"
local inside_gitdir="${repo_info##*$'\n'}"
local g="${repo_info%$'\n'*}"
local r=""
local b=""
local step=""
local total=""
if [ -d "$g/rebase-merge" ]; then
b="$(cat "$g/rebase-merge/head-name" 2>/dev/null)"
step=$(cat "$g/rebase-merge/msgnum" 2>/dev/null)
total=$(cat "$g/rebase-merge/end" 2>/dev/null)
read b 2>/dev/null <"$g/rebase-merge/head-name"
read step 2>/dev/null <"$g/rebase-merge/msgnum"
read total 2>/dev/null <"$g/rebase-merge/end"
if [ -f "$g/rebase-merge/interactive" ]; then
r="|REBASE-i"
else
@ -357,10 +352,10 @@ __git_ps1 ()
fi
else
if [ -d "$g/rebase-apply" ]; then
step=$(cat "$g/rebase-apply/next" 2>/dev/null)
total=$(cat "$g/rebase-apply/last" 2>/dev/null)
read step 2>/dev/null <"$g/rebase-apply/next"
read total 2>/dev/null <"$g/rebase-apply/last"
if [ -f "$g/rebase-apply/rebasing" ]; then
b="$(cat "$g/rebase-apply/head-name" 2>/dev/null)"
read b 2>/dev/null <"$g/rebase-apply/head-name"
r="|REBASE"
elif [ -f "$g/rebase-apply/applying" ]; then
r="|AM"
@ -377,8 +372,22 @@ __git_ps1 ()
r="|BISECTING"
fi
test -n "$b" ||
b="$(git symbolic-ref HEAD 2>/dev/null)" || {
if [ -n "$b" ]; then
:
elif [ -h "$g/HEAD" ]; then
# symlink symbolic ref
b="$(git symbolic-ref HEAD 2>/dev/null)"
else
local head=""
if ! read head 2>/dev/null <"$g/HEAD"; then
if [ $pcmode = yes ]; then
PS1="$ps1pc_start$ps1pc_end"
fi
return
fi
# is it a symbolic ref?
b="${head#ref: }"
if [ "$head" = "$b" ]; then
detached=yes
b="$(
case "${GIT_PS1_DESCRIBE_STYLE-}" in
@ -392,10 +401,10 @@ __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
@ -409,30 +418,31 @@ __git_ps1 ()
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
if [ "true" = "$inside_gitdir" ]; then
if [ "true" = "$bare_repo" ]; then
c="BARE:"
else
b="GIT_DIR!"
fi
elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
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 git rev-parse --quiet --verify HEAD >/dev/null; then
if [ -n "$short_sha" ]; 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="$"
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)" ]
git ls-files --others --exclude-standard --error-unmatch -- '*' >/dev/null 2>/dev/null
then
u="%${ZSH_VERSION+%}"
fi
@ -451,11 +461,14 @@ __git_ps1 ()
else
gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
fi
if [[ -n ${ZSH_VERSION-} ]]; then
gitstring=$(printf -- "$printf_format" "$gitstring")
else
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"
fi
fi
}

View File

@ -122,6 +122,140 @@ test_gitcomp_nl ()
invalid_variable_name='${foo.bar}'
actual="$TRASH_DIRECTORY/actual"
test_expect_success 'setup for __gitdir tests' '
mkdir -p subdir/subsubdir &&
git init otherrepo
'
test_expect_success '__gitdir - from command line (through $__git_dir)' '
echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
(
__git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
__gitdir >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success '__gitdir - repo as argument' '
echo "otherrepo/.git" >expected &&
__gitdir "otherrepo" >"$actual" &&
test_cmp expected "$actual"
'
test_expect_success '__gitdir - remote as argument' '
echo "remote" >expected &&
__gitdir "remote" >"$actual" &&
test_cmp expected "$actual"
'
test_expect_success '__gitdir - .git directory in cwd' '
echo ".git" >expected &&
__gitdir >"$actual" &&
test_cmp expected "$actual"
'
test_expect_success '__gitdir - .git directory in parent' '
echo "$(pwd -P)/.git" >expected &&
(
cd subdir/subsubdir &&
__gitdir >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success '__gitdir - cwd is a .git directory' '
echo "." >expected &&
(
cd .git &&
__gitdir >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success '__gitdir - parent is a .git directory' '
echo "$(pwd -P)/.git" >expected &&
(
cd .git/refs/heads &&
__gitdir >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success '__gitdir - $GIT_DIR set while .git directory in cwd' '
echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
(
GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
export GIT_DIR &&
__gitdir >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '
echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
(
GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
export GIT_DIR &&
cd subdir &&
__gitdir >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success '__gitdir - non-existing $GIT_DIR' '
(
GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
export GIT_DIR &&
test_must_fail __gitdir
)
'
test_expect_success '__gitdir - gitfile in cwd' '
echo "$(pwd -P)/otherrepo/.git" >expected &&
echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" >subdir/.git &&
test_when_finished "rm -f subdir/.git" &&
(
cd subdir &&
__gitdir >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success '__gitdir - gitfile in parent' '
echo "$(pwd -P)/otherrepo/.git" >expected &&
echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" >subdir/.git &&
test_when_finished "rm -f subdir/.git" &&
(
cd subdir/subsubdir &&
__gitdir >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' '
echo "$(pwd -P)/otherrepo/.git" >expected &&
mkdir otherrepo/dir &&
test_when_finished "rm -rf otherrepo/dir" &&
ln -s otherrepo/dir link &&
test_when_finished "rm -f link" &&
(
cd link &&
__gitdir >"$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success '__gitdir - not a git repository' '
(
cd subdir/subsubdir &&
GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
export GIT_CEILING_DIRECTORIES &&
test_must_fail __gitdir
)
'
test_expect_success '__gitcomp - trailing space - options' '
test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
--reset-author" <<-EOF

View File

@ -12,7 +12,6 @@ test_description='test git-specific bash prompt functions'
actual="$TRASH_DIRECTORY/actual"
test_expect_success 'setup for prompt tests' '
mkdir -p subdir/subsubdir &&
git init otherrepo &&
echo 1 >file &&
git add file &&
@ -35,141 +34,32 @@ test_expect_success 'setup for prompt tests' '
git checkout master
'
test_expect_success 'gitdir - from command line (through $__git_dir)' '
echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
(
__git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
__gitdir > "$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'gitdir - repo as argument' '
echo "otherrepo/.git" > expected &&
__gitdir "otherrepo" > "$actual" &&
test_cmp expected "$actual"
'
test_expect_success 'gitdir - remote as argument' '
echo "remote" > expected &&
__gitdir "remote" > "$actual" &&
test_cmp expected "$actual"
'
test_expect_success 'gitdir - .git directory in cwd' '
echo ".git" > expected &&
__gitdir > "$actual" &&
test_cmp expected "$actual"
'
test_expect_success 'gitdir - .git directory in parent' '
echo "$(pwd -P)/.git" > expected &&
(
cd subdir/subsubdir &&
__gitdir > "$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'gitdir - cwd is a .git directory' '
echo "." > expected &&
(
cd .git &&
__gitdir > "$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'gitdir - parent is a .git directory' '
echo "$(pwd -P)/.git" > expected &&
(
cd .git/refs/heads &&
__gitdir > "$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'gitdir - $GIT_DIR set while .git directory in cwd' '
echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
(
GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
export GIT_DIR &&
__gitdir > "$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'gitdir - $GIT_DIR set while .git directory in parent' '
echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
(
GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
export GIT_DIR &&
cd subdir &&
__gitdir > "$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'gitdir - non-existing $GIT_DIR' '
(
GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
export GIT_DIR &&
test_must_fail __gitdir
)
'
test_expect_success 'gitdir - gitfile in cwd' '
echo "$(pwd -P)/otherrepo/.git" > expected &&
echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&
test_when_finished "rm -f subdir/.git" &&
(
cd subdir &&
__gitdir > "$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'gitdir - gitfile in parent' '
echo "$(pwd -P)/otherrepo/.git" > expected &&
echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&
test_when_finished "rm -f subdir/.git" &&
(
cd subdir/subsubdir &&
__gitdir > "$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success SYMLINKS 'gitdir - resulting path avoids symlinks' '
echo "$(pwd -P)/otherrepo/.git" > expected &&
mkdir otherrepo/dir &&
test_when_finished "rm -rf otherrepo/dir" &&
ln -s otherrepo/dir link &&
test_when_finished "rm -f link" &&
(
cd link &&
__gitdir > "$actual"
) &&
test_cmp expected "$actual"
'
test_expect_success 'gitdir - not a git repository' '
(
cd subdir/subsubdir &&
GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
export GIT_CEILING_DIRECTORIES &&
test_must_fail __gitdir
)
'
test_expect_success 'prompt - branch name' '
printf " (master)" >expected &&
__git_ps1 >"$actual" &&
test_cmp expected "$actual"
'
test_expect_success SYMLINKS 'prompt - branch name - symlink symref' '
printf " (master)" >expected &&
test_when_finished "git checkout master" &&
test_config core.preferSymlinkRefs true &&
git checkout master &&
__git_ps1 >"$actual" &&
test_cmp expected "$actual"
'
test_expect_success 'prompt - unborn branch' '
printf " (unborn)" >expected &&
git checkout --orphan unborn &&
test_when_finished "git checkout master" &&
__git_ps1 >"$actual" &&
test_cmp expected "$actual"
'
test_expect_success 'prompt - detached head' '
printf " ((%s...))" $(git log -1 --format="%h" b1^) > expected &&
printf " ((%s...))" $(git log -1 --format="%h" --abbrev=13 b1^) >expected &&
test_config core.abbrev 13 &&
git checkout b1^ &&
test_when_finished "git checkout master" &&
__git_ps1 >"$actual" &&
@ -248,14 +138,12 @@ test_expect_success 'prompt - inside bare repository' '
test_expect_success 'prompt - interactive rebase' '
printf " (b1|REBASE-i 2/3)" >expected
echo "#!$SHELL_PATH" >fake_editor.sh &&
cat >>fake_editor.sh <<\EOF &&
write_script fake_editor.sh <<-\EOF &&
echo "exec echo" >"$1"
echo "edit $(git log -1 --format="%h")" >>"$1"
echo "exec echo" >>"$1"
EOF
test_when_finished "rm -f fake_editor.sh" &&
chmod a+x fake_editor.sh &&
test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
git checkout b1 &&
test_when_finished "git checkout master" &&
@ -440,6 +328,7 @@ test_expect_success 'prompt - stash status indicator - stash' '
echo 2 >file &&
git stash &&
test_when_finished "git stash drop" &&
git pack-refs --all &&
(
GIT_PS1_SHOWSTASHSTATE=y &&
__git_ps1 >"$actual"