version.dart 3.04 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:io';

7
import 'base/process.dart';
8
import 'cache.dart';
9

10 11 12 13
final Set<String> kKnownBranchNames = new Set<String>.from(<String>[
  'master',
  'alpha',
  'hackathon',
14
  'codelab',
15 16 17
  'beta'
]);

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
class FlutterVersion {
  FlutterVersion(this.flutterRoot) {
    _channel = _runGit('git rev-parse --abbrev-ref --symbolic @{u}');

    int slash = _channel.indexOf('/');
    if (slash != -1) {
      String remote = _channel.substring(0, slash);
      _repositoryUrl = _runGit('git ls-remote --get-url $remote');
      _channel = _channel.substring(slash + 1);
    } else if (_channel.isEmpty) {
      _channel = 'unknown';
    }

    _frameworkRevision = _runGit('git log -n 1 --pretty=format:%H');
    _frameworkAge = _runGit('git log -n 1 --pretty=format:%ar');
33
  }
34

35 36 37 38 39 40 41 42 43 44 45
  final String flutterRoot;

  String _repositoryUrl;
  String get repositoryUrl => _repositoryUrl;

  String _channel;
  /// `master`, `alpha`, `hackathon`, ...
  String get channel => _channel;

  String _frameworkRevision;
  String get frameworkRevision => _frameworkRevision;
46
  String get frameworkRevisionShort => _shortGitRevision(frameworkRevision);
47 48 49

  String _frameworkAge;
  String get frameworkAge => _frameworkAge;
50

51
  String get engineRevision => Cache.engineRevision;
52
  String get engineRevisionShort => _shortGitRevision(engineRevision);
53 54 55 56 57

  String _runGit(String command) => runSync(command.split(' '), workingDirectory: flutterRoot);

  @override
  String toString() {
58 59
    String from = 'Flutter on channel $channel (from ${repositoryUrl == null ? 'unknown source' : repositoryUrl})';
    String flutterText = 'Framework revision $frameworkRevisionShort ($frameworkAge); engine revision $engineRevisionShort';
60

61
    return '$from\n$flutterText';
62 63 64
  }

  static FlutterVersion getVersion([String flutterRoot]) {
65
    return new FlutterVersion(flutterRoot != null ? flutterRoot : Cache.flutterRoot);
66
  }
67

68
  /// Return a short string for the version (`alpha/a76bc8e22b`).
69
  static String getVersionString({ bool whitelistBranchName: false }) {
70
    final String cwd = Cache.flutterRoot;
71

72 73
    String commit = _shortGitRevision(_runSync('git', <String>['rev-parse', 'HEAD'], cwd));
    commit = commit.isEmpty ? 'unknown' : commit;
74 75

    String branch = _runSync('git', <String>['rev-parse', '--abbrev-ref', 'HEAD'], cwd);
76 77 78 79 80 81 82
    branch = branch == 'HEAD' ? 'master' : branch;

    if (whitelistBranchName || branch.isEmpty) {
      // Only return the branch names we know about; arbitrary branch names might contain PII.
      if (!kKnownBranchNames.contains(branch))
        branch = 'dev';
    }
83

84
    return '$branch/$commit';
85 86 87 88 89 90
  }
}

String _runSync(String executable, List<String> arguments, String cwd) {
  ProcessResult results = Process.runSync(executable, arguments, workingDirectory: cwd);
  return results.exitCode == 0 ? results.stdout.trim() : '';
91
}
92 93 94 95

String _shortGitRevision(String revision) {
  return revision.length > 10 ? revision.substring(0, 10) : revision;
}