OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Helper functionality for invoking Git. | 5 /// Helper functionality for invoking Git. |
6 library git; | 6 library git; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'io.dart'; | 9 import 'io.dart'; |
10 import 'log.dart' as log; | 10 import 'log.dart' as log; |
11 import 'utils.dart'; | 11 import 'utils.dart'; |
12 | 12 |
13 /// Tests whether or not the git command-line app is available for use. | 13 /// Tests whether or not the git command-line app is available for use. |
14 Future<bool> get isInstalled { | 14 Future<bool> get isInstalled { |
15 if (_isGitInstalledCache != null) { | 15 if (_isGitInstalledCache != null) { |
16 // TODO(rnystrom): The sleep is to pump the message queue. Can use | 16 return new Future.immediate(_isGitInstalledCache); |
17 // Future.immediate() when #3356 is fixed. | |
18 return sleep(0).then((_) => _isGitInstalledCache); | |
19 } | 17 } |
20 | 18 |
21 return _gitCommand.then((git) => git != null); | 19 return _gitCommand.then((git) => git != null); |
22 } | 20 } |
23 | 21 |
24 /// Run a git process with [args] from [workingDir]. Returns the stdout as a | 22 /// Run a git process with [args] from [workingDir]. Returns the stdout as a |
25 /// list of strings if it succeeded. Completes to an exception if it failed. | 23 /// list of strings if it succeeded. Completes to an exception if it failed. |
26 Future<List<String>> run(List<String> args, {String workingDir}) { | 24 Future<List<String>> run(List<String> args, |
| 25 {String workingDir, Map<String, String> environment}) { |
27 return _gitCommand.then((git) { | 26 return _gitCommand.then((git) { |
28 return runProcess(git, args, workingDir: workingDir); | 27 return runProcess(git, args, workingDir: workingDir, |
| 28 environment: environment); |
29 }).then((result) { | 29 }).then((result) { |
30 if (!result.success) throw new Exception( | 30 if (!result.success) throw new Exception( |
31 'Git error. Command: git ${Strings.join(args, " ")}\n' | 31 'Git error. Command: git ${Strings.join(args, " ")}\n' |
32 '${Strings.join(result.stderr, "\n")}'); | 32 '${Strings.join(result.stderr, "\n")}'); |
33 | 33 |
34 return result.stdout; | 34 return result.stdout; |
35 }); | 35 }); |
36 } | 36 } |
37 | 37 |
38 bool _isGitInstalledCache; | 38 bool _isGitInstalledCache; |
39 | 39 |
40 /// The cached Git command. | 40 /// The cached Git command. |
41 String _gitCommandCache; | 41 String _gitCommandCache; |
42 | 42 |
43 /// Returns the name of the git command-line app, or null if Git could not be | 43 /// Returns the name of the git command-line app, or null if Git could not be |
44 /// found on the user's PATH. | 44 /// found on the user's PATH. |
45 Future<String> get _gitCommand { | 45 Future<String> get _gitCommand { |
46 // TODO(nweiz): Just use Future.immediate once issue 3356 is fixed. | |
47 if (_gitCommandCache != null) { | 46 if (_gitCommandCache != null) { |
48 return sleep(0).then((_) => _gitCommandCache); | 47 return new Future.immediate(_gitCommandCache); |
49 } | 48 } |
50 | 49 |
51 return _tryGitCommand("git").then((success) { | 50 return _tryGitCommand("git").then((success) { |
52 if (success) return new Future.immediate("git"); | 51 if (success) return "git"; |
53 | 52 |
54 // Git is sometimes installed on Windows as `git.cmd` | 53 // Git is sometimes installed on Windows as `git.cmd` |
55 return _tryGitCommand("git.cmd").then((success) { | 54 return _tryGitCommand("git.cmd").then((success) { |
56 if (success) return "git.cmd"; | 55 if (success) return "git.cmd"; |
57 return null; | 56 return null; |
58 }); | 57 }); |
59 }).then((command) { | 58 }).then((command) { |
60 log.fine('Determined git command $command.'); | 59 log.fine('Determined git command $command.'); |
61 _gitCommandCache = command; | 60 _gitCommandCache = command; |
62 return command; | 61 return command; |
63 }); | 62 }); |
64 } | 63 } |
65 | 64 |
66 /// Checks whether [command] is the Git command for this computer. | 65 /// Checks whether [command] is the Git command for this computer. |
67 Future<bool> _tryGitCommand(String command) { | 66 Future<bool> _tryGitCommand(String command) { |
68 // If "git --version" prints something familiar, git is working. | 67 // If "git --version" prints something familiar, git is working. |
69 return runProcess(command, ["--version"]).then((results) { | 68 return runProcess(command, ["--version"]).then((results) { |
70 var regexp = new RegExp("^git version"); | 69 var regexp = new RegExp("^git version"); |
71 return results.stdout.length == 1 && regexp.hasMatch(results.stdout[0]); | 70 return results.stdout.length == 1 && regexp.hasMatch(results.stdout[0]); |
72 }).catchError((err) { | 71 }).catchError((err) { |
73 // If the process failed, they probably don't have it. | 72 // If the process failed, they probably don't have it. |
74 return false; | 73 return false; |
75 }); | 74 }); |
76 } | 75 } |
OLD | NEW |