diff options
Diffstat (limited to 'contrib/scripts/nm-code-format.sh')
-rwxr-xr-x | contrib/scripts/nm-code-format.sh | 50 |
1 files changed, 39 insertions, 11 deletions
diff --git a/contrib/scripts/nm-code-format.sh b/contrib/scripts/nm-code-format.sh index 0aaa14752c..cd45874f16 100755 --- a/contrib/scripts/nm-code-format.sh +++ b/contrib/scripts/nm-code-format.sh @@ -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 ) |