Thread (1 message) 1 message, 1 author, 2016-06-15

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

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:53:54
Subsystem: the rest · Maintainer: Linus Torvalds

Possibly related (same subject, not in this thread)

Thomas Rast [off-list ref] 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help