blob: 5fa7536c1e3c3828812e7c1cef04f18c8ad7ced3 [file] [log] [blame]
[email protected]ddfb1642012-10-09 00:56:57 +00001// 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]cefd1f22012-12-20 02:04:28 +00005/// Helper functionality for invoking Git.
[email protected]e04ae072013-12-03 08:37:49 +00006import 'dart:async';
[email protected]16104662014-06-17 21:16:18 +00007
Sigurd Meldgaard07644372021-10-08 00:35:05 -07008import 'package:path/path.dart' as p;
9
[email protected]da5bae22014-07-30 21:09:05 +000010import 'exceptions.dart';
[email protected]05c9cc02012-10-09 01:25:43 +000011import 'io.dart';
[email protected]71976ef2012-12-08 02:57:00 +000012import 'log.dart' as log;
[email protected]6a0d2a82014-06-03 00:54:43 +000013import 'utils.dart';
[email protected]ddfb1642012-10-09 00:56:57 +000014
[email protected]e5d4dbe2014-04-02 00:46:50 +000015/// An exception thrown because a git command failed.
[email protected]da5bae22014-07-30 21:09:05 +000016class GitException implements ApplicationException {
[email protected]e5d4dbe2014-04-02 00:46:50 +000017 /// The arguments to the git command.
18 final List<String> args;
19
20 /// The standard error emitted by git.
21 final String stderr;
22
Sigurd Meldgaard72686562020-01-13 14:31:50 +010023 /// The standard out emitted by git.
24 final String stdout;
[email protected]e5d4dbe2014-04-02 00:46:50 +000025
Sigurd Meldgaard72686562020-01-13 14:31:50 +010026 /// The error code
27 final int exitCode;
28
29 @override
30 String get message => 'Git error. Command: `git ${args.join(' ')}`\n'
31 'stdout: $stdout\n'
32 'stderr: $stderr\n'
33 'exit code: $exitCode';
34
35 GitException(Iterable<String> args, this.stdout, this.stderr, this.exitCode)
36 : args = args.toList();
[email protected]e5d4dbe2014-04-02 00:46:50 +000037
Nate Boschceaa86f2020-01-06 14:12:36 -080038 @override
[email protected]e5d4dbe2014-04-02 00:46:50 +000039 String toString() => message;
40}
41
[email protected]ddfb1642012-10-09 00:56:57 +000042/// Tests whether or not the git command-line app is available for use.
Jonas Finnemann Jensencfa9dc72019-09-16 17:45:59 +020043bool get isInstalled => command != null;
[email protected]ddfb1642012-10-09 00:56:57 +000044
[email protected]7ce95642014-06-18 20:46:51 +000045/// Run a git process with [args] from [workingDir].
46///
47/// Returns the stdout as a list of strings if it succeeded. Completes to an
48/// exception if it failed.
[email protected]c6eca942013-01-24 01:13:36 +000049Future<List<String>> run(List<String> args,
István Soós30580be2021-09-29 10:08:17 +020050 {String? workingDir, Map<String, String>? environment}) async {
[email protected]16104662014-06-17 21:16:18 +000051 if (!isInstalled) {
Nate Boschceaa86f2020-01-06 14:12:36 -080052 fail('Cannot find a Git executable.\n'
53 'Please ensure Git is correctly installed.');
[email protected]16104662014-06-17 21:16:18 +000054 }
[email protected]6a0d2a82014-06-03 00:54:43 +000055
[email protected]a1375062015-01-08 23:58:32 +000056 log.muteProgress();
Natalie Weizenbaum83fc6eb2016-08-18 15:04:44 -070057 try {
István Soós30580be2021-09-29 10:08:17 +020058 final result = await runProcess(command!, args,
Sigurd Meldgaard31cfec52020-05-12 15:36:54 +020059 workingDir: workingDir,
60 environment: {...?environment, 'LANG': 'en_GB'});
Sigurd Meldgaard72686562020-01-13 14:31:50 +010061 if (!result.success) {
62 throw GitException(args, result.stdout.join('\n'),
63 result.stderr.join('\n'), result.exitCode);
64 }
[email protected]ddfb1642012-10-09 00:56:57 +000065 return result.stdout;
Natalie Weizenbaum83fc6eb2016-08-18 15:04:44 -070066 } finally {
[email protected]a1375062015-01-08 23:58:32 +000067 log.unmuteProgress();
Natalie Weizenbaum83fc6eb2016-08-18 15:04:44 -070068 }
[email protected]ddfb1642012-10-09 00:56:57 +000069}
70
[email protected]16104662014-06-17 21:16:18 +000071/// Like [run], but synchronous.
Jacob MacDonaldedd32952017-03-15 11:12:46 -070072List<String> runSync(List<String> args,
István Soós30580be2021-09-29 10:08:17 +020073 {String? workingDir, Map<String, String>? environment}) {
[email protected]16104662014-06-17 21:16:18 +000074 if (!isInstalled) {
Nate Boschceaa86f2020-01-06 14:12:36 -080075 fail('Cannot find a Git executable.\n'
76 'Please ensure Git is correctly installed.');
[email protected]16104662014-06-17 21:16:18 +000077 }
[email protected]ddfb1642012-10-09 00:56:57 +000078
István Soós30580be2021-09-29 10:08:17 +020079 final result = runProcessSync(command!, args,
Jacob MacDonaldedd32952017-03-15 11:12:46 -070080 workingDir: workingDir, environment: environment);
Sigurd Meldgaard72686562020-01-13 14:31:50 +010081 if (!result.success) {
82 throw GitException(args, result.stdout.join('\n'), result.stderr.join('\n'),
83 result.exitCode);
84 }
85
[email protected]16104662014-06-17 21:16:18 +000086 return result.stdout;
87}
[email protected]ddfb1642012-10-09 00:56:57 +000088
Jonas Finnemann Jensencfa9dc72019-09-16 17:45:59 +020089/// Returns the name of the git command-line app, or `null` if Git could not be
[email protected]ddfb1642012-10-09 00:56:57 +000090/// found on the user's PATH.
István Soós30580be2021-09-29 10:08:17 +020091String? get command {
[email protected]16104662014-06-17 21:16:18 +000092 if (_commandCache != null) return _commandCache;
93
Nate Boschceaa86f2020-01-06 14:12:36 -080094 if (_tryGitCommand('git')) {
95 _commandCache = 'git';
96 } else if (_tryGitCommand('git.cmd')) {
97 _commandCache = 'git.cmd';
[email protected]16104662014-06-17 21:16:18 +000098 } else {
99 return null;
[email protected]ddfb1642012-10-09 00:56:57 +0000100 }
101
Kevin Moore297f0172019-01-25 21:23:41 -0800102 log.fine('Determined git command $_commandCache.');
[email protected]16104662014-06-17 21:16:18 +0000103 return _commandCache;
[email protected]ddfb1642012-10-09 00:56:57 +0000104}
Jacob MacDonaldedd32952017-03-15 11:12:46 -0700105
István Soós30580be2021-09-29 10:08:17 +0200106String? _commandCache;
[email protected]ddfb1642012-10-09 00:56:57 +0000107
Sigurd Meldgaard07644372021-10-08 00:35:05 -0700108/// Returns the root of the git repo [dir] belongs to. Returns `null` if not
109/// in a git repo or git is not installed.
110String? repoRoot(String dir) {
111 if (isInstalled) {
112 try {
113 return p.normalize(
114 runSync(['rev-parse', '--show-toplevel'], workingDir: dir).first,
115 );
116 } on GitException {
117 // Not in a git folder.
118 return null;
119 }
120 }
121 return null;
122}
123
[email protected]ddfb1642012-10-09 00:56:57 +0000124/// Checks whether [command] is the Git command for this computer.
[email protected]16104662014-06-17 21:16:18 +0000125bool _tryGitCommand(String command) {
[email protected]ddfb1642012-10-09 00:56:57 +0000126 // If "git --version" prints something familiar, git is working.
[email protected]16104662014-06-17 21:16:18 +0000127 try {
Nate Boschceaa86f2020-01-06 14:12:36 -0800128 var result = runProcessSync(command, ['--version']);
129 var regexp = RegExp('^git version');
[email protected]16104662014-06-17 21:16:18 +0000130 return result.stdout.length == 1 && regexp.hasMatch(result.stdout.single);
Jonas Finnemann Jensen8ea96122020-12-08 16:22:53 +0100131 } on RunProcessException catch (err) {
[email protected]fc7fe362013-01-09 21:37:40 +0000132 // If the process failed, they probably don't have it.
Jonas Finnemann Jensen8ea96122020-12-08 16:22:53 +0100133 log.error('Git command is not "$command": $err');
[email protected]16104662014-06-17 21:16:18 +0000134 return false;
135 }
[email protected]cefd1f22012-12-20 02:04:28 +0000136}