Skip to content

Commit 31a7c1e

Browse files
committed
remove the dependency on gitpython
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent 2cb3560 commit 31a7c1e

12 files changed

Lines changed: 40 additions & 96 deletions

dejacode/__init__.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
#
88

99
import os
10+
import shutil
11+
import subprocess
1012
import sys
1113
import warnings
1214
from contextlib import suppress
1315
from pathlib import Path
1416

15-
import git
16-
1717
VERSION = "5.7.1"
1818

1919
PROJECT_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+
3666
def 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

4580
def 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

pyproject.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,6 @@ dependencies = [
141141
"cyclonedx-python-lib==11.6.0",
142142
"sortedcontainers==2.4.0",
143143
"py-serializable==2.1.0",
144-
# Git
145-
"gitpython==3.1.50",
146-
"gitdb==4.0.12",
147-
"smmap==5.0.3",
148144
# CSAF
149145
"pydantic==2.12.5",
150146
"pydantic-core==2.41.5",
-61.3 KB
Binary file not shown.

thirdparty/dist/gitdb-4.0.12-py3-none-any.whl.ABOUT

Lines changed: 0 additions & 14 deletions
This file was deleted.
-204 KB
Binary file not shown.

thirdparty/dist/gitpython-3.1.46-py3-none-any.whl.ABOUT

Lines changed: 0 additions & 14 deletions
This file was deleted.
-207 KB
Binary file not shown.
-208 KB
Binary file not shown.
-23.7 KB
Binary file not shown.

thirdparty/dist/smmap-5.0.2-py3-none-any.whl.ABOUT

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)