candidates.dart 2.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
// 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<void> {
  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(
      <String>['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<String> branches = git.getOutput(
      <String>[
        '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)!);

85 86 87 88 89
      final RegExpMatch? match = remotePattern.firstMatch(branchName);
      // If this is not the correct remote
      if (match == null) {
        continue;
      }
90 91 92 93 94 95
      if (x < currentVersion.x) {
        continue;
      }
      if (x == currentVersion.x && y < currentVersion.y) {
        continue;
      }
96
      if (x == currentX && y == currentY && currentZ == 0 && m <= currentM) {
97 98 99 100 101 102
        continue;
      }
      stdio.printStatus(match.group(1)!);
    }
  }
}