summaryrefslogtreecommitdiffstats
path: root/extras/formatting.sh
diff options
context:
space:
mode:
authorJanne Kiviluoto (NVIDIA) <235827468+jkiviluoto-nv@users.noreply.github.com>2025-10-08 22:39:40 +0300
committerGitHub <noreply@github.com>2025-10-08 19:39:40 +0000
commiteda080bbf7a2b48e26342df13005a40434f7d802 (patch)
tree157de9de12d762cb4668004ec4ed4ac7a0522d6e /extras/formatting.sh
parentd30ae275e5ff53650526755ffa2a7a42992d88a8 (diff)
Feature/improve formatting sh (#8641)
Diffstat (limited to 'extras/formatting.sh')
-rwxr-xr-xextras/formatting.sh90
1 files changed, 79 insertions, 11 deletions
diff --git a/extras/formatting.sh b/extras/formatting.sh
index e5a73762a..736997ec3 100755
--- a/extras/formatting.sh
+++ b/extras/formatting.sh
@@ -2,12 +2,58 @@
set -e
+# Check Bash version
+if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then
+ echo "Error: Bash 4 or newer is required. Current version: $BASH_VERSION" >&2
+ if [[ "$(uname)" == "Darwin" ]]; then
+ echo "Please install a newer version of Bash using Homebrew:" >&2
+ echo " brew install bash" >&2
+ fi
+ exit 1
+fi
+
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
source_dir="$(dirname "$script_dir")"
since_rev=""
+# Detect macOS and set appropriate binary names
+if [[ "$(uname)" == "Darwin" ]]; then
+ # On macOS, check for GNU versions
+ # grep and xargs use g-prefix, diff is installed as /opt/homebrew/bin/diff
+ missing_tools=()
+
+ if ! command -v ggrep &>/dev/null; then
+ missing_tools+=("grep")
+ fi
+
+ if ! command -v gxargs &>/dev/null; then
+ missing_tools+=("findutils")
+ fi
+
+ if ! command -v /opt/homebrew/bin/diff &>/dev/null; then
+ missing_tools+=("diffutils")
+ fi
+
+ if [ ${#missing_tools[@]} -gt 0 ]; then
+ echo "Error: GNU versions of grep, xargs, and diff are required on macOS." >&2
+ echo "Please install them using Homebrew:" >&2
+ echo " brew install ${missing_tools[*]}" >&2
+ exit 1
+ fi
+
+ GREP_BIN="ggrep"
+ XARGS_BIN="gxargs"
+ DIFF_BIN="/opt/homebrew/bin/diff"
+else
+ # On other systems, use standard binaries
+ GREP_BIN="grep"
+ XARGS_BIN="xargs"
+ DIFF_BIN="diff"
+fi
+
check_only=0
no_version_check=0
+modified_files=0
run_cpp=0
run_yaml=0
run_markdown=0
@@ -43,6 +89,7 @@ while [[ "$#" -gt 0 ]]; do
;;
--check-only) check_only=1 ;;
--no-version-check) no_version_check=1 ;;
+ --modified) modified_files=1 ;;
--cpp)
run_cpp=1
run_all=0
@@ -95,7 +142,7 @@ require_bin() {
fi
if [ "$no_version_check" -eq 0 ]; then
- version=$("$name" --version | grep -oP "\d+\.\d+\.?\d*" | head -n1)
+ version=$("$name" --version | $GREP_BIN -oP "\d+\.\d+\.?\d*" | head -n1)
# Debug output to stderr
if [ -n "$max_version" ]; then
@@ -122,8 +169,8 @@ require_bin() {
require_bin "git" "1.8"
((run_all || run_cmake)) && require_bin "gersemi" "0.21" "0.22"
-((run_all || run_cpp)) && require_bin "xargs" "3"
-require_bin "diff" "2"
+((run_all || run_cpp)) && require_bin "$XARGS_BIN" "3"
+require_bin "$DIFF_BIN" "2"
((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"
@@ -132,11 +179,32 @@ if [ "$missing_bin" ]; then
exit 1
fi
+get_nproc() {
+ local nproc_count
+ if command -v nproc &>/dev/null; then
+ nproc_count=$(nproc)
+ elif [[ "$(uname)" == "Darwin" ]] && command -v sysctl &>/dev/null; then
+ nproc_count=$(sysctl -n hw.logicalcpu)
+ elif command -v getconf &>/dev/null; then
+ nproc_count=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
+ fi
+ echo "${nproc_count:-1}"
+}
+
exit_code=0
function list_files() {
- if [ "$since_rev" ]; then
- git diff --name-only "$since_rev" HEAD $@
+ if [ "$since_rev" ] || [ "$modified_files" -eq 1 ]; then
+ command="git diff --name-only"
+ if [ "$since_rev" ]; then
+ command="$command $since_rev"
+ else
+ command="$command HEAD"
+ fi
+ if [ "$modified_files" -eq 0 ]; then
+ command="$command HEAD"
+ fi
+ $command "$@"
else
git ls-files $@
fi
@@ -192,9 +260,9 @@ cpp_formatting() {
tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT
- printf '%s\n' "${files[@]}" | xargs --verbose -P "$(nproc)" -I{} bash -c "
+ printf '%s\n' "${files[@]}" | $XARGS_BIN --verbose -P "$(get_nproc)" -I{} bash -c "
mkdir -p \"\$(dirname \"$tmpdir/{}\")\"
- diff -u --color=always --label \"{}\" --label \"{}\" \"{}\" <(clang-format \"{}\") > \"$tmpdir/{}\"
+ $DIFF_BIN -u --color=always --label \"{}\" --label \"{}\" \"{}\" <(clang-format \"{}\") > \"$tmpdir/{}\"
:
" |& track_progress ${#files[@]}
@@ -206,7 +274,7 @@ cpp_formatting() {
fi
done
else
- printf '%s\n' "${files[@]}" | xargs --verbose -n1 -P "$(nproc)" clang-format -i |&
+ printf '%s\n' "${files[@]}" | $XARGS_BIN --verbose -n1 -P "$(get_nproc)" clang-format -i |&
track_progress ${#files[@]}
fi
}
@@ -219,13 +287,13 @@ prettier_formatting() {
if ! output=$(prettier "$file" 2>/dev/null); then
continue
fi
- if ! diff -q "$file" <(echo "$output") >/dev/null 2>&1; then
- diff --color -u --label "$file" --label "$file" "$file" <(echo "$output") || :
+ if ! $DIFF_BIN -q "$file" <(echo "$output") >/dev/null 2>&1; then
+ $DIFF_BIN --color -u --label "$file" --label "$file" "$file" <(echo "$output") || :
exit_code=1
fi
done
else
- prettier --write "${files[@]}" | grep -v '(unchanged)' >&2 || :
+ prettier --write "${files[@]}" | $GREP_BIN -v '(unchanged)' >&2 || :
fi
}