Som*_*ter 5 python git githooks
我想创建一个 git 预提交挂钩,以防止未注释的pytest.set_trace()或 apdb.set_trace()和其他.set_trace(). 这是因为我经常从命令行进行调试,有时忘记了我在代码中留下了调试语句。通过使用预提交挂钩,我将来应该能够避免这种情况。
Keul 的博客对此有一个解决方案,但会话必须位于 git 存储库的根目录中才能工作,否则会抱怨。
我基本上希望not与此等效的工作grep
#(\s+)?.*\.set_trace\(\)
Run Code Online (Sandbox Code Playgroud)
请参阅regexr测试
谢谢
正确的正则表达式是^\s?[^#]+\.set_trace\(\)
解释:
^以。。开始\s?匹配一个空格或多个空格或无空格[^#]+匹配任何字符,排除#\.set_trace\(\)匹配任何以结尾的函数.set_trace()
pdbor来更明确pytest set_trace,但可能还有其他包也有set_trace示例 python 代码
import pdb
if __name__ == "__main__":
pdb.set_trace()
pytest.set_trace()
# pdb.set_trace()
# pytest.set_trace()
#pytest.set_trace()
# pdb.set_trace()
# somethingelse.set_trace()
Run Code Online (Sandbox Code Playgroud)
验证使用ripgrep
$ rg '^\s?[^#]+\.set_trace\(\)'
main.py
4: pdb.set_trace()
5: pytest.set_trace()
Run Code Online (Sandbox Code Playgroud)
现在我们可以使用git-secrets它在预提交挂钩上触发,这将阻止我们提交这一行
$ git secrets --add '^\s?[^#]+\.set_trace\(\)'
$ git add main.py
$ git commit -m 'test'
main.py:4: pdb.set_trace()
main.py:5: pytest.set_trace()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
451 次 |
| 最近记录: |