Reland fix --version (#52141)

This commit is contained in:
Dan Field
2020-03-06 21:38:35 -08:00
committed by GitHub
parent db1f33fd70
commit e13e17009d
7 changed files with 158 additions and 6 deletions

View File

@@ -6,6 +6,7 @@ import 'dart:async';
import '../base/common.dart';
import '../doctor.dart';
import '../globals.dart' as globals;
import '../runner/flutter_command.dart';
class DoctorCommand extends FlutterCommand {
@@ -42,6 +43,7 @@ class DoctorCommand extends FlutterCommand {
@override
Future<FlutterCommandResult> runCommand() async {
globals.flutterVersion.fetchTagsAndUpdate();
if (argResults.wasParsed('check-for-remote-artifacts')) {
final String engineRevision = stringArg('check-for-remote-artifacts');
if (engineRevision.startsWith(RegExp(r'[a-f0-9]{1,40}'))) {

View File

@@ -322,6 +322,7 @@ class FlutterCommandRunner extends CommandRunner<void> {
if (topLevelResults['version'] as bool) {
globals.flutterUsage.sendCommand('version');
globals.flutterVersion.fetchTagsAndUpdate();
String status;
if (machineFlag) {
status = const JsonEncoder.withIndent(' ').convert(globals.flutterVersion.toJson());

View File

@@ -23,6 +23,9 @@ enum Channel {
stable,
}
/// The flutter GitHub repository.
const String _flutterGit = 'https://github.com/flutter/flutter.git';
/// Retrieve a human-readable name for a given [channel].
///
/// Requires [FlutterVersion.officialChannels] to be correctly ordered.
@@ -42,13 +45,29 @@ Channel getChannelForName(String name) {
}
class FlutterVersion {
/// Parses the Flutter version from currently available tags in the local
/// repo.
///
/// Call [fetchTagsAndUpdate] to update the version based on the latest tags
/// available upstream.
FlutterVersion([this._clock = const SystemClock(), this._workingDirectory]) {
_frameworkRevision = _runGit(
gitLog(<String>['-n', '1', '--pretty=format:%H']).join(' '),
processUtils,
_workingDirectory,
);
_gitTagVersion = GitTagVersion.determine(processUtils, _workingDirectory);
_gitTagVersion = GitTagVersion.determine(processUtils, workingDirectory: _workingDirectory, fetchTags: false);
_frameworkVersion = gitTagVersion.frameworkVersionFor(_frameworkRevision);
}
/// Fetchs tags from the upstream Flutter repository and re-calculates the
/// version.
///
/// This carries a performance penalty, and should only be called when the
/// user explicitly wants to get the version, e.g. for `flutter --version` or
/// `flutter doctor`.
void fetchTagsAndUpdate() {
_gitTagVersion = GitTagVersion.determine(processUtils, workingDirectory: _workingDirectory, fetchTags: true);
_frameworkVersion = gitTagVersion.frameworkVersionFor(_frameworkRevision);
}
@@ -233,7 +252,7 @@ class FlutterVersion {
'remote',
'add',
_versionCheckRemote,
'https://github.com/flutter/flutter.git',
_flutterGit,
]);
await _run(<String>['git', 'fetch', _versionCheckRemote, branch]);
return _latestGitCommitDate(
@@ -701,7 +720,10 @@ class GitTagVersion {
/// The git hash (or an abbreviation thereof) for this commit.
final String hash;
static GitTagVersion determine(ProcessUtils processUtils, [String workingDirectory]) {
static GitTagVersion determine(ProcessUtils processUtils, {String workingDirectory, bool fetchTags = false}) {
if (fetchTags) {
_runGit('git fetch $_flutterGit --tags', processUtils, workingDirectory);
}
return parse(_runGit('git describe --match v*.*.* --first-parent --long --tags', processUtils, workingDirectory));
}