Unverified Commit 8dcb4c33 authored by Christopher Fujino's avatar Christopher Fujino Committed by GitHub

Fix version parsing to use git tag --points-at (#63505)

parent c36b75e4
...@@ -760,22 +760,20 @@ class GitTagVersion { ...@@ -760,22 +760,20 @@ class GitTagVersion {
} }
} }
final List<String> tags = _runGit( final List<String> tags = _runGit(
'git tag --contains HEAD', processUtils, workingDirectory).split('\n'); 'git tag --points-at HEAD', processUtils, workingDirectory).trim().split('\n');
// Check first for a stable tag // Check first for a stable tag
final RegExp stableTagPattern = RegExp(r'^\d+\.\d+\.\d+$'); final RegExp stableTagPattern = RegExp(r'^\d+\.\d+\.\d+$');
for (final String tag in tags) { for (final String tag in tags) {
final String trimmedTag = tag.trim(); if (stableTagPattern.hasMatch(tag.trim())) {
if (stableTagPattern.hasMatch(trimmedTag)) { return parse(tag);
return parse(trimmedTag);
} }
} }
// Next check for a dev tag // Next check for a dev tag
final RegExp devTagPattern = RegExp(r'^\d+\.\d+\.\d+-\d+\.\d+\.pre$'); final RegExp devTagPattern = RegExp(r'^\d+\.\d+\.\d+-\d+\.\d+\.pre$');
for (final String tag in tags) { for (final String tag in tags) {
final String trimmedTag = tag.trim(); if (devTagPattern.hasMatch(tag.trim())) {
if (devTagPattern.hasMatch(trimmedTag)) { return parse(tag);
return parse(trimmedTag);
} }
} }
......
...@@ -323,7 +323,7 @@ void main() { ...@@ -323,7 +323,7 @@ void main() {
fakeProcessManager = FakeProcessManager.list(<FakeCommand>[ fakeProcessManager = FakeProcessManager.list(<FakeCommand>[
const FakeCommand( const FakeCommand(
command: <String>[ command: <String>[
'git', 'tag', '--contains', 'HEAD', 'git', 'tag', '--points-at', 'HEAD',
], ],
stdout: '', stdout: '',
), ),
......
...@@ -185,7 +185,7 @@ void main() { ...@@ -185,7 +185,7 @@ void main() {
workingDirectory: Cache.flutterRoot)).thenReturn(result); workingDirectory: Cache.flutterRoot)).thenReturn(result);
when(processManager.runSync('git fetch https://github.com/flutter/flutter.git --tags'.split(' '), when(processManager.runSync('git fetch https://github.com/flutter/flutter.git --tags'.split(' '),
workingDirectory: Cache.flutterRoot)).thenReturn(result); workingDirectory: Cache.flutterRoot)).thenReturn(result);
when(processManager.runSync('git tag --contains HEAD'.split(' '), when(processManager.runSync('git tag --points-at HEAD'.split(' '),
workingDirectory: Cache.flutterRoot)).thenReturn(result); workingDirectory: Cache.flutterRoot)).thenReturn(result);
when(processManager.runSync('git describe --match *.*.* --first-parent --long --tags'.split(' '), when(processManager.runSync('git describe --match *.*.* --first-parent --long --tags'.split(' '),
workingDirectory: Cache.flutterRoot)).thenReturn(result); workingDirectory: Cache.flutterRoot)).thenReturn(result);
......
...@@ -7,6 +7,7 @@ import 'dart:convert'; ...@@ -7,6 +7,7 @@ import 'dart:convert';
import 'package:collection/collection.dart' show ListEquality; import 'package:collection/collection.dart' show ListEquality;
import 'package:flutter_tools/src/base/context.dart'; import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart'; import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/process.dart'; import 'package:flutter_tools/src/base/process.dart';
import 'package:flutter_tools/src/base/time.dart'; import 'package:flutter_tools/src/base/time.dart';
...@@ -458,27 +459,66 @@ void main() { ...@@ -458,27 +459,66 @@ void main() {
); );
}); });
testUsingContext('determine favors stable tags over dev tags', () { testUsingContext('determine reports correct stable version if HEAD is at a tag', () {
final MockProcessUtils mockProcessUtils = MockProcessUtils(); const String stableTag = '1.2.3';
when(mockProcessUtils.runSync( final FakeProcessManager fakeProcessManager = FakeProcessManager.list(
<String>['git', 'tag', '--contains', 'HEAD'], <FakeCommand>[
workingDirectory: anyNamed('workingDirectory'), const FakeCommand(
environment: anyNamed('environment'), command: <String>['git', 'tag', '--points-at', 'HEAD'],
)).thenReturn(RunResult( stdout: stableTag,
ProcessResult(1, 0, '1.2.3-0.0.pre\n1.2.3\n1.2.3-0.1.pre', ''), ),
<String>['git', 'tag', '--contains', 'HEAD'], ],
)); );
final GitTagVersion version = GitTagVersion.determine(mockProcessUtils, workingDirectory: '.'); final ProcessUtils processUtils = ProcessUtils(
expect(version.gitTag, '1.2.3'); processManager: fakeProcessManager,
expect(version.devPatch, null); logger: BufferLogger.test(),
expect(version.devVersion, null); );
// We shouldn't have to fallback to git describe, because we are exactly final GitTagVersion gitTagVersion = GitTagVersion.determine(processUtils, workingDirectory: '.');
// on a release tag. expect(gitTagVersion.frameworkVersionFor('abcd1234'), stableTag);
verifyNever(mockProcessUtils.runSync( });
<String>['git', 'describe', '--match', '*.*.*', '--first-parent', '--long', '--tags'],
workingDirectory: anyNamed('workingDirectory'), testUsingContext('determine favors stable tag over dev tag if both idenitfy HEAD', () {
environment: anyNamed('environment'), const String stableTag = '1.2.3';
)); final FakeProcessManager fakeProcessManager = FakeProcessManager.list(
<FakeCommand>[
const FakeCommand(
command: <String>['git', 'tag', '--points-at', 'HEAD'],
// This tests the unlikely edge case where a dev release made it to stable without any cherry picks
stdout: '1.2.3-6.0.pre\n$stableTag',
),
],
);
final ProcessUtils processUtils = ProcessUtils(
processManager: fakeProcessManager,
logger: BufferLogger.test(),
);
final GitTagVersion gitTagVersion = GitTagVersion.determine(processUtils, workingDirectory: '.');
expect(gitTagVersion.frameworkVersionFor('abcd1234'), stableTag);
});
testUsingContext('determine reports correct git describe version if HEAD is not at a tag', () {
const String devTag = '1.2.3-2.0.pre';
const String headRevision = 'abcd1234';
const String commitsAhead = '12';
final FakeProcessManager fakeProcessManager = FakeProcessManager.list(
<FakeCommand>[
const FakeCommand(
command: <String>['git', 'tag', '--points-at', 'HEAD'],
stdout: '', // no tag
),
const FakeCommand(
command: <String>['git', 'describe', '--match', '*.*.*', '--first-parent', '--long', '--tags'],
stdout: '$devTag-$commitsAhead-g$headRevision',
),
],
);
final ProcessUtils processUtils = ProcessUtils(
processManager: fakeProcessManager,
logger: BufferLogger.test(),
);
final GitTagVersion gitTagVersion = GitTagVersion.determine(processUtils, workingDirectory: '.');
// reported version should increment the number after the dash
expect(gitTagVersion.frameworkVersionFor(headRevision), '1.2.3-3.0.pre.12');
}); });
testUsingContext('determine does not call fetch --tags', () { testUsingContext('determine does not call fetch --tags', () {
...@@ -494,12 +534,12 @@ void main() { ...@@ -494,12 +534,12 @@ void main() {
environment: anyNamed('environment'), environment: anyNamed('environment'),
)).thenReturn(RunResult(ProcessResult(106, 0, 'v0.1.2-3-1234abcd', ''), <String>['git', 'describe'])); )).thenReturn(RunResult(ProcessResult(106, 0, 'v0.1.2-3-1234abcd', ''), <String>['git', 'describe']));
when(processUtils.runSync( when(processUtils.runSync(
<String>['git', 'tag', '--contains', 'HEAD'], <String>['git', 'tag', '--points-at', 'HEAD'],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
environment: anyNamed('environment'), environment: anyNamed('environment'),
)).thenReturn( )).thenReturn(
RunResult(ProcessResult(110, 0, '', ''), RunResult(ProcessResult(110, 0, '', ''),
<String>['git', 'tag', '--contains', 'HEAD'], <String>['git', 'tag', '--points-at', 'HEAD'],
)); ));
GitTagVersion.determine(processUtils, workingDirectory: '.'); GitTagVersion.determine(processUtils, workingDirectory: '.');
...@@ -539,12 +579,12 @@ void main() { ...@@ -539,12 +579,12 @@ void main() {
environment: anyNamed('environment'), environment: anyNamed('environment'),
)).thenReturn(RunResult(ProcessResult(107, 0, 'v0.1.2-3-1234abcd', ''), <String>['git', 'describe'])); )).thenReturn(RunResult(ProcessResult(107, 0, 'v0.1.2-3-1234abcd', ''), <String>['git', 'describe']));
when(processUtils.runSync( when(processUtils.runSync(
<String>['git', 'tag', '--contains', 'HEAD'], <String>['git', 'tag', '--points-at', 'HEAD'],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
environment: anyNamed('environment'), environment: anyNamed('environment'),
)).thenReturn( )).thenReturn(
RunResult(ProcessResult(108, 0, '', ''), RunResult(ProcessResult(108, 0, '', ''),
<String>['git', 'tag', '--contains', 'HEAD'], <String>['git', 'tag', '--points-at', 'HEAD'],
)); ));
GitTagVersion.determine(processUtils, workingDirectory: '.', fetchTags: true); GitTagVersion.determine(processUtils, workingDirectory: '.', fetchTags: true);
...@@ -579,12 +619,12 @@ void main() { ...@@ -579,12 +619,12 @@ void main() {
environment: anyNamed('environment'), environment: anyNamed('environment'),
)).thenReturn(RunResult(ProcessResult(109, 0, '', ''), <String>['git', 'fetch'])); )).thenReturn(RunResult(ProcessResult(109, 0, '', ''), <String>['git', 'fetch']));
when(processUtils.runSync( when(processUtils.runSync(
<String>['git', 'tag', '--contains', 'HEAD'], <String>['git', 'tag', '--points-at', 'HEAD'],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
environment: anyNamed('environment'), environment: anyNamed('environment'),
)).thenReturn( )).thenReturn(
RunResult(ProcessResult(110, 0, '', ''), RunResult(ProcessResult(110, 0, '', ''),
<String>['git', 'tag', '--contains', 'HEAD'], <String>['git', 'tag', '--points-at', 'HEAD'],
)); ));
when(processUtils.runSync( when(processUtils.runSync(
<String>['git', 'describe', '--match', '*.*.*', '--first-parent', '--long', '--tags'], <String>['git', 'describe', '--match', '*.*.*', '--first-parent', '--long', '--tags'],
...@@ -624,12 +664,12 @@ void main() { ...@@ -624,12 +664,12 @@ void main() {
environment: anyNamed('environment'), environment: anyNamed('environment'),
)).thenReturn(RunResult(ProcessResult(109, 0, '', ''), <String>['git', 'fetch'])); )).thenReturn(RunResult(ProcessResult(109, 0, '', ''), <String>['git', 'fetch']));
when(processUtils.runSync( when(processUtils.runSync(
<String>['git', 'tag', '--contains', 'HEAD'], <String>['git', 'tag', '--points-at', 'HEAD'],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
environment: anyNamed('environment'), environment: anyNamed('environment'),
)).thenReturn( )).thenReturn(
RunResult(ProcessResult(110, 0, '', ''), RunResult(ProcessResult(110, 0, '', ''),
<String>['git', 'tag', '--contains', 'HEAD'], <String>['git', 'tag', '--points-at', 'HEAD'],
)); ));
when(processUtils.runSync( when(processUtils.runSync(
<String>['git', 'describe', '--match', '*.*.*', '--first-parent', '--long', '--tags'], <String>['git', 'describe', '--match', '*.*.*', '--first-parent', '--long', '--tags'],
...@@ -765,7 +805,7 @@ void fakeData( ...@@ -765,7 +805,7 @@ void fakeData(
environment: anyNamed('environment'), environment: anyNamed('environment'),
)).thenReturn(ProcessResult(105, 0, '', '')); )).thenReturn(ProcessResult(105, 0, '', ''));
when(pm.runSync( when(pm.runSync(
<String>['git', 'tag', '--contains', 'HEAD'], <String>['git', 'tag', '--points-at', 'HEAD'],
workingDirectory: anyNamed('workingDirectory'), workingDirectory: anyNamed('workingDirectory'),
environment: anyNamed('environment'), environment: anyNamed('environment'),
)).thenReturn(ProcessResult(106, 0, '', '')); )).thenReturn(ProcessResult(106, 0, '', ''));
......
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