Skip to content

Commit e8f19ed

Browse files
[tool] Only run postsubmit on changed packages (#6516)
1 parent 35b9755 commit e8f19ed

File tree

5 files changed

+71
-34
lines changed

5 files changed

+71
-34
lines changed

script/tool/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
## 0.11.1
1+
## 12.0
22

3+
* Changes the behavior of `--packages-for-branch` on main/master to run for
4+
packages changed in the last commit, rather than running for all packages.
5+
This allows CI to test the same filtered set of packages in post-submit as are
6+
tested in presubmit.
37
* Adds a `fix` command to run `dart fix --apply` in target packages.
48

59
## 0.11

script/tool/lib/src/common/git_version_finder.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class GitVersionFinder {
103103
<String>['merge-base', '--fork-point', 'FETCH_HEAD', 'HEAD'],
104104
throwOnError: false);
105105
final String stdout = (baseShaFromMergeBase.stdout as String? ?? '').trim();
106-
final String stderr = (baseShaFromMergeBase.stdout as String? ?? '').trim();
106+
final String stderr = (baseShaFromMergeBase.stderr as String? ?? '').trim();
107107
if (stderr.isNotEmpty || stdout.isEmpty) {
108108
baseShaFromMergeBase = await baseGitDir
109109
.runCommand(<String>['merge-base', 'FETCH_HEAD', 'HEAD']);

script/tool/lib/src/common/package_command.dart

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ abstract class PackageCommand extends Command<void> {
8484
'Cannot be combined with $_packagesArg.\n',
8585
hide: true);
8686
argParser.addFlag(_packagesForBranchArg,
87-
help:
88-
'This runs on all packages (equivalent to no package selection flag)\n'
89-
'on main (or master), and behaves like --run-on-changed-packages on '
87+
help: 'This runs on all packages changed in the last commit on main '
88+
'(or master), and behaves like --run-on-changed-packages on '
9089
'any other branch.\n\n'
9190
'Cannot be combined with $_packagesArg.\n\n'
9291
'This is intended for use in CI.\n',
@@ -311,34 +310,38 @@ abstract class PackageCommand extends Command<void> {
311310

312311
Set<String> packages = Set<String>.from(getStringListArg(_packagesArg));
313312

314-
final bool runOnChangedPackages;
313+
final GitVersionFinder? changedFileFinder;
315314
if (getBoolArg(_runOnChangedPackagesArg)) {
316-
runOnChangedPackages = true;
315+
changedFileFinder = await retrieveVersionFinder();
317316
} else if (getBoolArg(_packagesForBranchArg)) {
318317
final String? branch = await _getBranch();
319318
if (branch == null) {
320319
printError('Unabled to determine branch; --$_packagesForBranchArg can '
321320
'only be used in a git repository.');
322321
throw ToolExit(exitInvalidArguments);
323322
} else {
324-
runOnChangedPackages = branch != 'master' && branch != 'main';
325-
// Log the mode for auditing what was intended to run.
326-
print('--$_packagesForBranchArg: running on '
327-
'${runOnChangedPackages ? 'changed' : 'all'} packages');
323+
// Configure the change finder the correct mode for the branch.
324+
final bool lastCommitOnly = branch == 'main' || branch == 'master';
325+
if (lastCommitOnly) {
326+
// Log the mode to make it easier to audit logs to see that the
327+
// intended diff was used.
328+
print('--$_packagesForBranchArg: running on default branch; '
329+
'using parent commit as the diff base.');
330+
changedFileFinder = GitVersionFinder(await gitDir, 'HEAD~');
331+
} else {
332+
changedFileFinder = await retrieveVersionFinder();
333+
}
328334
}
329335
} else {
330-
runOnChangedPackages = false;
336+
changedFileFinder = null;
331337
}
332338

333-
final Set<String> excludedPackageNames = getExcludedPackageNames();
334-
335-
if (runOnChangedPackages) {
336-
final GitVersionFinder gitVersionFinder = await retrieveVersionFinder();
337-
final String baseSha = await gitVersionFinder.getBaseSha();
339+
if (changedFileFinder != null) {
340+
final String baseSha = await changedFileFinder.getBaseSha();
338341
print(
339-
'Running for all packages that have changed relative to "$baseSha"\n');
342+
'Running for all packages that have diffs relative to "$baseSha"\n');
340343
final List<String> changedFiles =
341-
await gitVersionFinder.getChangedFiles();
344+
await changedFileFinder.getChangedFiles();
342345
if (!_changesRequireFullTest(changedFiles)) {
343346
packages = _getChangedPackageNames(changedFiles);
344347
}
@@ -362,6 +365,7 @@ abstract class PackageCommand extends Command<void> {
362365
.childDirectory('third_party')
363366
.childDirectory('packages');
364367

368+
final Set<String> excludedPackageNames = getExcludedPackageNames();
365369
for (final Directory dir in <Directory>[
366370
packagesDir,
367371
if (thirdPartyPackagesDirectory.existsSync()) thirdPartyPackagesDirectory,

script/tool/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: flutter_plugin_tools
22
description: Productivity utils for flutter/plugins and flutter/packages
33
repository: https://github.com/flutter/plugins/tree/main/script/tool
4-
version: 0.11.0
4+
version: 0.12.0
55

66
dependencies:
77
args: ^2.1.0

script/tool/test/common/package_command_test.dart

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ packages/plugin1/CHANGELOG
523523
output,
524524
containsAllInOrder(<Matcher>[
525525
contains(
526-
'Running for all packages that have changed relative to "main"'),
526+
'Running for all packages that have diffs relative to "main"'),
527527
]));
528528

529529
expect(command.plugins, unorderedEquals(<String>[plugin1.path]));
@@ -732,13 +732,17 @@ packages/b_package/lib/src/foo.dart
732732
});
733733

734734
group('--packages-for-branch', () {
735-
test('only tests changed packages on a branch', () async {
735+
test('only tests changed packages relative to the merge base on a branch',
736+
() async {
736737
processRunner.mockProcessesForExecutable['git-diff'] = <Process>[
737738
MockProcess(stdout: 'packages/plugin1/plugin1.dart'),
738739
];
739740
processRunner.mockProcessesForExecutable['git-rev-parse'] = <Process>[
740741
MockProcess(stdout: 'a-branch'),
741742
];
743+
processRunner.mockProcessesForExecutable['git-merge-base'] = <Process>[
744+
MockProcess(stdout: 'abc123'),
745+
];
742746
final RepositoryPackage plugin1 =
743747
createFakePlugin('plugin1', packagesDir);
744748
createFakePlugin('plugin2', packagesDir);
@@ -750,11 +754,20 @@ packages/b_package/lib/src/foo.dart
750754
expect(
751755
output,
752756
containsAllInOrder(<Matcher>[
753-
contains('--packages-for-branch: running on changed packages'),
757+
contains(
758+
'Running for all packages that have diffs relative to "abc123"'),
754759
]));
760+
// Ensure that it's diffing against the merge-base.
761+
expect(
762+
processRunner.recordedCalls,
763+
contains(
764+
const ProcessCall(
765+
'git-diff', <String>['--name-only', 'abc123', 'HEAD'], null),
766+
));
755767
});
756768

757-
test('tests all packages on main', () async {
769+
test('only tests changed packages relative to the previous commit on main',
770+
() async {
758771
processRunner.mockProcessesForExecutable['git-diff'] = <Process>[
759772
MockProcess(stdout: 'packages/plugin1/plugin1.dart'),
760773
];
@@ -763,19 +776,27 @@ packages/b_package/lib/src/foo.dart
763776
];
764777
final RepositoryPackage plugin1 =
765778
createFakePlugin('plugin1', packagesDir);
766-
final RepositoryPackage plugin2 =
767-
createFakePlugin('plugin2', packagesDir);
779+
createFakePlugin('plugin2', packagesDir);
768780

769781
final List<String> output = await runCapturingPrint(
770782
runner, <String>['sample', '--packages-for-branch']);
771783

772-
expect(command.plugins,
773-
unorderedEquals(<String>[plugin1.path, plugin2.path]));
784+
expect(command.plugins, unorderedEquals(<String>[plugin1.path]));
774785
expect(
775786
output,
776787
containsAllInOrder(<Matcher>[
777-
contains('--packages-for-branch: running on all packages'),
788+
contains('--packages-for-branch: running on default branch; '
789+
'using parent commit as the diff base'),
790+
contains(
791+
'Running for all packages that have diffs relative to "HEAD~"'),
778792
]));
793+
// Ensure that it's diffing against the prior commit.
794+
expect(
795+
processRunner.recordedCalls,
796+
contains(
797+
const ProcessCall(
798+
'git-diff', <String>['--name-only', 'HEAD~', 'HEAD'], null),
799+
));
779800
});
780801

781802
test('tests all packages on master', () async {
@@ -787,19 +808,27 @@ packages/b_package/lib/src/foo.dart
787808
];
788809
final RepositoryPackage plugin1 =
789810
createFakePlugin('plugin1', packagesDir);
790-
final RepositoryPackage plugin2 =
791-
createFakePlugin('plugin2', packagesDir);
811+
createFakePlugin('plugin2', packagesDir);
792812

793813
final List<String> output = await runCapturingPrint(
794814
runner, <String>['sample', '--packages-for-branch']);
795815

796-
expect(command.plugins,
797-
unorderedEquals(<String>[plugin1.path, plugin2.path]));
816+
expect(command.plugins, unorderedEquals(<String>[plugin1.path]));
798817
expect(
799818
output,
800819
containsAllInOrder(<Matcher>[
801-
contains('--packages-for-branch: running on all packages'),
820+
contains('--packages-for-branch: running on default branch; '
821+
'using parent commit as the diff base'),
822+
contains(
823+
'Running for all packages that have diffs relative to "HEAD~"'),
802824
]));
825+
// Ensure that it's diffing against the prior commit.
826+
expect(
827+
processRunner.recordedCalls,
828+
contains(
829+
const ProcessCall(
830+
'git-diff', <String>['--name-only', 'HEAD~', 'HEAD'], null),
831+
));
803832
});
804833

805834
test('throws if getting the branch fails', () async {

0 commit comments

Comments
 (0)