git: 465a3b7adc00 - main - newvers.sh: Avoid spurious -dirty in git revision

From: Ed Maste <emaste_at_FreeBSD.org>
Date: Fri, 03 Jul 2026 13:35:21 UTC
The branch main has been updated by emaste:

URL: https://cgit.FreeBSD.org/src/commit/?id=465a3b7adc008c068741594f6c922ede98924f39

commit 465a3b7adc008c068741594f6c922ede98924f39
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2026-07-01 14:35:10 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2026-07-03 13:34:58 +0000

    newvers.sh: Avoid spurious -dirty in git revision
    
    If git is installed and .git exists but git rev-parse failed to report a
    hash we previously produced just "-dirty" as the git revision.  Gate the
    git commit count and -dirty check on the rev-parse passing.
    
    Reviewed by:    jlduran
    Sponsored by:   The FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D57995
---
 sys/conf/newvers.sh | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/sys/conf/newvers.sh b/sys/conf/newvers.sh
index 145377c1e75e..de696a192853 100644
--- a/sys/conf/newvers.sh
+++ b/sys/conf/newvers.sh
@@ -260,21 +260,23 @@ fi
 
 if [ -n "$git_cmd" ] ; then
 	git=$($git_cmd rev-parse --verify --short=12 HEAD 2>/dev/null)
-	if [ "$($git_cmd rev-parse --is-shallow-repository)" = false ] ; then
-		git_cnt=$($git_cmd rev-list --first-parent --count HEAD 2>/dev/null)
-		if [ -n "$git_cnt" ] ; then
-			git="n${git_cnt}-${git}"
+	if [ $? -eq 0 ]; then
+		if [ "$($git_cmd rev-parse --is-shallow-repository)" = false ] ; then
+			git_cnt=$($git_cmd rev-list --first-parent --count HEAD 2>/dev/null)
+			if [ -n "$git_cnt" ] ; then
+				git="n${git_cnt}-${git}"
+			fi
 		fi
+		git_b=$($git_cmd rev-parse --abbrev-ref HEAD)
+		if [ -n "$git_b" -a "$git_b" != "HEAD" ] ; then
+			git="${git_b}-${git}"
+		fi
+		if git_tree_modified; then
+			git="${git}-dirty"
+			modified=yes
+		fi
+		git=" ${git}"
 	fi
-	git_b=$($git_cmd rev-parse --abbrev-ref HEAD)
-	if [ -n "$git_b" -a "$git_b" != "HEAD" ] ; then
-		git="${git_b}-${git}"
-	fi
-	if git_tree_modified; then
-		git="${git}-dirty"
-		modified=yes
-	fi
-	git=" ${git}"
 fi
 
 if [ -n "$gituprevision" ] ; then