From 435af8b833ac0ac87e330f9237fbf11434ab24cb Mon Sep 17 00:00:00 2001 From: antonio2368 Date: Tue, 16 Feb 2021 11:22:59 +0100 Subject: [PATCH] Remove clang tidy from hooks (#94) * Removed clang-tidy from hooks * Improved clang-format output * Remove lcp from types to format --- .githooks/pre-commit | 10 +------- tools/git-clang-format | 54 ++++++++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 4c127c3d4..286008807 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -15,7 +15,7 @@ exec 1>&2 tmpdir=$(mktemp -d repo-XXXXXXXX) trap "rm -rf $tmpdir" EXIT INT -modified_files=$(git diff --cached --name-only --diff-filter=AM $against | sed -nE "/.*\.(cpp|cc|cxx|c|h|hpp|lcp)$/p") +modified_files=$(git diff --cached --name-only --diff-filter=AM $against | sed -nE "/.*\.(cpp|cc|cxx|c|h|hpp)$/p") for file in $modified_files; do echo "Checking $file..." @@ -30,14 +30,6 @@ for file in $modified_files; do if [ $code -ne 0 ]; then break fi - - echo "Running clang-tidy..." - $project_folder/tools/git-clang-tidy $tmpdir/$file - code=$? - - if [ $code -ne 0 ]; then - break - fi done; return $code diff --git a/tools/git-clang-format b/tools/git-clang-format index 59310d3c1..767efe84b 100755 --- a/tools/git-clang-format +++ b/tools/git-clang-format @@ -2,22 +2,46 @@ import re import sys import subprocess +import difflib -# Run this script through arc lint -MESSAGE = "Wrong formatting!" +def colorize(diff_lines): + def bold(s): + return '\x1b[1m' + s + '\x1b[0m' -data = subprocess.run( - ["clang-format", "--output-replacements-xml", sys.argv[1]], - stdout=subprocess.PIPE, check=True).stdout.decode( - "utf-8").strip().split("\n") -has_error = False -for row in data: - match = re.match( - r"^", row) - if match: - has_error = True - offset = int(match.group(1)) + int(match.group(2)) - print("warning:{}:{}".format(offset, MESSAGE)) + def cyan(s): + return '\x1b[36m' + s + '\x1b[0m' -sys.exit(1 if has_error else 0) + def green(s): + return '\x1b[32m' + s + '\x1b[0m' + + def red(s): + return '\x1b[31m' + s + '\x1b[0m' + + for line in diff_lines: + if line[:4] in ['--- ', '+++ ']: + yield bold(line) + elif line.startswith('@@ '): + yield cyan(line) + elif line.startswith('+'): + yield green(line) + elif line.startswith('-'): + yield red(line) + else: + yield line + + +proc = subprocess.Popen( + ["clang-format", sys.argv[1]], + stdout=subprocess.PIPE, encoding='utf-8') +with open(sys.argv[1], 'r') as original: + diff_lines = list( + difflib.unified_diff( + original.readlines(), + proc.stdout.readlines(), + fromfile="unformatted", + tofile="formatted", + n=3)) + +sys.stdout.writelines(colorize(diff_lines)) +sys.exit(1 if len(diff_lines) > 0 else 0)