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

Commit 3843b38

Browse files
[tool] Improve main-branch detection (#7038)
* [tool] Improve main-branch detection Currently main-branch detection for `--packages-for-branch` looks at branch names, but this no longer works on LUCI which now seems to be checking out specific hashes rather than branches. This updates the behavior so that it will treat any hash that is an ancestor of `main` as being part of `main`, which should allow post-submit detection to work under LUCI. Fixes flutter/flutter#119330 * Fix throw * Fix typos * Update comment
1 parent 8f12b27 commit 3843b38

File tree

4 files changed

+83
-14
lines changed

4 files changed

+83
-14
lines changed

script/tool/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.13.4+1
2+
3+
* Makes `--packages-for-branch` detect any commit on `main` as being `main`,
4+
so that it works with pinned checkouts (e.g., on LUCI).
5+
16
## 0.13.4
27

38
* Adds the ability to validate minimum supported Dart/Flutter versions in

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,28 @@ abstract class PackageCommand extends Command<void> {
316316
} else if (getBoolArg(_packagesForBranchArg)) {
317317
final String? branch = await _getBranch();
318318
if (branch == null) {
319-
printError('Unabled to determine branch; --$_packagesForBranchArg can '
319+
printError('Unable to determine branch; --$_packagesForBranchArg can '
320320
'only be used in a git repository.');
321321
throw ToolExit(exitInvalidArguments);
322322
} else {
323323
// Configure the change finder the correct mode for the branch.
324-
final bool lastCommitOnly = branch == 'main' || branch == 'master';
324+
// Log the mode to make it easier to audit logs to see that the
325+
// intended diff was used (or why).
326+
final bool lastCommitOnly;
327+
if (branch == 'main' || branch == 'master') {
328+
print('--$_packagesForBranchArg: running on default branch.');
329+
lastCommitOnly = true;
330+
} else if (await _isCheckoutFromBranch('main')) {
331+
print(
332+
'--$_packagesForBranchArg: running on a commit from default branch.');
333+
lastCommitOnly = true;
334+
} else {
335+
print('--$_packagesForBranchArg: running on branch "$branch".');
336+
lastCommitOnly = false;
337+
}
325338
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.');
339+
print(
340+
'--$_packagesForBranchArg: using parent commit as the diff base.');
330341
changedFileFinder = GitVersionFinder(await gitDir, 'HEAD~');
331342
} else {
332343
changedFileFinder = await retrieveVersionFinder();
@@ -522,6 +533,17 @@ abstract class PackageCommand extends Command<void> {
522533
return packages;
523534
}
524535

536+
// Returns true if the current checkout is on an ancestor of [branch].
537+
//
538+
// This is used because CI may check out a specific hash rather than a branch,
539+
// in which case branch-name detection won't work.
540+
Future<bool> _isCheckoutFromBranch(String branchName) async {
541+
final io.ProcessResult result = await (await gitDir).runCommand(
542+
<String>['merge-base', '--is-ancestor', 'HEAD', branchName],
543+
throwOnError: false);
544+
return result.exitCode == 0;
545+
}
546+
525547
Future<String?> _getBranch() async {
526548
final io.ProcessResult branchResult = await (await gitDir).runCommand(
527549
<String>['rev-parse', '--abbrev-ref', 'HEAD'],

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.13.4
4+
version: 0.13.4+1
55

66
dependencies:
77
args: ^2.1.0

script/tool/test/common/package_command_test.dart

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,8 @@ packages/b_package/lib/src/foo.dart
778778
MockProcess(stdout: 'a-branch'),
779779
];
780780
processRunner.mockProcessesForExecutable['git-merge-base'] = <Process>[
781-
MockProcess(stdout: 'abc123'),
781+
MockProcess(exitCode: 1), // --is-ancestor check
782+
MockProcess(stdout: 'abc123'), // finding merge base
782783
];
783784
final RepositoryPackage plugin1 =
784785
createFakePlugin('plugin1', packagesDir);
@@ -791,6 +792,7 @@ packages/b_package/lib/src/foo.dart
791792
expect(
792793
output,
793794
containsAllInOrder(<Matcher>[
795+
contains('--packages-for-branch: running on branch "a-branch"'),
794796
contains(
795797
'Running for all packages that have diffs relative to "abc123"'),
796798
]));
@@ -822,8 +824,9 @@ packages/b_package/lib/src/foo.dart
822824
expect(
823825
output,
824826
containsAllInOrder(<Matcher>[
825-
contains('--packages-for-branch: running on default branch; '
826-
'using parent commit as the diff base'),
827+
contains('--packages-for-branch: running on default branch.'),
828+
contains(
829+
'--packages-for-branch: using parent commit as the diff base'),
827830
contains(
828831
'Running for all packages that have diffs relative to "HEAD~"'),
829832
]));
@@ -836,7 +839,45 @@ packages/b_package/lib/src/foo.dart
836839
));
837840
});
838841

839-
test('tests all packages on master', () async {
842+
test(
843+
'only tests changed packages relative to the previous commit if '
844+
'running on a specific hash from main', () async {
845+
processRunner.mockProcessesForExecutable['git-diff'] = <Process>[
846+
MockProcess(stdout: 'packages/plugin1/plugin1.dart'),
847+
];
848+
processRunner.mockProcessesForExecutable['git-rev-parse'] = <Process>[
849+
MockProcess(stdout: 'HEAD'),
850+
];
851+
final RepositoryPackage plugin1 =
852+
createFakePlugin('plugin1', packagesDir);
853+
createFakePlugin('plugin2', packagesDir);
854+
855+
final List<String> output = await runCapturingPrint(
856+
runner, <String>['sample', '--packages-for-branch']);
857+
858+
expect(command.plugins, unorderedEquals(<String>[plugin1.path]));
859+
expect(
860+
output,
861+
containsAllInOrder(<Matcher>[
862+
contains(
863+
'--packages-for-branch: running on a commit from default branch.'),
864+
contains(
865+
'--packages-for-branch: using parent commit as the diff base'),
866+
contains(
867+
'Running for all packages that have diffs relative to "HEAD~"'),
868+
]));
869+
// Ensure that it's diffing against the prior commit.
870+
expect(
871+
processRunner.recordedCalls,
872+
contains(
873+
const ProcessCall(
874+
'git-diff', <String>['--name-only', 'HEAD~', 'HEAD'], null),
875+
));
876+
});
877+
878+
test(
879+
'only tests changed packages relative to the previous commit on master',
880+
() async {
840881
processRunner.mockProcessesForExecutable['git-diff'] = <Process>[
841882
MockProcess(stdout: 'packages/plugin1/plugin1.dart'),
842883
];
@@ -854,8 +895,9 @@ packages/b_package/lib/src/foo.dart
854895
expect(
855896
output,
856897
containsAllInOrder(<Matcher>[
857-
contains('--packages-for-branch: running on default branch; '
858-
'using parent commit as the diff base'),
898+
contains('--packages-for-branch: running on default branch.'),
899+
contains(
900+
'--packages-for-branch: using parent commit as the diff base'),
859901
contains(
860902
'Running for all packages that have diffs relative to "HEAD~"'),
861903
]));
@@ -887,7 +929,7 @@ packages/b_package/lib/src/foo.dart
887929
expect(
888930
output,
889931
containsAllInOrder(<Matcher>[
890-
contains('Unabled to determine branch'),
932+
contains('Unable to determine branch'),
891933
]));
892934
});
893935
});

0 commit comments

Comments
 (0)