We have a CI script that does style checking on all Python files in the repository like this:
#!/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
However, there are a few things that are either not checked in, or .gitignore
ed. So I would like to take the output of git ls-files
and run the command only on those. I could loop but I don't want to make assumptions about the developer's chosen shell. Ideally, I would like to filter find
s output via set difference with git ls-files
.
#!/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
Should do you.
git ls-files -z
and xargs -0
allow spaces in file names etc
from the xargs
man
-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.
from the git ls-files
man
-z
\\0 line termination on output.
I was surprised because I had an insane amount of line of codes, turn out that it was because of .png files and other files that don't mean much.
In case you are looking for only certain extensions (in my case dart, py and c), kindly find the following script for your convenience :
git ls-files | grep -e "\.\(dart\|py\|c\)$" | xargs wc -l
So basically if for example java is your main extension file (/trigger all the java haters and lovers) you put java inside the escape sequence:
git ls-files | grep -e "\.\(java\)$" | xargs wc -l
(copy/paste and replace java with your current main file extension)
And then add:
\|file_extension
Right after the first extension (in this case after the 'a' from java) for each subsequent file extension that you also want to analyze.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.