Unverified Commit ae805de4 authored by Anurag Roy's avatar Anurag Roy Committed by GitHub

[flutter_tools] Remove usage of globals.flutterGit from version (#100744)

parent fe11f57c
...@@ -75,7 +75,7 @@ class UpgradeCommand extends FlutterCommand { ...@@ -75,7 +75,7 @@ class UpgradeCommand extends FlutterCommand {
force: boolArg('force'), force: boolArg('force'),
continueFlow: boolArg('continue'), continueFlow: boolArg('continue'),
testFlow: stringArg('working-directory') != null, testFlow: stringArg('working-directory') != null,
gitTagVersion: GitTagVersion.determine(globals.processUtils), gitTagVersion: GitTagVersion.determine(globals.processUtils, globals.platform),
flutterVersion: stringArg('working-directory') == null flutterVersion: stringArg('working-directory') == null
? globals.flutterVersion ? globals.flutterVersion
: FlutterVersion(workingDirectory: _commandRunner.workingDirectory), : FlutterVersion(workingDirectory: _commandRunner.workingDirectory),
...@@ -243,9 +243,9 @@ class UpgradeCommandRunner { ...@@ -243,9 +243,9 @@ class UpgradeCommandRunner {
throwOnError: true, throwOnError: true,
workingDirectory: workingDirectory, workingDirectory: workingDirectory,
); );
// '@{u}' means upstream HEAD // Get the latest commit revision of the upstream
final RunResult result = await globals.processUtils.run( final RunResult result = await globals.processUtils.run(
<String>[ 'git', 'rev-parse', '--verify', '@{u}'], <String>['git', 'rev-parse', '--verify', kGitTrackingUpstream],
throwOnError: true, throwOnError: true,
workingDirectory: workingDirectory, workingDirectory: workingDirectory,
); );
......
...@@ -46,9 +46,6 @@ import 'reporting/reporting.dart'; ...@@ -46,9 +46,6 @@ import 'reporting/reporting.dart';
import 'runner/local_engine.dart'; import 'runner/local_engine.dart';
import 'version.dart'; import 'version.dart';
/// The flutter GitHub repository.
String get flutterGit => platform.environment['FLUTTER_GIT_URL'] ?? 'https://github.com/flutter/flutter.git';
Artifacts? get artifacts => context.get<Artifacts>(); Artifacts? get artifacts => context.get<Artifacts>();
BuildSystem get buildSystem => context.get<BuildSystem>()!; BuildSystem get buildSystem => context.get<BuildSystem>()!;
Cache get cache => context.get<Cache>()!; Cache get cache => context.get<Cache>()!;
......
...@@ -17,6 +17,11 @@ import 'globals.dart' as globals; ...@@ -17,6 +17,11 @@ import 'globals.dart' as globals;
const String _unknownFrameworkVersion = '0.0.0-unknown'; const String _unknownFrameworkVersion = '0.0.0-unknown';
/// A git shortcut for the branch that is being tracked by the current one.
///
/// See `man gitrevisions` for more information.
const String kGitTrackingUpstream = '@{upstream}';
/// This maps old branch names to the names of branches that replaced them. /// This maps old branch names to the names of branches that replaced them.
/// ///
/// For example, in 2021 we deprecated the "dev" channel and transitioned "dev" /// For example, in 2021 we deprecated the "dev" channel and transitioned "dev"
...@@ -75,7 +80,7 @@ class FlutterVersion { ...@@ -75,7 +80,7 @@ class FlutterVersion {
globals.processUtils, globals.processUtils,
_workingDirectory, _workingDirectory,
); );
_gitTagVersion = GitTagVersion.determine(globals.processUtils, workingDirectory: _workingDirectory, gitRef: _frameworkRevision); _gitTagVersion = GitTagVersion.determine(globals.processUtils, globals.platform, workingDirectory: _workingDirectory, gitRef: _frameworkRevision);
_frameworkVersion = gitTagVersion.frameworkVersionFor(_frameworkRevision); _frameworkVersion = gitTagVersion.frameworkVersionFor(_frameworkRevision);
} }
...@@ -89,7 +94,7 @@ class FlutterVersion { ...@@ -89,7 +94,7 @@ class FlutterVersion {
/// user explicitly wants to get the version, e.g. for `flutter --version` or /// user explicitly wants to get the version, e.g. for `flutter --version` or
/// `flutter doctor`. /// `flutter doctor`.
void fetchTagsAndUpdate() { void fetchTagsAndUpdate() {
_gitTagVersion = GitTagVersion.determine(globals.processUtils, workingDirectory: _workingDirectory, fetchTags: true); _gitTagVersion = GitTagVersion.determine(globals.processUtils, globals.platform, workingDirectory: _workingDirectory, fetchTags: true);
_frameworkVersion = gitTagVersion.frameworkVersionFor(_frameworkRevision); _frameworkVersion = gitTagVersion.frameworkVersionFor(_frameworkRevision);
} }
...@@ -106,7 +111,7 @@ class FlutterVersion { ...@@ -106,7 +111,7 @@ class FlutterVersion {
String? channel = _channel; String? channel = _channel;
if (channel == null) { if (channel == null) {
final String gitChannel = _runGit( final String gitChannel = _runGit(
'git rev-parse --abbrev-ref --symbolic @{u}', 'git rev-parse --abbrev-ref --symbolic $kGitTrackingUpstream',
globals.processUtils, globals.processUtils,
_workingDirectory, _workingDirectory,
); );
...@@ -195,19 +200,19 @@ class FlutterVersion { ...@@ -195,19 +200,19 @@ class FlutterVersion {
/// A date String describing the last framework commit. /// A date String describing the last framework commit.
/// ///
/// If a git command fails, this will return a placeholder date. /// If a git command fails, this will return a placeholder date.
String get frameworkCommitDate => _latestGitCommitDate(lenient: true); String get frameworkCommitDate => _gitCommitDate(lenient: true);
// The date of the latest commit on the given branch. If no branch is // The date of the given commit hash as [gitRef]. If no hash is specified,
// specified, then it is the current local branch. // then it is the HEAD of the current local branch.
// //
// If lenient is true, and the git command fails, a placeholder date is // If lenient is true, and the git command fails, a placeholder date is
// returned. Otherwise, the VersionCheckError exception is propagated. // returned. Otherwise, the VersionCheckError exception is propagated.
static String _latestGitCommitDate({ static String _gitCommitDate({
String? branch, String gitRef = 'HEAD',
bool lenient = false, bool lenient = false,
}) { }) {
final List<String> args = gitLog(<String>[ final List<String> args = gitLog(<String>[
if (branch != null) branch, gitRef,
'-n', '-n',
'1', '1',
'--pretty=format:%ad', '--pretty=format:%ad',
...@@ -247,7 +252,7 @@ class FlutterVersion { ...@@ -247,7 +252,7 @@ class FlutterVersion {
DateTime localFrameworkCommitDate; DateTime localFrameworkCommitDate;
try { try {
// Don't perform the update check if fetching the latest local commit failed. // Don't perform the update check if fetching the latest local commit failed.
localFrameworkCommitDate = DateTime.parse(_latestGitCommitDate()); localFrameworkCommitDate = DateTime.parse(_gitCommitDate());
} on VersionCheckError { } on VersionCheckError {
return; return;
} }
...@@ -264,51 +269,18 @@ class FlutterVersion { ...@@ -264,51 +269,18 @@ class FlutterVersion {
).run(); ).run();
} }
/// The name of the temporary git remote used to check for the latest
/// available Flutter framework version.
///
/// In the absence of bugs and crashes a Flutter developer should never see
/// this remote appear in their `git remote` list, but also if it happens to
/// persist we do the proper clean-up for extra robustness.
static const String _versionCheckRemote = '__flutter_version_check__';
/// The date of the latest framework commit in the remote repository. /// The date of the latest framework commit in the remote repository.
/// ///
/// Throws [VersionCheckError] if a git command fails, for example, when the /// Throws [VersionCheckError] if a git command fails, for example, when the
/// remote git repository is not reachable due to a network issue. /// remote git repository is not reachable due to a network issue.
static Future<String> fetchRemoteFrameworkCommitDate(String branch) async { static Future<String> fetchRemoteFrameworkCommitDate(String branch) async {
await _removeVersionCheckRemoteIfExists();
try { try {
await _run(<String>[ // Fetch upstream branch's commit and tags
'git', await _run(<String>['git', 'fetch', '--tags']);
'remote', return _gitCommitDate(gitRef: kGitTrackingUpstream);
'add',
_versionCheckRemote,
globals.flutterGit,
]);
await _run(<String>['git', 'fetch', _versionCheckRemote, branch]);
return _latestGitCommitDate(
branch: '$_versionCheckRemote/$branch',
);
} on VersionCheckError catch (error) { } on VersionCheckError catch (error) {
if (globals.platform.environment.containsKey('FLUTTER_GIT_URL')) { globals.printError(error.message);
globals.printWarning('Warning: the Flutter git upstream was overridden '
'by the environment variable FLUTTER_GIT_URL = ${globals.flutterGit}');
}
globals.printError(error.toString());
rethrow; rethrow;
} finally {
await _removeVersionCheckRemoteIfExists();
}
}
static Future<void> _removeVersionCheckRemoteIfExists() async {
final List<String> remotes = (await _run(<String>['git', 'remote']))
.split('\n')
.map<String>((String name) => name.trim()) // to account for OS-specific line-breaks
.toList();
if (remotes.contains(_versionCheckRemote)) {
await _run(<String>['git', 'remote', 'remove', _versionCheckRemote]);
} }
} }
...@@ -728,13 +700,20 @@ class GitTagVersion { ...@@ -728,13 +700,20 @@ class GitTagVersion {
/// The git tag that is this version's closest ancestor. /// The git tag that is this version's closest ancestor.
final String? gitTag; final String? gitTag;
static GitTagVersion determine(ProcessUtils processUtils, {String? workingDirectory, bool fetchTags = false, String gitRef = 'HEAD'}) { static GitTagVersion determine(
ProcessUtils processUtils,
Platform platform, {
String? workingDirectory,
bool fetchTags = false,
String gitRef = 'HEAD'
}) {
if (fetchTags) { if (fetchTags) {
final String channel = _runGit('git rev-parse --abbrev-ref HEAD', processUtils, workingDirectory); final String channel = _runGit('git rev-parse --abbrev-ref HEAD', processUtils, workingDirectory);
if (channel == 'dev' || channel == 'beta' || channel == 'stable') { if (channel == 'dev' || channel == 'beta' || channel == 'stable') {
globals.printTrace('Skipping request to fetchTags - on well known channel $channel.'); globals.printTrace('Skipping request to fetchTags - on well known channel $channel.');
} else { } else {
_runGit('git fetch ${globals.flutterGit} --tags -f', processUtils, workingDirectory); final String flutterGit = platform.environment['FLUTTER_GIT_URL'] ?? 'https://github.com/flutter/flutter.git';
_runGit('git fetch $flutterGit --tags -f', processUtils, workingDirectory);
} }
} }
// find all tags attached to the given [gitRef] // find all tags attached to the given [gitRef]
......
...@@ -63,7 +63,7 @@ void main() { ...@@ -63,7 +63,7 @@ void main() {
command: <String>['git', 'fetch', '--tags'], command: <String>['git', 'fetch', '--tags'],
), ),
const FakeCommand( const FakeCommand(
command: <String>['git', 'rev-parse', '--verify', '@{u}'], command: <String>['git', 'rev-parse', '--verify', '@{upstream}'],
stdout: upstreamHeadRevision, stdout: upstreamHeadRevision,
), ),
const FakeCommand( const FakeCommand(
......
...@@ -160,7 +160,7 @@ void main() { ...@@ -160,7 +160,7 @@ void main() {
'git', 'fetch', '--tags' 'git', 'fetch', '--tags'
]), ]),
const FakeCommand(command: <String>[ const FakeCommand(command: <String>[
'git', 'rev-parse', '--verify', '@{u}', 'git', 'rev-parse', '--verify', '@{upstream}',
], ],
stdout: revision), stdout: revision),
const FakeCommand(command: <String>[ const FakeCommand(command: <String>[
...@@ -188,10 +188,10 @@ void main() { ...@@ -188,10 +188,10 @@ void main() {
'git', 'fetch', '--tags' 'git', 'fetch', '--tags'
]), ]),
FakeCommand( FakeCommand(
command: <String>['git', 'rev-parse', '--verify', '@{u}'], command: <String>['git', 'rev-parse', '--verify', '@{upstream}'],
exception: ProcessException( exception: ProcessException(
'git', 'git',
<String>['rev-parse', '--verify', '@{u}'], <String>['rev-parse', '--verify', '@{upstream}'],
'fatal: HEAD does not point to a branch', 'fatal: HEAD does not point to a branch',
), ),
), ),
...@@ -217,10 +217,10 @@ void main() { ...@@ -217,10 +217,10 @@ void main() {
'git', 'fetch', '--tags' 'git', 'fetch', '--tags'
]), ]),
FakeCommand( FakeCommand(
command: <String>['git', 'rev-parse', '--verify', '@{u}'], command: <String>['git', 'rev-parse', '--verify', '@{upstream}'],
exception: ProcessException( exception: ProcessException(
'git', 'git',
<String>['rev-parse', '--verify', '@{u}'], <String>['rev-parse', '--verify', '@{upstream}'],
'fatal: no upstream configured for branch', 'fatal: no upstream configured for branch',
), ),
), ),
......
...@@ -70,7 +70,7 @@ void main() { ...@@ -70,7 +70,7 @@ void main() {
stdout: '0.1.2-3-1234abcd', stdout: '0.1.2-3-1234abcd',
), ),
FakeCommand( FakeCommand(
command: const <String>['git', 'rev-parse', '--abbrev-ref', '--symbolic', '@{u}'], command: const <String>['git', 'rev-parse', '--abbrev-ref', '--symbolic', '@{upstream}'],
stdout: 'origin/$channel', stdout: 'origin/$channel',
), ),
const FakeCommand( const FakeCommand(
...@@ -78,31 +78,22 @@ void main() { ...@@ -78,31 +78,22 @@ void main() {
stdout: flutterUpstreamUrl, stdout: flutterUpstreamUrl,
), ),
FakeCommand( FakeCommand(
command: const <String>['git', '-c', 'log.showSignature=false', 'log', '-n', '1', '--pretty=format:%ad', '--date=iso'], command: const <String>['git', '-c', 'log.showSignature=false', 'log', 'HEAD', '-n', '1', '--pretty=format:%ad', '--date=iso'],
stdout: getChannelUpToDateVersion().toString(), stdout: getChannelUpToDateVersion().toString(),
), ),
const FakeCommand( const FakeCommand(
command: <String>['git', 'remote'], command: <String>['git', 'fetch', '--tags'],
),
const FakeCommand(
command: <String>['git', 'remote', 'add', '__flutter_version_check__', flutterUpstreamUrl],
), ),
FakeCommand( FakeCommand(
command: <String>['git', 'fetch', '__flutter_version_check__', channel], command: const <String>['git', '-c', 'log.showSignature=false', 'log', '@{upstream}', '-n', '1', '--pretty=format:%ad', '--date=iso'],
),
FakeCommand(
command: <String>['git', '-c', 'log.showSignature=false', 'log', '__flutter_version_check__/$channel', '-n', '1', '--pretty=format:%ad', '--date=iso'],
stdout: getChannelOutOfDateVersion().toString(), stdout: getChannelOutOfDateVersion().toString(),
), ),
const FakeCommand(
command: <String>['git', 'remote'],
),
const FakeCommand( const FakeCommand(
command: <String>['git', '-c', 'log.showSignature=false', 'log', '-n', '1', '--pretty=format:%ar'], command: <String>['git', '-c', 'log.showSignature=false', 'log', '-n', '1', '--pretty=format:%ar'],
stdout: '1 second ago', stdout: '1 second ago',
), ),
FakeCommand( FakeCommand(
command: const <String>['git', '-c', 'log.showSignature=false', 'log', '-n', '1', '--pretty=format:%ad', '--date=iso'], command: const <String>['git', '-c', 'log.showSignature=false', 'log', 'HEAD', '-n', '1', '--pretty=format:%ad', '--date=iso'],
stdout: getChannelUpToDateVersion().toString(), stdout: getChannelUpToDateVersion().toString(),
), ),
FakeCommand( FakeCommand(
...@@ -114,6 +105,7 @@ void main() { ...@@ -114,6 +105,7 @@ void main() {
final FlutterVersion flutterVersion = globals.flutterVersion; final FlutterVersion flutterVersion = globals.flutterVersion;
await flutterVersion.checkFlutterVersionFreshness(); await flutterVersion.checkFlutterVersionFreshness();
expect(flutterVersion.channel, channel); expect(flutterVersion.channel, channel);
expect(flutterVersion.repositoryUrl, flutterUpstreamUrl);
expect(flutterVersion.frameworkRevision, '1234abcd'); expect(flutterVersion.frameworkRevision, '1234abcd');
expect(flutterVersion.frameworkRevisionShort, '1234abcd'); expect(flutterVersion.frameworkRevisionShort, '1234abcd');
expect(flutterVersion.frameworkVersion, '0.0.0-unknown'); expect(flutterVersion.frameworkVersion, '0.0.0-unknown');
...@@ -423,7 +415,7 @@ void main() { ...@@ -423,7 +415,7 @@ void main() {
stdout: '0.1.2-3-1234abcd', stdout: '0.1.2-3-1234abcd',
), ),
const FakeCommand( const FakeCommand(
command: <String>['git', 'rev-parse', '--abbrev-ref', '--symbolic', '@{u}'], command: <String>['git', 'rev-parse', '--abbrev-ref', '--symbolic', '@{upstream}'],
stdout: 'feature-branch', stdout: 'feature-branch',
), ),
const FakeCommand( const FakeCommand(
...@@ -526,7 +518,8 @@ void main() { ...@@ -526,7 +518,8 @@ void main() {
processManager: fakeProcessManager, processManager: fakeProcessManager,
logger: BufferLogger.test(), logger: BufferLogger.test(),
); );
final GitTagVersion gitTagVersion = GitTagVersion.determine(processUtils, workingDirectory: '.'); final FakePlatform platform = FakePlatform();
final GitTagVersion gitTagVersion = GitTagVersion.determine(processUtils, platform, workingDirectory: '.');
expect(gitTagVersion.frameworkVersionFor('abcd1234'), stableTag); expect(gitTagVersion.frameworkVersionFor('abcd1234'), stableTag);
}); });
...@@ -545,7 +538,9 @@ void main() { ...@@ -545,7 +538,9 @@ void main() {
processManager: fakeProcessManager, processManager: fakeProcessManager,
logger: BufferLogger.test(), logger: BufferLogger.test(),
); );
final GitTagVersion gitTagVersion = GitTagVersion.determine(processUtils, workingDirectory: '.'); final FakePlatform platform = FakePlatform();
final GitTagVersion gitTagVersion = GitTagVersion.determine(processUtils, platform, workingDirectory: '.');
expect(gitTagVersion.frameworkVersionFor('abcd1234'), stableTag); expect(gitTagVersion.frameworkVersionFor('abcd1234'), stableTag);
}); });
...@@ -569,7 +564,9 @@ void main() { ...@@ -569,7 +564,9 @@ void main() {
processManager: fakeProcessManager, processManager: fakeProcessManager,
logger: BufferLogger.test(), logger: BufferLogger.test(),
); );
final GitTagVersion gitTagVersion = GitTagVersion.determine(processUtils, workingDirectory: '.'); final FakePlatform platform = FakePlatform();
final GitTagVersion gitTagVersion = GitTagVersion.determine(processUtils, platform, workingDirectory: '.');
// reported version should increment the y // reported version should increment the y
expect(gitTagVersion.frameworkVersionFor(headRevision), '1.3.0-0.0.pre.12'); expect(gitTagVersion.frameworkVersionFor(headRevision), '1.3.0-0.0.pre.12');
}); });
...@@ -588,8 +585,9 @@ void main() { ...@@ -588,8 +585,9 @@ void main() {
processManager: fakeProcessManager, processManager: fakeProcessManager,
logger: BufferLogger.test(), logger: BufferLogger.test(),
); );
final FakePlatform platform = FakePlatform();
GitTagVersion.determine(processUtils, workingDirectory: '.'); GitTagVersion.determine(processUtils, platform, workingDirectory: '.');
expect(fakeProcessManager, hasNoRemainingExpectations); expect(fakeProcessManager, hasNoRemainingExpectations);
}); });
...@@ -611,8 +609,9 @@ void main() { ...@@ -611,8 +609,9 @@ void main() {
processManager: fakeProcessManager, processManager: fakeProcessManager,
logger: BufferLogger.test(), logger: BufferLogger.test(),
); );
final FakePlatform platform = FakePlatform();
GitTagVersion.determine(processUtils, workingDirectory: '.', fetchTags: true); GitTagVersion.determine(processUtils, platform, workingDirectory: '.', fetchTags: true);
expect(fakeProcessManager, hasNoRemainingExpectations); expect(fakeProcessManager, hasNoRemainingExpectations);
}); });
...@@ -637,8 +636,9 @@ void main() { ...@@ -637,8 +636,9 @@ void main() {
processManager: fakeProcessManager, processManager: fakeProcessManager,
logger: BufferLogger.test(), logger: BufferLogger.test(),
); );
final FakePlatform platform = FakePlatform();
GitTagVersion.determine(processUtils, workingDirectory: '.', fetchTags: true); GitTagVersion.determine(processUtils, platform, workingDirectory: '.', fetchTags: true);
expect(fakeProcessManager, hasNoRemainingExpectations); expect(fakeProcessManager, hasNoRemainingExpectations);
}); });
...@@ -663,13 +663,12 @@ void main() { ...@@ -663,13 +663,12 @@ void main() {
processManager: fakeProcessManager, processManager: fakeProcessManager,
logger: BufferLogger.test(), logger: BufferLogger.test(),
); );
final FakePlatform platform = FakePlatform(
environment: <String, String> {'FLUTTER_GIT_URL': 'https://githubmirror.com/flutter.git'},
);
GitTagVersion.determine(processUtils, workingDirectory: '.', fetchTags: true); GitTagVersion.determine(processUtils, platform, workingDirectory: '.', fetchTags: true);
expect(fakeProcessManager, hasNoRemainingExpectations); expect(fakeProcessManager, hasNoRemainingExpectations);
}, overrides: <Type, Generator>{
Platform: () => FakePlatform(environment: <String, String>{
'FLUTTER_GIT_URL': 'https://githubmirror.com/flutter.git',
}),
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment