contrib: add "--fast" flag to nm-core-format.sh to only check changed files

It's not the default, because a caller might not be aware that this flag
exists, and when calling the script without arguments, it should do
correct (albeit slow) thing.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1341
This commit is contained in:
Thomas Haller 2022-08-19 13:45:47 +02:00
parent dc01d353aa
commit 90b267afa7
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -47,26 +47,28 @@ FILES=()
HAS_EXPLICIT_FILES=0
SHOW_FILENAMES=0
TEST_ONLY=0
CHECK_ALL=1
usage() {
printf "Usage: %s [OPTION]... [FILE]...\n" "$(basename "$0")"
printf "Reformat source files using NetworkManager's code-style.\n\n"
printf "If no file is given the script runs on the whole codebase.\n"
printf "OPTIONS:\n"
printf " -i Reformat files (this is the default)\n"
printf " -n|--dry-run Only check the files (contrary to \"-i\")\n"
printf " -h Print this help message\n"
printf " -h Print this help message.\n"
printf " -i Reformat files (the default).\n"
printf " -n|--dry-run Only check the files (contrary to \"-i\").\n"
printf " -a|--all Check all files (the default).\n"
printf " -F|--fast Check only files from \`git diff --name-only HEAD^\` (contrary to \"-a\").\n"
printf " This also affects directories given in the [FILE] list, but not files.\n"
printf " --show-filenames Only print the filenames that would be checked/formatted\n"
printf " -- Separate options from filenames/directories\n"
}
g_ls_files() {
ls_files_filter() {
local OLD_IFS="$IFS"
local pattern="$1"
shift
IFS=$'\n'
for f in $(git ls-files -- "$pattern") ; do
for f in $(cat) ; do
local found=1
local p
for p; do
@ -78,6 +80,17 @@ g_ls_files() {
IFS="$OLD_IFS"
}
g_ls_files() {
local pattern="$1"
shift
if [ $CHECK_ALL = 1 ]; then
git ls-files -- "$pattern"
else
git diff --name-only HEAD^ -- "$pattern"
fi | ls_files_filter "$@"
}
HAD_DASHDASH=0
while (( $# )); do
if [ "$HAD_DASHDASH" = 0 ]; then
@ -91,6 +104,16 @@ while (( $# )); do
shift
continue
;;
-a|--all)
CHECK_ALL=1
shift
continue
;;
-F|--fast)
CHECK_ALL=0
shift
continue
;;
-n|--dry-run)
TEST_ONLY=1
shift
@ -111,7 +134,7 @@ while (( $# )); do
if [ -d "$1" ]; then
while IFS='' read -r line;
do FILES+=("$line")
done < <(g_ls_files "${1}/*.[hc]" "${EXCLUDE_PATHS[@]}")
done < <(CHECK_ALL=$CHECK_ALL g_ls_files "${1}/*.[hc]" "${EXCLUDE_PATHS[@]}")
elif [ -f "$1" ]; then
FILES+=("$1")
else
@ -126,16 +149,21 @@ done
if [ $HAS_EXPLICIT_FILES = 0 ]; then
while IFS='' read -r line; do
FILES+=("$line")
done < <(g_ls_files '*.[ch]' "${EXCLUDE_PATHS[@]}")
done < <(CHECK_ALL=$CHECK_ALL g_ls_files '*.[ch]' "${EXCLUDE_PATHS[@]}")
fi
if [ $SHOW_FILENAMES = 1 ]; then
printf '%s\n' "${FILES[@]}"
for f in "${FILES[@]}" ; do
printf '%s\n' "$f"
done
exit 0
fi
if [ "${#FILES[@]}" = 0 ]; then
die "Error: no files to check"
if [ $CHECK_ALL = 1 ]; then
die "Error: no files to check"
fi
exit 0
fi
FLAGS_TEST=( --Werror -n --ferror-limit=1 )