我正在實作一個小的預提交鉤子,它在每次提交之前呼叫 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 中?
uj5u.com熱心網友回復:
不是最優雅的解決方案,但這有效。FileNotFoundError我們使用的是回傳碼,而不是例外subprocess.run。
#!/usr/bin/env python3
# pylint: disable=C0116,W0613
import sys
import subprocess
dev_null = subprocess.DEVNULL
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def gitleaks_installed():
exit_code = subprocess.run("gitleaks version", shell=True, stdout=dev_null, stderr=dev_null)
if exit_code.returncode == 0:
return True
else:
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)
uj5u.com熱心網友回復:
回傳的物件subprocess.run是 CompletedProcess 物件,而不是回傳碼。您必須訪問它的.returncode屬性來檢查它回傳的內容。
但是,您可以改為添加check=True讓 Python 在失敗時拋出錯誤。
shell=True您幾乎肯定也應該通過自己將命令列決議為標記來擺脫多余的東西。
try:
subprocess.run(
["gitleaks", "protect", "-v", "--staged", "-c", "gitleaks.toml"],
check=True)
except subprocess.CalledProcessError:
eprint("gitleaks has detected sensitive information in your changes. Commit aborted.")
sys.exit(1)
except FileNotFoundError:
eprint("gitleaks is not installed or in the PATH.")
sys.exit(1)
這也避免了為了獲得輸出而第二次運行該程序。如果您想強制將自己的訊息放在頂部,請捕獲輸出并在您的訊息之后列印(添加capture_output=True和text=True)。
您可能還想替換eprint為logging.warn,并且可能在不同的錯誤場景中回傳不同的退出代碼。(當找不到二進制檔案時,Bash 通常會回傳 127,但這只是 Bash。)
subprocess.run在字串上運行,而不是在串列上運行,shell=True在 Windows 上不會發生奇怪的事情,但我的建議是始終避免為了方便而嘗試利用它。或許也可以看看in subprocess的實際含義shell=True
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/419594.html
標籤:
上一篇:在master之上重新建立基礎后,我如何gitdiff分支
下一篇:gitclean-x選項
