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

Re: [PATCH 2/2] completion: split __git_ps1 into a separate script

From
Junio C Hamano <gitster@pobox.com>
Date
May 25, 2012, 18:03 UTC
Message-ID
<7v8vggt9y5.fsf@alter.siamese.dyndns.org>
In-Reply-To
<87ehq8its8.fsf@thomas.inf.ethz.ch>
Thomas Rast <trast@inf.ethz.ch> writes:
> Why not make a git builtin command that figures out everything that
> __git_ps1 does?  Perhaps in a format that can be eval'd and processed to
> the user's taste.

I'd rather not to see something so specific for one interpreter like that in the core. How about doing it this way instead?

diff --git a/contrib/completion/Makefile b/contrib/completion/Makefile
new file mode 100644
index 0000000..71c600f
--- /dev/null
+++ b/contrib/completion/Makefile
@@ -0,0 +1,13 @@
+# The default target is ...
+all::
+
+SCRIPTS = git-completion.bash git-prompt.sh
+
+all:: $(SCRIPTS)
+clean::
+	rm -f $(SCRIPTS)
+
+$(SCRIPTS): % : %.shc
+	rm -f $@+ $@
+	sed -e '/## include common-bits/r common-bits' $< >$@+
+	mv $@+ $@
diff --git a/contrib/completion/common-bits b/contrib/completion/common-bits
new file mode 100644
index 0000000..06c2845
--- /dev/null
+++ b/contrib/completion/common-bits
@@ -0,0 +1,22 @@
+# __gitdir accepts 0 or 1 arguments (i.e., location)
+# returns location of .git repo
+__gitdir ()
+{
+	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
+}
+
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash.shc
similarity index 99%
rename from contrib/completion/git-completion.bash
rename to contrib/completion/git-completion.bash.shc
index abf8215..cf30f01 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash.shc
@@ -32,24 +32,8 @@ case "$COMP_WORDBREAKS" in
 *)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
 esac
 
-# __gitdir accepts 0 or 1 arguments (i.e., location)
-# returns location of .git repo
-__gitdir ()
-{
-	if [ -z "${1-}" ]; then
-		if [ -n "${__git_dir-}" ]; then
-			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
-}
+## include common-bits here
+## common-bits ends here
 
 __gitcomp_1 ()
 {
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh.shc
similarity index 94%
rename from contrib/completion/git-prompt.sh
rename to contrib/completion/git-prompt.sh.shc
index 8e2e9f3..d442a1a 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh.shc
@@ -49,27 +49,8 @@
 # GIT_PS1_SHOWUPSTREAM, you can override it on a per-repository basis by
 # setting the bash.showUpstream config variable.
 
-# __gitdir accepts 0 or 1 arguments (i.e., location)
-# returns location of .git repo
-__gitdir ()
-{
-	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
-}
+## include common-bits here
+## common-bits ends here
 
 # stores the divergence from upstream in $p
 # used by GIT_PS1_SHOWUPSTREAM
Previous: Felipe Contreras
Message 34 of 34 in “completion: split into git-prompt.sh”
  1. 0/2 completion: split into git-prompt.shFelipe Contreras, May 22, 2012
  2. 1/2 completion: remove executable modeFelipe Contreras, May 22, 2012
  3. 2/2 completion: split __git_ps1 into a separate scriptFelipe Contreras, May 22, 2012
  4. Zbigniew Jędrzejewski-SzmekMay 22, 2012
  5. Ted PavlicMay 22, 2012
  6. Felipe ContrerasMay 23, 2012
  7. Felipe ContrerasMay 23, 2012
  8. Junio C HamanoMay 23, 2012
  9. Felipe ContrerasMay 23, 2012
  10. Junio C HamanoMay 23, 2012
  11. Ted PavlicMay 23, 2012
  12. Felipe ContrerasMay 23, 2012
  13. Ted PavlicMay 23, 2012
  14. SZEDER GáborMay 24, 2012
  15. SZEDER GáborMay 24, 2012
  16. Ted PavlicMay 22, 2012
  17. Felipe ContrerasMay 23, 2012
  18. Junio C HamanoMay 23, 2012
  19. SZEDER GáborMay 24, 2012
  20. Ville SkyttäMay 25, 2012
  21. Where should git-prompt.sh be installed?Jonathan Nieder, Oct 25, 2012
  22. Drew NorthupOct 25, 2012
  23. Danny YatesOct 25, 2012
  24. __git_ps1: migrate out of contrib/completionJonathan Nieder, Oct 25, 2012
  25. Felipe ContrerasOct 25, 2012
  26. Todd ZullingerNov 8, 2012
  27. Anders KaseorgOct 25, 2012
  28. SZEDER GáborOct 25, 2012
  29. Jonathan NiederOct 25, 2012
  30. Junio C HamanoMay 23, 2012
  31. SZEDER GáborMay 25, 2012
  32. Thomas RastMay 25, 2012
  33. Felipe ContrerasMay 25, 2012
  34. Junio C HamanoMay 25, 2012

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.