From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
Hi,
displaying the git-specific bash prompt on Windows/MinGW takes quite
long, long enough to be noticeable. This is mainly caused by the
numerous fork()s and exec()s to create subshells and run git or other
commands, which are rather expensive on Windows.
This patch series eliminates many command substitutions and command
executions in __git_ps1() from top to bottom by replacing them with
bash builtins or consolidating them. A few timing results are shown
in the log message of the last patch.
Changes since v2 [1]:
- The detached HEAD abbreviated object name is now unique and
respects core.abbrev; see patches 5 and 11, replacing v2's patch 9.
(This is why I asked the detached HEAD before root commit thing
yesterday.)
- Patches 12 and 16 are new.
- Incorporated Peff's suggestion about using the 'write_script'
helper into patch 2.
- Incorporated Eric's typofix.
- Rephrased a few commit messages.
It applies on top of current master; 2847cae8 (prompt: squelch error
output from cat, 2013-06-14) graduated recently.
This patch series will conflict with Eduardo's work on refactoring the
colorizing function, and the conflict is not trivial. Although there
are still some open questions left with that series (using tput, zsh
tests), those won't affect the conflicts between the two patch series.
So, for the convenience of our maintainer, I picked up Eduardo's
series, took the liberty to apply a fixup commit on top with my
suggestions from [2], merged the two series, and published the result
at:
https://github.com/szeder/git.git bash-prompt-speedup-and-color-refactorization
Eduardo, could you please also check that my conflict resolution is
correct? Thanks.
Best,
Gábor
[1] - http://thread.gmane.org/gmane.comp.version-control.git/228132
[2] - http://article.gmane.org/gmane.comp.version-control.git/228707
SZEDER Gábor (16):
bash prompt: fix redirection coding style in tests
bash prompt: use 'write_script' helper in interactive rebase test
completion, bash prompt: move __gitdir() tests to completion test
suite
bash prompt: add a test for symbolic link symbolic refs
bash prompt: print unique detached HEAD abbreviated object name
bash prompt: return early from __git_ps1() when not in a git
repository
bash prompt: run 'git rev-parse --git-dir' directly instead of
__gitdir()
bash prompt: use bash builtins to find out rebase state
bash prompt: use bash builtins to find out current branch
bash prompt: combine 'git rev-parse' executions in the main code path
bash prompt: combine 'git rev-parse' for detached head
bash prompt: use bash builtins to check for unborn branch for dirty
state
bash prompt: use bash builtins to check stash state
bash prompt: avoid command substitution when checking for untracked
files
bash prompt: avoid command substitution when finalizing gitstring
bash prompt: mention that PROMPT_COMMAND mode is faster
contrib/completion/git-completion.bash | 2 -
contrib/completion/git-prompt.sh | 241 ++++++++++++------------
t/t9902-completion.sh | 134 ++++++++++++++
t/t9903-bash-prompt.sh | 323 +++++++++++----------------------
4 files changed, 367 insertions(+), 333 deletions(-)
--
1.8.3.1.599.g4459181
From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
Use '>file' instead of '> file', in accordance with the coding
guidelines.
Signed-off-by: SZEDER Gábor <redacted>
---
t/t9903-bash-prompt.sh | 232 ++++++++++++++++++++++++-------------------------
1 file changed, 116 insertions(+), 116 deletions(-)
@@ -14,98 +14,98 @@ actual="$TRASH_DIRECTORY/actual" test_expect_success'setup for prompt tests''mkdir-psubdir/subsubdir&&gitinitotherrepo&&-echo1>file&&+echo1>file&&gitaddfile&&test_tick&&gitcommit-minitial&&gittag-a-mmsg1t1&&gitcheckout-bb1&&-echo2>file&&+echo2>file&&gitcommit-m"second b1"file&&-echo3>file&&+echo3>file&&gitcommit-m"third b1"file&&gittag-a-mmsg2t2&&gitcheckout-bb2master&&-echo0>file&&+echo0>file&&gitcommit-m"second b2"file&&-echo00>file&&+echo00>file&&gitcommit-m"another b2"file&&-echo000>file&&+echo000>file&&gitcommit-m"yet another b2"file&&gitcheckoutmaster' test_expect_success'gitdir - from command line (through $__git_dir)''-echo"$TRASH_DIRECTORY/otherrepo/.git">expected&&+echo"$TRASH_DIRECTORY/otherrepo/.git">expected&&(__git_dir="$TRASH_DIRECTORY/otherrepo/.git"&&-__gitdir>"$actual"+__gitdir>"$actual")&&test_cmpexpected"$actual"' test_expect_success'gitdir - repo as argument''-echo"otherrepo/.git">expected&&-__gitdir"otherrepo">"$actual"&&+echo"otherrepo/.git">expected&&+__gitdir"otherrepo">"$actual"&&test_cmpexpected"$actual"' test_expect_success'gitdir - remote as argument''-echo"remote">expected&&-__gitdir"remote">"$actual"&&+echo"remote">expected&&+__gitdir"remote">"$actual"&&test_cmpexpected"$actual"' test_expect_success'gitdir - .git directory in cwd''-echo".git">expected&&-__gitdir>"$actual"&&+echo".git">expected&&+__gitdir>"$actual"&&test_cmpexpected"$actual"' test_expect_success'gitdir - .git directory in parent''-echo"$(pwd-P)/.git">expected&&+echo"$(pwd-P)/.git">expected&&(cdsubdir/subsubdir&&-__gitdir>"$actual"+__gitdir>"$actual")&&test_cmpexpected"$actual"' test_expect_success'gitdir - cwd is a .git directory''-echo".">expected&&+echo".">expected&&(cd.git&&-__gitdir>"$actual"+__gitdir>"$actual")&&test_cmpexpected"$actual"' test_expect_success'gitdir - parent is a .git directory''-echo"$(pwd-P)/.git">expected&&+echo"$(pwd-P)/.git">expected&&(cd.git/refs/heads&&-__gitdir>"$actual"+__gitdir>"$actual")&&test_cmpexpected"$actual"' test_expect_success'gitdir - $GIT_DIR set while .git directory in cwd''-echo"$TRASH_DIRECTORY/otherrepo/.git">expected&&+echo"$TRASH_DIRECTORY/otherrepo/.git">expected&&(GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git"&&exportGIT_DIR&&-__gitdir>"$actual"+__gitdir>"$actual")&&test_cmpexpected"$actual"' test_expect_success'gitdir - $GIT_DIR set while .git directory in parent''-echo"$TRASH_DIRECTORY/otherrepo/.git">expected&&+echo"$TRASH_DIRECTORY/otherrepo/.git">expected&&(GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git"&&exportGIT_DIR&&cdsubdir&&-__gitdir>"$actual"+__gitdir>"$actual")&&test_cmpexpected"$actual"'
From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
Currently __gitdir() is duplicated in the git completion and prompt
scripts, while its tests are in the prompt test suite. This patch
series is about to change __git_ps1() in a way that it won't need
__gitdir() anymore and __gitdir() will be removed from the prompt
script.
So move all __gitdir() tests from the prompt test suite over to the
completion test suite. Update the setup tests so that they perform
only those steps that are necessary for each test suite.
Signed-off-by: SZEDER Gábor <redacted>
---
t/t9902-completion.sh | 134 +++++++++++++++++++++++++++++++++++++++++++++++++
t/t9903-bash-prompt.sh | 128 ----------------------------------------------
2 files changed, 134 insertions(+), 128 deletions(-)
@@ -122,6 +122,140 @@ test_gitcomp_nl ()invalid_variable_name='${foo.bar}'+actual="$TRASH_DIRECTORY/actual"++test_expect_success'setup for __gitdir tests''+mkdir-psubdir/subsubdir&&+gitinitotherrepo+'++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_cmpexpected"$actual"+'++test_expect_success'__gitdir - repo as argument''+echo"otherrepo/.git">expected&&+__gitdir"otherrepo">"$actual"&&+test_cmpexpected"$actual"+'++test_expect_success'__gitdir - remote as argument''+echo"remote">expected&&+__gitdir"remote">"$actual"&&+test_cmpexpected"$actual"+'++test_expect_success'__gitdir - .git directory in cwd''+echo".git">expected&&+__gitdir>"$actual"&&+test_cmpexpected"$actual"+'++test_expect_success'__gitdir - .git directory in parent''+echo"$(pwd-P)/.git">expected&&+(+cdsubdir/subsubdir&&+__gitdir>"$actual"+)&&+test_cmpexpected"$actual"+'++test_expect_success'__gitdir - cwd is a .git directory''+echo".">expected&&+(+cd.git&&+__gitdir>"$actual"+)&&+test_cmpexpected"$actual"+'++test_expect_success'__gitdir - parent is a .git directory''+echo"$(pwd-P)/.git">expected&&+(+cd.git/refs/heads&&+__gitdir>"$actual"+)&&+test_cmpexpected"$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"&&+exportGIT_DIR&&+__gitdir>"$actual"+)&&+test_cmpexpected"$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"&&+exportGIT_DIR&&+cdsubdir&&+__gitdir>"$actual"+)&&+test_cmpexpected"$actual"+'++test_expect_success'__gitdir - non-existing $GIT_DIR''+(+GIT_DIR="$TRASH_DIRECTORY/non-existing"&&+exportGIT_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"&&+(+cdsubdir&&+__gitdir>"$actual"+)&&+test_cmpexpected"$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"&&+(+cdsubdir/subsubdir&&+__gitdir>"$actual"+)&&+test_cmpexpected"$actual"+'++test_expect_successSYMLINKS'__gitdir - resulting path avoids symlinks''+echo"$(pwd-P)/otherrepo/.git">expected&&+mkdirotherrepo/dir&&+test_when_finished"rm -rf otherrepo/dir"&&+ln-sotherrepo/dirlink&&+test_when_finished"rm -f link"&&+(+cdlink&&+__gitdir>"$actual"+)&&+test_cmpexpected"$actual"+'++test_expect_success'__gitdir - not a git repository''+(+cdsubdir/subsubdir&&+GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY"&&+exportGIT_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
@@ -35,133 +34,6 @@ test_expect_success 'setup for prompt tests' 'gitcheckoutmaster'-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_cmpexpected"$actual"-'--test_expect_success'gitdir - repo as argument''-echo"otherrepo/.git">expected&&-__gitdir"otherrepo">"$actual"&&-test_cmpexpected"$actual"-'--test_expect_success'gitdir - remote as argument''-echo"remote">expected&&-__gitdir"remote">"$actual"&&-test_cmpexpected"$actual"-'--test_expect_success'gitdir - .git directory in cwd''-echo".git">expected&&-__gitdir>"$actual"&&-test_cmpexpected"$actual"-'--test_expect_success'gitdir - .git directory in parent''-echo"$(pwd-P)/.git">expected&&-(-cdsubdir/subsubdir&&-__gitdir>"$actual"-)&&-test_cmpexpected"$actual"-'--test_expect_success'gitdir - cwd is a .git directory''-echo".">expected&&-(-cd.git&&-__gitdir>"$actual"-)&&-test_cmpexpected"$actual"-'--test_expect_success'gitdir - parent is a .git directory''-echo"$(pwd-P)/.git">expected&&-(-cd.git/refs/heads&&-__gitdir>"$actual"-)&&-test_cmpexpected"$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"&&-exportGIT_DIR&&-__gitdir>"$actual"-)&&-test_cmpexpected"$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"&&-exportGIT_DIR&&-cdsubdir&&-__gitdir>"$actual"-)&&-test_cmpexpected"$actual"-'--test_expect_success'gitdir - non-existing $GIT_DIR''-(-GIT_DIR="$TRASH_DIRECTORY/non-existing"&&-exportGIT_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"&&-(-cdsubdir&&-__gitdir>"$actual"-)&&-test_cmpexpected"$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"&&-(-cdsubdir/subsubdir&&-__gitdir>"$actual"-)&&-test_cmpexpected"$actual"-'--test_expect_successSYMLINKS'gitdir - resulting path avoids symlinks''-echo"$(pwd-P)/otherrepo/.git">expected&&-mkdirotherrepo/dir&&-test_when_finished"rm -rf otherrepo/dir"&&-ln-sotherrepo/dirlink&&-test_when_finished"rm -f link"&&-(-cdlink&&-__gitdir>"$actual"-)&&-test_cmpexpected"$actual"-'--test_expect_success'gitdir - not a git repository''-(-cdsubdir/subsubdir&&-GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY"&&-exportGIT_CEILING_DIRECTORIES&&-test_must_fail__gitdir-)-'- test_expect_success'prompt - branch name''printf" (master)">expected&&__git_ps1>"$actual"&&
From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
When describing a detached HEAD according to the $GIT_PS1_DESCRIBE
environment variable fails, __git_ps1() runs 'cut -c1-7 .git/HEAD' to
show the 7 hexdigits abbreviated commit object name in the prompt.
Obviously, this neither respects core.abbrev nor produces a unique
object name.
Fix this by using 'git rev-parse --short HEAD' instead and adjust the
corresponding test to use non-standard number of hexdigits.
Signed-off-by: SZEDER Gábor <redacted>
---
contrib/completion/git-prompt.sh | 2 +-
t/t9903-bash-prompt.sh | 3 ++-
2 files changed, 3 insertions(+), 2 deletions(-)
From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
... to gain one level of indentation for the bulk of the function.
(The patch looks quite unreadable, you'd better check it with 'git
diff -w'.)
Signed-off-by: SZEDER Gábor <redacted>
---
contrib/completion/git-prompt.sh | 201 ++++++++++++++++++++-------------------
1 file changed, 101 insertions(+), 100 deletions(-)
@@ -341,121 +341,122 @@ __git_ps1 ()#In PC mode PS1 always needs to be setPS1="$ps1pc_start$ps1pc_end"fi+return+fi++localr=""+localb=""+localstep=""+localtotal=""+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)+if[-f"$g/rebase-merge/interactive"];then+r="|REBASE-i"+else+r="|REBASE-m"+fielse-localr=""-localb=""-localstep=""-localtotal=""-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)-if[-f"$g/rebase-merge/interactive"];then-r="|REBASE-i"+if[-d"$g/rebase-apply"];then+step=$(cat"$g/rebase-apply/next"2>/dev/null)+total=$(cat"$g/rebase-apply/last"2>/dev/null)+if[-f"$g/rebase-apply/rebasing"];then+b="$(cat"$g/rebase-apply/head-name"2>/dev/null)"+r="|REBASE"+elif[-f"$g/rebase-apply/applying"];then+r="|AM"else-r="|REBASE-m"-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)-if[-f"$g/rebase-apply/rebasing"];then-b="$(cat"$g/rebase-apply/head-name"2>/dev/null)"-r="|REBASE"-elif[-f"$g/rebase-apply/applying"];then-r="|AM"-else-r="|AM/REBASE"-fi-elif[-f"$g/MERGE_HEAD"];then-r="|MERGING"-elif[-f"$g/CHERRY_PICK_HEAD"];then-r="|CHERRY-PICKING"-elif[-f"$g/REVERT_HEAD"];then-r="|REVERTING"-elif[-f"$g/BISECT_LOG"];then-r="|BISECTING"+r="|AM/REBASE"fi+elif[-f"$g/MERGE_HEAD"];then+r="|MERGING"+elif[-f"$g/CHERRY_PICK_HEAD"];then+r="|CHERRY-PICKING"+elif[-f"$g/REVERT_HEAD"];then+r="|REVERTING"+elif[-f"$g/BISECT_LOG"];then+r="|BISECTING"+fi-test-n"$b"||-b="$(gitsymbolic-refHEAD2>/dev/null)"||{-detached=yes-b="$(-case"${GIT_PS1_DESCRIBE_STYLE-}"in-(contains)-gitdescribe--containsHEAD;;-(branch)-gitdescribe--contains--allHEAD;;-(describe)-gitdescribeHEAD;;-(*|default)-gitdescribe--tags--exact-matchHEAD;;-esac2>/dev/null)" ||+test-n"$b"||+b="$(gitsymbolic-refHEAD2>/dev/null)"||{+detached=yes+b="$(+case"${GIT_PS1_DESCRIBE_STYLE-}"in+(contains)+gitdescribe--containsHEAD;;+(branch)+gitdescribe--contains--allHEAD;;+(describe)+gitdescribeHEAD;;+(*|default)+gitdescribe--tags--exact-matchHEAD;;+esac2>/dev/null)" ||-b="$(gitrev-parse--shortHEAD2>/dev/null)..."||-b="unknown"-b="($b)"-}-fi+b="$(gitrev-parse--shortHEAD2>/dev/null)..."||+b="unknown"+b="($b)"+}+fi-if[-n"$step"]&&[-n"$total"];then-r="$r$step/$total"-fi+if[-n"$step"]&&[-n"$total"];then+r="$r$step/$total"+fi-localw=""-locali=""-locals=""-localu=""-localc=""-localp=""+localw=""+locali=""+locals=""+localu=""+localc=""+localp=""-if["true"="$(gitrev-parse--is-inside-git-dir2>/dev/null)"];then-if["true"="$(gitrev-parse--is-bare-repository2>/dev/null)"];then-c="BARE:"+if["true"="$(gitrev-parse--is-inside-git-dir2>/dev/null)"];then+if["true"="$(gitrev-parse--is-bare-repository2>/dev/null)"];then+c="BARE:"+else+b="GIT_DIR!"+fi+elif["true"="$(gitrev-parse--is-inside-work-tree2>/dev/null)"];then+if[-n"${GIT_PS1_SHOWDIRTYSTATE-}"]&&+["$(gitconfig--boolbash.showDirtyState)"!="false"]+then+gitdiff--no-ext-diff--quiet--exit-code||w="*"+ifgitrev-parse--quiet--verifyHEAD>/dev/null;then+gitdiff-index--cached--quietHEAD--||i="+"else-b="GIT_DIR!"-fi-elif["true"="$(gitrev-parse--is-inside-work-tree2>/dev/null)"];then-if[-n"${GIT_PS1_SHOWDIRTYSTATE-}"]&&-["$(gitconfig--boolbash.showDirtyState)"!="false"]-then-gitdiff--no-ext-diff--quiet--exit-code||w="*"-ifgitrev-parse--quiet--verifyHEAD>/dev/null;then-gitdiff-index--cached--quietHEAD--||i="+"-else-i="#"-fi-fi-if[-n"${GIT_PS1_SHOWSTASHSTATE-}"];then-gitrev-parse--verifyrefs/stash>/dev/null2>&1&&s="$"+i="#"fi+fi+if[-n"${GIT_PS1_SHOWSTASHSTATE-}"];then+gitrev-parse--verifyrefs/stash>/dev/null2>&1&&s="$"+fi-if[-n"${GIT_PS1_SHOWUNTRACKEDFILES-}"]&&-["$(gitconfig--boolbash.showUntrackedFiles)"!="false"]&&-[-n"$(gitls-files--others--exclude-standard)"]-then-u="%${ZSH_VERSION+%}"-fi+if[-n"${GIT_PS1_SHOWUNTRACKEDFILES-}"]&&+["$(gitconfig--boolbash.showUntrackedFiles)"!="false"]&&+[-n"$(gitls-files--others--exclude-standard)"]+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_upstreamfi+fi-localz="${GIT_PS1_STATESEPARATOR-" "}"-localf="$w$i$s$u"-if[$pcmode=yes];then-localgitstring=-if[-n"${GIT_PS1_SHOWCOLORHINTS-}"];then-__git_ps1_colorize_gitstring-else-gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"-fi-gitstring=$(printf--"$printf_format""$gitstring")-PS1="$ps1pc_start$gitstring$ps1pc_end"+localz="${GIT_PS1_STATESEPARATOR-" "}"+localf="$w$i$s$u"+if[$pcmode=yes];then+localgitstring=+if[-n"${GIT_PS1_SHOWCOLORHINTS-}"];then+__git_ps1_colorize_gitstringelse-# NO color option unless in PROMPT_COMMAND mode-printf--"$printf_format""$c${b##refs/heads/}${f:+$z$f}$r$p"+gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"fi+gitstring=$(printf--"$printf_format""$gitstring")+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}
From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
__git_ps1() finds out the path to the repository by using the
__gitdir() helper function. __gitdir() is basically just a wrapper
around 'git rev-parse --git-dir', extended with support for
recognizing a remote repository given as argument, to use the path
given on the command line, and with a few shortcuts to recognize a git
repository in cwd or at $GIT_DIR quickly without actually running 'git
rev-parse'. However, the former two is only necessary for the
completion script but makes no sense for the bash prompt, while the
latter shortcuts are performance optimizations __git_ps1() can do
without (they just avoid the overhead of fork()+exec()ing a git
process).
Run 'git rev-parse --git-dir' directly in __git_ps1(), because it will
allow this patch series to combine several $(git rev-parse ...)
command substitutions in the main code path, and the overall
performance benefit will far outweigh the loss of those few shortcuts
in __gitdir(). Furthermore, since __gitdir() is not needed anymore
for the prompt, remove it from the prompt script finally eliminating
its duplication between the prompt and completion scripts. Also
remove the comment from the completion script warning about this code
duplication.
Signed-off-by: SZEDER Gábor <redacted>
---
contrib/completion/git-completion.bash | 2 --
contrib/completion/git-prompt.sh | 26 +-------------------------
2 files changed, 1 insertion(+), 27 deletions(-)
@@ -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-}"];thenif[-n"${__git_dir-}"];thenecho"$__git_dir"
@@ -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-}"||return1-echo"$GIT_DIR"-elif[-d.git];then-echo.git-else-gitrev-parse--git-dir2>/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,7 +311,7 @@ __git_ps1 ();;esac-localg="$(__gitdir)"+localg="$(gitrev-parse--git-dir2>/dev/null)"if[-z"$g"];thenif[$pcmode=yes];then#In PC mode PS1 always needs to be set
From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
__git_ps1() runs the '$(git symbolic-ref HEAD)' command substitution
to find out whether we are on a branch and to find out the name of
that branch. This imposes the overhead of fork()ing a subshell and
fork()+exec()ing a git process.
Since HEAD is in most cases a single-line file and the symbolic ref
format is quite simple to recognize and parse, read and parse it using
only bash builtins, thereby sparing all that fork()+exec() overhead.
Don't display the git prompt if reading HEAD fails, because a readable
HEAD is required for a git repository. HEAD can also be a symlink
symbolic ref (due to 'core.preferSymlinkRefs'), so use bash builtins
for reading HEAD only when HEAD is not a symlink.
Signed-off-by: SZEDER Gábor <redacted>
---
contrib/completion/git-prompt.sh | 51 ++++++++++++++++++++++++++--------------
1 file changed, 33 insertions(+), 18 deletions(-)
From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
During an ongoing interactive rebase __git_ps1() finds out the name of
the rebased branch, the total number of patches and the number of the
current patch by executing a '$(cat .git/rebase-merge/<FILE>)' command
substitution for each. That is not quite the most efficient way to
read single line single word files, because it imposes the overhead of
fork()ing a subshell and fork()+exec()ing 'cat' several times.
Use the 'read' bash builtin instead to avoid those overheads.
Signed-off-by: SZEDER Gábor <redacted>
---
contrib/completion/git-prompt.sh | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
There are a couple of '$(git rev-parse --<opt>)' command substitutions
in __git_ps1() and three of them are executed in the main code path:
- the first to get the path to the .git directory ('--git-dir'),
- the second to check whether we're inside the .git directory
('--is-inside-git-dir'),
- and the last, depending on the results of the second, either
* to check whether it's a bare repo ('--is-bare-repository'), or
* to check whether inside a work tree ('--is-inside-work-tree').
Naturally, this imposes the overhead of fork()ing three subshells and
fork()+exec()ing three git commands.
Combine these four 'git rev-parse' queries into a single one and use
bash parameter expansions to parse the combined output, i.e. to
separate the path to the .git directory from the true/false of
'--is-inside-git-dir', etc. This way we can eliminate two of the
three subshells and git commands.
Signed-off-by: SZEDER Gábor <redacted>
---
contrib/completion/git-prompt.sh | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)
@@ -311,8 +311,9 @@ __git_ps1 ();;esac-localg="$(gitrev-parse--git-dir2>/dev/null)"-if[-z"$g"];then+localrepo_info="$(gitrev-parse--git-dir--is-inside-git-dir\+--is-bare-repository--is-inside-work-tree2>/dev/null)"+if[-z"$repo_info"];thenif[$pcmode=yes];then#In PC mode PS1 always needs to be setPS1="$ps1pc_start$ps1pc_end"
From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
When describing a detached HEAD according to the $GIT_PS1_DESCRIBE
environment variable fails, __git_ps1() now runs the '$(git rev-parse
--short HEAD)' command substitution to get the abbreviated detached
HEAD commit object name. This imposes the overhead of fork()ing a
subshell and fork()+exec()ing a git process.
Avoid this overhead by combining this command substitution with the
"main" 'git rev-parse' execution for getting the path to the .git
directory & co. This means that we'll look for the abbreviated commit
object name even when it's not necessary, because we're on a branch or
the detached HEAD can be described. It doesn't matter, however,
because once 'git rev-parse' is up and running to fulfill all those
other queries, the additional overhead of looking for the abbreviated
commit object name is not measurable because it's lost in the noise.
There is a caveat, however, when we are on an unborn branch, because
in that case HEAD doesn't point to a valid commit, hence the query for
the abbreviated commit object name fails. Therefore, '--short HEAD'
must be the last options to 'git rev-parse' in order to get all the
other necessary information for the prompt even on an unborn branch.
Furthermore, in that case, and in that case only, 'git rev-parse'
doesn't output the last line containing the abbreviated commit object
name, obviously, so we have to take care to only parse it if 'git
rev-parse' exited without any error.
Although there are tests already excercising __git_ps1() on unborn
branches, they all do so implicitly. Add a test that checks this
explicitly.
Signed-off-by: SZEDER Gábor <redacted>
---
contrib/completion/git-prompt.sh | 16 ++++++++++++----
t/t9903-bash-prompt.sh | 8 ++++++++
2 files changed, 20 insertions(+), 4 deletions(-)
@@ -311,8 +311,12 @@ __git_ps1 ();;esac-localrepo_info="$(gitrev-parse--git-dir--is-inside-git-dir\---is-bare-repository--is-inside-work-tree2>/dev/null)"+localrepo_inforev_parse_exit_code+repo_info="$(gitrev-parse--git-dir--is-inside-git-dir\+--is-bare-repository--is-inside-work-tree\+--shortHEAD2>/dev/null)"+rev_parse_exit_code="$?"+if[-z"$repo_info"];thenif[$pcmode=yes];then#In PC mode PS1 always needs to be set
From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
When the dirty work tree and index status indicator is enabled,
__git_ps1() checks for changes in the index by running 'git diff-index
--cached --quiet HEAD --' and looking at its exit code. However, that
makes sense only when HEAD points to a valid commit: on an unborn
branch the failure of said command would be caused by the invalid
HEAD, not by changes in the index. Therefore, __git_ps1() first
checks for a valid HEAD by running 'git rev-parse --quiet --verify
HEAD'.
Since the previous patch we implicitly check HEAD's validity by
running 'git rev-parse ... --short HEAD', making the dirty status
indicator's 'git rev-parse' check redundant. It's sufficient to check
for non-emptyness of the variable holding the abbreviated commit
object name, thereby sparing the overhead of fork()+exec()ing a git
process.
Signed-off-by: SZEDER Gábor <redacted>
---
contrib/completion/git-prompt.sh | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
When the environment variable $GIT_PS1_SHOWSTASHSTATE is set
__git_ps1() checks the presence of stashes by running 'git rev-parse
--verify refs/stash'. This command not only checks that the
'refs/stash' ref exists but also, well, verifies that it's a valid
ref.
However, we don't need to be that thorough for the bash prompt. We
can omit that verification and only check whether 'refs/stash' exists
or not. Since 'git pack-refs' never packs 'refs/stash', it's a matter
of checking the existence of a ref file. Perform this check using
only bash builtins to spare the overhead of fork()+exec()ing a git
process.
Also run 'git pack-refs --all' in the corresponding test to document
that the prompt script depends on 'git pack-refs' not packing
'refs/stash' and to catch possible breakages should this behavior ever
change.
Signed-off-by: SZEDER Gábor <redacted>
---
contrib/completion/git-prompt.sh | 5 +++--
t/t9903-bash-prompt.sh | 1 +
2 files changed, 4 insertions(+), 2 deletions(-)
From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
When enabled, the bash prompt can indicate the presence of untracked
files with a '%' sign. __git_ps1() checks for untracked files by running the
'$(git ls-files --others --exclude-standard)' command substitution,
and displays the indicator when there is no output.
Avoid this command substitution by additionally passing
'--error-unmatch *', and checking the command's return value.
Signed-off-by: SZEDER Gábor <redacted>
---
contrib/completion/git-prompt.sh | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
Before setting $PS1, __git_ps1() uses a command substitution to
redirect the output from a printf into a variable. Spare the overhead
of fork()ing a subshell by using 'printf -v <var>' to directly assign
the output to that variable.
zsh's printf doesn't support the '-v <var>' option, so stick with the
command substitution when under zsh.
Signed-off-by: SZEDER Gábor <redacted>
---
contrib/completion/git-prompt.sh | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
@@ -461,7 +461,11 @@ __git_ps1 ()elsegitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"fi-gitstring=$(printf--"$printf_format""$gitstring")+if[[-n${ZSH_VERSION-}]];then+gitstring=$(printf--"$printf_format""$gitstring")+else+printf-vgitstring--"$printf_format""$gitstring"+fiPS1="$ps1pc_start$gitstring$ps1pc_end"else# NO color option unless in PROMPT_COMMAND mode
From: SZEDER Gábor <hidden> Date: 2016-06-15 22:57:54
__git_ps1() is usually added to the prompt inside a command
substitution, imposing the overhead of fork()ing a subshell. Using
__git_ps1() for $PROMPT_COMMAND is slightly faster, because it avoids
that command substitution.
Mention this in the comments about setting up the git prompt.
The whole series speeds up the bash prompt on Windows/MSysGit
considerably. Here are some timing results in three scenarios, each
repeated 10 times:
At the top of the work tree, before:
$ time for i in {0..9} ; do prompt="$(__git_ps1)" ; done
real 0m1.716s
user 0m0.301s
sys 0m0.772s
After:
real 0m0.687s
user 0m0.075s
sys 0m0.396s
After, from $PROMPT_COMMAND:
$ time for i in {0..9} ; do __git_ps1 '\h:\w' '$ ' ; done
real 0m0.546s
user 0m0.075s
sys 0m0.181s
At the top of the work tree, detached head, before:
real 0m2.574s
user 0m0.376s
sys 0m1.207s
After:
real 0m1.139s
user 0m0.151s
sys 0m0.500s
After, from $PROMPT_COMMAND:
real 0m1.030s
user 0m0.245s
sys 0m0.336s
In a subdirectory, during rebase, stash status indicator enabled,
before:
real 0m3.557s
user 0m0.495s
sys 0m1.767s
After:
real 0m0.717s
user 0m0.120s
sys 0m0.300s
After, from $PROMPT_COMMAND:
real 0m0.577s
user 0m0.047s
sys 0m0.258s
On Linux the speedup ratio is comparable to Windows, but overall it
was about an order of magnitude faster to begin with. The last case
from above, repeated 100 times, before:
$ time for i in {0..99} ; do prompt="$(__git_ps1)" ; done
real 0m2.806s
user 0m0.180s
sys 0m0.264s
After:
real 0m0.857s
user 0m0.020s
sys 0m0.028s
Signed-off-by: SZEDER Gábor <redacted>
---
contrib/completion/git-prompt.sh | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
@@ -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
From: Eduardo R. D'Avila <hidden> Date: 2016-06-15 22:57:55
2013/6/24 SZEDER Gábor [off-list ref]:
This patch series will conflict with Eduardo's work on refactoring the
colorizing function, and the conflict is not trivial. Although there
are still some open questions left with that series (using tput, zsh
tests), those won't affect the conflicts between the two patch series.
So, for the convenience of our maintainer, I picked up Eduardo's
series, took the liberty to apply a fixup commit on top with my
suggestions from [2], merged the two series, and published the result
at:
https://github.com/szeder/git.git bash-prompt-speedup-and-color-refactorization
Eduardo, could you please also check that my conflict resolution is
correct? Thanks.
Gábor, the conflict resolution is quite correct.
Thanks,
Eduardo