diff mbox series

[4/5] scripts/setlocalversion: factor out 12-chars hash construction

Message ID 20210523031428.164186-4-masahiroy@kernel.org (mailing list archive)
State New, archived
Headers show
Series [1/5] scripts/setlocalversion: remove mercurial, svn and git-svn supports | expand

Commit Message

Masahiro Yamada May 23, 2021, 3:14 a.m. UTC
Both of if and else parts append exactly 12 hex chars, but in
different ways.

Factor out the else part because we need to support it without relying
on git-describe. Remove the --abbrev=12 option since we do not use the
hash from git-describe anyway.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
---

 scripts/setlocalversion | 22 +++++-----------------
 1 file changed, 5 insertions(+), 17 deletions(-)

Comments

Nico Schottelius May 23, 2021, 9:04 a.m. UTC | #1
Hey Masahiro,

Masahiro Yamada <masahiroy@kernel.org> writes:
> -			if atag="$(git describe --abbrev=12 2>/dev/null)"; then
> -				echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),substr($(NF),0,13))}'
> -
> -			# If we don't have a tag at all we print -g{commitish},
> -			# again using exactly 12 hex chars.
> -			else
> -				head="$(echo $head | cut -c1-12)"
> -				printf '%s%s' -g $head
> +			if atag="$(git describe 2>/dev/null)"; then
> +				echo "$atag" | awk -F- '{printf("-%05d", $(NF-1))}'
>  			fi
> +
> +			# Add -g and exactly 12 hex chars.
> +			printf '%s%s' -g "$(echo $head | cut -c1-12)"
>  		fi
>
>  		# Check for uncommitted changes.

That was quite fun reviewing and wrapping my head around ~20y old code.

I had a very long mail prepared looking at all the corner cases, just to
see they are actually handled before the actual change.

Nicely simplified, for all 5 of them:

Reviewed-by: Nico Schottelius <nico-linuxsetlocalversion@schottelius.org>

--
Sustainable and modern Infrastructures by ungleich.ch
diff mbox series

Patch

diff --git a/scripts/setlocalversion b/scripts/setlocalversion
index 6865df6699c8..62c0bcce1575 100755
--- a/scripts/setlocalversion
+++ b/scripts/setlocalversion
@@ -59,24 +59,12 @@  scm_version()
 			fi
 			# If we are past a tagged commit (like
 			# "v2.6.30-rc5-302-g72357d5"), we pretty print it.
-			#
-			# Ensure the abbreviated sha1 has exactly 12
-			# hex characters, to make the output
-			# independent of git version, local
-			# core.abbrev settings and/or total number of
-			# objects in the current repository - passing
-			# --abbrev=12 ensures a minimum of 12, and the
-			# awk substr() then picks the 'g' and first 12
-			# hex chars.
-			if atag="$(git describe --abbrev=12 2>/dev/null)"; then
-				echo "$atag" | awk -F- '{printf("-%05d-%s", $(NF-1),substr($(NF),0,13))}'
-
-			# If we don't have a tag at all we print -g{commitish},
-			# again using exactly 12 hex chars.
-			else
-				head="$(echo $head | cut -c1-12)"
-				printf '%s%s' -g $head
+			if atag="$(git describe 2>/dev/null)"; then
+				echo "$atag" | awk -F- '{printf("-%05d", $(NF-1))}'
 			fi
+
+			# Add -g and exactly 12 hex chars.
+			printf '%s%s' -g "$(echo $head | cut -c1-12)"
 		fi
 
 		# Check for uncommitted changes.