From ae805de4687480beee3c5f46acb84cfc98dd0fed Mon Sep 17 00:00:00 2001 From: Anurag Roy Date: Fri, 15 Apr 2022 03:14:09 +0530 Subject: [PATCH] [flutter_tools] Remove usage of globals.flutterGit from version (#100744) --- .../lib/src/commands/upgrade.dart | 6 +- packages/flutter_tools/lib/src/globals.dart | 3 - packages/flutter_tools/lib/src/version.dart | 77 +++++++------------ .../commands.shard/hermetic/upgrade_test.dart | 2 +- .../permeable/upgrade_test.dart | 10 +-- .../test/general.shard/version_test.dart | 51 ++++++------ 6 files changed, 62 insertions(+), 87 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/upgrade.dart b/packages/flutter_tools/lib/src/commands/upgrade.dart index 836b90fe91..2580e2830c 100644 --- a/packages/flutter_tools/lib/src/commands/upgrade.dart +++ b/packages/flutter_tools/lib/src/commands/upgrade.dart @@ -75,7 +75,7 @@ class UpgradeCommand extends FlutterCommand { force: boolArg('force'), continueFlow: boolArg('continue'), testFlow: stringArg('working-directory') != null, - gitTagVersion: GitTagVersion.determine(globals.processUtils), + gitTagVersion: GitTagVersion.determine(globals.processUtils, globals.platform), flutterVersion: stringArg('working-directory') == null ? globals.flutterVersion : FlutterVersion(workingDirectory: _commandRunner.workingDirectory), @@ -243,9 +243,9 @@ class UpgradeCommandRunner { throwOnError: true, workingDirectory: workingDirectory, ); - // '@{u}' means upstream HEAD + // Get the latest commit revision of the upstream final RunResult result = await globals.processUtils.run( - [ 'git', 'rev-parse', '--verify', '@{u}'], + ['git', 'rev-parse', '--verify', kGitTrackingUpstream], throwOnError: true, workingDirectory: workingDirectory, ); diff --git a/packages/flutter_tools/lib/src/globals.dart b/packages/flutter_tools/lib/src/globals.dart index adb934f94a..778790fc53 100644 --- a/packages/flutter_tools/lib/src/globals.dart +++ b/packages/flutter_tools/lib/src/globals.dart @@ -46,9 +46,6 @@ import 'reporting/reporting.dart'; import 'runner/local_engine.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(); BuildSystem get buildSystem => context.get()!; Cache get cache => context.get()!; diff --git a/packages/flutter_tools/lib/src/version.dart b/packages/flutter_tools/lib/src/version.dart index f2068a6ca2..deabee8ea9 100644 --- a/packages/flutter_tools/lib/src/version.dart +++ b/packages/flutter_tools/lib/src/version.dart @@ -17,6 +17,11 @@ import 'globals.dart' as globals; 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. /// /// For example, in 2021 we deprecated the "dev" channel and transitioned "dev" @@ -75,7 +80,7 @@ class FlutterVersion { globals.processUtils, _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); } @@ -89,7 +94,7 @@ class FlutterVersion { /// user explicitly wants to get the version, e.g. for `flutter --version` or /// `flutter doctor`. 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); } @@ -106,7 +111,7 @@ class FlutterVersion { String? channel = _channel; if (channel == null) { final String gitChannel = _runGit( - 'git rev-parse --abbrev-ref --symbolic @{u}', + 'git rev-parse --abbrev-ref --symbolic $kGitTrackingUpstream', globals.processUtils, _workingDirectory, ); @@ -195,19 +200,19 @@ class FlutterVersion { /// A date String describing the last framework commit. /// /// 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 - // specified, then it is the current local branch. + // The date of the given commit hash as [gitRef]. If no hash is specified, + // then it is the HEAD of the current local branch. // // If lenient is true, and the git command fails, a placeholder date is // returned. Otherwise, the VersionCheckError exception is propagated. - static String _latestGitCommitDate({ - String? branch, + static String _gitCommitDate({ + String gitRef = 'HEAD', bool lenient = false, }) { final List args = gitLog([ - if (branch != null) branch, + gitRef, '-n', '1', '--pretty=format:%ad', @@ -247,7 +252,7 @@ class FlutterVersion { DateTime localFrameworkCommitDate; try { // Don't perform the update check if fetching the latest local commit failed. - localFrameworkCommitDate = DateTime.parse(_latestGitCommitDate()); + localFrameworkCommitDate = DateTime.parse(_gitCommitDate()); } on VersionCheckError { return; } @@ -264,51 +269,18 @@ class FlutterVersion { ).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. /// /// Throws [VersionCheckError] if a git command fails, for example, when the /// remote git repository is not reachable due to a network issue. static Future fetchRemoteFrameworkCommitDate(String branch) async { - await _removeVersionCheckRemoteIfExists(); try { - await _run([ - 'git', - 'remote', - 'add', - _versionCheckRemote, - globals.flutterGit, - ]); - await _run(['git', 'fetch', _versionCheckRemote, branch]); - return _latestGitCommitDate( - branch: '$_versionCheckRemote/$branch', - ); + // Fetch upstream branch's commit and tags + await _run(['git', 'fetch', '--tags']); + return _gitCommitDate(gitRef: kGitTrackingUpstream); } on VersionCheckError catch (error) { - if (globals.platform.environment.containsKey('FLUTTER_GIT_URL')) { - globals.printWarning('Warning: the Flutter git upstream was overridden ' - 'by the environment variable FLUTTER_GIT_URL = ${globals.flutterGit}'); - } - globals.printError(error.toString()); + globals.printError(error.message); rethrow; - } finally { - await _removeVersionCheckRemoteIfExists(); - } - } - - static Future _removeVersionCheckRemoteIfExists() async { - final List remotes = (await _run(['git', 'remote'])) - .split('\n') - .map((String name) => name.trim()) // to account for OS-specific line-breaks - .toList(); - if (remotes.contains(_versionCheckRemote)) { - await _run(['git', 'remote', 'remove', _versionCheckRemote]); } } @@ -728,13 +700,20 @@ class GitTagVersion { /// The git tag that is this version's closest ancestor. 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) { final String channel = _runGit('git rev-parse --abbrev-ref HEAD', processUtils, workingDirectory); if (channel == 'dev' || channel == 'beta' || channel == 'stable') { globals.printTrace('Skipping request to fetchTags - on well known channel $channel.'); } 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] diff --git a/packages/flutter_tools/test/commands.shard/hermetic/upgrade_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/upgrade_test.dart index 7b2013076e..5e31c7ec3d 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/upgrade_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/upgrade_test.dart @@ -63,7 +63,7 @@ void main() { command: ['git', 'fetch', '--tags'], ), const FakeCommand( - command: ['git', 'rev-parse', '--verify', '@{u}'], + command: ['git', 'rev-parse', '--verify', '@{upstream}'], stdout: upstreamHeadRevision, ), const FakeCommand( diff --git a/packages/flutter_tools/test/commands.shard/permeable/upgrade_test.dart b/packages/flutter_tools/test/commands.shard/permeable/upgrade_test.dart index 5d2b5984ed..c98fe64080 100644 --- a/packages/flutter_tools/test/commands.shard/permeable/upgrade_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/upgrade_test.dart @@ -160,7 +160,7 @@ void main() { 'git', 'fetch', '--tags' ]), const FakeCommand(command: [ - 'git', 'rev-parse', '--verify', '@{u}', + 'git', 'rev-parse', '--verify', '@{upstream}', ], stdout: revision), const FakeCommand(command: [ @@ -188,10 +188,10 @@ void main() { 'git', 'fetch', '--tags' ]), FakeCommand( - command: ['git', 'rev-parse', '--verify', '@{u}'], + command: ['git', 'rev-parse', '--verify', '@{upstream}'], exception: ProcessException( 'git', - ['rev-parse', '--verify', '@{u}'], + ['rev-parse', '--verify', '@{upstream}'], 'fatal: HEAD does not point to a branch', ), ), @@ -217,10 +217,10 @@ void main() { 'git', 'fetch', '--tags' ]), FakeCommand( - command: ['git', 'rev-parse', '--verify', '@{u}'], + command: ['git', 'rev-parse', '--verify', '@{upstream}'], exception: ProcessException( 'git', - ['rev-parse', '--verify', '@{u}'], + ['rev-parse', '--verify', '@{upstream}'], 'fatal: no upstream configured for branch', ), ), diff --git a/packages/flutter_tools/test/general.shard/version_test.dart b/packages/flutter_tools/test/general.shard/version_test.dart index d69ee4a452..61395bd443 100644 --- a/packages/flutter_tools/test/general.shard/version_test.dart +++ b/packages/flutter_tools/test/general.shard/version_test.dart @@ -70,7 +70,7 @@ void main() { stdout: '0.1.2-3-1234abcd', ), FakeCommand( - command: const ['git', 'rev-parse', '--abbrev-ref', '--symbolic', '@{u}'], + command: const ['git', 'rev-parse', '--abbrev-ref', '--symbolic', '@{upstream}'], stdout: 'origin/$channel', ), const FakeCommand( @@ -78,31 +78,22 @@ void main() { stdout: flutterUpstreamUrl, ), FakeCommand( - command: const ['git', '-c', 'log.showSignature=false', 'log', '-n', '1', '--pretty=format:%ad', '--date=iso'], + command: const ['git', '-c', 'log.showSignature=false', 'log', 'HEAD', '-n', '1', '--pretty=format:%ad', '--date=iso'], stdout: getChannelUpToDateVersion().toString(), ), const FakeCommand( - command: ['git', 'remote'], - ), - const FakeCommand( - command: ['git', 'remote', 'add', '__flutter_version_check__', flutterUpstreamUrl], + command: ['git', 'fetch', '--tags'], ), FakeCommand( - command: ['git', 'fetch', '__flutter_version_check__', channel], - ), - FakeCommand( - command: ['git', '-c', 'log.showSignature=false', 'log', '__flutter_version_check__/$channel', '-n', '1', '--pretty=format:%ad', '--date=iso'], + command: const ['git', '-c', 'log.showSignature=false', 'log', '@{upstream}', '-n', '1', '--pretty=format:%ad', '--date=iso'], stdout: getChannelOutOfDateVersion().toString(), ), - const FakeCommand( - command: ['git', 'remote'], - ), const FakeCommand( command: ['git', '-c', 'log.showSignature=false', 'log', '-n', '1', '--pretty=format:%ar'], stdout: '1 second ago', ), FakeCommand( - command: const ['git', '-c', 'log.showSignature=false', 'log', '-n', '1', '--pretty=format:%ad', '--date=iso'], + command: const ['git', '-c', 'log.showSignature=false', 'log', 'HEAD', '-n', '1', '--pretty=format:%ad', '--date=iso'], stdout: getChannelUpToDateVersion().toString(), ), FakeCommand( @@ -114,6 +105,7 @@ void main() { final FlutterVersion flutterVersion = globals.flutterVersion; await flutterVersion.checkFlutterVersionFreshness(); expect(flutterVersion.channel, channel); + expect(flutterVersion.repositoryUrl, flutterUpstreamUrl); expect(flutterVersion.frameworkRevision, '1234abcd'); expect(flutterVersion.frameworkRevisionShort, '1234abcd'); expect(flutterVersion.frameworkVersion, '0.0.0-unknown'); @@ -423,7 +415,7 @@ void main() { stdout: '0.1.2-3-1234abcd', ), const FakeCommand( - command: ['git', 'rev-parse', '--abbrev-ref', '--symbolic', '@{u}'], + command: ['git', 'rev-parse', '--abbrev-ref', '--symbolic', '@{upstream}'], stdout: 'feature-branch', ), const FakeCommand( @@ -526,7 +518,8 @@ void main() { processManager: fakeProcessManager, 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); }); @@ -545,7 +538,9 @@ void main() { processManager: fakeProcessManager, 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); }); @@ -569,7 +564,9 @@ void main() { processManager: fakeProcessManager, 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 expect(gitTagVersion.frameworkVersionFor(headRevision), '1.3.0-0.0.pre.12'); }); @@ -588,8 +585,9 @@ void main() { processManager: fakeProcessManager, logger: BufferLogger.test(), ); + final FakePlatform platform = FakePlatform(); - GitTagVersion.determine(processUtils, workingDirectory: '.'); + GitTagVersion.determine(processUtils, platform, workingDirectory: '.'); expect(fakeProcessManager, hasNoRemainingExpectations); }); @@ -611,8 +609,9 @@ void main() { processManager: fakeProcessManager, logger: BufferLogger.test(), ); + final FakePlatform platform = FakePlatform(); - GitTagVersion.determine(processUtils, workingDirectory: '.', fetchTags: true); + GitTagVersion.determine(processUtils, platform, workingDirectory: '.', fetchTags: true); expect(fakeProcessManager, hasNoRemainingExpectations); }); @@ -637,8 +636,9 @@ void main() { processManager: fakeProcessManager, logger: BufferLogger.test(), ); + final FakePlatform platform = FakePlatform(); - GitTagVersion.determine(processUtils, workingDirectory: '.', fetchTags: true); + GitTagVersion.determine(processUtils, platform, workingDirectory: '.', fetchTags: true); expect(fakeProcessManager, hasNoRemainingExpectations); }); @@ -663,13 +663,12 @@ void main() { processManager: fakeProcessManager, logger: BufferLogger.test(), ); + final FakePlatform platform = FakePlatform( + environment: {'FLUTTER_GIT_URL': 'https://githubmirror.com/flutter.git'}, + ); - GitTagVersion.determine(processUtils, workingDirectory: '.', fetchTags: true); + GitTagVersion.determine(processUtils, platform, workingDirectory: '.', fetchTags: true); expect(fakeProcessManager, hasNoRemainingExpectations); - }, overrides: { - Platform: () => FakePlatform(environment: { - 'FLUTTER_GIT_URL': 'https://githubmirror.com/flutter.git', - }), }); }