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

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

Commit Message

Lev Stipakov Dec. 19, 2022, 3:02 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>
---
 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(-)

Comments

Frank Lichtenheld Dec. 19, 2022, 3:29 p.m. UTC | #1
On Mon, Dec 19, 2022 at 05:02:26PM +0200, Lev Stipakov wrote:
[...]
>  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"

>  build/msvc/msvc-generate/git-version.py | 17 ++++++++++++-----
>  1 file changed, 12 insertions(+), 5 deletions(-)
> 
> diff --git a/build/msvc/msvc-generate/git-version.py b/build/msvc/msvc-generate/git-version.py
> index 814dc86a..89fd44f2 100644
> --- a/build/msvc/msvc-generate/git-version.py
> +++ b/build/msvc/msvc-generate/git-version.py
> @@ -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"]

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

> -    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,

Patch

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