77#
88
99import os
10+ import shutil
11+ import subprocess
1012import sys
1113import warnings
1214from contextlib import suppress
1315from pathlib import Path
1416
15- import git
16-
1717VERSION = "5.7.1"
1818
1919PROJECT_DIR = Path (__file__ ).resolve ().parent
@@ -33,13 +33,48 @@ def get_version(version):
3333 return version
3434
3535
36+ def run_command_safely (command_args ):
37+ """
38+ Execute an external command and return its stdout.
39+
40+ Runs without a shell (shell=False) to prevent injection vulnerabilities.
41+
42+ Usage notes:
43+ - Provide the command as a list of arguments.
44+ - Use full executable paths to avoid ambiguity.
45+ - Use the "--option=value" form, or split it as two list entries
46+ ["--option", "value"], but never join an option and its value in a
47+ single entry ("--option value").
48+ - Sanitize and validate any user input before passing it in.
49+
50+ Raise a SubprocessError if the exit code is non-zero.
51+ """
52+ completed_process = subprocess .run ( # noqa: S603
53+ command_args ,
54+ capture_output = True ,
55+ text = True ,
56+ )
57+ if completed_process .returncode :
58+ error_msg = (
59+ f'Error while executing cmd="{ completed_process .args } ": '
60+ f'"{ completed_process .stderr .strip ()} "'
61+ )
62+ raise subprocess .SubprocessError (error_msg )
63+ return completed_process .stdout
64+
65+
3666def get_git_describe_from_local_checkout ():
3767 """
3868 Return the git describe tag from the local checkout.
3969 This will only provide a result when the codebase is a git clone.
4070 """
41- with suppress (git .GitError ):
42- return git .Repo ("." ).git .describe (tags = True , always = True )
71+ git_executable = shutil .which ("git" )
72+ if not git_executable :
73+ return
74+
75+ with suppress (subprocess .SubprocessError ):
76+ git_describe = run_command_safely ([git_executable , "describe" , "--tags" , "--always" ])
77+ return git_describe .strip ()
4378
4479
4580def get_git_describe_from_version_file (version_file_location = ROOT_DIR / ".VERSION" ):
@@ -56,15 +91,6 @@ def get_git_describe_from_version_file(version_file_location=ROOT_DIR / ".VERSIO
5691 return version
5792
5893
59- def extract_short_commit (git_describe ):
60- """
61- Extract the short commit hash from a Git describe string while removing
62- any leading "g" character if present.
63- """
64- short_commit = git_describe .split ("-" )[- 1 ]
65- return short_commit .lstrip ("g" )
66-
67-
6894__version__ = get_version (VERSION )
6995
7096
0 commit comments