Skip to contentSkip to conversation

shell-prompt: clean up nested if-then

PATCH In review Martin Erik Werner 4 participants 8 messages Jun 15, 2016 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 1361204512.4758.10.camel@mas

#1 lore raw

PATCH 1/1 shell-prompt: clean up nested if-then

Minor clean up of if-then nesting in checks for environment variables and config options. No functional changes.

---
 contrib/completion/git-prompt.sh |   27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
contrib/completion/git-prompt.sh +13 -14
index 9b2eec2..e29694d 100644
@@ -320,26 +320,25 @@ __git_ps1 ()
 				b="GIT_DIR!"
 			fi
 		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 --quiet --exit-code || w="*"
-					if git rev-parse --quiet --verify HEAD >/dev/null; then
-						git diff-index --cached --quiet HEAD -- || i="+"
-					else
-						i="#"
-					fi
+			if test -n "${GIT_PS1_SHOWDIRTYSTATE-}" &&
+			   test "$(git config --bool bash.showDirtyState)" != "false"
+			then
+				git diff --no-ext-diff --quiet --exit-code || w="*"
+				if git rev-parse --quiet --verify HEAD >/dev/null; then
+					git diff-index --cached --quiet HEAD -- || i="+"
+				else
+					i="#"
 				fi
 			fi
 			if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
 				git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
 			fi
 
-			if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
-				if [ "$(git config --bool bash.showUntrackedFiles)" != "false" ]; then
-					if [ -n "$(git ls-files --others --exclude-standard)" ]; then
-						u="%"
-					fi
-				fi
+			if test -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" &&
+			   test "$(git config --bool bash.showUntrackedFiles)" != "false" &&
+			   test -n "$(git ls-files --others --exclude-standard)"
+			then
+				u="%"
 			fi
 
 			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
Signature · 1 line
1.7.10.4
Reviewed-by on v1 1/1
#2 lore raw

Hi Martin,

Quoted 2 lines · Martin Erik Werner

Minor clean up of if-then nesting in checks for environment variables and config options. No functional changes.

Yeah, the nesting was getting a little deep. Thanks for the cleanup. May we have your sign-off?

Once this is signed off,

Patch left unsnipped for reference.

Quoted 49 lines
---
 contrib/completion/git-prompt.sh |   27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
contrib/completion/git-prompt.sh +13 -14
index 9b2eec2..e29694d 100644
@@ -320,26 +320,25 @@ __git_ps1 ()
 				b="GIT_DIR!"
 			fi
 		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 --quiet --exit-code || w="*"
-					if git rev-parse --quiet --verify HEAD >/dev/null; then
-						git diff-index --cached --quiet HEAD -- || i="+"
-					else
-						i="#"
-					fi
+			if test -n "${GIT_PS1_SHOWDIRTYSTATE-}" &&
+			   test "$(git config --bool bash.showDirtyState)" != "false"
+			then
+				git diff --no-ext-diff --quiet --exit-code || w="*"
+				if git rev-parse --quiet --verify HEAD >/dev/null; then
+					git diff-index --cached --quiet HEAD -- || i="+"
+				else
+					i="#"
 				fi
 			fi
 			if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
 				git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
 			fi
 
-			if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
-				if [ "$(git config --bool bash.showUntrackedFiles)" != "false" ]; then
-					if [ -n "$(git ls-files --others --exclude-standard)" ]; then
-						u="%"
-					fi
-				fi
+			if test -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" &&
+			   test "$(git config --bool bash.showUntrackedFiles)" != "false" &&
+			   test -n "$(git ls-files --others --exclude-standard)"
+			then
+				u="%"
 			fi
 
 			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 6 lines · Simon vanaf Telefoon

Hi all, sorry for top posting :-( blame the phone and k9

I have a small issue with the use of test instead of [ If that only applies to this section of the entire file. Coding style has some value.

Quoted 6 lines

Combining nested ifs with && seems harmless enough, though should be well tested.

Cheers
Simon

Ah, indeed, I looked around a bit more, and as per
http://mywiki.wooledge.org/BashPitfalls it seems like 'test' is bad to use with multiple &&'s anyways.

I've changed to using [] && [] and rerolled the patch.

Quoted 5 lines

Jonathan Nieder <[email protected]> wrote:
Hi Martin,

Martin Erik Werner wrote:

Quoted 8 lines

Minor clean up of if-then nesting in checks for environment variables and config options. No functional changes.

Yeah, the nesting was getting a little deep. Thanks for the cleanup. May we have your sign-off?

Once this is signed off,

Meh, I keep on missing that :/
Old (and new) patch is:

Quoted 56 lines

Patch left unsnipped for reference.

---
contrib/completion/git-prompt.sh | 27 +++++++++++++--------------
1 file changed, 13 insertions(+), 14 deletions(-)

diff --git
a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh index 9b2eec2..e29694d 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -320,26 +320,25 @@ __git_ps1 ()
b="GIT_DIR!"
fi
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 --quiet --exit-code || w="*"
- if git rev-parse --quiet --verify HEAD >/dev/null; then
- git diff-index --cached --quiet HEAD -- || i="+"
- else
- i="#"
- fi
+ if test -n "${GIT_PS1_SHOWDIRTYSTATE-}" &&
+ test "$(git config --bool bash.showDirtyState)" !=
"false"
+ then
+ git diff --no-ext-diff --quiet --exit-code || w="*"
+ if git rev-parse --quiet --verify HEAD >/dev/null; then
+ git diff-index --cached --quiet HEAD -- || i="+"
+ else
+ i="#"
fi
fi
if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
fi

- if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
- if [ "$(git config --bool bash.showUntrackedFiles)" != "false" ]; then
- if [ -n "$(git ls-files --others --exclude-standard)" ]; then
- u="%"
- fi
- fi
+ if test -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" &&
+ test "$(git config --bool bash.showUntrackedFiles)" != "false" &&
+ test -n "$(git ls-files --others --exclude-standard)"
+ then
+ u="%"
fi

if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ];!
then
--

#5 lore raw

PATCH v2 1/1 shell-prompt: clean up nested if-then

From: Martin Erik Werner <[email protected]>

Minor clean up of if-then nesting in checks for environment variables and config options. No functional changes.

---
 contrib/completion/git-prompt.sh |   27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
contrib/completion/git-prompt.sh +13 -14
index 9b2eec2..341422a 100644
@@ -320,26 +320,25 @@ __git_ps1 ()
 				b="GIT_DIR!"
 			fi
 		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 --quiet --exit-code || w="*"
-					if git rev-parse --quiet --verify HEAD >/dev/null; then
-						git diff-index --cached --quiet HEAD -- || i="+"
-					else
-						i="#"
-					fi
+			if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&
+			   [ "$(git config --bool bash.showDirtyState)" != "false" ]
+			then
+				git diff --no-ext-diff --quiet --exit-code || w="*"
+				if git rev-parse --quiet --verify HEAD >/dev/null; then
+					git diff-index --cached --quiet HEAD -- || i="+"
+				else
+					i="#"
 				fi
 			fi
 			if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
 				git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
 			fi
 
-			if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
-				if [ "$(git config --bool bash.showUntrackedFiles)" != "false" ]; then
-					if [ -n "$(git ls-files --others --exclude-standard)" ]; then
-						u="%"
-					fi
-				fi
+			if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ] &&
+			   [ "$(git config --bool bash.showUntrackedFiles)" != "false" ] &&
+			   [ -n "$(git ls-files --others --exclude-standard)" ]
+			then
+				u="%"
 			fi
 
 			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
Signature · 1 line
1.7.10.4
#7 lore raw
In reply to Martin Erik Werner
Quoted 10 lines · Martin Erik Werner
Quoted 9 lines · Simon vanaf Telefoon

Hi all, sorry for top posting :-( blame the phone and k9

I have a small issue with the use of test instead of [ If that only applies to this section of the entire file. Coding style has some value.

Combining nested ifs with && seems harmless enough, though should be well tested.

Quoted 6 lines
Quoted 3 lines

Cheers
Simon

Ah, indeed, I looked around a bit more, and as per
http://mywiki.wooledge.org/BashPitfalls it seems like 'test' is bad to use with multiple &&'s anyways.

I think you are misreading a suggestion that is somewhat misguided (yes "[ <condition> && <another> ]" does not make sense, but that is not applicable to "test <conditon> && test <another>"); ignore it.

It is fine to write "test <condition> && test <another>" and that works portably to even pre-posix systems.

But the existing code the patch touches favors [] over test consistently; that alone is a good reason to stick with [] in _this_ script, even though it is against Git's overall shell script style.

#8 lore raw
Quoted 7 lines · Junio C Hamano

I think you are misreading a suggestion that is somewhat misguided (yes "[ <condition> && <another> ]" does not make sense, but that is not applicable to "test <conditon> && test <another>"); ignore it.

It is fine to write "test <condition> && test <another>" and that works portably to even pre-posix systems.

(that's like doing "ls file && rm file" )

Quoted 5 lines

But the existing code the patch touches favors [] over test consistently; that alone is a good reason to stick with [] in _this_ script, even though it is against Git's overall shell script style.

I suppose it would be fine if a patch was sent to update the entire git-prompt.sh code to be more in line with the Git shell script style...

My original gripe was just with doing it in one place while leaving all the others unchanged. It makes for messy reading and leads to confusion.

Cheers

Simon

#9 lore raw
Quoted 2 lines · Simon Oosthoek

I suppose it would be fine if a patch was sent to update the entire git-prompt.sh code to be more in line with the Git shell script style...

Please don't. We do not want a "style conversion" for the sole purpose of conversion, especially when a subsystem is already internally consistent.

Besides, the git-prompt.sh thing needs to be fairly bash specific so the usual "Git Porcelain scripts targetted for POSIX/Bourne shells" rules does not apply there.

Quoted 2 lines

My original gripe was just with doing it in one place while leaving all the others unchanged. It makes for messy reading and leads to confusion.

Yes, it is always preferred to match the _local_ convention.