Skip to contentSkip to conversation

completion: Fixes and better non-work-tree support

PATCH In review Ted Pavlic 5 participants 21 messages Jun 15, 2016

lore
Share & tools
lore

Ted Pavlic

author
#0 lore raw

cover completion: Fixes and better non-work-tree support

The first patch has already been Acked by spearce.

The second patch uses a consistent "if..." convention throughout the completion script.

The third patch improves how the __git_ps1 works when (a) the CWD is a git dir and (b) when the CWD has a .git but no .git/HEAD.

The fourth patch fixes unbound variable errors by replacing $__vars with ${__var-}s.

Ted Pavlic (4):
completion: For consistency, changed "git rev-parse" to __gitdir
calls.
completion: Use consistent if [...] convention. No test.
completion: Better __git_ps1 support when not in working directory
completion: More fixes to prevent unbound variable errors.

 contrib/completion/git-completion.bash |   75 ++++++++++++++++----------------
 1 files changed, 37 insertions(+), 38 deletions(-)
author
#1 lore raw

PATCH 1/4 completion: For consistency, changed "git rev-parse" to __gitdir calls.

---
 contrib/completion/git-completion.bash |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
contrib/completion/git-completion.bash +2 -2
index f44f63c..6bbe09a 100755
@@ -80,7 +80,7 @@ __gitdir ()
 # returns text to add to bash PS1 prompt (includes branch name)
 __git_ps1 ()
 {
-	local g="$(git rev-parse --git-dir 2>/dev/null)"
+	local g="$(__gitdir)"
 	if [ -n "$g" ]; then
 		local r
 		local b
@@ -1797,7 +1797,7 @@ _gitk ()
 	__git_has_doubledash && return
 
 	local cur="${COMP_WORDS[COMP_CWORD]}"
-	local g="$(git rev-parse --git-dir 2>/dev/null)"
+	local g="$(__gitdir)"
 	local merge=""
 	if [ -f $g/MERGE_HEAD ]; then
 		merge="--merge"
Signature · 1 line
1.6.1.2.390.gba743
author
#2 lore raw

PATCH 2/4 completion: Use consistent if [...] convention. No test.

The local coding convention in bash completion is to use [...] rather than test. Additionally,

    if [...]; then

is preferred over

    if [...]
    then

and so matching "if [...]\nthen" were changed accordingly.

---
 contrib/completion/git-completion.bash |   31 +++++++++++--------------------
 1 files changed, 11 insertions(+), 20 deletions(-)
contrib/completion/git-completion.bash +11 -20
index 6bbe09a..7706170 100755
@@ -84,39 +84,30 @@ __git_ps1 ()
 	if [ -n "$g" ]; then
 		local r
 		local b
-		if [ -d "$g/rebase-apply" ]
-		then
-			if test -f "$g/rebase-apply/rebasing"
-			then
+		if [ -d "$g/rebase-apply" ]; then
+			if [ -f "$g/rebase-apply/rebasing" ]; then
 				r="|REBASE"
-			elif test -f "$g/rebase-apply/applying"
-			then
+            elif [ -f "$g/rebase-apply/applying" ]; then
 				r="|AM"
 			else
 				r="|AM/REBASE"
 			fi
 			b="$(git symbolic-ref HEAD 2>/dev/null)"
-		elif [ -f "$g/rebase-merge/interactive" ]
-		then
+		elif [ -f "$g/rebase-merge/interactive" ]; then
 			r="|REBASE-i"
 			b="$(cat "$g/rebase-merge/head-name")"
-		elif [ -d "$g/rebase-merge" ]
-		then
+		elif [ -d "$g/rebase-merge" ]; then
 			r="|REBASE-m"
 			b="$(cat "$g/rebase-merge/head-name")"
-		elif [ -f "$g/MERGE_HEAD" ]
-		then
+		elif [ -f "$g/MERGE_HEAD" ]; then
 			r="|MERGING"
 			b="$(git symbolic-ref HEAD 2>/dev/null)"
 		else
-			if [ -f "$g/BISECT_LOG" ]
-			then
+			if [ -f "$g/BISECT_LOG" ]; then
 				r="|BISECTING"
 			fi
-			if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
-			then
-				if ! b="$(git describe --exact-match HEAD 2>/dev/null)"
-				then
+			if ! b="$(git symbolic-ref HEAD 2>/dev/null)"; then
+				if ! b="$(git describe --exact-match HEAD 2>/dev/null)"; then
 					b="$(cut -c1-7 "$g/HEAD")..."
 				fi
 			fi
@@ -125,8 +116,8 @@ __git_ps1 ()
 		local w
 		local i
 
-		if test -n "${GIT_PS1_SHOWDIRTYSTATE-}"; then
-			if test "$(git config --bool bash.showDirtyState)" != "false"; then
+		if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
+			if [ "$(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
Signature · 1 line
1.6.1.2.390.gba743
author
#3 lore raw

PATCH 3/4 completion: Better __git_ps1 support when not in working directory

If .git/HEAD is not readable, __git_ps1 does nothing.

If --is-in-git-dir, __git_ps1 returns " (GIT_DIR!)" as a cautionary note. The previous behavior would show the branch name (and would optionally attempt to determine the dirtyState of the directory, which was impossible because a "git diff" was used).

If --is-in-work-tree, __git_ps1 returns the branch name. Additionally, if showDirtyState is on, the dirty state is displayed.

---
 contrib/completion/git-completion.bash |   36 +++++++++++++++++++------------
 1 files changed, 22 insertions(+), 14 deletions(-)
contrib/completion/git-completion.bash +22 -14
index 7706170..c28d6be 100755
@@ -108,7 +108,9 @@ __git_ps1 ()
 			fi
 			if ! b="$(git symbolic-ref HEAD 2>/dev/null)"; then
 				if ! b="$(git describe --exact-match HEAD 2>/dev/null)"; then
-					b="$(cut -c1-7 "$g/HEAD")..."
+					if [ -r "$g/HEAD" ]; then
+						b="$(cut -c1-7 "$g/HEAD")..."
+					fi
 				fi
 			fi
 		fi
@@ -116,23 +118,29 @@ __git_ps1 ()
 		local w
 		local i
 
-		if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
-			if [ "$(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="#"
+		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
+			b="GIT_DIR!"
+		elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
+			if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
+				if [ "$(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
 		fi
 
-		if [ -n "${1-}" ]; then
-			printf "$1" "${b##refs/heads/}$w$i$r"
-		else
-			printf " (%s)" "${b##refs/heads/}$w$i$r"
+		if [ -n "$b" ]; then
+			if [ -n "${1-}" ]; then
+				printf "$1" "${b##refs/heads/}$w$i$r"
+			else
+				printf " (%s)" "${b##refs/heads/}$w$i$r"
+			fi
 		fi
 	fi
 }
Signature · 1 line
1.6.1.2.390.gba743
author
#4 lore raw

PATCH 4/4 completion: More fixes to prevent unbound variable errors.

Several functions make use of "[-n ...]" and "[-z ...]". In many cases, the variables being tested were declared with "local." However, several __variables are not, and so they must be replaced with their ${__-} equivalents.

---
 contrib/completion/git-completion.bash |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
contrib/completion/git-completion.bash +4 -4
index c28d6be..371148b 100755
@@ -62,7 +62,7 @@ esac
 __gitdir ()
 {
 	if [ -z "${1-}" ]; then
-		if [ -n "$__git_dir" ]; then
+		if [ -n "${__git_dir-}" ]; then
 			echo "$__git_dir"
 		elif [ -d .git ]; then
 			echo .git
@@ -298,7 +298,7 @@ __git_remotes ()
 
 __git_merge_strategies ()
 {
-	if [ -n "$__git_merge_strategylist" ]; then
+	if [ -n "${__git_merge_strategylist-}" ]; then
 		echo "$__git_merge_strategylist"
 		return
 	fi
@@ -384,7 +384,7 @@ __git_complete_revlist ()
 
 __git_all_commands ()
 {
-	if [ -n "$__git_all_commandlist" ]; then
+	if [ -n "${__git_all_commandlist-}" ]; then
 		echo "$__git_all_commandlist"
 		return
 	fi
@@ -402,7 +402,7 @@ __git_all_commandlist="$(__git_all_commands 2>/dev/null)"
 
 __git_porcelain_commands ()
 {
-	if [ -n "$__git_porcelain_commandlist" ]; then
+	if [ -n "${__git_porcelain_commandlist-}" ]; then
 		echo "$__git_porcelain_commandlist"
 		return
 	fi
Signature · 1 line
1.6.1.2.390.gba743
Acked-by on v1 4/4
#5 lore raw

Re: [PATCH 4/4] completion: More fixes to prevent unbound variable errors.

Quoted 6 lines · Ted Pavlic

Several functions make use of "[-n ...]" and "[-z ...]". In many cases, the variables being tested were declared with "local." However, several __variables are not, and so they must be replaced with their ${__-} equivalents.

Quoted 47 lines
---
 contrib/completion/git-completion.bash |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
contrib/completion/git-completion.bash +4 -4
index c28d6be..371148b 100755
@@ -62,7 +62,7 @@ esac
 __gitdir ()
 {
 	if [ -z "${1-}" ]; then
-		if [ -n "$__git_dir" ]; then
+		if [ -n "${__git_dir-}" ]; then
 			echo "$__git_dir"
 		elif [ -d .git ]; then
 			echo .git
@@ -298,7 +298,7 @@ __git_remotes ()
 
 __git_merge_strategies ()
 {
-	if [ -n "$__git_merge_strategylist" ]; then
+	if [ -n "${__git_merge_strategylist-}" ]; then
 		echo "$__git_merge_strategylist"
 		return
 	fi
@@ -384,7 +384,7 @@ __git_complete_revlist ()
 
 __git_all_commands ()
 {
-	if [ -n "$__git_all_commandlist" ]; then
+	if [ -n "${__git_all_commandlist-}" ]; then
 		echo "$__git_all_commandlist"
 		return
 	fi
@@ -402,7 +402,7 @@ __git_all_commandlist="$(__git_all_commands 2>/dev/null)"
 
 __git_porcelain_commands ()
 {
-	if [ -n "$__git_porcelain_commandlist" ]; then
+	if [ -n "${__git_porcelain_commandlist-}" ]; then
 		echo "$__git_porcelain_commandlist"
 		return
 	fi
Signature · 1 line
1.6.1.2.390.gba743
Signature · 1 line
Shawn.
Acked-by on v1 3/4
#6 lore raw

Re: [PATCH 3/4] completion: Better __git_ps1 support when not in working directory

In reply to Ted Pavlic
Quoted 11 lines · Ted Pavlic

If .git/HEAD is not readable, __git_ps1 does nothing.

If --is-in-git-dir, __git_ps1 returns " (GIT_DIR!)" as a cautionary note. The previous behavior would show the branch name (and would optionally attempt to determine the dirtyState of the directory, which was impossible because a "git diff" was used).

If --is-in-work-tree, __git_ps1 returns the branch name. Additionally, if showDirtyState is on, the dirty state is displayed.

Quoted 65 lines
---
 contrib/completion/git-completion.bash |   36 +++++++++++++++++++------------
 1 files changed, 22 insertions(+), 14 deletions(-)
contrib/completion/git-completion.bash +22 -14
index 7706170..c28d6be 100755
@@ -108,7 +108,9 @@ __git_ps1 ()
 			fi
 			if ! b="$(git symbolic-ref HEAD 2>/dev/null)"; then
 				if ! b="$(git describe --exact-match HEAD 2>/dev/null)"; then
-					b="$(cut -c1-7 "$g/HEAD")..."
+					if [ -r "$g/HEAD" ]; then
+						b="$(cut -c1-7 "$g/HEAD")..."
+					fi
 				fi
 			fi
 		fi
@@ -116,23 +118,29 @@ __git_ps1 ()
 		local w
 		local i
 
-		if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
-			if [ "$(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="#"
+		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
+			b="GIT_DIR!"
+		elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
+			if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
+				if [ "$(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
 		fi
 
-		if [ -n "${1-}" ]; then
-			printf "$1" "${b##refs/heads/}$w$i$r"
-		else
-			printf " (%s)" "${b##refs/heads/}$w$i$r"
+		if [ -n "$b" ]; then
+			if [ -n "${1-}" ]; then
+				printf "$1" "${b##refs/heads/}$w$i$r"
+			else
+				printf " (%s)" "${b##refs/heads/}$w$i$r"
+			fi
 		fi
 	fi
 }
Signature · 1 line
1.6.1.2.390.gba743
Signature · 1 line
Shawn.
#7 lore raw

Re: [PATCH 2/4] completion: Use consistent if [...] convention. No test.

In reply to Ted Pavlic
Quoted 25 lines · Ted Pavlic

The local coding convention in bash completion is to use [...] rather than test. Additionally,

    if [...]; then

is preferred over

    if [...]
    then

and so matching "if [...]\nthen" were changed accordingly.

---
 contrib/completion/git-completion.bash |   31 +++++++++++--------------------
 1 files changed, 11 insertions(+), 20 deletions(-)
contrib/completion/git-completion.bash +0 -0
index 6bbe09a..7706170 100755
@@ -84,39 +84,30 @@ __git_ps1 ()
 	if [ -n "$g" ]; then
 		local r
 		local b

contrib/completion/git-completion.bash __git_ps1 () · line 90

Quoted 10 lines
contrib/completion/git-completion.bash +3 -6
-		if [ -d "$g/rebase-apply" ]
-		then
-			if test -f "$g/rebase-apply/rebasing"
-			then
+		if [ -d "$g/rebase-apply" ]; then
+			if [ -f "$g/rebase-apply/rebasing" ]; then
 				r="|REBASE"
-			elif test -f "$g/rebase-apply/applying"
-			then
+            elif [ -f "$g/rebase-apply/applying" ]; then

There is some sort of whitespace damage right here, the elif doesn't seem to line up correctly.

Signature · 1 line
Shawn.
#8 lore raw

Re: [PATCH 2/4] completion: Use consistent if [...] convention. No test.

Quoted 6 lines · Shawn O. Pearce

contrib/completion/git-completion.bash __git_ps1 () · line 90

Quoted 3 lines
contrib/completion/git-completion.bash +1 -2
-			elif test -f "$g/rebase-apply/applying"
-			then
+            elif [ -f "$g/rebase-apply/applying" ]; then

There is some sort of whitespace damage right here, the elif doesn't seem to line up correctly.

If that is the only gripe and otherwise if you are Ok with the patch, I'll queue the entire series with a fix-up here myself.

#9 lore raw

Re: [PATCH 2/4] completion: Use consistent if [...] convention. No test.

Quoted 11 lines · Junio C Hamano
Quoted 6 lines · Shawn O. Pearce

contrib/completion/git-completion.bash __git_ps1 () · line 90

Quoted 3 lines
contrib/completion/git-completion.bash +1 -2
-			elif test -f "$g/rebase-apply/applying"
-			then
+            elif [ -f "$g/rebase-apply/applying" ]; then

There is some sort of whitespace damage right here, the elif doesn't seem to line up correctly.

If that is the only gripe and otherwise if you are Ok with the patch, I'll queue the entire series with a fix-up here myself.

Yes, this patch is fine, if you can hand-fix the whitespace damage, please feel free to add my Ack.

Signature · 1 line
Shawn.
#10 lore raw

Re: [PATCH 2/4] completion: Use consistent if [...] convention. No test.

In reply to Ted Pavlic

contrib/completion/git-completion.bash __git_ps1 () · line 92

Quoted 12 lines · Ted Pavlic
contrib/completion/git-completion.bash +3 -6
-		if [ -d "$g/rebase-apply" ]
-		then
-			if test -f "$g/rebase-apply/rebasing"
-			then
+		if [ -d "$g/rebase-apply" ]; then
+			if [ -f "$g/rebase-apply/rebasing" ]; then
 				r="|REBASE"
-			elif test -f "$g/rebase-apply/applying"
-			then
+            elif [ -f "$g/rebase-apply/applying" ]; then
 				r="|AM"
 			else

What's with this funny indentation?

As a general rule, it usually is a good idea to apply clean-up to the codebase before starting substantial work, but that holds true only when the clean-up is undisputed. Otherwise you would end up holding the later, more "interesting" work a hostage to an earlier potentially controversial "clean-up".

I think this particular clean-up makes the odd-ball __git_ps1 more consnstent with the rest of the script, but it ultimately is Shawn's call.

author
#11 lore raw

cover completion fixes: Acks, whitespace, and r=""

* Added Shawn O. Pearce acks
* Fixed whitespace problem in second patch (vim:expandtabs was on)
* When in GIT_DIR, set r="" in __git_ps1 as well

Ted Pavlic (4):
completion: For consistency, changed "git rev-parse" to __gitdir
calls.
completion: Use consistent if [...] convention. No test.
completion: Better __git_ps1 support when not in working directory
completion: More fixes to prevent unbound variable errors.

 contrib/completion/git-completion.bash |   76 ++++++++++++++++----------------
 1 files changed, 38 insertions(+), 38 deletions(-)
author
#12 lore raw

PATCH 1/4 completion: For consistency, changed "git rev-parse" to __gitdir calls.

---
 contrib/completion/git-completion.bash |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
contrib/completion/git-completion.bash +2 -2
index f44f63c..6bbe09a 100755
@@ -80,7 +80,7 @@ __gitdir ()
 # returns text to add to bash PS1 prompt (includes branch name)
 __git_ps1 ()
 {
-	local g="$(git rev-parse --git-dir 2>/dev/null)"
+	local g="$(__gitdir)"
 	if [ -n "$g" ]; then
 		local r
 		local b
@@ -1797,7 +1797,7 @@ _gitk ()
 	__git_has_doubledash && return
 
 	local cur="${COMP_WORDS[COMP_CWORD]}"
-	local g="$(git rev-parse --git-dir 2>/dev/null)"
+	local g="$(__gitdir)"
 	local merge=""
 	if [ -f $g/MERGE_HEAD ]; then
 		merge="--merge"
Signature · 1 line
1.6.1.2.390.gba743
author
#13 lore raw

PATCH 2/4 completion: Use consistent if [...] convention. No test.

The local coding convention in bash completion is to use [...] rather than test. Additionally,

    if [...]; then

is preferred over

    if [...]
    then

and so matching "if [...]\nthen" were changed accordingly.

---
 contrib/completion/git-completion.bash |   31 +++++++++++--------------------
 1 files changed, 11 insertions(+), 20 deletions(-)
contrib/completion/git-completion.bash +11 -20
index 6bbe09a..e729944 100755
@@ -84,39 +84,30 @@ __git_ps1 ()
 	if [ -n "$g" ]; then
 		local r
 		local b
-		if [ -d "$g/rebase-apply" ]
-		then
-			if test -f "$g/rebase-apply/rebasing"
-			then
+		if [ -d "$g/rebase-apply" ]; then
+			if [ -f "$g/rebase-apply/rebasing" ]; then
 				r="|REBASE"
-			elif test -f "$g/rebase-apply/applying"
-			then
+			elif [ -f "$g/rebase-apply/applying" ]; then
 				r="|AM"
 			else
 				r="|AM/REBASE"
 			fi
 			b="$(git symbolic-ref HEAD 2>/dev/null)"
-		elif [ -f "$g/rebase-merge/interactive" ]
-		then
+		elif [ -f "$g/rebase-merge/interactive" ]; then
 			r="|REBASE-i"
 			b="$(cat "$g/rebase-merge/head-name")"
-		elif [ -d "$g/rebase-merge" ]
-		then
+		elif [ -d "$g/rebase-merge" ]; then
 			r="|REBASE-m"
 			b="$(cat "$g/rebase-merge/head-name")"
-		elif [ -f "$g/MERGE_HEAD" ]
-		then
+		elif [ -f "$g/MERGE_HEAD" ]; then
 			r="|MERGING"
 			b="$(git symbolic-ref HEAD 2>/dev/null)"
 		else
-			if [ -f "$g/BISECT_LOG" ]
-			then
+			if [ -f "$g/BISECT_LOG" ]; then
 				r="|BISECTING"
 			fi
-			if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
-			then
-				if ! b="$(git describe --exact-match HEAD 2>/dev/null)"
-				then
+			if ! b="$(git symbolic-ref HEAD 2>/dev/null)"; then
+				if ! b="$(git describe --exact-match HEAD 2>/dev/null)"; then
 					b="$(cut -c1-7 "$g/HEAD")..."
 				fi
 			fi
@@ -125,8 +116,8 @@ __git_ps1 ()
 		local w
 		local i
 
-		if test -n "${GIT_PS1_SHOWDIRTYSTATE-}"; then
-			if test "$(git config --bool bash.showDirtyState)" != "false"; then
+		if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
+			if [ "$(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
Signature · 1 line
1.6.1.2.390.gba743
author
#14 lore raw

PATCH 3/4 completion: Better __git_ps1 support when not in working directory

If .git/HEAD is not readable, __git_ps1 does nothing.

If --is-in-git-dir, __git_ps1 returns " (GIT_DIR!)" as a cautionary note. The previous behavior would show the branch name (and would optionally attempt to determine the dirtyState of the directory, which was impossible because a "git diff" was used).

If --is-in-work-tree, __git_ps1 returns the branch name. Additionally, if showDirtyState is on, the dirty state is displayed.

---
 contrib/completion/git-completion.bash |   37 +++++++++++++++++++------------
 1 files changed, 23 insertions(+), 14 deletions(-)
contrib/completion/git-completion.bash +23 -14
index e729944..3d48a65 100755
@@ -108,7 +108,9 @@ __git_ps1 ()
 			fi
 			if ! b="$(git symbolic-ref HEAD 2>/dev/null)"; then
 				if ! b="$(git describe --exact-match HEAD 2>/dev/null)"; then
-					b="$(cut -c1-7 "$g/HEAD")..."
+					if [ -r "$g/HEAD" ]; then
+						b="$(cut -c1-7 "$g/HEAD")..."
+					fi
 				fi
 			fi
 		fi
@@ -116,23 +118,30 @@ __git_ps1 ()
 		local w
 		local i
 
-		if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
-			if [ "$(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="#"
+		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
+			b="GIT_DIR!"
+            r=""
+		elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
+			if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
+				if [ "$(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
 		fi
 
-		if [ -n "${1-}" ]; then
-			printf "$1" "${b##refs/heads/}$w$i$r"
-		else
-			printf " (%s)" "${b##refs/heads/}$w$i$r"
+		if [ -n "$b" ]; then
+			if [ -n "${1-}" ]; then
+				printf "$1" "${b##refs/heads/}$w$i$r"
+			else
+				printf " (%s)" "${b##refs/heads/}$w$i$r"
+			fi
 		fi
 	fi
 }
Signature · 1 line
1.6.1.2.390.gba743
author
#15 lore raw

PATCH 4/4 completion: More fixes to prevent unbound variable errors.

Several functions make use of "[-n ...]" and "[-z ...]". In many cases, the variables being tested were declared with "local." However, several __variables are not, and so they must be replaced with their ${__-} equivalents.

---
 contrib/completion/git-completion.bash |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
contrib/completion/git-completion.bash +4 -4
index 3d48a65..8d62b2b 100755
@@ -62,7 +62,7 @@ esac
 __gitdir ()
 {
 	if [ -z "${1-}" ]; then
-		if [ -n "$__git_dir" ]; then
+		if [ -n "${__git_dir-}" ]; then
 			echo "$__git_dir"
 		elif [ -d .git ]; then
 			echo .git
@@ -299,7 +299,7 @@ __git_remotes ()
 
 __git_merge_strategies ()
 {
-	if [ -n "$__git_merge_strategylist" ]; then
+	if [ -n "${__git_merge_strategylist-}" ]; then
 		echo "$__git_merge_strategylist"
 		return
 	fi
@@ -385,7 +385,7 @@ __git_complete_revlist ()
 
 __git_all_commands ()
 {
-	if [ -n "$__git_all_commandlist" ]; then
+	if [ -n "${__git_all_commandlist-}" ]; then
 		echo "$__git_all_commandlist"
 		return
 	fi
@@ -403,7 +403,7 @@ __git_all_commandlist="$(__git_all_commands 2>/dev/null)"
 
 __git_porcelain_commands ()
 {
-	if [ -n "$__git_porcelain_commandlist" ]; then
+	if [ -n "${__git_porcelain_commandlist-}" ]; then
 		echo "$__git_porcelain_commandlist"
 		return
 	fi
Signature · 1 line
1.6.1.2.390.gba743
author
#19 lore raw

Re: [PATCH 2/4] completion: Use consistent if [...] convention. No test.

In reply to Junio C Hamano
Quoted 1 line

What's with this funny indentation?

In Vim, I have "exapndtabs" set by default. I manually turned it off, but during testing, I must have closed Vim and re-opened and forgot to turn it back off.

Some time ago, I suggested adding a vim modeline to the file to handle turning expandtabs off for everyone who edits it with vim. That change got denied.

If you'd like, I can clean up the whitespace and resubmit with Shawn's Acks.

--Ted

Signature · 5 lines
Ted Pavlic <[email protected]>

  Please visit my ALS association page:
        http://web.alsa.org/goto/tedpavlic
  My family appreciates your support in the fight to defeat ALS.
#20 lore raw

Re: [PATCH 2/4] completion: Use consistent if [...] convention. No test.

Quoted 7 lines · Ted Pavlic

In Vim, I have "exapndtabs" set by default. I manually turned it off, but during testing, I must have closed Vim and re-opened and forgot to turn it back off.

Some time ago, I suggested adding a vim modeline to the file to handle turning expandtabs off for everyone who edits it with vim. That change got denied.

FWIW, I do this in my .vimrc:

au BufNewFile,BufRead /path/to/git/repo/* set noet sts=8 sw=8 ts=8

-Peff

author
#16 lore raw

Re: [PATCH 0/4] completion fixes: Acks, whitespace, and r=""

In reply to Ted Pavlic

In my latest "git pull," I noticed a few completion changes, but none of these changes were included (and thus on some of my machines, I get unbound variable errors again).

Were these changes finally rejected?

Thanks --
Ted

Quoted 10 lines · Ted Pavlic

* Added Shawn O. Pearce acks
* Fixed whitespace problem in second patch (vim:expandtabs was on)
* When in GIT_DIR, set r="" in __git_ps1 as well

Ted Pavlic (4):
completion: For consistency, changed "git rev-parse" to __gitdir
calls.
completion: Use consistent if [...] convention. No test.
completion: Better __git_ps1 support when not in working directory
completion: More fixes to prevent unbound variable errors.

Signature · 5 lines
Ted Pavlic <[email protected]>

  Please visit my ALS association page:
        http://web.alsa.org/goto/tedpavlic
  My family appreciates your support in the fight to defeat ALS.
#17 lore raw

Re: [PATCH 0/4] completion fixes: Acks, whitespace, and r=""

Quoted 5 lines · Ted Pavlic

In my latest "git pull," I noticed a few completion changes, but none of these changes were included (and thus on some of my machines, I get unbound variable errors again).

Were these changes finally rejected?

$ git log --pretty=oneline --abbrev-commit -4 --author=Pavlic origin/next
5c9cc64 completion: More fixes to prevent unbound variable errors
e5dd864 completion: Better __git_ps1 support when not in working directory
ad244d2 completion: Use consistent if [...] convention, not "test"
fa26a40 completion: For consistency, change "git rev-parse" to __gitdir calls
Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.ch
author
#18 lore raw

Re: [PATCH 0/4] completion fixes: Acks, whitespace, and r=""

Quoted 1 line
$ git log --pretty=oneline --abbrev-commit -4 --author=Pavlic origin/next

Ah -- next branch. I git it.

This is all very fancy. Sorry for the spam.

--Ted

Signature · 5 lines
Ted Pavlic <[email protected]>

  Please visit my ALS association page:
        http://web.alsa.org/goto/tedpavlic
  My family appreciates your support in the fight to defeat ALS.