Skip to contentSkip to conversation

git-version.py: use tag as branch name

PATCH NAK Lev Stipakov 4 participants 8 messages Dec 16, 2022 – Dec 19, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 git-version.py: use tag as branch name

From: Lev Stipakov <[email protected]...>

Git magic to get branch name

  git rev-parse --symbolic-full-name HEAD

doesn't work when we're on tag, which is the case when we build releases. In this case, use tag name as branch name with another git magic:

  git describe --tags

This fixes https://github.com/OpenVPN/openvpn/issues/199.

---
 build/msvc/msvc-generate/git-version.py | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
build/msvc/msvc-generate/git-version.py +10 -3
index 814dc86a..513f489a 100644
@@ -30,9 +30,16 @@ def get_branch_commit_id():
     if not commit_id:
         raise
     l = os.popen("git rev-parse --symbolic-full-name HEAD").read().split("/")[2:]
-    if not l:
-        l = ["none\n"]
-    branch = "/" .join(l)[:-1]
+    if l:
+        branch = "/" .join(l)[:-1]
+    else:
+        # are we on tag?
+        l = os.popen("git describe --tags").read()
+        if l:
+            branch = l[:-1]
+        else:
+            branch = "none"
+
     return branch, commit_id
 
 def main():
Signature · 1 line
2.38.1.windows.1
#1 lore raw

Hi,

Quoted 10 lines

From: Lev Stipakov <[email protected]...>

Git magic to get branch name

  git rev-parse --symbolic-full-name HEAD

doesn't work when we're on tag, which is the case when we build releases. In this case, use tag name as branch name with another git magic:

Quoted 6 lines
  git describe --tags

This fixes https://github.com/OpenVPN/openvpn/issues/199.

---

For me, reason for "rev-parse" failing is detached HEAD (not clear from commit message). Likely, builder creates checkout for commit-ID without local branches.

For stability purposes, I'd throw in an "--exact-match" to increase "none" fallback conditions, so things like "v2.6_beta1-22-g73cab722/g73c…" will not end up in the version string.

Kind Regards,
Marc Becker

#2 lore raw
In reply to Lev Stipakov
Quoted 6 lines · Lev Stipakov

From: Lev Stipakov <[email protected]...>

Git magic to get branch name

  git rev-parse --symbolic-full-name HEAD
Quoted 7 lines

doesn't work when we're on tag, which is the case when we build releases. In this case, use tag name as branch name with another git magic:

  git describe --tags

This fixes https://github.com/OpenVPN/openvpn/issues/199.

NAK.

I would propose to invert the logic:

Do "git describe --exact-match" first.

That matches only annotated tags that point to this commit. So it will actually use the tag consistently independent of whether you have the commit checked out with detached HEAD or via the release branch.

And then fall back to the previous mechanism if that doesn't match.

Regards,

Signature · 1 line
  Frank Lichtenheld
#3 lore raw

PATCH v2 1/1 git-version.py: proper support for tags

From: Lev Stipakov <[email protected]...>

Git magic to get branch name

  git rev-parse --symbolic-full-name HEAD

doesn't work when we're on tag, which is the case when we build releases.

First, try to get tag name with

   git describe --exact-match

and if this fails, get branch name as before.

Use subprocess.Popen() to suppress stdout/stderr output.

This fixes https://github.com/OpenVPN/openvpn/issues/199.

---
v2:
- try to get tag name before branch
- use "git describe --exact-match" instead of "git describe --tags"
- use subprocess.Popen() instead of os.popen() to suppress output

 build/msvc/msvc-generate/git-version.py | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
build/msvc/msvc-generate/git-version.py +12 -5
index 814dc86a..89fd44f2 100644
@@ -24,15 +24,22 @@
 
 import os
 import sys
+import subprocess
+
+def run_command(args):
+    sp = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
+    o, _ = sp.communicate()
+    return o.decode("utf-8")[:-1]
 
 def get_branch_commit_id():
-    commit_id = os.popen("git rev-parse --short=16 HEAD").read()[:-1]
+    commit_id = run_command(["git", "rev-parse", "--short=16", "HEAD"])
     if not commit_id:
         raise
-    l = os.popen("git rev-parse --symbolic-full-name HEAD").read().split("/")[2:]
-    if not l:
-        l = ["none\n"]
-    branch = "/" .join(l)[:-1]
+    branch = run_command(["git", "describe", "--exact-match"])
+    if not branch:
+        branch = run_command(["git", "rev-parse", "--symbolic-full-name", "HEAD"]).split("/")[2:]
+        branch = "/" .join(branch) # handle cases like release/2.6
+
     return branch, commit_id
 
 def main():
Signature · 1 line
2.23.0.windows.1
#4 lore raw

Re: [Openvpn-devel] [PATCH v2] git-version.py: proper support for tags

On Mon, Dec 19, 2022 at 05:02:26PM +0200, Lev Stipakov wrote:
[...]

Quoted 4 lines

v2:
- try to get tag name before branch
- use "git describe --exact-match" instead of "git describe --tags"
- use subprocess.Popen() instead of os.popen() to suppress output

This now handles the tag case correctly.

But now you mishandle the no-branch and no-tag case because you removed the "none" fallback. So if I just check out a commit that is not tagged I get something like
#define CONFIGURE_GIT_REVISION "/73cab72281deb631"

Quoted 16 lines
 build/msvc/msvc-generate/git-version.py | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
build/msvc/msvc-generate/git-version.py +5 -0
index 814dc86a..89fd44f2 100644
@@ -24,15 +24,22 @@
 
 import os
 import sys
+import subprocess
+
+def run_command(args):
+    sp = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
+    o, _ = sp.communicate()

build/msvc/msvc-generate/git-version.py line 34

Quoted 10 lines
build/msvc/msvc-generate/git-version.py +2 -4
+    return o.decode("utf-8")[:-1]
 
 def get_branch_commit_id():
-    commit_id = os.popen("git rev-parse --short=16 HEAD").read()[:-1]
+    commit_id = run_command(["git", "rev-parse", "--short=16", "HEAD"])
     if not commit_id:
         raise
-    l = os.popen("git rev-parse --symbolic-full-name HEAD").read().split("/")[2:]
-    if not l:
-        l = ["none\n"]

I think it should be enough to reintroduce these two lines.

build/msvc/msvc-generate/git-version.py line 43

Quoted 7 lines
build/msvc/msvc-generate/git-version.py +5 -1
-    branch = "/" .join(l)[:-1]
+    branch = run_command(["git", "describe", "--exact-match"])
+    if not branch:
+        branch = run_command(["git", "rev-parse", "--symbolic-full-name", "HEAD"]).split("/")[2:]
+        branch = "/" .join(branch) # handle cases like release/2.6
+
     return branch, commit_id

Regards,

Signature · 1 line
  Frank Lichtenheld
#5 lore raw

PATCH v3 1/1 git-version.py: proper support for tags

From: Lev Stipakov <[email protected]...>

Git magic to get branch name

  git rev-parse --symbolic-full-name HEAD

doesn't work when we're on tag, which is the case when we build releases.

First, try to get tag name with

   git describe --exact-match

and if this fails, get branch name as before.

Use subprocess.Popen() to suppress stdout/stderr output.

This fixes https://github.com/OpenVPN/openvpn/issues/199.

---
v3:
- use "none" as branch name when we're not on a branch nor tag

v2:
- try to get tag name before branch
- use "git describe --exact-match" instead of "git describe --tags"
- use subprocess.Popen() instead of os.popen() to suppress output

 build/msvc/msvc-generate/git-version.py | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
build/msvc/msvc-generate/git-version.py +15 -5
index 814dc86a..00458955 100644
@@ -24,15 +24,25 @@
 
 import os
 import sys
+import subprocess
+
+def run_command(args):
+    sp = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
+    o, _ = sp.communicate()
+    return o.decode("utf-8")[:-1]
 
 def get_branch_commit_id():
-    commit_id = os.popen("git rev-parse --short=16 HEAD").read()[:-1]
+    commit_id = run_command(["git", "rev-parse", "--short=16", "HEAD"])
     if not commit_id:
         raise
-    l = os.popen("git rev-parse --symbolic-full-name HEAD").read().split("/")[2:]
-    if not l:
-        l = ["none\n"]
-    branch = "/" .join(l)[:-1]
+    branch = run_command(["git", "describe", "--exact-match"])
+    if not branch:
+        # this returns an array like ["master"] or ["release", "2.6"]
+        branch = run_command(["git", "rev-parse", "--symbolic-full-name", "HEAD"]).split("/")[2:]
+        if not branch:
+            branch = ["none"]
+        branch = "/" .join(branch) # handle cases like release/2.6
+
     return branch, commit_id
 
 def main():
Signature · 1 line
2.23.0.windows.1
Acked-by on v3 1/1
#6 lore raw

Re: [Openvpn-devel] [PATCH v3] git-version.py: proper support for tags

Quoted 20 lines · Lev Stipakov

From: Lev Stipakov <[email protected]...>

Git magic to get branch name

  git rev-parse --symbolic-full-name HEAD

doesn't work when we're on tag, which is the case when we build releases.

First, try to get tag name with

   git describe --exact-match

and if this fails, get branch name as before.

Use subprocess.Popen() to suppress stdout/stderr output.

This fixes https://github.com/OpenVPN/openvpn/issues/199.

Quoted 9 lines

---
v3:
- use "none" as branch name when we're not on a branch nor tag

v2:
- try to get tag name before branch
- use "git describe --exact-match" instead of "git describe --tags"
- use subprocess.Popen() instead of os.popen() to suppress output

Signature · 1 line
  Frank Lichtenheld
#7 lore raw

[Openvpn-devel] [PATCH applied] Re: git-version.py: proper support for tags

In reply to Lev Stipakov

I haven't tested this, but I'm not the one who gets more grey hair if this doesn't work - Frank is, and he's ACKed it, so all fine with me.

I have changed the Github reference to

Github: Fixes OpenVPN/openvpn#199

(we always prefix them with "Github:", and this notation is known to make GH understand the reference - not sure if the "Fixes" part also works, we'll see)

Your patch has been applied to the master, release/2.6 and release/2.5 branch (as instructed by Lev).

commit 91ab3d022e2652a46e6d6f25ab62f7c903e583c1 (master) commit 0bcbfea85dd737b7b68937697fd13e96439a756f (release/2.6) commit 4a165e1ea58eb395865a9975cefcfbfe7b41c356 (release/2.5)

Author: Lev Stipakov
Date:   Mon Dec 19 17:56:38 2022 +0200
     git-version.py: proper support for tags

     Signed-off-by: Lev Stipakov <[email protected]...>
     Acked-by: Frank Lichtenheld <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg25773.html
     Signed-off-by: Gert Doering <[email protected]...>

--
kind regards,

Gert Doering