# [PATCH v2 00/15] version-gen: complete revamp/rewrite

16 messages from 2023-04-24 to 2023-04-24. Participants: Felipe Contreras.
Thread: https://gitlist.dev/t/59645

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 00/15] version-gen: complete revamp/rewrite
Message-ID: <20230424165041.25180-1-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-1-felipe.contreras%40gmail.com

```
The version generation script needs some love, as the last true change was done in 2008.

This series step by step revamps the whole script ending in only a few lines of code.

There should be no functional changes.

It's hard to see the actual changes to this script as 99% of the commits are
just to bump the default version (which I don't know why it even exists).

For reference, cleaning the history I came up with these actual changes in case
anyone is interested:

c48799e560 (Teach GIT-VERSION-GEN about the .git file, 2008-02-20)
1100ac81a9 (Change GIT-VERSION-GEN to call git commands with "git" not "git-"., 2006-05-22)
374dfaa2e3 (Make GIT-VERSION-GEN tolerate missing git describe command again, 2006-01-26)
5c7d3c9507 (Allow building of RPM from interim snapshot., 2006-01-16)
181129d24c (For release tarballs, include the proper version, 2006-01-09)
026351a035 (Make GIT-VERSION-GEN tolerate missing git describe command, 2005-12-30)
9b88fcef7d (Makefile: use git-describe to mark the git version., 2005-12-27)

Cheers.

Changes since v1:

I removed all the controverrsial functional changes (those will come in
a separate series) due to comments by Todd Zullinger.

The non-functional changes are already plenty, so let's focus on those
first.

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 9a1111af9b..3d30ce74af 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -1,40 +1,18 @@
 #!/bin/sh
 
-GVF=GIT-VERSION-FILE
-DEF_VER=v2.40.GIT
+DEF_VER=2.40.GIT
 
-LF='
-'
-
-# First see if there is a version file (included in release tarballs),
-# then try git-describe, then default.
-if test -f version
-then
-	VN=$(cat version) || VN="$DEF_VER"
-elif test -d ${GIT_DIR:-.git} -o -f .git &&
-	VN=$(git describe --match "v[0-9]*" HEAD 2>/dev/null) &&
-	case "$VN" in
-	*$LF*) (exit 1) ;;
-	v[0-9]*)
-		git update-index -q --refresh
-		test -z "$(git diff-index --name-only HEAD --)" ||
-		VN="$VN-dirty" ;;
-	esac
-then
-	VN=$(echo "$VN" | sed -e 's/-/./g');
-else
-	VN="$DEF_VER"
-fi
-
-VN=$(expr "$VN" : v*'\(.*\)')
-
-if test -r $GVF
-then
-	VC=$(sed -e 's/^GIT_VERSION = //' <$GVF)
-else
-	VC=unset
-fi
-test "$VN" = "$VC" || {
-	echo >&2 "GIT_VERSION = $VN"
-	echo "GIT_VERSION = $VN" >$GVF
+get_version () {
+	test -f version && cat version && return
+	test -d "${GIT_DIR:-.git}" -o -f .git || return
+	git describe --match "v[0-9]*" --dirty 2>/dev/null | sed -e 's/-/./g' -e 's/^v//'
 }
+
+VN=$(get_version)
+
+: "${VN:=$DEF_VER}"
+
+NEW="GIT_VERSION = $VN"
+
+test -r GIT-VERSION-FILE && test "$NEW" = "$(cat GIT-VERSION-FILE)" && exit
+echo "$NEW" | tee GIT-VERSION-FILE >&2

Felipe Contreras (15):
  version-gen: reorganize
  version-gen: trivial cleanup
  version-gen: refactor default version
  version-gen: simplify v prefix removal
  version-gen: simplify update check
  version-gen: simplify `git describe` checks
  version-gen: simplify dirty check
  version-gen: move describe fix into function
  version-gen: describe and sed in one go
  version-gen: refactor describe function
  version-gen: do v fix only when necessary
  version-gen: move v fix into sed
  version-gen: refactor main functionality
  version-gen: refactor GIT_VERSION string
  version-gen: get rid of GVF variable

 GIT-VERSION-GEN | 50 ++++++++++++++-----------------------------------
 1 file changed, 14 insertions(+), 36 deletions(-)

Range-diff against v1:
 1:  18ac0e3784 =  1:  dbe102186a version-gen: reorganize
 2:  280efc616f =  2:  9a1a46481b version-gen: trivial cleanup
 3:  af67bdb106 =  3:  04d6741689 version-gen: refactor default version
 4:  798fd082f3 =  4:  770eeb9abc version-gen: simplify v prefix removal
 5:  adbad7eab8 =  5:  2234d37305 version-gen: simplify update check
 6:  2742189cc7 <  -:  ---------- version-gen: remove redundant check
 7:  414e2efddd =  6:  0784430fe6 version-gen: simplify `git describe` checks
 8:  04b755dcc1 =  7:  aa0ac9d10c version-gen: simplify dirty check
 9:  05519e8ece !  8:  51272021d9 version-gen: move describe fix into function
    @@ GIT-VERSION-GEN: describe () {
      if test -f version
      then
      	VN=$(cat version)
    --elif describe
    --then
    +-elif test -d "${GIT_DIR:-.git}" -o -f .git && describe
    ++elif test -d "${GIT_DIR:-.git}" -o -f .git
    + then
     -	VN=$(echo "$VN" | sed -e 's/-/./g')
    -+else
     +	describe
      fi
      
10:  f9b0092966 =  9:  06eb36044f version-gen: describe and sed in one go
11:  2a5167e4f0 ! 10:  4c0785fb5d version-gen: refactor describe function
    @@ GIT-VERSION-GEN: GVF=GIT-VERSION-FILE
      }
      
      # First see if there is a version file (included in release tarballs),
    -@@ GIT-VERSION-GEN: if test -f version
    - then
    +@@ GIT-VERSION-GEN: then
      	VN=$(cat version)
    - else
    + elif test -d "${GIT_DIR:-.git}" -o -f .git
    + then
     -	describe
     +	VN=$(describe)
      fi
12:  a71fe9229b ! 11:  7bd0e69a1b version-gen: do v fix only when necessary
    @@ GIT-VERSION-GEN
      describe () {
      	git describe --match "v[0-9]*" --dirty 2>/dev/null | sed -e 's/-/./g'
     @@ GIT-VERSION-GEN: then
    - 	VN=$(cat version)
    - else
    + elif test -d "${GIT_DIR:-.git}" -o -f .git
    + then
      	VN=$(describe)
     +	VN=${VN#v}
      fi
13:  5462474c52 ! 12:  3834398857 version-gen: move v fix into sed
    @@ GIT-VERSION-GEN: GVF=GIT-VERSION-FILE
      
      # First see if there is a version file (included in release tarballs),
     @@ GIT-VERSION-GEN: then
    - 	VN=$(cat version)
    - else
    + elif test -d "${GIT_DIR:-.git}" -o -f .git
    + then
      	VN=$(describe)
     -	VN=${VN#v}
      fi
14:  64cb12343a ! 13:  18c2cbaf9c version-gen: refactor main functionality
    @@ GIT-VERSION-GEN
     -describe () {
     +get_version () {
     +	test -f version && cat version && return
    ++	test -d "${GIT_DIR:-.git}" -o -f .git || return
      	git describe --match "v[0-9]*" --dirty 2>/dev/null | sed -e 's/-/./g' -e 's/^v//'
      }
      
    @@ GIT-VERSION-GEN
     -if test -f version
     -then
     -	VN=$(cat version)
    --else
    +-elif test -d "${GIT_DIR:-.git}" -o -f .git
    +-then
     -	VN=$(describe)
     -fi
     +VN=$(get_version)
15:  16b6e38ace <  -:  ---------- version-gen: remove default version
16:  7bb1206f00 <  -:  ---------- version-gen: refactor GIT_VERSION string
 -:  ---------- > 14:  6c594361df version-gen: refactor GIT_VERSION string
17:  e0cd32d548 ! 15:  edf380d2fa version-gen: get rid of GVF variable
    @@ GIT-VERSION-GEN
      #!/bin/sh
      
     -GVF=GIT-VERSION-FILE
    --
    - get_version () {
    - 	test -f version && cat version && return
    - 	git describe --match "v[0-9]*" --dirty 2>/dev/null | sed -e 's/-/./g' -e 's/^v//'
    -@@ GIT-VERSION-GEN: get_version () {
    + DEF_VER=2.40.GIT
      
    - NEW="GIT_VERSION = $(get_version)"
    + get_version () {
    +@@ GIT-VERSION-GEN: VN=$(get_version)
    + 
    + NEW="GIT_VERSION = $VN"
      
     -test -r $GVF && test "$NEW" = "$(cat $GVF)" && exit
     -echo "$NEW" | tee $GVF >&2
18:  43a59f630a <  -:  ---------- version-gen: generate proper interim versions
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 04/15] version-gen: simplify v prefix removal
Message-ID: <20230424165041.25180-5-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-5-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
There is a much simpler way.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 0d00fa3d9a..c0f6bb242f 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -33,7 +33,7 @@ fi
 
 : "${VN:=$DEF_VER}"
 
-VN=$(expr "$VN" : 'v*\(.*\)')
+VN=${VN#v}
 
 if test -r $GVF
 then
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 01/15] version-gen: reorganize
Message-ID: <20230424165041.25180-2-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-2-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
Simply move some code into a `describe` function so it's clear what code
is related to dealing with `git describe`, and what code is the main
functionality.

No functional changes.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 9a1111af9b..29d634a30b 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -6,20 +6,27 @@ DEF_VER=v2.40.GIT
 LF='
 '
 
+describe () {
+	VN=$(git describe --match "v[0-9]*" HEAD 2>/dev/null) || return 1
+	case "$VN" in
+	*$LF*)
+		return 1
+		;;
+	v[0-9]*)
+		git update-index -q --refresh
+		test -z "$(git diff-index --name-only HEAD --)" ||
+		VN="$VN-dirty"
+		return 0
+		;;
+	esac
+}
+
 # First see if there is a version file (included in release tarballs),
 # then try git-describe, then default.
 if test -f version
 then
 	VN=$(cat version) || VN="$DEF_VER"
-elif test -d ${GIT_DIR:-.git} -o -f .git &&
-	VN=$(git describe --match "v[0-9]*" HEAD 2>/dev/null) &&
-	case "$VN" in
-	*$LF*) (exit 1) ;;
-	v[0-9]*)
-		git update-index -q --refresh
-		test -z "$(git diff-index --name-only HEAD --)" ||
-		VN="$VN-dirty" ;;
-	esac
+elif test -d ${GIT_DIR:-.git} -o -f .git && describe
 then
 	VN=$(echo "$VN" | sed -e 's/-/./g');
 else
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 03/15] version-gen: refactor default version
Message-ID: <20230424165041.25180-4-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-4-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
It's not clear how `cat version` might fail, but either way if VN is
empty (or unset), assign the default value.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 6dd7683ee7..0d00fa3d9a 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -25,14 +25,14 @@ describe () {
 # then try `git describe`, then default.
 if test -f version
 then
-	VN=$(cat version) || VN="$DEF_VER"
+	VN=$(cat version)
 elif test -d "${GIT_DIR:-.git}" -o -f .git && describe
 then
 	VN=$(echo "$VN" | sed -e 's/-/./g')
-else
-	VN="$DEF_VER"
 fi
 
+: "${VN:=$DEF_VER}"
+
 VN=$(expr "$VN" : 'v*\(.*\)')
 
 if test -r $GVF
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 02/15] version-gen: trivial cleanup
Message-ID: <20230424165041.25180-3-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-3-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
We don't use `git-foo` since git 1.6.

HEAD is the default of `git describe`.

Also, deal with a bunch of shellcheck warnings.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 29d634a30b..6dd7683ee7 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -7,7 +7,7 @@ LF='
 '
 
 describe () {
-	VN=$(git describe --match "v[0-9]*" HEAD 2>/dev/null) || return 1
+	VN=$(git describe --match "v[0-9]*" 2>/dev/null) || return 1
 	case "$VN" in
 	*$LF*)
 		return 1
@@ -22,26 +22,24 @@ describe () {
 }
 
 # First see if there is a version file (included in release tarballs),
-# then try git-describe, then default.
+# then try `git describe`, then default.
 if test -f version
 then
 	VN=$(cat version) || VN="$DEF_VER"
-elif test -d ${GIT_DIR:-.git} -o -f .git && describe
+elif test -d "${GIT_DIR:-.git}" -o -f .git && describe
 then
-	VN=$(echo "$VN" | sed -e 's/-/./g');
+	VN=$(echo "$VN" | sed -e 's/-/./g')
 else
 	VN="$DEF_VER"
 fi
 
-VN=$(expr "$VN" : v*'\(.*\)')
+VN=$(expr "$VN" : 'v*\(.*\)')
 
 if test -r $GVF
 then
 	VC=$(sed -e 's/^GIT_VERSION = //' <$GVF)
 else
-	VC=unset
+	VC='unset'
 fi
-test "$VN" = "$VC" || {
-	echo >&2 "GIT_VERSION = $VN"
-	echo "GIT_VERSION = $VN" >$GVF
-}
+test "$VN" = "$VC" && exit
+echo "GIT_VERSION = $VN" | tee $GVF >&2
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 05/15] version-gen: simplify update check
Message-ID: <20230424165041.25180-6-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-6-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
We don't need to extract the version when we can compare the whole
contents.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index c0f6bb242f..34f561752b 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -35,11 +35,5 @@ fi
 
 VN=${VN#v}
 
-if test -r $GVF
-then
-	VC=$(sed -e 's/^GIT_VERSION = //' <$GVF)
-else
-	VC='unset'
-fi
-test "$VN" = "$VC" && exit
+test -r $GVF && test "GIT_VERSION = $VN" = "$(cat $GVF)" && exit
 echo "GIT_VERSION = $VN" | tee $GVF >&2
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 06/15] version-gen: simplify `git describe` checks
Message-ID: <20230424165041.25180-7-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-7-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
How can `git describe --match 'foo*'` return something that doesn't
contain 'foo' and without error?

It can't, so no need for check for the impossible.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 17 +++--------------
 1 file changed, 3 insertions(+), 14 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 34f561752b..e99c7b45c0 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -3,22 +3,11 @@
 GVF=GIT-VERSION-FILE
 DEF_VER=v2.40.GIT
 
-LF='
-'
-
 describe () {
 	VN=$(git describe --match "v[0-9]*" 2>/dev/null) || return 1
-	case "$VN" in
-	*$LF*)
-		return 1
-		;;
-	v[0-9]*)
-		git update-index -q --refresh
-		test -z "$(git diff-index --name-only HEAD --)" ||
-		VN="$VN-dirty"
-		return 0
-		;;
-	esac
+	git update-index -q --refresh
+	test -z "$(git diff-index --name-only HEAD --)" ||
+	VN="$VN-dirty"
 }
 
 # First see if there is a version file (included in release tarballs),
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 07/15] version-gen: simplify dirty check
Message-ID: <20230424165041.25180-8-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-8-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index e99c7b45c0..8edaf8f335 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -4,10 +4,7 @@ GVF=GIT-VERSION-FILE
 DEF_VER=v2.40.GIT
 
 describe () {
-	VN=$(git describe --match "v[0-9]*" 2>/dev/null) || return 1
-	git update-index -q --refresh
-	test -z "$(git diff-index --name-only HEAD --)" ||
-	VN="$VN-dirty"
+	VN=$(git describe --match "v[0-9]*" --dirty 2>/dev/null) || return 1
 }
 
 # First see if there is a version file (included in release tarballs),
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 08/15] version-gen: move describe fix into function
Message-ID: <20230424165041.25180-9-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-9-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 8edaf8f335..7a7a7fc591 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -5,6 +5,7 @@ DEF_VER=v2.40.GIT
 
 describe () {
 	VN=$(git describe --match "v[0-9]*" --dirty 2>/dev/null) || return 1
+	VN=$(echo "$VN" | sed -e 's/-/./g')
 }
 
 # First see if there is a version file (included in release tarballs),
@@ -12,9 +13,9 @@ describe () {
 if test -f version
 then
 	VN=$(cat version)
-elif test -d "${GIT_DIR:-.git}" -o -f .git && describe
+elif test -d "${GIT_DIR:-.git}" -o -f .git
 then
-	VN=$(echo "$VN" | sed -e 's/-/./g')
+	describe
 fi
 
 : "${VN:=$DEF_VER}"
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 09/15] version-gen: describe and sed in one go
Message-ID: <20230424165041.25180-10-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-10-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 7a7a7fc591..c5265cf9ad 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -4,8 +4,7 @@ GVF=GIT-VERSION-FILE
 DEF_VER=v2.40.GIT
 
 describe () {
-	VN=$(git describe --match "v[0-9]*" --dirty 2>/dev/null) || return 1
-	VN=$(echo "$VN" | sed -e 's/-/./g')
+	VN=$(git describe --match "v[0-9]*" --dirty 2>/dev/null | sed -e 's/-/./g') || return 1
 }
 
 # First see if there is a version file (included in release tarballs),
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 10/15] version-gen: refactor describe function
Message-ID: <20230424165041.25180-11-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-11-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
No functional changes.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index c5265cf9ad..0eaa813cca 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -4,7 +4,7 @@ GVF=GIT-VERSION-FILE
 DEF_VER=v2.40.GIT
 
 describe () {
-	VN=$(git describe --match "v[0-9]*" --dirty 2>/dev/null | sed -e 's/-/./g') || return 1
+	git describe --match "v[0-9]*" --dirty 2>/dev/null | sed -e 's/-/./g'
 }
 
 # First see if there is a version file (included in release tarballs),
@@ -14,7 +14,7 @@ then
 	VN=$(cat version)
 elif test -d "${GIT_DIR:-.git}" -o -f .git
 then
-	describe
+	VN=$(describe)
 fi
 
 : "${VN:=$DEF_VER}"
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 11/15] version-gen: do v fix only when necessary
Message-ID: <20230424165041.25180-12-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-12-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
There's no point in having a v in the default version only to be
removed.

The only time we need to remove the v is from `git describe`.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 0eaa813cca..40502363dd 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -1,7 +1,7 @@
 #!/bin/sh
 
 GVF=GIT-VERSION-FILE
-DEF_VER=v2.40.GIT
+DEF_VER=2.40.GIT
 
 describe () {
 	git describe --match "v[0-9]*" --dirty 2>/dev/null | sed -e 's/-/./g'
@@ -15,11 +15,10 @@ then
 elif test -d "${GIT_DIR:-.git}" -o -f .git
 then
 	VN=$(describe)
+	VN=${VN#v}
 fi
 
 : "${VN:=$DEF_VER}"
 
-VN=${VN#v}
-
 test -r $GVF && test "GIT_VERSION = $VN" = "$(cat $GVF)" && exit
 echo "GIT_VERSION = $VN" | tee $GVF >&2
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 12/15] version-gen: move v fix into sed
Message-ID: <20230424165041.25180-13-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-13-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
We are already using sed, might as well take advantage of it.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 40502363dd..6bf932c281 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -4,7 +4,7 @@ GVF=GIT-VERSION-FILE
 DEF_VER=2.40.GIT
 
 describe () {
-	git describe --match "v[0-9]*" --dirty 2>/dev/null | sed -e 's/-/./g'
+	git describe --match "v[0-9]*" --dirty 2>/dev/null | sed -e 's/-/./g' -e 's/^v//'
 }
 
 # First see if there is a version file (included in release tarballs),
@@ -15,7 +15,6 @@ then
 elif test -d "${GIT_DIR:-.git}" -o -f .git
 then
 	VN=$(describe)
-	VN=${VN#v}
 fi
 
 : "${VN:=$DEF_VER}"
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 13/15] version-gen: refactor main functionality
Message-ID: <20230424165041.25180-14-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-14-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
It's pretty clear that the `version` file overrides `describe`, so do it
in one function.

There's no need for the comment as the code is self-describing.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 6bf932c281..5b75cb4976 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -3,19 +3,13 @@
 GVF=GIT-VERSION-FILE
 DEF_VER=2.40.GIT
 
-describe () {
+get_version () {
+	test -f version && cat version && return
+	test -d "${GIT_DIR:-.git}" -o -f .git || return
 	git describe --match "v[0-9]*" --dirty 2>/dev/null | sed -e 's/-/./g' -e 's/^v//'
 }
 
-# First see if there is a version file (included in release tarballs),
-# then try `git describe`, then default.
-if test -f version
-then
-	VN=$(cat version)
-elif test -d "${GIT_DIR:-.git}" -o -f .git
-then
-	VN=$(describe)
-fi
+VN=$(get_version)
 
 : "${VN:=$DEF_VER}"
 
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 14/15] version-gen: refactor GIT_VERSION string
Message-ID: <20230424165041.25180-15-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-15-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index 5b75cb4976..a1c50cb06b 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -13,5 +13,7 @@ VN=$(get_version)
 
 : "${VN:=$DEF_VER}"
 
-test -r $GVF && test "GIT_VERSION = $VN" = "$(cat $GVF)" && exit
-echo "GIT_VERSION = $VN" | tee $GVF >&2
+NEW="GIT_VERSION = $VN"
+
+test -r $GVF && test "$NEW" = "$(cat $GVF)" && exit
+echo "$NEW" | tee $GVF >&2
-- 
2.40.0+fc1


```

## Felipe Contreras, 2023-04-24 16:50

Subject: [PATCH v2 15/15] version-gen: get rid of GVF variable
Message-ID: <20230424165041.25180-16-felipe.contreras@gmail.com>
URL: https://gitlist.dev/e/20230424165041.25180-16-felipe.contreras%40gmail.com
In-Reply-To: <20230424165041.25180-1-felipe.contreras@gmail.com>

```
There's not much point in a variable which is never going to change and
doesn't really add any readability.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 GIT-VERSION-GEN | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/GIT-VERSION-GEN b/GIT-VERSION-GEN
index a1c50cb06b..3d30ce74af 100755
--- a/GIT-VERSION-GEN
+++ b/GIT-VERSION-GEN
@@ -1,6 +1,5 @@
 #!/bin/sh
 
-GVF=GIT-VERSION-FILE
 DEF_VER=2.40.GIT
 
 get_version () {
@@ -15,5 +14,5 @@ VN=$(get_version)
 
 NEW="GIT_VERSION = $VN"
 
-test -r $GVF && test "$NEW" = "$(cat $GVF)" && exit
-echo "$NEW" | tee $GVF >&2
+test -r GIT-VERSION-FILE && test "$NEW" = "$(cat GIT-VERSION-FILE)" && exit
+echo "$NEW" | tee GIT-VERSION-FILE >&2
-- 
2.40.0+fc1


```
