git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Ramkumar Ramachandra <artagnon@gmail.com>
Date
Jun 18, 2013, 17:13 UTC
Message-ID
<1371575608-9980-2-git-send-email-artagnon@gmail.com>
In-Reply-To
<1371575608-9980-1-git-send-email-artagnon@gmail.com>
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@gmail.com>
---
 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
Previous: Ramkumar RamachandraNext: Ramkumar Ramachandra
Message 2 of 6 in “Fix describe --contains --all”
  1. 0/5 Fix describe --contains --allRamkumar Ramachandra, Jun 18, 2013
  2. 1/5 prompt: clean up describe logicRamkumar Ramachandra, Jun 18, 2013
  3. 2/5 prompt: do not double-discriminate detached HEADRamkumar Ramachandra, Jun 18, 2013
  4. 3/5 name-rev: fix assumption about --name-only usageRamkumar Ramachandra, Jun 18, 2013
  5. 4/5 name-rev: strip trailing ^0 in when --name-onlyRamkumar Ramachandra, Jun 18, 2013
  6. 5/5 name-rev doc: rewrite --stdin paragraphRamkumar Ramachandra, Jun 18, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.