diff --git a/Makefile b/Makefile index e7d83a2b6b895..a9763177ea4f8 100644 --- a/Makefile +++ b/Makefile @@ -1102,22 +1102,18 @@ else @echo Cannot run an astyle check, your system either does not have astyle, or it is too old. endif -JSON_FILES = $(shell find data -name "*.json" | sed "s|^\./||") -JSON_WHITELIST = $(filter-out $(shell cat json_blacklist), $(JSON_FILES)) - -style-json: $(JSON_WHITELIST) - -$(JSON_WHITELIST): json_blacklist json_formatter +style-json: json_blacklist $(JSON_FORMATTER_BIN) ifndef CROSS - @$(JSON_FORMATTER_BIN) $@ + find data -name "*.json" -print0 | grep -v -z -F -f json_blacklist | \ + xargs -0 -L 1 $(JSON_FORMATTER_BIN) else @echo Cannot run json formatter in cross compiles. endif -style-all-json: json_formatter +style-all-json: $(JSON_FORMATTER_BIN) find data -name "*.json" -print0 | xargs -0 -L 1 $(JSON_FORMATTER_BIN) -json_formatter: $(JSON_FORMATTER_SOURCES) +$(JSON_FORMATTER_BIN): $(JSON_FORMATTER_SOURCES) $(CXX) $(CXXFLAGS) $(TOOL_CXXFLAGS) -Itools/format -Isrc \ $(JSON_FORMATTER_SOURCES) -o $(JSON_FORMATTER_BIN) diff --git a/doc/DEVELOPER_TOOLING.md b/doc/DEVELOPER_TOOLING.md index bd4d001395204..d7d86864be1e3 100644 --- a/doc/DEVELOPER_TOOLING.md +++ b/doc/DEVELOPER_TOOLING.md @@ -1,3 +1,18 @@ +## Pre-commit hook + +If you have all the relevant tools installed, you can have git automatically +check the style of code and json by adding these commands to your git +pre-commit hook (typically at `.git/hooks/pre-commit`): + +```BASH +git diff --cached --name-only -z HEAD | grep -z 'data/.*\.json' | \ + xargs -r -0 -L 1 ./tools/format/json_formatter.[ce]* || exit 1 + +make astyle-check || exit 1 +``` + +More details below on how to make these work and other ways to invoke these tools. + ## Code style (astyle) Automatic formatting of source code is performed by [Artistic Style](http://astyle.sourceforge.net/). diff --git a/tools/format/format.cpp b/tools/format/format.cpp index 01027a3e77f40..69c9f99fc7df5 100644 --- a/tools/format/format.cpp +++ b/tools/format/format.cpp @@ -223,11 +223,9 @@ int main( int argc, char *argv[] ) #else bool supports_color = isatty( STDOUT_FILENO ); #endif - std::string color_good = supports_color ? "\x1b[32m" : std::string(); std::string color_bad = supports_color ? "\x1b[31m" : std::string(); std::string color_end = supports_color ? "\x1b[0m" : std::string(); if( in_str == out.str() ) { - std::cout << color_good << "Well formatted: " << color_end << filename << std::endl; exit( EXIT_SUCCESS ); } else { std::ofstream fout( filename, std::ios::binary | std::ios::trunc );