Skip to content

Commit

Permalink
Some usability improvements to formatting script (#6153)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
aleino-nv and slangbot authored Jan 25, 2025
1 parent 1abba25 commit a7958af
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions extras/formatting.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -65,6 +67,10 @@ while [[ "$#" -gt 0 ]]; do
source_dir="$2"
shift
;;
--since)
since_rev="$2"
shift
;;
*)
echo "unrecognized argument: $1"
show_help
Expand Down Expand Up @@ -108,23 +114,31 @@ 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
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
Expand All @@ -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
Expand All @@ -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...
Expand Down Expand Up @@ -211,23 +225,23 @@ 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
}

markdown_formatting() {
echo "Formatting markdown files..." >&2

readarray -t files < <(git ls-files "*.md" ':!external/**')
readarray -t files < <(list_files "*.md" ':!external/**')

prettier_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
Expand Down

0 comments on commit a7958af

Please sign in to comment.