Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit c0ee5f0

Browse files
authored
Move git_repo_tools and process_fakes outside of clang_tidy. (#46017)
Closes flutter/flutter#134988.
1 parent 0dbdd85 commit c0ee5f0

File tree

13 files changed

+419
-24
lines changed

13 files changed

+419
-24
lines changed

testing/run_tests.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,25 @@ def gather_engine_repo_tools_tests(build_dir):
10451045
)
10461046

10471047

1048+
def gather_git_repo_tools_tests(build_dir):
1049+
test_dir = os.path.join(
1050+
BUILDROOT_DIR, 'flutter', 'tools', 'pkg', 'git_repo_tools'
1051+
)
1052+
dart_tests = glob.glob('%s/*_test.dart' % test_dir)
1053+
for dart_test_file in dart_tests:
1054+
opts = [
1055+
'--disable-dart-dev',
1056+
dart_test_file,
1057+
]
1058+
yield EngineExecutableTask(
1059+
build_dir,
1060+
os.path.join('dart-sdk', 'bin', 'dart'),
1061+
None,
1062+
flags=opts,
1063+
cwd=test_dir
1064+
)
1065+
1066+
10481067
def gather_api_consistency_tests(build_dir):
10491068
test_dir = os.path.join(BUILDROOT_DIR, 'flutter', 'tools', 'api_check')
10501069
dart_tests = glob.glob('%s/test/*_test.dart' % test_dir)
@@ -1355,6 +1374,7 @@ def main():
13551374
tasks += list(gather_build_bucket_golden_scraper_tests(build_dir))
13561375
tasks += list(gather_engine_build_configs_tests(build_dir))
13571376
tasks += list(gather_engine_repo_tools_tests(build_dir))
1377+
tasks += list(gather_git_repo_tools_tests(build_dir))
13581378
tasks += list(gather_api_consistency_tests(build_dir))
13591379
tasks += list(gather_path_ops_tests(build_dir))
13601380
tasks += list(gather_const_finder_tests(build_dir))

tools/clang_tidy/lib/clang_tidy.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import 'dart:convert' show LineSplitter, jsonDecode;
66
import 'dart:io' as io show File, stderr, stdout;
77

88
import 'package:engine_repo_tools/engine_repo_tools.dart';
9+
import 'package:git_repo_tools/git_repo_tools.dart';
910
import 'package:meta/meta.dart';
1011
import 'package:path/path.dart' as path;
1112
import 'package:process/process.dart';
1213
import 'package:process_runner/process_runner.dart';
1314

1415
import 'src/command.dart';
15-
import 'src/git_repo.dart';
1616
import 'src/options.dart';
1717

1818
const String _linterOutputHeader = '''
@@ -211,7 +211,7 @@ class ClangTidy {
211211
.toList();
212212
}
213213

214-
final GitRepo repo = GitRepo(
214+
final GitRepo repo = GitRepo.fromRoot(
215215
options.repoPath,
216216
processManager: _processManager,
217217
verbose: options.verbose,

tools/clang_tidy/pubspec.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ environment:
1818

1919
dependencies:
2020
args: any
21+
git_repo_tools: any
2122
engine_repo_tools: any
2223
meta: any
2324
path: any
@@ -28,6 +29,7 @@ dev_dependencies:
2829
async_helper: any
2930
expect: any
3031
litetest: any
32+
process_fakes: any
3133
smith: any
3234

3335
dependency_overrides:
@@ -45,6 +47,8 @@ dependency_overrides:
4547
path: ../../../third_party/dart/pkg/expect
4648
file:
4749
path: ../../../third_party/pkg/file/packages/file
50+
git_repo_tools:
51+
path: ../pkg/git_repo_tools
4852
litetest:
4953
path: ../../testing/litetest
5054
meta:
@@ -55,6 +59,8 @@ dependency_overrides:
5559
path: ../../../third_party/pkg/platform
5660
process:
5761
path: ../../../third_party/pkg/process
62+
process_fakes:
63+
path: ../pkg/process_fakes
5864
process_runner:
5965
path: ../../../third_party/pkg/process_runner
6066
smith:

tools/clang_tidy/test/clang_tidy_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import 'package:engine_repo_tools/engine_repo_tools.dart';
1111
import 'package:litetest/litetest.dart';
1212
import 'package:path/path.dart' as path;
1313
import 'package:process/process.dart';
14+
import 'package:process_fakes/process_fakes.dart';
1415
import 'package:process_runner/process_runner.dart';
1516

16-
import 'process_fakes.dart';
1717

1818
/// A test fixture for the `clang-tidy` tool.
1919
final class Fixture {

tools/githooks/pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ dependency_overrides:
4545
path: ../../../third_party/dart/pkg/expect
4646
file:
4747
path: ../../../third_party/pkg/file/packages/file
48+
git_repo_tools:
49+
path: ../pkg/git_repo_tools
4850
litetest:
4951
path: ../../testing/litetest
5052
meta:

tools/pkg/git_repo_tools/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# `git_repo_tools`
2+
3+
This is a repo-internal library for `flutter/engine`, that contains shared code
4+
for writing tools that want to interact with the `git` repository. For example,
5+
finding all changed files in the current branch:
6+
7+
```dart
8+
import 'dart:io' as io show File, Platform;
9+
10+
import 'package:engine_repo_tools/engine_repo_tools.dart';
11+
import 'package:git_repo_tools/git_repo_tools.dart';
12+
import 'package:path/path.dart' as path;
13+
14+
void main() async {
15+
// Finds the root of the engine repository from the current script.
16+
final Engine engine = Engine.findWithin(path.dirname(path.fromUri(io.Platform.script)));
17+
final GitRepo gitRepo = GitRepo(engine.flutterDir);
18+
19+
for (final io.File file in gitRepo.changedFiles) {
20+
print('Changed file: ${file.path}');
21+
}
22+
}
23+
```

tools/clang_tidy/lib/src/git_repo.dart renamed to tools/pkg/git_repo_tools/lib/git_repo_tools.dart

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,66 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:io' as io show Directory, File;
5+
import 'dart:io' as io show Directory, File, stdout;
66

77
import 'package:path/path.dart' as path;
88
import 'package:process/process.dart';
99
import 'package:process_runner/process_runner.dart';
1010

11-
/// Utility methods for working with a git repo.
12-
class GitRepo {
11+
/// Utility methods for working with a git repository.
12+
final class GitRepo {
1313
/// The git repository rooted at `root`.
14-
GitRepo(this.root, {
14+
GitRepo.fromRoot(this.root, {
1515
this.verbose = false,
16+
StringSink? logSink,
1617
ProcessManager processManager = const LocalProcessManager(),
17-
}) : _processManager = processManager;
18+
}) :
19+
_processManager = processManager,
20+
logSink = logSink ?? io.stdout;
1821

1922
/// Whether to produce verbose log output.
23+
///
24+
/// If true, output of git commands will be printed to [logSink].
2025
final bool verbose;
2126

27+
/// Where to send verbose log output.
28+
///
29+
/// Defaults to [io.stdout].
30+
final StringSink logSink;
31+
2232
/// The root of the git repo.
2333
final io.Directory root;
2434

2535
/// The delegate to use for running processes.
2636
final ProcessManager _processManager;
2737

28-
List<io.File>? _changedFiles;
29-
3038
/// Returns a list of all non-deleted files which differ from the nearest
3139
/// merge-base with `main`. If it can't find a fork point, uses the default
3240
/// merge-base.
3341
///
3442
/// This is only computed once and cached. Subsequent invocations of the
3543
/// getter will return the same result.
36-
Future<List<io.File>> get changedFiles async =>
37-
_changedFiles ??= await _getChangedFiles();
38-
39-
List<io.File>? _changedFilesAtHead;
40-
41-
/// Returns a list of non-deleted files which differ between the HEAD
42-
/// commit and its parent.
43-
Future<List<io.File>> get changedFilesAtHead async =>
44-
_changedFilesAtHead ??= await _getChangedFilesAtHead();
44+
late final Future<List<io.File>> changedFiles = _changedFiles();
4545

46-
Future<List<io.File>> _getChangedFiles() async {
46+
Future<List<io.File>> _changedFiles() async {
4747
final ProcessRunner processRunner = ProcessRunner(
4848
defaultWorkingDirectory: root,
4949
processManager: _processManager,
5050
);
5151
await _fetch(processRunner);
52+
// Find the merge base between the current branch and the branch that was
53+
// checked out at the time of the last fetch. The merge base is the common
54+
// ancestor of the two branches, and the output is the hash of the merge
55+
// base.
5256
ProcessRunnerResult mergeBaseResult = await processRunner.runProcess(
5357
<String>['git', 'merge-base', '--fork-point', 'FETCH_HEAD', 'HEAD'],
5458
failOk: true,
5559
);
5660
if (mergeBaseResult.exitCode != 0) {
61+
if (verbose) {
62+
logSink.writeln('git merge-base --fork-point failed, using default merge-base');
63+
logSink.writeln('Output:\n${mergeBaseResult.stdout}');
64+
}
5765
mergeBaseResult = await processRunner.runProcess(<String>[
5866
'git',
5967
'merge-base',
@@ -62,8 +70,7 @@ class GitRepo {
6270
]);
6371
}
6472
final String mergeBase = mergeBaseResult.stdout.trim();
65-
final ProcessRunnerResult masterResult = await processRunner
66-
.runProcess(<String>[
73+
final ProcessRunnerResult masterResult = await processRunner.runProcess(<String>[
6774
'git',
6875
'diff',
6976
'--name-only',
@@ -73,9 +80,17 @@ class GitRepo {
7380
return _gitOutputToList(masterResult);
7481
}
7582

76-
Future<List<io.File>> _getChangedFilesAtHead() async {
83+
/// Returns a list of non-deleted files which differ between the HEAD
84+
/// commit and its parent.
85+
///
86+
/// This is only computed once and cached. Subsequent invocations of the
87+
/// getter will return the same result.
88+
late final Future<List<io.File>> changedFilesAtHead = _changedFilesAtHead();
89+
90+
Future<List<io.File>> _changedFilesAtHead() async {
7791
final ProcessRunner processRunner = ProcessRunner(
7892
defaultWorkingDirectory: root,
93+
processManager: _processManager,
7994
);
8095
await _fetch(processRunner);
8196
final ProcessRunnerResult diffTreeResult = await processRunner.runProcess(
@@ -98,6 +113,10 @@ class GitRepo {
98113
failOk: true,
99114
);
100115
if (fetchResult.exitCode != 0) {
116+
if (verbose) {
117+
logSink.writeln('git fetch upstream main failed, using origin main');
118+
logSink.writeln('Output:\n${fetchResult.stdout}');
119+
}
101120
await processRunner.runProcess(<String>[
102121
'git',
103122
'fetch',
@@ -110,7 +129,7 @@ class GitRepo {
110129
List<io.File> _gitOutputToList(ProcessRunnerResult result) {
111130
final String diffOutput = result.stdout.trim();
112131
if (verbose) {
113-
print('git diff output:\n$diffOutput');
132+
logSink.writeln('git diff output:\n$diffOutput');
114133
}
115134
final Set<String> resultMap = <String>{};
116135
resultMap.addAll(diffOutput.split('\n').where(

tools/pkg/git_repo_tools/pubspec.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2013 The Flutter Authors. All rights reserved.
2+
# Use of this source code is governed by a BSD-style license that can be
3+
# found in the LICENSE file.
4+
5+
name: git_repo_tools
6+
publish_to: none
7+
environment:
8+
sdk: '>=3.2.0-0 <4.0.0'
9+
10+
# Do not add any dependencies that require more than what is provided in
11+
# //third_party/pkg, //third_party/dart/pkg, or
12+
# //third_party/dart/third_party/pkg. In particular, package:test is not usable
13+
# here.
14+
15+
# If you do add packages here, make sure you can run `pub get --offline`, and
16+
# check the .packages and .package_config to make sure all the paths are
17+
# relative to this directory into //third_party/dart
18+
19+
dependencies:
20+
meta: any
21+
path: any
22+
process: any
23+
process_runner: any
24+
25+
dev_dependencies:
26+
async_helper: any
27+
expect: any
28+
litetest: any
29+
process_fakes: any
30+
smith: any
31+
32+
dependency_overrides:
33+
args:
34+
path: ../../../../third_party/dart/third_party/pkg/args
35+
async:
36+
path: ../../../../third_party/dart/third_party/pkg/async
37+
async_helper:
38+
path: ../../../../third_party/dart/pkg/async_helper
39+
collection:
40+
path: ../../../../third_party/dart/third_party/pkg/collection
41+
expect:
42+
path: ../../../../third_party/dart/pkg/expect
43+
file:
44+
path: ../../../../third_party/pkg/file/packages/file
45+
litetest:
46+
path: ../../../testing/litetest
47+
meta:
48+
path: ../../../../third_party/dart/pkg/meta
49+
path:
50+
path: ../../../../third_party/dart/third_party/pkg/path
51+
platform:
52+
path: ../../../../third_party/pkg/platform
53+
process:
54+
path: ../../../../third_party/pkg/process
55+
process_fakes:
56+
path: ../process_fakes
57+
process_runner:
58+
path: ../../../../third_party/pkg/process_runner
59+
smith:
60+
path: ../../../../third_party/dart/pkg/smith

0 commit comments

Comments
 (0)