[go: nahoru, domu]

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

[tool] Improve main-branch detection #7038

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions script/tool/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.13.4+1

* Makes `--packages-for-branch` detect any commin on `main` as being `main`,
tarrinneal marked this conversation as resolved.
Show resolved Hide resolved
so that it works with pinned checkouts (e.g., on LUCI).

## 0.13.4

* Adds the ability to validate minimum supported Dart/Flutter versions in
Expand Down
35 changes: 29 additions & 6 deletions script/tool/lib/src/common/package_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,28 @@ abstract class PackageCommand extends Command<void> {
} else if (getBoolArg(_packagesForBranchArg)) {
final String? branch = await _getBranch();
if (branch == null) {
printError('Unabled to determine branch; --$_packagesForBranchArg can '
printError('Unable to determine branch; --$_packagesForBranchArg can '
'only be used in a git repository.');
throw ToolExit(exitInvalidArguments);
} else {
// Configure the change finder the correct mode for the branch.
final bool lastCommitOnly = branch == 'main' || branch == 'master';
// Log the mode to make it easier to audit logs to see that the
// intended diff was used (or why).
final bool lastCommitOnly;
if (branch == 'main' || branch == 'master') {
print('--$_packagesForBranchArg: running on default branch.');
lastCommitOnly = true;
} else if (await _isCheckoutFromBranch('main')) {
print(
'--$_packagesForBranchArg: running on a commit from default branch.');
lastCommitOnly = true;
} else {
print('--$_packagesForBranchArg: running on branch "$branch".');
lastCommitOnly = false;
}
if (lastCommitOnly) {
// Log the mode to make it easier to audit logs to see that the
// intended diff was used.
print('--$_packagesForBranchArg: running on default branch; '
'using parent commit as the diff base.');
print(
'--$_packagesForBranchArg: using parent commit as the diff base.');
changedFileFinder = GitVersionFinder(await gitDir, 'HEAD~');
} else {
changedFileFinder = await retrieveVersionFinder();
Expand Down Expand Up @@ -522,6 +533,18 @@ abstract class PackageCommand extends Command<void> {
return packages;
}

// Retruns true if the environment indicates that the current treeish is from
tarrinneal marked this conversation as resolved.
Show resolved Hide resolved
// [branch].
//
// This is used because CI may check out a specific hash rather than a branch,
// in which case branch-name detection won't work.
Future<bool> _isCheckoutFromBranch(String branchName) async {
final io.ProcessResult result = await (await gitDir).runCommand(
<String>['merge-base', '--is-ancestor', 'HEAD', branchName],
throwOnError: false);
return result.exitCode == 0;
}

Future<String?> _getBranch() async {
final io.ProcessResult branchResult = await (await gitDir).runCommand(
<String>['rev-parse', '--abbrev-ref', 'HEAD'],
Expand Down
2 changes: 1 addition & 1 deletion script/tool/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: flutter_plugin_tools
description: Productivity utils for flutter/plugins and flutter/packages
repository: https://github.com/flutter/plugins/tree/main/script/tool
version: 0.13.4
version: 0.13.4+1

dependencies:
args: ^2.1.0
Expand Down
56 changes: 49 additions & 7 deletions script/tool/test/common/package_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,8 @@ packages/b_package/lib/src/foo.dart
MockProcess(stdout: 'a-branch'),
];
processRunner.mockProcessesForExecutable['git-merge-base'] = <Process>[
MockProcess(stdout: 'abc123'),
MockProcess(exitCode: 1), // --is-ancestor check
MockProcess(stdout: 'abc123'), // finding merge base
];
final RepositoryPackage plugin1 =
createFakePlugin('plugin1', packagesDir);
Expand All @@ -791,6 +792,7 @@ packages/b_package/lib/src/foo.dart
expect(
output,
containsAllInOrder(<Matcher>[
contains('--packages-for-branch: running on branch "a-branch"'),
contains(
'Running for all packages that have diffs relative to "abc123"'),
]));
Expand Down Expand Up @@ -822,8 +824,9 @@ packages/b_package/lib/src/foo.dart
expect(
output,
containsAllInOrder(<Matcher>[
contains('--packages-for-branch: running on default branch; '
'using parent commit as the diff base'),
contains('--packages-for-branch: running on default branch.'),
contains(
'--packages-for-branch: using parent commit as the diff base'),
contains(
'Running for all packages that have diffs relative to "HEAD~"'),
]));
Expand All @@ -836,7 +839,45 @@ packages/b_package/lib/src/foo.dart
));
});

test('tests all packages on master', () async {
test(
'only tests changed packages relative to the previous commit if '
'running on a specific hash from main', () async {
processRunner.mockProcessesForExecutable['git-diff'] = <Process>[
MockProcess(stdout: 'packages/plugin1/plugin1.dart'),
];
processRunner.mockProcessesForExecutable['git-rev-parse'] = <Process>[
MockProcess(stdout: 'HEAD'),
];
final RepositoryPackage plugin1 =
createFakePlugin('plugin1', packagesDir);
createFakePlugin('plugin2', packagesDir);

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

expect(command.plugins, unorderedEquals(<String>[plugin1.path]));
expect(
output,
containsAllInOrder(<Matcher>[
contains(
'--packages-for-branch: running on a commit from default branch.'),
contains(
'--packages-for-branch: using parent commit as the diff base'),
contains(
'Running for all packages that have diffs relative to "HEAD~"'),
]));
// Ensure that it's diffing against the prior commit.
expect(
processRunner.recordedCalls,
contains(
const ProcessCall(
'git-diff', <String>['--name-only', 'HEAD~', 'HEAD'], null),
));
});

test(
'only tests changed packages relative to the previous commit on master',
() async {
processRunner.mockProcessesForExecutable['git-diff'] = <Process>[
MockProcess(stdout: 'packages/plugin1/plugin1.dart'),
];
Expand All @@ -854,8 +895,9 @@ packages/b_package/lib/src/foo.dart
expect(
output,
containsAllInOrder(<Matcher>[
contains('--packages-for-branch: running on default branch; '
'using parent commit as the diff base'),
contains('--packages-for-branch: running on default branch.'),
contains(
'--packages-for-branch: using parent commit as the diff base'),
contains(
'Running for all packages that have diffs relative to "HEAD~"'),
]));
Expand Down Expand Up @@ -887,7 +929,7 @@ packages/b_package/lib/src/foo.dart
expect(
output,
containsAllInOrder(<Matcher>[
contains('Unabled to determine branch'),
contains('Unable to determine branch'),
]));
});
});
Expand Down