git: b67bd2859d7d - stable/12 - newvers.sh: fix git false positive -dirty tag

From: Ed Maste <emaste_at_FreeBSD.org>
Date: Tue, 06 Sep 2022 19:08:27 UTC
The branch stable/12 has been updated by emaste:

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

commit b67bd2859d7d757ee9ceb8269a5790f4f1091be5
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2018-11-02 21:20:46 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2022-09-06 17:15:05 +0000

    newvers.sh: fix git false positive -dirty tag
    
    Assuming that any output from `git diff-index --name-only` implies
    changes in the working tree results in false positives: files with
    metadata, but not content, changes are also listed.
    
    Check that content differences exist before adding the -dirty tag to
    the git hash.
    
    PR:             229230
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D15968
    
    (cherry picked from commit 50b53a8dc35f38ab6920dd83409541e875d01551)
    (cherry picked from commit 5186028dc4a2a74d007fa65857667efb2742eaea)
---
 sys/conf/newvers.sh | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/sys/conf/newvers.sh b/sys/conf/newvers.sh
index 751d395ad519..e1aa29f8d98d 100644
--- a/sys/conf/newvers.sh
+++ b/sys/conf/newvers.sh
@@ -79,6 +79,34 @@ findvcs()
 	return 1
 }
 
+git_tree_modified()
+{
+	# git diff-index lists both files that are known to have changes as
+	# well as those with metadata that does not match what is recorded in
+	# git's internal state.  The latter case is indicated by an all-zero
+	# destination file hash.
+
+	local fifo
+
+	fifo=$(mktemp -u)
+	mkfifo -m 600 $fifo
+	$git_cmd --work-tree=${VCSTOP} diff-index HEAD > $fifo &
+	while read smode dmode ssha dsha status file; do
+		if ! expr $dsha : '^00*$' >/dev/null; then
+			rm $fifo
+			return 0
+		fi
+		if ! $git_cmd --work-tree=${VCSTOP} diff --quiet -- "${file}"; then
+			rm $fifo
+			return 0
+		fi
+	done < $fifo
+	# No files with content differences.
+	rm $fifo
+	return 1
+}
+
+
 if [ -z "${SYSDIR}" ]; then
     SYSDIR=$(dirname $0)/..
 fi
@@ -219,8 +247,7 @@ if [ -n "$git_cmd" ] ; then
 	if [ -n "$git_b" -a "$git_b" != "HEAD" ] ; then
 		git="${git_b}-${git}"
 	fi
-	if $git_cmd --work-tree=${VCSTOP} diff-index \
-	    --name-only HEAD | read dummy; then
+	if git_tree_modified; then
 		git="${git}-dirty"
 		modified=true
 	fi