gpt4 book ai didi

Python Black Code Formatter - 在将项目提交到 Github 之前是否有任何方法可以应用自动黑色格式化

转载 作者:行者123 更新时间:2023-12-05 03:28:43 28 4
gpt4 key购买 nike

最近我开始使用Black我的项目的代码格式化程序,有时很难跟踪我是否格式化了所有更改的文件。假设我的项目包含 20 个不同的脚本(.py 文件),并且我对其中的最后 5 个进行了更改,这是我在提交之前通常所做的。

第一个选项

black /project_directory

第二个选项

black script1.py

black script2.py

等等

有没有办法在 git commit 之前自动化这个过程?

提前致谢

最佳答案

是的,您可以使用预提交 Hook 。如果您使用的是 Linux 或 macOS,或带有 Git Bash 的 Windows,请将以下内容放入名为 .git/hooks/pre-commit 的文件中:

#!/bin/bash

# Paths are relative to the root of the git repository
black .
black script1.py
black script2.py

使用 chmod +x .git/hooks/pre-commit 使其可运行。然后,每次执行 commit 时, Hook 都会首先运行并格式化所有代码。您可以在 Git Hooks documentation 上阅读更多关于钩子(Hook)的信息。 .

关于Python Black Code Formatter - 在将项目提交到 Github 之前是否有任何方法可以应用自动黑色格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71180810/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com