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

Message ID 20221219155638.497-1-lstipakov@gmail.com
State Accepted
Headers show
Series [Openvpn-devel,v3] git-version.py: proper support for tags | expand

Commit Message

Lev Stipakov Dec. 19, 2022, 3:56 p.m. UTC
From: Lev Stipakov <lev@openvpn.net>

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.

Signed-off-by: Lev Stipakov <lev@openvpn.net>
---
 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(-)

Comments

Frank Lichtenheld Dec. 19, 2022, 4:03 p.m. UTC | #1
On Mon, Dec 19, 2022 at 05:56:38PM +0200, Lev Stipakov wrote:
> From: Lev Stipakov <lev@openvpn.net>
> 
> 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.
> 
> Signed-off-by: Lev Stipakov <lev@openvpn.net>
> ---
>  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 
> 

Acked-By: Frank Lichtenheld <frank@lichtenheld.com>
Gert Doering Dec. 19, 2022, 4:46 p.m. UTC | #2
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 <lev@openvpn.net>
     Acked-by: Frank Lichtenheld <frank@lichtenheld.com>
     Message-Id: <20221219155638.497-1-lstipakov@gmail.com>
     URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg25773.html
     Signed-off-by: Gert Doering <gert@greenie.muc.de>


--
kind regards,

Gert Doering

Patch

diff --git a/build/msvc/msvc-generate/git-version.py b/build/msvc/msvc-generate/git-version.py
index 814dc86a..00458955 100644
--- a/build/msvc/msvc-generate/git-version.py
+++ b/build/msvc/msvc-generate/git-version.py
@@ -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():