OpenVPN
git-version.py
Go to the documentation of this file.
2# OpenVPN -- An application to securely tunnel IP networks
3# over a single UDP port, with support for SSL/TLS-based
4# session authentication and key exchange,
5# packet encryption, packet authentication, and
6# packet compression.
7#
8# Copyright (C) 2022-2025 OpenVPN Inc <sales@openvpn.net>
9# Copyright (C) 2022-2022 Lev Stipakov <lev@lestisoftware.fi>
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License version 2
13# as published by the Free Software Foundation.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License along
21# with this program; if not, see <https://www.gnu.org/licenses/>.
22#
23
24# Usage: ./git-version.py [directory]
25# Find a good textual representation of the git commit currently checked out.
26# Make that representation available as CONFIGURE_GIT_REVISION in
27# <directory>/config-version.h.
28# It will prefer a tag name if it is checked out exactly, otherwise will use
29# the branch name. 'none' if no branch is checked out (detached HEAD).
30# This is used to enhance the output of openvpn --version with Git information.
31
32import os
33import sys
34import subprocess
35
36def run_command(args):
37 sp = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
38 o, _ = sp.communicate()
39 return o.decode("utf-8")[:-1]
40
42 commit_id = run_command(["git", "rev-parse", "--short=16", "HEAD"])
43 if not commit_id:
44 raise
45 branch = run_command(["git", "describe", "--exact-match"])
46 if not branch:
47 # this returns an array like ["master"] or ["release", "2.6"]
48 branch = run_command(["git", "rev-parse", "--symbolic-full-name", "HEAD"]).split("/")[2:]
49 if not branch:
50 branch = ["none"]
51 branch = "/" .join(branch) # handle cases like release/2.6
52
53 return branch, commit_id
54
55def main():
56 try:
57 branch, commit_id = get_branch_commit_id()
58 except:
59 branch, commit_id = "unknown", "unknown"
60
61 prev_content = ""
62
63 name = os.path.join("%s" % (sys.argv[1] if len(sys.argv) > 1 else "."), "config-version.h")
64 try:
65 with open(name, "r") as f:
66 prev_content = f.read()
67 except:
68 # file doesn't exist
69 pass
70
71 content = "#define CONFIGURE_GIT_REVISION \"%s/%s\"\n" % (branch, commit_id)
72 content += "#define CONFIGURE_GIT_FLAGS \"\"\n"
73
74 if prev_content != content:
75 print("Writing %s" % name)
76 with open(name, "w") as f:
77 f.write(content)
78 else:
79 print("Content of %s hasn't changed" % name)
80
81if __name__ == "__main__":
82 main()
run_command(args)
get_branch_commit_id()