Skip to content

Commit a7958af

Browse files
aleino-nvslangbot
andauthored
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>
1 parent 1abba25 commit a7958af

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

extras/formatting.sh

+25-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set -e
44

55
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
66
source_dir="$(dirname "$script_dir")"
7+
since_rev=""
78

89
check_only=0
910
no_version_check=0
@@ -30,6 +31,7 @@ Options:
3031
--md Format only markdown files
3132
--sh Format only shell script files
3233
--cmake Format only CMake files
34+
--since <rev> Only format files since Git revision <rev>
3335
EOF
3436
}
3537

@@ -65,6 +67,10 @@ while [[ "$#" -gt 0 ]]; do
6567
source_dir="$2"
6668
shift
6769
;;
70+
--since)
71+
since_rev="$2"
72+
shift
73+
;;
6874
*)
6975
echo "unrecognized argument: $1"
7076
show_help
@@ -108,23 +114,31 @@ require_bin() {
108114
}
109115

110116
require_bin "git" "1.8"
111-
require_bin "gersemi" "0.17"
112-
require_bin "xargs" "3"
117+
((run_all || run_cmake)) && require_bin "gersemi" "0.17"
118+
((run_all || run_cpp)) && require_bin "xargs" "3"
113119
require_bin "diff" "2"
114-
require_bin "clang-format" "17" "18"
115-
require_bin "prettier" "3"
116-
require_bin "shfmt" "3"
120+
((run_all || run_cpp)) && require_bin "clang-format" "17" "18"
121+
((run_all || run_yaml || run_markdown)) && require_bin "prettier" "3"
122+
((run_all || run_sh)) && require_bin "shfmt" "3"
117123

118124
if [ "$missing_bin" ]; then
119125
exit 1
120126
fi
121127

122128
exit_code=0
123129

130+
function list_files() {
131+
if [ "$since_rev" ]; then
132+
git diff --name-only "$since_rev" HEAD $@
133+
else
134+
git ls-files $@
135+
fi
136+
}
137+
124138
cmake_formatting() {
125139
echo "Formatting CMake files..." >&2
126140

127-
readarray -t files < <(git ls-files '*.cmake' 'CMakeLists.txt' '**/CMakeLists.txt')
141+
readarray -t files < <(list_files '*.cmake' 'CMakeLists.txt' '**/CMakeLists.txt')
128142

129143
common_args=(
130144
# turn on warning when this is fixed https://github.com/BlankSpruce/gersemi/issues/39
@@ -150,7 +164,7 @@ track_progress() {
150164
local total=$1
151165
local current=0
152166

153-
while IFS= read -r _; do
167+
((total)) && while IFS= read -r _; do
154168
((current++)) || :
155169
percent=$((current * 100 / total))
156170
printf '\rProgress: [%-50s] %d%%' "$(printf '#%.0s' $(seq 1 $((percent / 2))))" "$percent" >&2
@@ -161,7 +175,7 @@ track_progress() {
161175
cpp_formatting() {
162176
echo "Formatting cpp files..." >&2
163177

164-
readarray -t files < <(git ls-files '*.cpp' '*.hpp' '*.c' '*.h' ':!external/**')
178+
readarray -t files < <(list_files '*.cpp' '*.hpp' '*.c' '*.h' ':!external/**')
165179

166180
# The progress reporting is a bit sneaky, we use `--verbose` with xargs which
167181
# prints a line to stderr for each command, and we simply count these...
@@ -211,23 +225,23 @@ prettier_formatting() {
211225
yaml_json_formatting() {
212226
echo "Formatting yaml and json files..." >&2
213227

214-
readarray -t files < <(git ls-files "*.yaml" "*.yml" "*.json" ':!external/**')
228+
readarray -t files < <(list_files "*.yaml" "*.yml" "*.json" ':!external/**')
215229

216230
prettier_formatting
217231
}
218232

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

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

224238
prettier_formatting
225239
}
226240

227241
sh_formatting() {
228242
echo "Formatting sh files..." >&2
229243

230-
readarray -t files < <(git ls-files "*.sh")
244+
readarray -t files < <(list_files "*.sh")
231245

232246
common_args=(
233247
# default 8 is way too wide

0 commit comments

Comments
 (0)