【发布时间】:2022-01-20 06:55:50
【问题描述】:
我正在实现一个小的预提交钩子,它在每次提交之前调用 gitleaks 保护。
这在终端中运行良好,但是当尝试从 VSCode 中提交时,会返回一个非描述性的“Git: O”(我假设这只是 gitleaks 的第一行,它的 ascii 徽标的一部分)。
如您所知,我尝试了多种方法让 VSCode 的 Git 模块在退出子模块时返回正确的消息。但是,在这方面似乎没有任何作用。
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")
当子进程以退出代码 1 退出时,如何在 VSCode 中返回一个显示消息的警报窗口?
编辑:
好的。这以某种方式起作用,但它失败了
subprocess.run("gitleaks version", shell=True, stdout=dev_null, stderr=dev_null) 仅适用于我的 WSL Bash,而 subprocess.run("gitleaks version", stdout=dev_null, stderr=dev_null)(没有 shell=True)仅适用于我的 VSCode 和 Windows Git Bash。
有什么方法可以使这个可移植的,所以 FileNotFoundError 在两个系统上都能正确抛出?
#!/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。 gitleaks_installed 部分在 WSL Bash 下根本不起作用。它要么总是 True 要么总是 False,这取决于我是否包含 shell=True。
有没有更好的方法来检测 gitleaks 是否安装/在 PATH 中?
【问题讨论】:
标签: python git visual-studio-code pre-commit-hook pre-commit