From c294c802141fdd5936b447017535ee995eed319d Mon Sep 17 00:00:00 2001 From: Aaron Gyes Date: Sun, 14 Aug 2022 17:41:05 -0700 Subject: [PATCH] style.fish --all improvements clang-format (since 10) can output diagnostics which indicate lines needing formatting with --dry-run and -Werror: the exit code indicates if a file is correctly formatted or not. We used to copy each .cpp file, run clang_format on the duplicate and then `cmp` to see if there were changes made, before just printing a line with the filename and moving the new ontop of the original. Now we show clang-format diagnostics which indicate which lines will be changed, prompt for confirmation and then let clang-format modify the files in-place without the juggling. Looks like this: https://user-images.githubusercontent.com/291142/184561633-c16754c8-179e-426b-ba15-345ba65b9cf9.png --- build_tools/style.fish | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/build_tools/style.fish b/build_tools/style.fish index cff0c8ea8..31b0d1b64 100755 --- a/build_tools/style.fish +++ b/build_tools/style.fish @@ -74,13 +74,20 @@ if set -q c_files[1] else if type -q clang-format echo === Running "$red"clang-format"$normal" for file in $c_files - cp $file $file.new # preserves mode bits - clang-format $file >$file.new - if cmp --quiet $file $file.new - rm $file.new - else - echo $file was NOT correctly formatted - mv $file.new $file + if clang-format --dry-run -Werror $file + # file was clean, remove it from the list + set -e c_files[(contains -i $file $c_files)] + end + end + if set -q c_files[1] + printf "Reformat those %d files?\n" (count $c_files) + read -P 'y/N? ' -n1 -l ans + if string match -qi "y" -- $ans + clang-format -i --verbose $c_files + else if string match -qi "n" -- $ans + echo Skipping + else # like they ctrl-C'd or something. + exit 1 end end else