I'm in the process of implementing a little pre-commit hook that calls gitleaks protect prior to every commit.
This works well in a terminal but when trying to commit from within VSCode, a non-descriptive "Git: O" is returned (I assume this is simply the first line of gitleaks, part of its ascii logo).
As you can tell, I've tried multiple ways to have VSCode's Git module return a proper message upon exit of the submodule. However, nothing seems to work in that regard.
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
exit_code = subprocess.run("gitleaks protect -v --staged -c gitleaks.toml",shell=True)
if exit_code.returncode == 1:
eprint("This is a test")
sys.exit("TEST")
How do I return an alert window in VSCode that displays a message whenever the subprocess is exiting with exit code 1?
EDIT:
Ok. This works somehow, but it fails in so far as that
subprocess.run("gitleaks version", shell=True, stdout=dev_null, stderr=dev_null)
only works with my WSL Bash whereas subprocess.run("gitleaks version", stdout=dev_null, stderr=dev_null)
(without the shell=True) only works for my VSCode with Windows Git Bash.
Any way to make this portable, so FileNotFoundError is correctly thrown on both systems?
#!/usr/bin/env python3
# pylint: disable=C0116,W0613
import sys
import warnings
import subprocess
dev_null = subprocess.DEVNULL
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def gitleaks_installed():
try:
subprocess.run("gitleaks version", shell=True, stdout=dev_null, stderr=dev_null)
return True
except FileNotFoundError:
return False
if gitleaks_installed():
exit_code = subprocess.run("gitleaks protect -v --staged -c gitleaks.toml", shell=True, stdout=dev_null, stderr=dev_null)
if exit_code.returncode == 1:
eprint("gitleaks has detected sensitive information in your changes. Commit aborted.")
subprocess.run("gitleaks protect -v --staged -c gitleaks.toml", shell=True)
sys.exit(1)
else:
eprint("gitleaks is not installed or in the PATH.")
sys.exit(1)
EDIT2: NVM. The gitleaks_installed
part doesn't work at all under WSL Bash. It either always True or always False, depending on whether I include shell=True
.
Is there a better way to detect whether gitleaks is installed/in the PATH or not?