blob: ded45e95cc1e63fc4f6f007281b1957a63800669 [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 Meldgaard83437002022-03-24 13:25:15 +01008import 'package:collection/collection.dart';
Sigurd Meldgaard07644372021-10-08 00:35:05 -07009import 'package:path/path.dart' as p;
Sigurd Meldgaard3174a262022-03-08 12:08:27 +010010import 'package:pub_semver/pub_semver.dart';
Sigurd Meldgaard07644372021-10-08 00:35:05 -070011
Sigurd Meldgaard3174a262022-03-08 12:08:27 +010012import 'command_runner.dart';
[email protected]da5bae22014-07-30 21:09:05 +000013import 'exceptions.dart';
[email protected]05c9cc02012-10-09 01:25:43 +000014import 'io.dart';
[email protected]71976ef2012-12-08 02:57:00 +000015import 'log.dart' as log;
[email protected]6a0d2a82014-06-03 00:54:43 +000016import 'utils.dart';
[email protected]ddfb1642012-10-09 00:56:57 +000017
[email protected]e5d4dbe2014-04-02 00:46:50 +000018/// An exception thrown because a git command failed.
[email protected]da5bae22014-07-30 21:09:05 +000019class GitException implements ApplicationException {
[email protected]e5d4dbe2014-04-02 00:46:50 +000020 /// 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 Meldgaard72686562020-01-13 14:31:50 +010026 /// The standard out emitted by git.
27 final String stdout;
[email protected]e5d4dbe2014-04-02 00:46:50 +000028
Sigurd Meldgaard72686562020-01-13 14:31:50 +010029 /// 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]e5d4dbe2014-04-02 00:46:50 +000040
Nate Boschceaa86f2020-01-06 14:12:36 -080041 @override
[email protected]e5d4dbe2014-04-02 00:46:50 +000042 String toString() => message;
43}
44
[email protected]ddfb1642012-10-09 00:56:57 +000045/// Tests whether or not the git command-line app is available for use.
Jonas Finnemann Jensencfa9dc72019-09-16 17:45:59 +020046bool get isInstalled => command != null;
[email protected]ddfb1642012-10-09 00:56:57 +000047
[email protected]7ce95642014-06-18 20:46:51 +000048/// 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]c6eca942013-01-24 01:13:36 +000052Future<List<String>> run(List<String> args,
István Soós30580be2021-09-29 10:08:17 +020053 {String? workingDir, Map<String, String>? environment}) async {
[email protected]16104662014-06-17 21:16:18 +000054 if (!isInstalled) {
Nate Boschceaa86f2020-01-06 14:12:36 -080055 fail('Cannot find a Git executable.\n'
56 'Please ensure Git is correctly installed.');
[email protected]16104662014-06-17 21:16:18 +000057 }
[email protected]6a0d2a82014-06-03 00:54:43 +000058
[email protected]a1375062015-01-08 23:58:32 +000059 log.muteProgress();
Natalie Weizenbaum83fc6eb2016-08-18 15:04:44 -070060 try {
István Soós30580be2021-09-29 10:08:17 +020061 final result = await runProcess(command!, args,
Sigurd Meldgaard31cfec52020-05-12 15:36:54 +020062 workingDir: workingDir,
63 environment: {...?environment, 'LANG': 'en_GB'});
Sigurd Meldgaard72686562020-01-13 14:31:50 +010064 if (!result.success) {
65 throw GitException(args, result.stdout.join('\n'),
66 result.stderr.join('\n'), result.exitCode);
67 }
[email protected]ddfb1642012-10-09 00:56:57 +000068 return result.stdout;
Natalie Weizenbaum83fc6eb2016-08-18 15:04:44 -070069 } finally {
[email protected]a1375062015-01-08 23:58:32 +000070 log.unmuteProgress();
Natalie Weizenbaum83fc6eb2016-08-18 15:04:44 -070071 }
[email protected]ddfb1642012-10-09 00:56:57 +000072}
73
[email protected]16104662014-06-17 21:16:18 +000074/// Like [run], but synchronous.
Jacob MacDonaldedd32952017-03-15 11:12:46 -070075List<String> runSync(List<String> args,
István Soós30580be2021-09-29 10:08:17 +020076 {String? workingDir, Map<String, String>? environment}) {
[email protected]16104662014-06-17 21:16:18 +000077 if (!isInstalled) {
Nate Boschceaa86f2020-01-06 14:12:36 -080078 fail('Cannot find a Git executable.\n'
79 'Please ensure Git is correctly installed.');
[email protected]16104662014-06-17 21:16:18 +000080 }
[email protected]ddfb1642012-10-09 00:56:57 +000081
István Soós30580be2021-09-29 10:08:17 +020082 final result = runProcessSync(command!, args,
Jacob MacDonaldedd32952017-03-15 11:12:46 -070083 workingDir: workingDir, environment: environment);
Sigurd Meldgaard72686562020-01-13 14:31:50 +010084 if (!result.success) {
85 throw GitException(args, result.stdout.join('\n'), result.stderr.join('\n'),
86 result.exitCode);
87 }
88
[email protected]16104662014-06-17 21:16:18 +000089 return result.stdout;
90}
[email protected]ddfb1642012-10-09 00:56:57 +000091
Sigurd Meldgaard83437002022-03-24 13:25:15 +010092/// The name of the git command-line app, or `null` if Git could not be found on
93/// the user's PATH.
94final String? command = ['git', 'git.cmd'].firstWhereOrNull(_tryGitCommand);
[email protected]ddfb1642012-10-09 00:56:57 +000095
Sigurd Meldgaard07644372021-10-08 00:35:05 -070096/// Returns the root of the git repo [dir] belongs to. Returns `null` if not
97/// in a git repo or git is not installed.
98String? 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 Meldgaard3174a262022-03-08 12:08:27 +0100112/// '--recourse-submodules' was introduced in Git 2.14
113/// (https://git-scm.com/book/en/v2/Git-Tools-Submodules).
114final _minSupportedGitVersion = Version(2, 14, 0);
115
[email protected]ddfb1642012-10-09 00:56:57 +0000116/// Checks whether [command] is the Git command for this computer.
[email protected]16104662014-06-17 21:16:18 +0000117bool _tryGitCommand(String command) {
[email protected]ddfb1642012-10-09 00:56:57 +0000118 // If "git --version" prints something familiar, git is working.
[email protected]16104662014-06-17 21:16:18 +0000119 try {
Nate Boschceaa86f2020-01-06 14:12:36 -0800120 var result = runProcessSync(command, ['--version']);
Sigurd Meldgaard3174a262022-03-08 12:08:27 +0100121
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('''
135You have a very old version of git (version ${output.substring('git version '.length)}),
136for $topLevelProgram it is recommended to use git version 2.14 or newer.
137''');
138 }
Sigurd Meldgaard83437002022-03-24 13:25:15 +0100139 log.fine('Determined git command $command.');
Sigurd Meldgaard3174a262022-03-08 12:08:27 +0100140 return true;
Jonas Finnemann Jensen8ea96122020-12-08 16:22:53 +0100141 } on RunProcessException catch (err) {
[email protected]fc7fe362013-01-09 21:37:40 +0000142 // If the process failed, they probably don't have it.
Jonas Finnemann Jensen8ea96122020-12-08 16:22:53 +0100143 log.error('Git command is not "$command": $err');
[email protected]16104662014-06-17 21:16:18 +0000144 return false;
145 }
[email protected]cefd1f22012-12-20 02:04:28 +0000146}