[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] | 05c9cc0 | 2012-10-09 01:25:43 +0000 | [diff] [blame] | 6 | library git; |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 7 | |
[email protected] | bd5f259 | 2013-01-07 11:23:16 +0000 | [diff] [blame] | 8 | import 'dart:async'; |
[email protected] | 05c9cc0 | 2012-10-09 01:25:43 +0000 | [diff] [blame] | 9 | import 'io.dart'; |
[email protected] | 71976ef | 2012-12-08 02:57:00 +0000 | [diff] [blame] | 10 | import 'log.dart' as log; |
[email protected] | 05c9cc0 | 2012-10-09 01:25:43 +0000 | [diff] [blame] | 11 | import 'utils.dart'; |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 12 | |
| 13 | /// Tests whether or not the git command-line app is available for use. |
| 14 | Future<bool> get isInstalled { |
| 15 | if (_isGitInstalledCache != null) { |
[email protected] | c6eca94 | 2013-01-24 01:13:36 +0000 | [diff] [blame^] | 16 | return new Future.immediate(_isGitInstalledCache); |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 17 | } |
| 18 | |
[email protected] | bd5f259 | 2013-01-07 11:23:16 +0000 | [diff] [blame] | 19 | return _gitCommand.then((git) => git != null); |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | /// Run a git process with [args] from [workingDir]. Returns the stdout as a |
| 23 | /// list of strings if it succeeded. Completes to an exception if it failed. |
[email protected] | c6eca94 | 2013-01-24 01:13:36 +0000 | [diff] [blame^] | 24 | Future<List<String>> run(List<String> args, |
| 25 | {String workingDir, Map<String, String> environment}) { |
[email protected] | bd5f259 | 2013-01-07 11:23:16 +0000 | [diff] [blame] | 26 | return _gitCommand.then((git) { |
[email protected] | c6eca94 | 2013-01-24 01:13:36 +0000 | [diff] [blame^] | 27 | return runProcess(git, args, workingDir: workingDir, |
| 28 | environment: environment); |
[email protected] | bd5f259 | 2013-01-07 11:23:16 +0000 | [diff] [blame] | 29 | }).then((result) { |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 30 | if (!result.success) throw new Exception( |
[email protected] | 30a360f | 2012-10-09 20:54:05 +0000 | [diff] [blame] | 31 | 'Git error. Command: git ${Strings.join(args, " ")}\n' |
| 32 | '${Strings.join(result.stderr, "\n")}'); |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 33 | |
| 34 | return result.stdout; |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | bool _isGitInstalledCache; |
| 39 | |
| 40 | /// The cached Git command. |
| 41 | String _gitCommandCache; |
| 42 | |
| 43 | /// Returns the name of the git command-line app, or null if Git could not be |
| 44 | /// found on the user's PATH. |
| 45 | Future<String> get _gitCommand { |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 46 | if (_gitCommandCache != null) { |
[email protected] | c6eca94 | 2013-01-24 01:13:36 +0000 | [diff] [blame^] | 47 | return new Future.immediate(_gitCommandCache); |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 48 | } |
| 49 | |
[email protected] | bd5f259 | 2013-01-07 11:23:16 +0000 | [diff] [blame] | 50 | return _tryGitCommand("git").then((success) { |
[email protected] | c6eca94 | 2013-01-24 01:13:36 +0000 | [diff] [blame^] | 51 | if (success) return "git"; |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 52 | |
| 53 | // Git is sometimes installed on Windows as `git.cmd` |
[email protected] | bd5f259 | 2013-01-07 11:23:16 +0000 | [diff] [blame] | 54 | return _tryGitCommand("git.cmd").then((success) { |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 55 | if (success) return "git.cmd"; |
| 56 | return null; |
| 57 | }); |
[email protected] | bd5f259 | 2013-01-07 11:23:16 +0000 | [diff] [blame] | 58 | }).then((command) { |
[email protected] | 71976ef | 2012-12-08 02:57:00 +0000 | [diff] [blame] | 59 | log.fine('Determined git command $command.'); |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 60 | _gitCommandCache = command; |
| 61 | return command; |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | /// Checks whether [command] is the Git command for this computer. |
| 66 | Future<bool> _tryGitCommand(String command) { |
[email protected] | ddfb164 | 2012-10-09 00:56:57 +0000 | [diff] [blame] | 67 | // If "git --version" prints something familiar, git is working. |
[email protected] | fc7fe36 | 2013-01-09 21:37:40 +0000 | [diff] [blame] | 68 | return runProcess(command, ["--version"]).then((results) { |
| 69 | var regexp = new RegExp("^git version"); |
| 70 | return results.stdout.length == 1 && regexp.hasMatch(results.stdout[0]); |
| 71 | }).catchError((err) { |
| 72 | // If the process failed, they probably don't have it. |
| 73 | return false; |
| 74 | }); |
[email protected] | cefd1f2 | 2012-12-20 02:04:28 +0000 | [diff] [blame] | 75 | } |