git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 10/13] bash prompt: combine 'git rev-parse' executions

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Jun 18, 2013, 02:17 UTC
Message-ID
<1371521826-3225-11-git-send-email-szeder@ira.uka.de>
In-Reply-To
<1371521826-3225-1-git-send-email-szeder@ira.uka.de>
From: SZEDER Gábor <szeder@ira.uka.de>
>From the four '$(git rev-parse --<opt>)' command substitutions
__git_ps1() executes three in its 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 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.

The whole series speeds up the bash prompt on Windows/MSysGit considerably. Here are some timing results in two scenarios, 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.686s
    user    0m0.075s
    sys     0m0.287s

In a subdirectory, during rebase, stash status indicator enabled, before:

    real    0m3.557s
    user    0m0.495s
    sys     0m1.767s
  After:
    real    0m0.702s
    user    0m0.045s
    sys     0m0.409s
Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 contrib/completion/git-prompt.sh | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 10ec6902..b73cebf7 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -311,8 +311,9 @@ __git_ps1 ()
 		;;
 	esac
 
-	local g=
-	if ! g="$(git rev-parse --git-dir 2>/dev/null)"; then
+	local repo_info=
+	if ! repo_info="$(git rev-parse --git-dir --is-inside-git-dir \
+		--is-bare-repository --is-inside-work-tree 2>/dev/null)"; then
 		if [ $pcmode = yes ]; then
 			#In PC mode PS1 always needs to be set
 			PS1="$ps1pc_start$ps1pc_end"
@@ -320,6 +321,13 @@ __git_ps1 ()
 		return
 	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=""
@@ -396,13 +404,13 @@ __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
-- 
1.8.3.1.487.g8f4672d
Previous: SZEDER GáborNext: Jeff King
Message 13 of 22 in “bash prompt speedup”
  1. 00/13 bash prompt speedupSZEDER Gábor, Jun 18, 2013
  2. 01/13 bash prompt: fix redirection coding style in testsSZEDER Gábor, Jun 18, 2013
  3. 02/13 bash prompt: fix here document indentation in interactive rebase testSZEDER Gábor, Jun 18, 2013
  4. Jeff KingJun 18, 2013
  5. 03/13 completion, bash prompt: move __gitdir() tests to completion test suiteSZEDER Gábor, Jun 18, 2013
  6. 04/13 bash prompt: add a test for symbolic link symbolic refsSZEDER Gábor, Jun 18, 2013
  7. 05/13 bash prompt: return early from __git_ps1() when not in a git repositorySZEDER Gábor, Jun 18, 2013
  8. 06/13 bash prompt: run 'git rev-parse --git-dir' directly instead of __gitdir()SZEDER Gábor, Jun 18, 2013
  9. Eric SunshineJun 18, 2013
  10. 07/13 bash prompt: use bash builtins to find out rebase stateSZEDER Gábor, Jun 18, 2013
  11. 08/13 bash prompt: use bash builtins to find out current branchSZEDER Gábor, Jun 18, 2013
  12. 09/13 bash prompt: use bash builtins to get detached HEAD abbrev. object nameSZEDER Gábor, Jun 18, 2013
  13. 10/13 bash prompt: combine 'git rev-parse' executionsSZEDER Gábor, Jun 18, 2013
  14. Jeff KingJun 18, 2013
  15. SZEDER GáborJun 18, 2013
  16. fixup! bash prompt: use bash builtins to find out current branchSZEDER Gábor, Jun 18, 2013
  17. 11/13 bash prompt: use bash builtins to check stash stateSZEDER Gábor, Jun 18, 2013
  18. 12/13 bash prompt: avoid command substitution when checking for untracked filesSZEDER Gábor, Jun 18, 2013
  19. 13/13 bash prompt: avoid command substitution when finalizing gitstringSZEDER Gábor, Jun 18, 2013
  20. SZEDER GáborJun 18, 2013
  21. Junio C HamanoJun 18, 2013
  22. Simon OosthoekJun 18, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.