summaryrefslogtreecommitdiffstats
path: root/extras/formatting.sh
diff options
context:
space:
mode:
authorAnders Leino <aleino@nvidia.com>2025-01-25 03:51:06 +0200
committerGitHub <noreply@github.com>2025-01-25 01:51:06 +0000
commita7958afa5ace2c92e10e1765a5bc33c891d09079 (patch)
tree9290ceb6215cebb44420e8f0759799206f5207f8 /extras/formatting.sh
parent1abba25401c59a5634955ca44806834186e6b082 (diff)
Some usability improvements to formatting script (#6153)
* Sharpen the requirements for formatting * Add option to only format files changed since a given revision * Avoid divison-by-zero when total is zero * format code --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
Diffstat (limited to 'extras/formatting.sh')
-rwxr-xr-xextras/formatting.sh36
1 files changed, 25 insertions, 11 deletions
diff --git a/extras/formatting.sh b/extras/formatting.sh
index f6a3134aa..94df651eb 100755
--- a/extras/formatting.sh
+++ b/extras/formatting.sh
@@ -4,6 +4,7 @@ set -e
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
source_dir="$(dirname "$script_dir")"
+since_rev=""
check_only=0
no_version_check=0
@@ -30,6 +31,7 @@ Options:
--md Format only markdown files
--sh Format only shell script files
--cmake Format only CMake files
+ --since <rev> Only format files since Git revision <rev>
EOF
}
@@ -65,6 +67,10 @@ while [[ "$#" -gt 0 ]]; do
source_dir="$2"
shift
;;
+ --since)
+ since_rev="$2"
+ shift
+ ;;
*)
echo "unrecognized argument: $1"
show_help
@@ -108,12 +114,12 @@ require_bin() {
}
require_bin "git" "1.8"
-require_bin "gersemi" "0.17"
-require_bin "xargs" "3"
+((run_all || run_cmake)) && require_bin "gersemi" "0.17"
+((run_all || run_cpp)) && require_bin "xargs" "3"
require_bin "diff" "2"
-require_bin "clang-format" "17" "18"
-require_bin "prettier" "3"
-require_bin "shfmt" "3"
+((run_all || run_cpp)) && require_bin "clang-format" "17" "18"
+((run_all || run_yaml || run_markdown)) && require_bin "prettier" "3"
+((run_all || run_sh)) && require_bin "shfmt" "3"
if [ "$missing_bin" ]; then
exit 1
@@ -121,10 +127,18 @@ fi
exit_code=0
+function list_files() {
+ if [ "$since_rev" ]; then
+ git diff --name-only "$since_rev" HEAD $@
+ else
+ git ls-files $@
+ fi
+}
+
cmake_formatting() {
echo "Formatting CMake files..." >&2
- readarray -t files < <(git ls-files '*.cmake' 'CMakeLists.txt' '**/CMakeLists.txt')
+ readarray -t files < <(list_files '*.cmake' 'CMakeLists.txt' '**/CMakeLists.txt')
common_args=(
# turn on warning when this is fixed https://github.com/BlankSpruce/gersemi/issues/39
@@ -150,7 +164,7 @@ track_progress() {
local total=$1
local current=0
- while IFS= read -r _; do
+ ((total)) && while IFS= read -r _; do
((current++)) || :
percent=$((current * 100 / total))
printf '\rProgress: [%-50s] %d%%' "$(printf '#%.0s' $(seq 1 $((percent / 2))))" "$percent" >&2
@@ -161,7 +175,7 @@ track_progress() {
cpp_formatting() {
echo "Formatting cpp files..." >&2
- readarray -t files < <(git ls-files '*.cpp' '*.hpp' '*.c' '*.h' ':!external/**')
+ readarray -t files < <(list_files '*.cpp' '*.hpp' '*.c' '*.h' ':!external/**')
# The progress reporting is a bit sneaky, we use `--verbose` with xargs which
# prints a line to stderr for each command, and we simply count these...
@@ -211,7 +225,7 @@ prettier_formatting() {
yaml_json_formatting() {
echo "Formatting yaml and json files..." >&2
- readarray -t files < <(git ls-files "*.yaml" "*.yml" "*.json" ':!external/**')
+ readarray -t files < <(list_files "*.yaml" "*.yml" "*.json" ':!external/**')
prettier_formatting
}
@@ -219,7 +233,7 @@ yaml_json_formatting() {
markdown_formatting() {
echo "Formatting markdown files..." >&2
- readarray -t files < <(git ls-files "*.md" ':!external/**')
+ readarray -t files < <(list_files "*.md" ':!external/**')
prettier_formatting
}
@@ -227,7 +241,7 @@ markdown_formatting() {
sh_formatting() {
echo "Formatting sh files..." >&2
- readarray -t files < <(git ls-files "*.sh")
+ readarray -t files < <(list_files "*.sh")
common_args=(
# default 8 is way too wide