# [PATCH 2/3] bash prompt: remove a redundant command line option

4 messages from 2015-11-21 to 2015-11-21. Participants: SZEDER Gábor.
Thread: https://gitlist.dev/t/40849

## SZEDER Gábor, 2015-11-21 11:30

Subject: [PATCH 1/3] bash prompt: test dirty index and worktree while on an orphan branch
Message-ID: <1448105409-4494-1-git-send-email-szeder@ira.uka.de>
URL: https://gitlist.dev/e/1448105409-4494-1-git-send-email-szeder%40ira.uka.de

```
There is only a single test exercising the dirty state indicator on an
orphan branch, and in that test neither the index nor the worktree are
dirty.

Add two failing tests to check the dirty state indicator while either
the index is dirty or while both the index and the worktree are dirty
on an orphan branch, and to show that the dirtiness of the index is
not displayed in these cases (the forth combination, i.e. clean index
and dirty worktree are impossible on an orphan branch).  Update the
existing dirty state indicator on clean orphan branch test to match
the style of the two new tests, most importantly to use 'git checkout
--orphan' instead of cd-ing into a repository that just happens to be
empty and clean.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 t/t9903-bash-prompt.sh | 31 ++++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)

diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index 6b68777b98..2c9d1f928a 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -273,11 +273,36 @@ test_expect_success 'prompt - dirty status indicator - dirty index and worktree'
 	test_cmp expected "$actual"
 '
 
-test_expect_success 'prompt - dirty status indicator - before root commit' '
-	printf " (master #)" >expected &&
+test_expect_success 'prompt - dirty status indicator - orphan branch - clean' '
+	printf " (orphan #)" >expected &&
+	test_when_finished "git checkout master" &&
+	git checkout --orphan orphan &&
+	git reset --hard &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_failure 'prompt - dirty status indicator - orphan branch - dirty index' '
+	printf " (orphan +)" >expected &&
+	test_when_finished "git checkout master" &&
+	git checkout --orphan orphan &&
+	(
+		GIT_PS1_SHOWDIRTYSTATE=y &&
+		__git_ps1 >"$actual"
+	) &&
+	test_cmp expected "$actual"
+'
+
+test_expect_failure 'prompt - dirty status indicator - orphan branch - dirty index and worktree' '
+	printf " (orphan *+)" >expected &&
+	test_when_finished "git checkout master" &&
+	git checkout --orphan orphan &&
+	>file &&
 	(
 		GIT_PS1_SHOWDIRTYSTATE=y &&
-		cd otherrepo &&
 		__git_ps1 >"$actual"
 	) &&
 	test_cmp expected "$actual"
-- 
2.6.3.402.geb6a0f7

```

## SZEDER Gábor, 2015-11-21 11:30

Subject: [PATCH 2/3] bash prompt: remove a redundant command line option
Message-ID: <1448105409-4494-2-git-send-email-szeder@ira.uka.de>
URL: https://gitlist.dev/e/1448105409-4494-2-git-send-email-szeder%40ira.uka.de
In-Reply-To: <1448105409-4494-1-git-send-email-szeder@ira.uka.de>

```
To get the dirty state indicator __git_ps1() runs 'git diff' with
'--quiet --exit-code' options.  '--quiet' already implies
'--exit-code', so the latter is unnecessary and can be removed.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 contrib/completion/git-prompt.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 07b52bedf1..7a95fbdcfd 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -476,7 +476,7 @@ __git_ps1 ()
 		if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&
 		   [ "$(git config --bool bash.showDirtyState)" != "false" ]
 		then
-			git diff --no-ext-diff --quiet --exit-code || w="*"
+			git diff --no-ext-diff --quiet || w="*"
 			if [ -n "$short_sha" ]; then
 				git diff-index --cached --quiet HEAD -- || i="+"
 			else
-- 
2.6.3.402.geb6a0f7

```

## SZEDER Gábor, 2015-11-21 11:30

Subject: [PATCH 3/3] bash prompt: indicate dirty index even on orphan branches
Message-ID: <1448105409-4494-3-git-send-email-szeder@ira.uka.de>
URL: https://gitlist.dev/e/1448105409-4494-3-git-send-email-szeder%40ira.uka.de
In-Reply-To: <1448105409-4494-1-git-send-email-szeder@ira.uka.de>

```
__git_ps1() doesn't indicate dirty index while on an orphan branch.

To check the dirtiness of the index, __git_ps1() runs 'git diff-index
--cached ... HEAD', which doesn't work on an orphan branch,
because HEAD doesn't point to a valid commit.

Run 'git diff ... --cached' instead, as it does the right thing both
on valid and invalid HEAD, i.e. compares the index to the existing
HEAD in the former case and to the empty tree in the latter.  This
fixes the two failing tests added in the first commit of this series.

The dirtiness of the worktree is already checked with 'git diff' and
is displayed correctly even on an orphan branch.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 contrib/completion/git-prompt.sh | 5 ++---
 t/t9903-bash-prompt.sh           | 4 ++--
 2 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 7a95fbdcfd..64219e631a 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -477,9 +477,8 @@ __git_ps1 ()
 		   [ "$(git config --bool bash.showDirtyState)" != "false" ]
 		then
 			git diff --no-ext-diff --quiet || w="*"
-			if [ -n "$short_sha" ]; then
-				git diff-index --cached --quiet HEAD -- || i="+"
-			else
+			git diff --no-ext-diff --cached --quiet || i="+"
+			if [ -z "$short_sha" ] && [ -z "$i" ]; then
 				i="#"
 			fi
 		fi
diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index 2c9d1f928a..af82049f82 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -285,7 +285,7 @@ test_expect_success 'prompt - dirty status indicator - orphan branch - clean' '
 	test_cmp expected "$actual"
 '
 
-test_expect_failure 'prompt - dirty status indicator - orphan branch - dirty index' '
+test_expect_success 'prompt - dirty status indicator - orphan branch - dirty index' '
 	printf " (orphan +)" >expected &&
 	test_when_finished "git checkout master" &&
 	git checkout --orphan orphan &&
@@ -296,7 +296,7 @@ test_expect_failure 'prompt - dirty status indicator - orphan branch - dirty ind
 	test_cmp expected "$actual"
 '
 
-test_expect_failure 'prompt - dirty status indicator - orphan branch - dirty index and worktree' '
+test_expect_success 'prompt - dirty status indicator - orphan branch - dirty index and worktree' '
 	printf " (orphan *+)" >expected &&
 	test_when_finished "git checkout master" &&
 	git checkout --orphan orphan &&
-- 
2.6.3.402.geb6a0f7

```

## SZEDER Gábor, 2015-11-21 14:46

Subject: [PATCH v1.5 2/3] bash prompt: remove a redundant 'git diff' option
Message-ID: <1448117200-7299-1-git-send-email-szeder@ira.uka.de>
URL: https://gitlist.dev/e/1448117200-7299-1-git-send-email-szeder%40ira.uka.de
In-Reply-To: <1448105409-4494-2-git-send-email-szeder@ira.uka.de>

```
To get the dirty state indicator __git_ps1() runs 'git diff' with
'--quiet --exit-code' options.  '--quiet' already implies
'--exit-code', so the latter is unnecessary and can be removed.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
Reworded the Subject: line, because it sounded as if the patch were to
remove a command line option of the bash prompt script.

The rest is unchanged.

 contrib/completion/git-prompt.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 07b52bedf1..7a95fbdcfd 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -476,7 +476,7 @@ __git_ps1 ()
 		if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] &&
 		   [ "$(git config --bool bash.showDirtyState)" != "false" ]
 		then
-			git diff --no-ext-diff --quiet --exit-code || w="*"
+			git diff --no-ext-diff --quiet || w="*"
 			if [ -n "$short_sha" ]; then
 				git diff-index --cached --quiet HEAD -- || i="+"
 			else
-- 
2.6.3.402.geb6a0f7

```
