- Simplified scripts.
- Made colored output work again.
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})
+++ /dev/null
-#!/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"
+++ /dev/null
-#!/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"
--- /dev/null
+#!/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
+++ /dev/null
-#!/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