From c4922e97aeb4def88fa62c579d607722fe3c6c20 Mon Sep 17 00:00:00 2001 From: Zachary Anderson Date: Sat, 26 Jun 2021 22:13:26 -0700 Subject: [PATCH] Use package:clang_tidy in git pre-push hook (flutter/engine#26986) --- .../src/flutter/tools/githooks/bin/main.dart | 6 ++- .../githooks/lib/src/pre_push_command.dart | 46 +++++++++++++++---- .../src/flutter/tools/githooks/pubspec.yaml | 14 ++++++ 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/engine/src/flutter/tools/githooks/bin/main.dart b/engine/src/flutter/tools/githooks/bin/main.dart index 48a4376f9c..197c6d8a63 100644 --- a/engine/src/flutter/tools/githooks/bin/main.dart +++ b/engine/src/flutter/tools/githooks/bin/main.dart @@ -4,8 +4,10 @@ // @dart = 2.12 +import 'dart:io' as io; + import 'package:githooks/githooks.dart'; -Future main(List args) async { - return run(args); +Future main(List args) async { + io.exitCode = await run(args); } diff --git a/engine/src/flutter/tools/githooks/lib/src/pre_push_command.dart b/engine/src/flutter/tools/githooks/lib/src/pre_push_command.dart index 3e27c09b58..83ec738d5c 100644 --- a/engine/src/flutter/tools/githooks/lib/src/pre_push_command.dart +++ b/engine/src/flutter/tools/githooks/lib/src/pre_push_command.dart @@ -7,6 +7,7 @@ import 'dart:io' as io; import 'package:args/command_runner.dart'; +import 'package:clang_tidy/clang_tidy.dart'; import 'package:path/path.dart' as path; /// The command that implements the pre-push githook @@ -23,7 +24,7 @@ class PrePushCommand extends Command { final bool verbose = globalResults!['verbose']! as bool; final String flutterRoot = globalResults!['flutter']! as String; final List checkResults = await Future.wait(>[ - _runLinter(flutterRoot, verbose), + _runClangTidy(flutterRoot, verbose), _runFormatter(flutterRoot, verbose), ]); sw.stop(); @@ -31,17 +32,42 @@ class PrePushCommand extends Command { return !checkResults.contains(false); } - Future _runLinter(String flutterRoot, bool verbose) async { - if (io.Platform.isWindows) { - return true; - } - return _runCheck( + Future _runClangTidy(String flutterRoot, bool verbose) async { + // First ensure that out/host_debug/compile_commands.json exists by running + // //flutter/tools/gn. + final io.File compileCommands = io.File(path.join( flutterRoot, - path.join(flutterRoot, 'ci', 'lint.sh'), - [], - 'Linting check', - verbose: verbose, + '..', + 'out', + 'host_debug', + 'compile_commands.json', + )); + if (!compileCommands.existsSync()) { + final bool gnResult = await _runCheck( + flutterRoot, + path.join(flutterRoot, 'tools', 'gn'), + [], + 'GN for host_debug', + verbose: verbose, + ); + if (!gnResult) { + return false; + } + } + final StringBuffer outBuffer = StringBuffer(); + final StringBuffer errBuffer = StringBuffer(); + final ClangTidy clangTidy = ClangTidy( + buildCommandsPath: compileCommands, + repoPath: io.Directory(flutterRoot), + outSink: outBuffer, + errSink: errBuffer, ); + final int clangTidyResult = await clangTidy.run(); + if (clangTidyResult != 0) { + io.stderr.write(errBuffer); + return false; + } + return true; } Future _runFormatter(String flutterRoot, bool verbose) { diff --git a/engine/src/flutter/tools/githooks/pubspec.yaml b/engine/src/flutter/tools/githooks/pubspec.yaml index 9af36bdba1..e74bd01502 100644 --- a/engine/src/flutter/tools/githooks/pubspec.yaml +++ b/engine/src/flutter/tools/githooks/pubspec.yaml @@ -29,13 +29,27 @@ dev_dependencies: dependency_overrides: args: path: ../../../third_party/dart/third_party/pkg/args + async: + path: ../../../third_party/dart/third_party/pkg/async async_helper: path: ../../../third_party/dart/pkg/async_helper + clang_tidy: + path: ../clang_tidy + collection: + path: ../../../third_party/dart/third_party/pkg/collection expect: path: ../../../third_party/dart/pkg/expect + file: + path: ../../../third_party/pkg/file/packages/file litetest: path: ../../testing/litetest meta: path: ../../../third_party/dart/pkg/meta path: path: ../../../third_party/dart/third_party/pkg/path + platform: + path: ../../../third_party/pkg/platform + process: + path: ../../../third_party/pkg/process + process_runner: + path: ../../../third_party/pkg/process_runner