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

Fix crash if git is not available #432

Merged
merged 5 commits into from
Nov 29, 2022

Conversation

agateau-gg
Copy link
Collaborator

Description

This PR fixes the crash which happens when running on a machine where git is not installed.

Before:

$ ggshield --help
Traceback (most recent call last):
  File "/root/.local/share/virtualenvs/src-NVTF7jWz/bin/ggshield", line 5, in <module>
    from ggshield.cmd.main import main
  File "/src/ggshield/cmd/main.py", line 10, in <module>
    from ggshield.cmd.auth import auth_group
  File "/src/ggshield/cmd/auth/__init__.py", line 5, in <module>
    from ggshield.cmd.common_options import add_common_options
  File "/src/ggshield/cmd/common_options.py", line 18, in <module>
    from ggshield.core.config.user_config import UserConfig
  File "/src/ggshield/core/config/__init__.py", line 1, in <module>
    from .auth_config import AccountConfig, InstanceConfig
  File "/src/ggshield/core/config/auth_config.py", line 20, in <module>
    from ggshield.core.utils import datetime_from_isoformat
  File "/src/ggshield/core/utils.py", line 16, in <module>
    from .git_shell import get_git_root, is_git_dir
  File "/src/ggshield/core/git_shell.py", line 41, in <module>
    GIT_PATH = get_git_path(os.getcwd())
  File "/src/ggshield/core/git_shell.py", line 24, in get_git_path
    raise Exception("unable to find git executable in PATH/PATHEXT")
Exception: unable to find git executable in PATH/PATHEXT

After:

$ ggshield --help
Usage: ggshield [OPTIONS] COMMAND [ARGS]...

Options:
  -c, --config-path FILE          Set a custom config file. Ignores local and
                                  global config files.
  --check-for-updates / --no-check-for-updates
                                  Check for ggshield updates.
  --allow-self-signed             Ignore ssl verification.
  --debug                         Show debug information.
  -v, --verbose                   Verbose display mode.
  --version                       Show the version and exit.
  -h, --help                      Show this message and exit.

Commands:
  api-status  Show API status.
  auth        Commands to manage authentication.
  config      Commands to manage configuration.
  iac         Commands to work with infrastructure as code.
  install     Install a pre-commit or pre-push git hook (local or global).
  quota       Show quotas overview.
  secret      Commands to work with secrets.

Scanning files work:

$ ggshield secret scan path setup.py
Scanning Path... ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 1 files scanned out of 1 0:00:00

When trying to run a command which requires git:

$ ggshield secret scan commit-range HEAD~2..
unable to find git executable in PATH/PATHEXT

$ apt install git
[...]

$ ggshield secret scan commit-range HEAD~2..
Scanning Commits... ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 2 commits scanned out of 2 0:00:00

Issue

Fixes #329.

Almost not used, and required to merge shell() and git()
Contrary to shell(), by default git() raises an exception when a git
command fails. This highlighted ignored errors in some tests running
functions which call git to extract information from commits. Adapt tests
accordingly.
While I am at it, use "git rev-parse --show-toplevel" to check if a
directory is a git checkout. This is much faster, especially on large
repositories.
It's not useful: get_git_path() checks this already.
Do not try to find git at import time. Look for it the first time we need
it.

Fixes #329
@agateau-gg agateau-gg requested a review from Walz November 25, 2022 15:23
@agateau-gg agateau-gg merged commit 599cb48 into main Nov 29, 2022
@agateau-gg agateau-gg deleted the agateau/fix-crash-if-git-is-not-available branch November 29, 2022 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ggshield does not handle absence of git correctly
2 participants