Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eat stderr messages from git commands. #399

Merged
merged 2 commits into from Aug 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions safety/util.py
Expand Up @@ -15,7 +15,6 @@
from packaging.version import parse as parse_version
from ruamel.yaml import YAML
from ruamel.yaml.error import MarkedYAMLError
import safety

from safety.constants import EXIT_CODE_FAILURE, EXIT_CODE_OK
from safety.models import Package, RequirementFile
Expand Down Expand Up @@ -213,8 +212,11 @@ def build_telemetry_data(telemetry=True):
def build_git_data():
import subprocess

def git_command(commandline):
return subprocess.run(commandline, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode('utf-8').strip()

try:
is_git = subprocess.run(["git", "rev-parse", "--is-inside-work-tree"], stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
is_git = git_command(["git", "rev-parse", "--is-inside-work-tree"])
except Exception:
is_git = False

Expand All @@ -228,14 +230,14 @@ def build_git_data():
}

try:
result['branch'] = subprocess.run(["git", "symbolic-ref", "--short", "-q", "HEAD"], stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
result['tag'] = subprocess.run(["git", "describe", "--tags", "--exact-match"], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode('utf-8').strip()
result['branch'] = git_command(["git", "symbolic-ref", "--short", "-q", "HEAD"])
result['tag'] = git_command(["git", "describe", "--tags", "--exact-match"])

commit = subprocess.run(["git", "describe", '--match=""', '--always', '--abbrev=40', '--dirty'], stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
commit = git_command(["git", "describe", '--match=""', '--always', '--abbrev=40', '--dirty'])
result['dirty'] = commit.endswith('-dirty')
result['commit'] = commit.split("-dirty")[0]

result['origin'] = subprocess.run(["git", "remote", "get-url", "origin"], stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
result['origin'] = git_command(["git", "remote", "get-url", "origin"])
except Exception:
pass

Expand Down