From 5b36e44eafb84319fd0a630266f7547d7f58d45e Mon Sep 17 00:00:00 2001 From: Zachary Anderson Date: Wed, 28 Sep 2022 14:28:03 -0700 Subject: [PATCH] Adds a --lint-head flag to the clang-tidy script (flutter/engine#36476) * Adds a --lint-head flag to the clang-tidy script * Update tools/clang_tidy/lib/src/git_repo.dart Co-authored-by: Jenn Magder * Fix comment syntax Co-authored-by: Jenn Magder --- .../tools/clang_tidy/lib/clang_tidy.dart | 12 ++- .../tools/clang_tidy/lib/src/git_repo.dart | 75 +++++++++++++++---- .../tools/clang_tidy/lib/src/options.dart | 18 ++++- .../clang_tidy/test/clang_tidy_test.dart | 23 ++++++ 4 files changed, 109 insertions(+), 19 deletions(-) diff --git a/engine/src/flutter/tools/clang_tidy/lib/clang_tidy.dart b/engine/src/flutter/tools/clang_tidy/lib/clang_tidy.dart index 58f9a6ee20..b1af4486dd 100644 --- a/engine/src/flutter/tools/clang_tidy/lib/clang_tidy.dart +++ b/engine/src/flutter/tools/clang_tidy/lib/clang_tidy.dart @@ -47,6 +47,7 @@ class ClangTidy { required io.File buildCommandsPath, String checksArg = '', bool lintAll = false, + bool lintHead = false, bool fix = false, StringSink? outSink, StringSink? errSink, @@ -55,6 +56,7 @@ class ClangTidy { buildCommandsPath: buildCommandsPath, checksArg: checksArg, lintAll: lintAll, + lintHead: lintHead, fix: fix, errSink: errSink, ), @@ -159,7 +161,15 @@ class ClangTidy { .whereType() .toList(); } - return GitRepo(options.repoPath).changedFiles; + + final GitRepo repo = GitRepo( + options.repoPath, + verbose: options.verbose, + ); + if (options.lintHead) { + return repo.changedFilesAtHead; + } + return repo.changedFiles; } /// Given a build commands json file, and the files with local changes, diff --git a/engine/src/flutter/tools/clang_tidy/lib/src/git_repo.dart b/engine/src/flutter/tools/clang_tidy/lib/src/git_repo.dart index b39769b684..49d55a442e 100644 --- a/engine/src/flutter/tools/clang_tidy/lib/src/git_repo.dart +++ b/engine/src/flutter/tools/clang_tidy/lib/src/git_repo.dart @@ -10,7 +10,12 @@ import 'package:process_runner/process_runner.dart'; /// Utility methods for working with a git repo. class GitRepo { /// The git repository rooted at `root`. - GitRepo(this.root); + GitRepo(this.root, { + this.verbose = false, + }); + + /// Whether to produce verbose log output. + final bool verbose; /// The root of the git repo. final io.Directory root; @@ -26,23 +31,18 @@ class GitRepo { Future> get changedFiles async => _changedFiles ??= await _getChangedFiles(); + List? _changedFilesAtHead; + + /// Returns a list of non-deleted files which differ between the HEAD + /// commit and its parent. + Future> get changedFilesAtHead async => + _changedFilesAtHead ??= await _getChangedFilesAtHead(); + Future> _getChangedFiles() async { final ProcessRunner processRunner = ProcessRunner( defaultWorkingDirectory: root, ); - final ProcessRunnerResult fetchResult = await processRunner.runProcess( - ['git', 'fetch', 'upstream', 'main'], - failOk: true, - ); - if (fetchResult.exitCode != 0) { - await processRunner.runProcess([ - 'git', - 'fetch', - 'origin', - 'main', - ]); - } - final Set result = {}; + await _fetch(processRunner); ProcessRunnerResult mergeBaseResult = await processRunner.runProcess( ['git', 'merge-base', '--fork-point', 'FETCH_HEAD', 'HEAD'], failOk: true, @@ -64,10 +64,53 @@ class GitRepo { '--diff-filter=ACMRT', mergeBase, ]); - result.addAll(masterResult.stdout.split('\n').where( + return _gitOutputToList(masterResult); + } + + Future> _getChangedFilesAtHead() async { + final ProcessRunner processRunner = ProcessRunner( + defaultWorkingDirectory: root, + ); + await _fetch(processRunner); + final ProcessRunnerResult diffTreeResult = await processRunner.runProcess( + [ + 'git', + 'diff-tree', + '--no-commit-id', + '--name-only', + '--diff-filter=ACMRT', // Added, copied, modified, renamed, or type-changed. + '-r', + 'HEAD', + ], + ); + return _gitOutputToList(diffTreeResult); + } + + Future _fetch(ProcessRunner processRunner) async { + final ProcessRunnerResult fetchResult = await processRunner.runProcess( + ['git', 'fetch', 'upstream', 'main'], + failOk: true, + ); + if (fetchResult.exitCode != 0) { + await processRunner.runProcess([ + 'git', + 'fetch', + 'origin', + 'main', + ]); + } + } + + List _gitOutputToList(ProcessRunnerResult result) { + final String diffOutput = result.stdout.trim(); + if (verbose) { + print('git diff output:\n$diffOutput'); + } + final Set resultMap = {}; + resultMap.addAll(diffOutput.split('\n').where( (String str) => str.isNotEmpty, )); - return result.map( + return resultMap.map( (String filePath) => io.File(path.join(root.path, filePath)), ).toList(); } diff --git a/engine/src/flutter/tools/clang_tidy/lib/src/options.dart b/engine/src/flutter/tools/clang_tidy/lib/src/options.dart index a73dbc5d80..1b07bcf0de 100644 --- a/engine/src/flutter/tools/clang_tidy/lib/src/options.dart +++ b/engine/src/flutter/tools/clang_tidy/lib/src/options.dart @@ -20,6 +20,7 @@ class Options { this.verbose = false, this.checksArg = '', this.lintAll = false, + this.lintHead = false, this.fix = false, this.errorMessage, StringSink? errSink, @@ -60,6 +61,7 @@ class Options { checksArg: options.wasParsed('checks') ? options['checks'] as String : '', lintAll: io.Platform.environment['FLUTTER_LINT_ALL'] != null || options['lint-all'] as bool, + lintHead: options['lint-head'] as bool, fix: options['fix'] as bool, errSink: errSink, ); @@ -104,7 +106,11 @@ class Options { ) ..addFlag( 'lint-all', - help: 'lint all of the sources, regardless of FLUTTER_NOLINT.', + help: 'Lint all of the sources, regardless of FLUTTER_NOLINT.', + ) + ..addFlag( + 'lint-head', + help: 'Lint files changed in the tip-of-tree commit.', ) ..addFlag( 'fix', @@ -163,6 +169,9 @@ class Options { /// Whether all files should be linted. final bool lintAll; + /// Whether to lint only files changed in the tip-of-tree commit. + final bool lintHead; + /// Whether checks should apply available fix-ups to the working copy. final bool fix; @@ -178,7 +187,8 @@ class Options { _errSink.writeln(message); } _errSink.writeln( - 'Usage: bin/main.dart [--help] [--lint-all] [--fix] [--verbose] [--diff-branch] [--target-variant variant] [--src-dir path/to/engine/src]', + 'Usage: bin/main.dart [--help] [--lint-all] [--lint-head] [--fix] [--verbose] ' + '[--diff-branch] [--target-variant variant] [--src-dir path/to/engine/src]', ); _errSink.writeln(_argParser.usage); } @@ -198,6 +208,10 @@ class Options { return 'ERROR: --compile-commands option cannot be used with --src-dir.'; } + if (argResults.wasParsed('lint-all') && argResults.wasParsed('lint-head')) { + return 'ERROR: At most one of --lint-all and --lint-head can be passed.'; + } + if (!buildCommandsPath.existsSync()) { return "ERROR: Build commands path ${buildCommandsPath.absolute.path} doesn't exist."; } diff --git a/engine/src/flutter/tools/clang_tidy/test/clang_tidy_test.dart b/engine/src/flutter/tools/clang_tidy/test/clang_tidy_test.dart index b96c5a307a..76e8eeab82 100644 --- a/engine/src/flutter/tools/clang_tidy/test/clang_tidy_test.dart +++ b/engine/src/flutter/tools/clang_tidy/test/clang_tidy_test.dart @@ -128,6 +128,29 @@ Future main(List args) async { )); }); + test('Error when --lint-all and --lint-head are used together', () async { + final StringBuffer outBuffer = StringBuffer(); + final StringBuffer errBuffer = StringBuffer(); + final ClangTidy clangTidy = ClangTidy.fromCommandLine( + [ + '--compile-commands', + '/unused', + '--lint-all', + '--lint-head', + ], + outSink: outBuffer, + errSink: errBuffer, + ); + + final int result = await clangTidy.run(); + + expect(clangTidy.options.help, isFalse); + expect(result, equals(1)); + expect(errBuffer.toString(), contains( + 'ERROR: At most one of --lint-all and --lint-head can be passed.', + )); + }); + test('lintAll=true checks all files', () async { final StringBuffer outBuffer = StringBuffer(); final StringBuffer errBuffer = StringBuffer();