[PATCH 1/5] prompt: clean up describe logic

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The describe logic is convoluted and unclean:

1. Reading .git/HEAD by hand and using the first 7 characters, of the
   SHA-1 does not guarantee an unambiguous output; we can use rev-parse
   --short in its place to get a unique SHA-1.

2. Use the --always option of describe to automatically output the short
   SHA-1 when everything else fails.

The patch introduces one small change: since we are not checking the
return value of describe (with --always, it always returns something
valid), we cannot discriminate a raw SHA-1 from everything else and
suffix it with a "...".

Signed-off-by: Ramkumar Ramachandra <artagnon@xxxxxxxxx>
---
 contrib/completion/git-prompt.sh | 16 +++++++---------
 t/t9903-bash-prompt.sh           |  2 +-
 2 files changed, 8 insertions(+), 10 deletions(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 86a4f3f..9ed6ff1 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -380,20 +380,18 @@ __git_ps1 ()
 			test -n "$b" ||
 			b="$(git symbolic-ref HEAD 2>/dev/null)" || {
 				detached=yes
-				b="$(
 				case "${GIT_PS1_DESCRIBE_STYLE-}" in
 				(contains)
-					git describe --contains HEAD ;;
+					b=$(git describe --always --contains HEAD) ;;
 				(branch)
-					git describe --contains --all HEAD ;;
+					b=$(git describe --always --contains --all HEAD) ;;
 				(describe)
-					git describe HEAD ;;
+					b=$(git describe --always HEAD) ;;
 				(* | default)
-					git describe --tags --exact-match HEAD ;;
-				esac 2>/dev/null)" ||
-
-				b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
-				b="unknown"
+					b=$(git describe --tags --exact-match HEAD 2>/dev/null)
+					test -z $b && b="$(git rev-parse --short HEAD)"
+					;;
+				esac
 				b="($b)"
 			}
 		fi
diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index 15521cc..b0ad477 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -169,7 +169,7 @@ test_expect_success 'prompt - branch name' '
 '
 
 test_expect_success 'prompt - detached head' '
-	printf " ((%s...))" $(git log -1 --format="%h" b1^) > expected &&
+	printf " ((%s))" $(git log -1 --format="%h" b1^) > expected &&
 	git checkout b1^ &&
 	test_when_finished "git checkout master" &&
 	__git_ps1 > "$actual" &&
-- 
1.8.3.1.456.gb7f4cb6.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]