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

[PATCH v3] bash: offer to show (un)staged changes

From
Thomas Rast <trast@student.ethz.ch>
Date
Feb 3, 2009, 09:20 UTC
Message-ID
<1233652854-29306-1-git-send-email-trast@student.ethz.ch>
In-Reply-To
<20090201222937.GP26880@spearce.org>

Add a bit of code to __git_ps1 that lets it append '*' to the branch name if there are any unstaged changes, and '+' if there are any staged changes.

Since this is a rather expensive operation and will force a lot of data into the cache whenever you first enter a repository, you have to enable it manually by setting GIT_PS1_SHOWDIRTYSTATE to a nonempty value. The configuration variable bash.showDirtyState can then be used to disable it again for some repositories.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---
Shawn O. Pearce wrote:
> But I'm a bit worried about the config --bool test in the prompt.
> Its a new fork+exec we weren't doing before.  I wonder if we should
> use a shell variable to consider whether or not this should even
> be executed and try to shortcut out if not.

Ok, why not. I changed the default of bash.showDirtyState to true since the user already opts in via GIT_PS1_SHOWDIRTYSTATE.

 contrib/completion/git-completion.bash |   26 ++++++++++++++++++++++++--
 1 files changed, 24 insertions(+), 2 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index f8b845a..13cae8d 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -34,6 +34,12 @@
 #       are currently in a git repository.  The %s token will be
 #       the name of the current branch.
 #
+#	In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty
+#	value, unstaged (*) and staged (+) changes will be shown next
+#	to the branch name.  You can configure this per-repository
+#	with the bash.showDirtyState variable, which defaults to true
+#	once GIT_PS1_SHOWDIRTYSTATE is enabled.
+#
 # To submit patches:
 #
 #    *) Read Documentation/SubmittingPatches
@@ -116,10 +122,26 @@ __git_ps1 ()
 			fi
 		fi
 
+		local w
+		local i
+
+		if test -n "$GIT_PS1_SHOWDIRTYSTATE"; then
+			if test "$(git config --bool bash.showDirtyState)" != "false"; then
+				git diff --no-ext-diff --ignore-submodules \
+					--quiet --exit-code || w="*"
+				if git rev-parse --quiet --verify HEAD >/dev/null; then
+					git diff-index --cached --quiet \
+						--ignore-submodules HEAD -- || i="+"
+				else
+					i="#"
+				fi
+			fi
+		fi
+
 		if [ -n "${1-}" ]; then
-			printf "$1" "${b##refs/heads/}$r"
+			printf "$1" "${b##refs/heads/}$w$i$r"
 		else
-			printf " (%s)" "${b##refs/heads/}$r"
+			printf " (%s)" "${b##refs/heads/}$w$i$r"
 		fi
 	fi
 }
-- 
tg: (7bbd8d6..) t/ps1-dirty-state (depends on: origin/master)
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 15 of 22 in “bash: offer to show (un)staged changes”
  1. bash: offer to show (un)staged changesThomas Rast, Jan 18, 2009
  2. Junio C HamanoJan 18, 2009
  3. Thomas RastJan 18, 2009
  4. Shawn O. PearceJan 19, 2009
  5. Martin LanghoffJan 19, 2009
  6. Shawn O. PearceJan 19, 2009
  7. Mike HommeyJan 19, 2009
  8. Martin LanghoffJan 19, 2009
  9. Boyd Stephen Smith Jr.Jan 19, 2009
  10. Martin LanghoffJan 19, 2009
  11. Boyd Stephen Smith Jr.Jan 19, 2009
  12. bash: offer to show (un)staged changesThomas Rast, Jan 19, 2009
  13. bash: offer to show (un)staged changesThomas Rast, Feb 1, 2009
  14. Shawn O. PearceFeb 1, 2009
  15. bash: offer to show (un)staged changesThomas Rast, Feb 3, 2009
  16. Shawn O. PearceFeb 3, 2009
  17. Tuncer AyazFeb 1, 2009
  18. Junio C HamanoFeb 1, 2009
  19. Tuncer AyazFeb 2, 2009
  20. Tuncer AyazFeb 2, 2009
  21. Junio C HamanoJan 18, 2009
  22. Thomas RastJan 18, 2009

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.