gpt4 book ai didi

git - 如何对 git ls-files 中的所有文件运行命令?

转载 作者:太空狗 更新时间:2023-10-29 14:31:45 24 4
gpt4 key购买 nike

我们有一个 CI 脚本,可以像这样对存储库中的所有 Python 文件进行样式检查:

#!/usr/bin/env bash
find . -name \*.py -exec pep8 --ignore=E402 --max-line-length=120 {} +
if [ $? -ne 0 ]; then
>&2 echo "=== PEP8 errors need to be solved ==="
else
echo "=== PEP8 check ok ==="
fi
pytest

但是,有些东西要么没有 checkin ,要么 .gitignoreed。所以我想获取 git ls-files 的输出并仅在这些文件上运行命令。我可以循环,但我不想对开发人员选择的 shell 做出假设。理想情况下,我想通过 git ls-files 的设置差异来过滤 find 的输出。

最佳答案

#!/usr/bin/env bash
git ls-files -z \*.py | xargs -0 pep8 --ignore=E402 --max-line-length=120
if [ $? -ne 0 ]; then
>&2 echo "=== PEP8 errors need to be solved ==="
else
echo "=== PEP8 check ok ==="
fi
pytest

你应该。

git ls-files -zxargs -0 允许在文件名等中使用空格

来自 xargs

-0, --null
Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.

来自 git ls-files

-z
\0 line termination on output.

关于git - 如何对 git ls-files 中的所有文件运行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41085382/

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