[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
[email protected] | cefd1f2 | 2012-12-20 02:04:28 +0000 | [diff] [blame] | 5 | /// Helper functionality for invoking Git. |
[email protected] | e04ae07 | 2013-12-03 08:37:49 +0000 | [diff] [blame] | 6 | import 'dart:async'; |
[email protected] | 1610466 | 2014-06-17 21:16:18 +0000 | [diff] [blame] | 7 | |
Sigurd Meldgaard | 8343700 | 2022-03-24 13:25:15 +0100 | [diff] [blame^] | 8 | import 'package:collection/collection.dart'; |
Sigurd Meldgaard | 0764437 | 2021-10-08 00:35:05 -0700 | [diff] [blame] | 9 | import 'package:path/path.dart' as p; |
Sigurd Meldgaard | 3174a26 | 2022-03-08 12:08:27 +0100 | [diff] [blame] | 10 | import 'package:pub_semver/pub_semver.dart'; |
Sigurd Meldgaard | 0764437 | 2021-10-08 00:35:05 -0700 | [diff] [blame] | 11 | |
Sigurd Meldgaard | 3174a26 | 2022-03-08 12:08:27 +0100 | [diff] [blame] | 12 | import 'command_runner.dart'; |
[email protected] | da5bae2 | 2014-07-30 21:09:05 +0000 | [diff] [blame] | 13 | import 'exceptions.dart'; |
[email protected] | 05c9cc0 | 2012-10-09 01:25:43 +0000 | [diff] [blame] | 14 | import 'io.dart'; |
[email protected] | 71976ef | 2012-12-08 02:57:00 +0000 | [diff] [blame] | 15 | import 'log.dart' as log; |
[email protected] | 6a0d2a8 | 2014-06-03 00:54:43 +0000 | [diff] [blame] | 16 | import 'utils.dart'; |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 17 | |
[email protected] | e5d4dbe | 2014-04-02 00:46:50 +0000 | [diff] [blame] | 18 | /// An exception thrown because a git command failed. |
[email protected] | da5bae2 | 2014-07-30 21:09:05 +0000 | [diff] [blame] | 19 | class GitException implements ApplicationException { |
[email protected] | e5d4dbe | 2014-04-02 00:46:50 +0000 | [diff] [blame] | 20 | /// The arguments to the git command. |
| 21 | final List<String> args; |
| 22 | |
| 23 | /// The standard error emitted by git. |
| 24 | final String stderr; |
| 25 | |
Sigurd Meldgaard | 7268656 | 2020-01-13 14:31:50 +0100 | [diff] [blame] | 26 | /// The standard out emitted by git. |
| 27 | final String stdout; |
[email protected] | e5d4dbe | 2014-04-02 00:46:50 +0000 | [diff] [blame] | 28 | |
Sigurd Meldgaard | 7268656 | 2020-01-13 14:31:50 +0100 | [diff] [blame] | 29 | /// The error code |
| 30 | final int exitCode; |
| 31 | |
| 32 | @override |
| 33 | String get message => 'Git error. Command: `git ${args.join(' ')}`\n' |
| 34 | 'stdout: $stdout\n' |
| 35 | 'stderr: $stderr\n' |
| 36 | 'exit code: $exitCode'; |
| 37 | |
| 38 | GitException(Iterable<String> args, this.stdout, this.stderr, this.exitCode) |
| 39 | : args = args.toList(); |
[email protected] | e5d4dbe | 2014-04-02 00:46:50 +0000 | [diff] [blame] | 40 | |
Nate Bosch | ceaa86f | 2020-01-06 14:12:36 -0800 | [diff] [blame] | 41 | @override |
[email protected] | e5d4dbe | 2014-04-02 00:46:50 +0000 | [diff] [blame] | 42 | String toString() => message; |
| 43 | } |
| 44 | |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 45 | /// Tests whether or not the git command-line app is available for use. |
Jonas Finnemann Jensen | cfa9dc7 | 2019-09-16 17:45:59 +0200 | [diff] [blame] | 46 | bool get isInstalled => command != null; |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 47 | |
[email protected] | 7ce9564 | 2014-06-18 20:46:51 +0000 | [diff] [blame] | 48 | /// Run a git process with [args] from [workingDir]. |
| 49 | /// |
| 50 | /// Returns the stdout as a list of strings if it succeeded. Completes to an |
| 51 | /// exception if it failed. |
[email protected] | c6eca94 | 2013-01-24 01:13:36 +0000 | [diff] [blame] | 52 | Future<List<String>> run(List<String> args, |
István Soós | 30580be | 2021-09-29 10:08:17 +0200 | [diff] [blame] | 53 | {String? workingDir, Map<String, String>? environment}) async { |
[email protected] | 1610466 | 2014-06-17 21:16:18 +0000 | [diff] [blame] | 54 | if (!isInstalled) { |
Nate Bosch | ceaa86f | 2020-01-06 14:12:36 -0800 | [diff] [blame] | 55 | fail('Cannot find a Git executable.\n' |
| 56 | 'Please ensure Git is correctly installed.'); |
[email protected] | 1610466 | 2014-06-17 21:16:18 +0000 | [diff] [blame] | 57 | } |
[email protected] | 6a0d2a8 | 2014-06-03 00:54:43 +0000 | [diff] [blame] | 58 | |
[email protected] | a137506 | 2015-01-08 23:58:32 +0000 | [diff] [blame] | 59 | log.muteProgress(); |
Natalie Weizenbaum | 83fc6eb | 2016-08-18 15:04:44 -0700 | [diff] [blame] | 60 | try { |
István Soós | 30580be | 2021-09-29 10:08:17 +0200 | [diff] [blame] | 61 | final result = await runProcess(command!, args, |
Sigurd Meldgaard | 31cfec5 | 2020-05-12 15:36:54 +0200 | [diff] [blame] | 62 | workingDir: workingDir, |
| 63 | environment: {...?environment, 'LANG': 'en_GB'}); |
Sigurd Meldgaard | 7268656 | 2020-01-13 14:31:50 +0100 | [diff] [blame] | 64 | if (!result.success) { |
| 65 | throw GitException(args, result.stdout.join('\n'), |
| 66 | result.stderr.join('\n'), result.exitCode); |
| 67 | } |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 68 | return result.stdout; |
Natalie Weizenbaum | 83fc6eb | 2016-08-18 15:04:44 -0700 | [diff] [blame] | 69 | } finally { |
[email protected] | a137506 | 2015-01-08 23:58:32 +0000 | [diff] [blame] | 70 | log.unmuteProgress(); |
Natalie Weizenbaum | 83fc6eb | 2016-08-18 15:04:44 -0700 | [diff] [blame] | 71 | } |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 72 | } |
| 73 | |
[email protected] | 1610466 | 2014-06-17 21:16:18 +0000 | [diff] [blame] | 74 | /// Like [run], but synchronous. |
Jacob MacDonald | edd3295 | 2017-03-15 11:12:46 -0700 | [diff] [blame] | 75 | List<String> runSync(List<String> args, |
István Soós | 30580be | 2021-09-29 10:08:17 +0200 | [diff] [blame] | 76 | {String? workingDir, Map<String, String>? environment}) { |
[email protected] | 1610466 | 2014-06-17 21:16:18 +0000 | [diff] [blame] | 77 | if (!isInstalled) { |
Nate Bosch | ceaa86f | 2020-01-06 14:12:36 -0800 | [diff] [blame] | 78 | fail('Cannot find a Git executable.\n' |
| 79 | 'Please ensure Git is correctly installed.'); |
[email protected] | 1610466 | 2014-06-17 21:16:18 +0000 | [diff] [blame] | 80 | } |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 81 | |
István Soós | 30580be | 2021-09-29 10:08:17 +0200 | [diff] [blame] | 82 | final result = runProcessSync(command!, args, |
Jacob MacDonald | edd3295 | 2017-03-15 11:12:46 -0700 | [diff] [blame] | 83 | workingDir: workingDir, environment: environment); |
Sigurd Meldgaard | 7268656 | 2020-01-13 14:31:50 +0100 | [diff] [blame] | 84 | if (!result.success) { |
| 85 | throw GitException(args, result.stdout.join('\n'), result.stderr.join('\n'), |
| 86 | result.exitCode); |
| 87 | } |
| 88 | |
[email protected] | 1610466 | 2014-06-17 21:16:18 +0000 | [diff] [blame] | 89 | return result.stdout; |
| 90 | } |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 91 | |
Sigurd Meldgaard | 8343700 | 2022-03-24 13:25:15 +0100 | [diff] [blame^] | 92 | /// The name of the git command-line app, or `null` if Git could not be found on |
| 93 | /// the user's PATH. |
| 94 | final String? command = ['git', 'git.cmd'].firstWhereOrNull(_tryGitCommand); |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 95 | |
Sigurd Meldgaard | 0764437 | 2021-10-08 00:35:05 -0700 | [diff] [blame] | 96 | /// Returns the root of the git repo [dir] belongs to. Returns `null` if not |
| 97 | /// in a git repo or git is not installed. |
| 98 | String? repoRoot(String dir) { |
| 99 | if (isInstalled) { |
| 100 | try { |
| 101 | return p.normalize( |
| 102 | runSync(['rev-parse', '--show-toplevel'], workingDir: dir).first, |
| 103 | ); |
| 104 | } on GitException { |
| 105 | // Not in a git folder. |
| 106 | return null; |
| 107 | } |
| 108 | } |
| 109 | return null; |
| 110 | } |
| 111 | |
Sigurd Meldgaard | 3174a26 | 2022-03-08 12:08:27 +0100 | [diff] [blame] | 112 | /// '--recourse-submodules' was introduced in Git 2.14 |
| 113 | /// (https://git-scm.com/book/en/v2/Git-Tools-Submodules). |
| 114 | final _minSupportedGitVersion = Version(2, 14, 0); |
| 115 | |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 116 | /// Checks whether [command] is the Git command for this computer. |
[email protected] | 1610466 | 2014-06-17 21:16:18 +0000 | [diff] [blame] | 117 | bool _tryGitCommand(String command) { |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 118 | // If "git --version" prints something familiar, git is working. |
[email protected] | 1610466 | 2014-06-17 21:16:18 +0000 | [diff] [blame] | 119 | try { |
Nate Bosch | ceaa86f | 2020-01-06 14:12:36 -0800 | [diff] [blame] | 120 | var result = runProcessSync(command, ['--version']); |
Sigurd Meldgaard | 3174a26 | 2022-03-08 12:08:27 +0100 | [diff] [blame] | 121 | |
| 122 | if (result.stdout.length != 1) return false; |
| 123 | final output = result.stdout.single; |
| 124 | final match = RegExp(r'^git version (\d+)\.(\d+)\.').matchAsPrefix(output); |
| 125 | |
| 126 | if (match == null) return false; |
| 127 | // Git seems to use many parts in the version number. We just check the |
| 128 | // first two. |
| 129 | final major = int.parse(match[1]!); |
| 130 | final minor = int.parse(match[2]!); |
| 131 | if (Version(major, minor, 0) < _minSupportedGitVersion) { |
| 132 | // We just warn here, as some features might work with older versions of |
| 133 | // git. |
| 134 | log.warning(''' |
| 135 | You have a very old version of git (version ${output.substring('git version '.length)}), |
| 136 | for $topLevelProgram it is recommended to use git version 2.14 or newer. |
| 137 | '''); |
| 138 | } |
Sigurd Meldgaard | 8343700 | 2022-03-24 13:25:15 +0100 | [diff] [blame^] | 139 | log.fine('Determined git command $command.'); |
Sigurd Meldgaard | 3174a26 | 2022-03-08 12:08:27 +0100 | [diff] [blame] | 140 | return true; |
Jonas Finnemann Jensen | 8ea9612 | 2020-12-08 16:22:53 +0100 | [diff] [blame] | 141 | } on RunProcessException catch (err) { |
[email protected] | fc7fe36 | 2013-01-09 21:37:40 +0000 | [diff] [blame] | 142 | // If the process failed, they probably don't have it. |
Jonas Finnemann Jensen | 8ea9612 | 2020-12-08 16:22:53 +0100 | [diff] [blame] | 143 | log.error('Git command is not "$command": $err'); |
[email protected] | 1610466 | 2014-06-17 21:16:18 +0000 | [diff] [blame] | 144 | return false; |
| 145 | } |
[email protected] | cefd1f2 | 2012-12-20 02:04:28 +0000 | [diff] [blame] | 146 | } |