]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Clean up scripts related to code formatting
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 20 Jun 2020 18:49:52 +0000 (20:49 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 23 Jun 2020 20:04:52 +0000 (22:04 +0200)
- Simplified scripts.
- Made colored output work again.

CMakeLists.txt
misc/check-format [deleted file]
misc/format [deleted file]
misc/format-files [new file with mode: 0755]
misc/run-clang-format [deleted file]

index 5f3c2f4996d8b236242e72e9ef1ec46212bde2b0..d5e02476e554b034bd1e0b5afc65635039310dcc 100644 (file)
@@ -117,14 +117,14 @@ if(NOT CLANG_FORMAT_EXE)
 else()
   add_custom_target(
     format
-    COMMAND misc/format.sh
+    COMMAND misc/format-files --all
     COMMENT "Formatting code"
     USES_TERMINAL
     WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
 
   add_custom_target(
     check_format
-    COMMAND misc/check_format.sh
+    COMMAND misc/format-files --all --check
     COMMENT "Checking code formatting"
     USES_TERMINAL
     WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
diff --git a/misc/check-format b/misc/check-format
deleted file mode 100755 (executable)
index 7af55d9..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-#!/bin/sh -e
-
-if [ -n "${VERBOSE}" ]; then
-  set -x
-fi
-
-# xarg returns 1 if any run-clang-format call returns 1.
-clang-format --version
-find src unittest -path src/third_party -prune -o -regex ".*\.[ch]p?p?" -print0 | xargs -0 -n1 misc/run-clang-format --check
-
-echo "Format is ok"
diff --git a/misc/format b/misc/format
deleted file mode 100755 (executable)
index 0e361a7..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/sh -e
-
-if [ -n "${VERBOSE}" ]; then
-  set -x
-fi
-
-find src unittest -path src/third_party -prune -o -regex ".*\.[ch]p?p?" -exec misc/run-clang-format {} \;
-
-echo "Formatting complete"
diff --git a/misc/format-files b/misc/format-files
new file mode 100755 (executable)
index 0000000..b9a2c28
--- /dev/null
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+set -eu
+
+tmp_file=$(mktemp)
+trap "rm $tmp_file" EXIT
+
+all=
+check=
+
+for arg in "$@"; do
+    case $arg in
+        --all)
+            all=$arg
+            ;;
+        --check)
+            check=$arg
+            ;;
+        *)
+            break
+            ;;
+    esac
+    shift
+done
+
+if [ -n "$all" ]; then
+    exec "$0" $check $(git ls-files '*.[ch]' '*.[ch]pp' ':!:src/third_party')
+fi
+
+clang_format=${CLANG_FORMAT:-clang-format}
+[ -t 1 ] && cf_color="--color=always" || cf_color=""
+
+if [ -n "${VERBOSE:-}" ]; then
+    "$clang_format" --version
+fi
+
+status=0
+for file in "$@"; do
+    "$clang_format" "$file" >"$tmp_file"
+    if cmp -s "$file" "$tmp_file"; then
+        continue
+    fi
+
+    if [ -n "$check" ]; then
+        echo "Error: $file not formatted with clang-format"
+        echo 'Run "make format" or apply this diff:'
+        git diff $cf_color --no-index "$file" "$tmp_file" \
+            | sed -r -e "s!^---.*!--- a/$file!" \
+                     -e "s!^\+\+\+.*!+++ b/$file!" \
+                     -e "/diff --/d" -e "/index /d" \
+                     -e "s/.[0-9]*.clang-format.tmp//"
+    else
+        echo "Reformatted $file"
+        cp "$tmp_file" "$file"
+    fi
+done
+
+exit $status
diff --git a/misc/run-clang-format b/misc/run-clang-format
deleted file mode 100755 (executable)
index 27544a8..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-#!/bin/bash
-
-# Reformat the given C++ file if required, or if --check
-# is specified, only print the diff.
-# Exits with 0 if the file was already formatted correctly.
-
-set -eu
-
-clang_format="${CLANG_FORMAT:-clang-format}"
-cf_diff_color="${cf_diff_color:-}"
-
-if [[ "${1:-}" == "--check" ]]; then
-    shift
-    check=true
-else
-    check=false
-fi
-
-if [[ $# -ne 1 ]]; then
-    echo "Usage: $0 [--check] file.cpp"
-    exit 1
-fi
-
-file="$1"
-
-if [[ ! -e "$file" ]]; then
-    echo "No such file: $file"
-    exit 1
-fi
-
-if [[ -d "$file" ]]; then
-    echo "Please pass files and not directories: $file"
-    exit 1
-fi
-
-tmp_file="$file.$$.clang-format.tmp"
-trap "rm -f \"$tmp_file\"" EXIT
-
-"$clang_format" "$file" >"$tmp_file"
-
-if ! cmp -s "$file" "$tmp_file"; then
-    if $check; then
-        git diff $cf_diff_color --no-index "$file" "$tmp_file" |
-            sed -r -e "s!^---.*!--- a/$file!" -e "s!^\+\+\+.*!+++ b/$file!" \
-            -e "/diff --/d" -e "/index /d" -e "s/.[0-9]*.clang-format.tmp//"
-        exit 1
-    else
-        echo "Reformatted $file"
-        mv "$tmp_file" "$file" && trap '' EXIT
-    fi
-fi