From 7b3ce8c003c73bdc169b1e4686c75119a96d7c9d Mon Sep 17 00:00:00 2001 From: Christopher Fujino Date: Mon, 3 May 2021 16:49:04 -0700 Subject: [PATCH] [flutter_conductor] Add candidates sub command (#81234) --- dev/tools/bin/conductor.dart | 5 ++ dev/tools/lib/candidates.dart | 98 +++++++++++++++++++++++++++++ dev/tools/lib/repository.dart | 69 ++++++++++++++++++++ dev/tools/test/candidates_test.dart | 97 ++++++++++++++++++++++++++++ 4 files changed, 269 insertions(+) create mode 100644 dev/tools/lib/candidates.dart create mode 100644 dev/tools/test/candidates_test.dart diff --git a/dev/tools/bin/conductor.dart b/dev/tools/bin/conductor.dart index 97e530ba5d..819200cde5 100644 --- a/dev/tools/bin/conductor.dart +++ b/dev/tools/bin/conductor.dart @@ -9,6 +9,7 @@ import 'dart:io' as io; import 'package:args/command_runner.dart'; +import 'package:dev_tools/candidates.dart'; import 'package:dev_tools/clean.dart'; import 'package:dev_tools/codesign.dart'; import 'package:dev_tools/globals.dart'; @@ -66,6 +67,10 @@ Future main(List args) async { CleanCommand( checkouts: checkouts, ), + CandidatesCommand( + checkouts: checkouts, + flutterRoot: localFlutterRoot, + ), ].forEach(runner.addCommand); if (!assertsEnabled()) { diff --git a/dev/tools/lib/candidates.dart b/dev/tools/lib/candidates.dart new file mode 100644 index 0000000000..b5782f1beb --- /dev/null +++ b/dev/tools/lib/candidates.dart @@ -0,0 +1,98 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:args/args.dart'; +import 'package:args/command_runner.dart'; +import 'package:file/file.dart'; + +import './git.dart'; +import './globals.dart' show releaseCandidateBranchRegex; +import './repository.dart'; +import './stdio.dart'; +import './version.dart'; + +const String kRemote = 'remote'; + +class CandidatesCommand extends Command { + CandidatesCommand({ + required this.flutterRoot, + required this.checkouts, + }) : git = Git(checkouts.processManager), stdio = checkouts.stdio { + argParser.addOption( + kRemote, + help: 'Which remote name to query for branches.', + defaultsTo: 'upstream', + ); + } + + final Checkouts checkouts; + final Directory flutterRoot; + final Git git; + final Stdio stdio; + + @override + String get name => 'candidates'; + + @override + String get description => 'List release candidates.'; + + @override + void run() { + final ArgResults results = argResults!; + git.run( + ['fetch', results[kRemote] as String], + 'Fetch from remote ${results[kRemote]}', + workingDirectory: flutterRoot.path, + ); + + final FrameworkRepository framework = HostFrameworkRepository( + checkouts: checkouts, + name: 'framework-for-candidates', + upstreamPath: flutterRoot.path, + ); + + final Version currentVersion = framework.flutterVersion(); + stdio.printStatus('currentVersion = $currentVersion'); + + final List branches = git.getOutput( + [ + 'branch', + '--no-color', + '--remotes', + '--list', + '${results[kRemote]}/*', + ], + 'List all remote branches', + workingDirectory: flutterRoot.path, + ).split('\n'); + + // Pattern for extracting only the branch name via sub-group 1 + final RegExp remotePattern = RegExp('${results[kRemote]}\\/(.*)'); + for (final String branchName in branches) { + final RegExpMatch? candidateMatch = releaseCandidateBranchRegex.firstMatch(branchName); + if (candidateMatch == null) { + continue; + } + final int currentX = currentVersion.x; + final int currentY = currentVersion.y; + final int currentZ = currentVersion.z; + final int currentM = currentVersion.m ?? 0; + final int x = int.parse(candidateMatch.group(1)!); + final int y = int.parse(candidateMatch.group(2)!); + final int m = int.parse(candidateMatch.group(3)!); + + final RegExpMatch match = remotePattern.firstMatch(branchName)!; + if (x < currentVersion.x) { + continue; + } + if (x == currentVersion.x && y < currentVersion.y) { + continue; + } + if (x == currentX && y == currentY && currentZ == 0 && m < currentM) { + continue; + } + stdio.printStatus(match.group(1)!); + } + } +} diff --git a/dev/tools/lib/repository.dart b/dev/tools/lib/repository.dart index 33632706a5..5e6f98f190 100644 --- a/dev/tools/lib/repository.dart +++ b/dev/tools/lib/repository.dart @@ -519,6 +519,75 @@ class FrameworkRepository extends Repository { } } +/// A wrapper around the host repository that is executing the conductor. +/// +/// [Repository] methods that mutate the underlying repository will throw a +/// [ConductorException]. +class HostFrameworkRepository extends FrameworkRepository { + HostFrameworkRepository({ + required Checkouts checkouts, + String name = 'host-framework', + bool useExistingCheckout = false, + required String upstreamPath, + }) : super( + checkouts, + name: name, + fetchRemote: Remote( + name: RemoteName.upstream, + url: 'file://$upstreamPath/', + ), + localUpstream: false, + useExistingCheckout: useExistingCheckout, + ) { + _checkoutDirectory = checkouts.fileSystem.directory(upstreamPath); + } + + @override + Directory get checkoutDirectory => _checkoutDirectory!; + + @override + void newBranch(String branchName) { + throw ConductorException('newBranch not implemented for the host repository'); + } + + @override + void checkout(String ref) { + throw ConductorException('checkout not implemented for the host repository'); + } + + @override + String cherryPick(String commit) { + throw ConductorException('cherryPick not implemented for the host repository'); + } + + @override + String reset(String ref) { + throw ConductorException('reset not implemented for the host repository'); + } + + @override + void tag(String commit, String tagName, String remote) { + throw ConductorException('tag not implemented for the host repository'); + } + + @override + void updateChannel( + String commit, + String remote, + String branch, { + bool force = false, + }) { + throw ConductorException('updateChannel not implemented for the host repository'); + } + + @override + String authorEmptyCommit([String message = 'An empty commit']) { + throw ConductorException( + 'authorEmptyCommit not implemented for the host repository', + ); + } +} + class EngineRepository extends Repository { EngineRepository( this.checkouts, { diff --git a/dev/tools/test/candidates_test.dart b/dev/tools/test/candidates_test.dart new file mode 100644 index 0000000000..0c42e8e361 --- /dev/null +++ b/dev/tools/test/candidates_test.dart @@ -0,0 +1,97 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:args/command_runner.dart'; +import 'package:dev_tools/candidates.dart'; +import 'package:dev_tools/repository.dart'; +import 'package:file/memory.dart'; +import 'package:platform/platform.dart'; + +import '../../../packages/flutter_tools/test/src/fake_process_manager.dart'; +import './common.dart'; + +void main() { + group('candidates command', () { + const String flutterRoot = '/flutter'; + const String flutterBinPath = '$flutterRoot/bin/flutter'; + const String checkoutsParentDirectory = '$flutterRoot/dev/tools/'; + const String remoteName = 'origin'; + + late MemoryFileSystem fileSystem; + late FakePlatform platform; + late TestStdio stdio; + late FakeProcessManager processManager; + final String operatingSystem = const LocalPlatform().operatingSystem; + + setUp(() { + stdio = TestStdio(); + fileSystem = MemoryFileSystem.test(); + }); + + CommandRunner createRunner({ + required Checkouts checkouts, + }) { + final CandidatesCommand command = CandidatesCommand( + checkouts: checkouts, + flutterRoot: fileSystem.directory(flutterRoot), + ); + return CommandRunner('clean-test', '')..addCommand(command); + } + + test('prints only branches from targeted remote', () async { + const String currentVersion = '1.2.3'; + const String branch = 'flutter-1.2-candidate.0'; + + processManager = FakeProcessManager.list([ + const FakeCommand( + command: ['git', 'fetch', remoteName], + ), + const FakeCommand( + command: [flutterBinPath, 'help'], + ), + const FakeCommand( + command: [flutterBinPath, '--version', '--machine'], + stdout: '{"frameworkVersion": "$currentVersion"}', + ), + FakeCommand( + command: const [ + 'git', + 'branch', + '--no-color', + '--remotes', + '--list', + '$remoteName/*', + ], + stdout: [ + 'other-remote/branch1', + '$remoteName/$branch', + ].join('\n'), + ), + ]); + final String pathSeparator = operatingSystem == 'windows' ? r'\' : '/'; + + platform = FakePlatform( + environment: { + 'HOME': ['path', 'to', 'home'].join(pathSeparator), + }, + pathSeparator: pathSeparator, + ); + final Checkouts checkouts = Checkouts( + fileSystem: fileSystem, + parentDirectory: fileSystem.directory(checkoutsParentDirectory), + platform: platform, + processManager: processManager, + stdio: stdio, + ); + + final CommandRunner runner = createRunner(checkouts: checkouts); + await runner.run(['candidates', '--$kRemote', remoteName]); + expect(stdio.stdout.contains('currentVersion = $currentVersion'), true); + expect(stdio.stdout.contains(branch), true); + expect(stdio.stdout.contains('branch1'), false); + }); + }, onPlatform: { + 'windows': const Skip('Flutter Conductor only supported on macos/linux'), + }); +}