| 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 'io.dart'; | 9 import 'io.dart'; |
| 9 import 'log.dart' as log; | 10 import 'log.dart' as log; |
| 10 import 'utils.dart'; | 11 import 'utils.dart'; |
| 11 | 12 |
| 12 /// 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. |
| 13 Future<bool> get isInstalled { | 14 Future<bool> get isInstalled { |
| 14 if (_isGitInstalledCache != null) { | 15 if (_isGitInstalledCache != null) { |
| 15 // TODO(rnystrom): The sleep is to pump the message queue. Can use | 16 // TODO(rnystrom): The sleep is to pump the message queue. Can use |
| 16 // Future.immediate() when #3356 is fixed. | 17 // Future.immediate() when #3356 is fixed. |
| 17 return sleep(0).transform((_) => _isGitInstalledCache); | 18 return sleep(0).then((_) => _isGitInstalledCache); |
| 18 } | 19 } |
| 19 | 20 |
| 20 return _gitCommand.transform((git) => git != null); | 21 return _gitCommand.then((git) => git != null); |
| 21 } | 22 } |
| 22 | 23 |
| 23 /// Run a git process with [args] from [workingDir]. Returns the stdout as a | 24 /// Run a git process with [args] from [workingDir]. Returns the stdout as a |
| 24 /// list of strings if it succeeded. Completes to an exception if it failed. | 25 /// list of strings if it succeeded. Completes to an exception if it failed. |
| 25 Future<List<String>> run(List<String> args, {String workingDir}) { | 26 Future<List<String>> run(List<String> args, {String workingDir}) { |
| 26 return _gitCommand.chain((git) { | 27 return _gitCommand.then((git) { |
| 27 return runProcess(git, args, workingDir: workingDir); | 28 return runProcess(git, args, workingDir: workingDir); |
| 28 }).transform((result) { | 29 }).then((result) { |
| 29 if (!result.success) throw new Exception( | 30 if (!result.success) throw new Exception( |
| 30 'Git error. Command: git ${Strings.join(args, " ")}\n' | 31 'Git error. Command: git ${Strings.join(args, " ")}\n' |
| 31 '${Strings.join(result.stderr, "\n")}'); | 32 '${Strings.join(result.stderr, "\n")}'); |
| 32 | 33 |
| 33 return result.stdout; | 34 return result.stdout; |
| 34 }); | 35 }); |
| 35 } | 36 } |
| 36 | 37 |
| 37 bool _isGitInstalledCache; | 38 bool _isGitInstalledCache; |
| 38 | 39 |
| 39 /// The cached Git command. | 40 /// The cached Git command. |
| 40 String _gitCommandCache; | 41 String _gitCommandCache; |
| 41 | 42 |
| 42 /// 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 |
| 43 /// found on the user's PATH. | 44 /// found on the user's PATH. |
| 44 Future<String> get _gitCommand { | 45 Future<String> get _gitCommand { |
| 45 // TODO(nweiz): Just use Future.immediate once issue 3356 is fixed. | 46 // TODO(nweiz): Just use Future.immediate once issue 3356 is fixed. |
| 46 if (_gitCommandCache != null) { | 47 if (_gitCommandCache != null) { |
| 47 return sleep(0).transform((_) => _gitCommandCache); | 48 return sleep(0).then((_) => _gitCommandCache); |
| 48 } | 49 } |
| 49 | 50 |
| 50 return _tryGitCommand("git").chain((success) { | 51 return _tryGitCommand("git").then((success) { |
| 51 if (success) return new Future.immediate("git"); | 52 if (success) return new Future.immediate("git"); |
| 52 | 53 |
| 53 // Git is sometimes installed on Windows as `git.cmd` | 54 // Git is sometimes installed on Windows as `git.cmd` |
| 54 return _tryGitCommand("git.cmd").transform((success) { | 55 return _tryGitCommand("git.cmd").then((success) { |
| 55 if (success) return "git.cmd"; | 56 if (success) return "git.cmd"; |
| 56 return null; | 57 return null; |
| 57 }); | 58 }); |
| 58 }).transform((command) { | 59 }).then((command) { |
| 59 log.fine('Determined git command $command.'); | 60 log.fine('Determined git command $command.'); |
| 60 _gitCommandCache = command; | 61 _gitCommandCache = command; |
| 61 return command; | 62 return command; |
| 62 }); | 63 }); |
| 63 } | 64 } |
| 64 | 65 |
| 65 /// Checks whether [command] is the Git command for this computer. | 66 /// Checks whether [command] is the Git command for this computer. |
| 66 Future<bool> _tryGitCommand(String command) { | 67 Future<bool> _tryGitCommand(String command) { |
| 67 var completer = new Completer<bool>(); | 68 var completer = new Completer<bool>(); |
| 68 | 69 |
| 69 // If "git --version" prints something familiar, git is working. | 70 // If "git --version" prints something familiar, git is working. |
| 70 var future = runProcess(command, ["--version"]); | 71 var future = runProcess(command, ["--version"]); |
| 71 | 72 |
| 72 future.then((results) { | 73 future |
| 73 var regex = new RegExp("^git version"); | 74 .then((results) { |
| 74 completer.complete(results.stdout.length == 1 && | 75 var regex = new RegExp("^git version"); |
| 75 regex.hasMatch(results.stdout[0])); | 76 completer.complete(results.stdout.length == 1 && |
| 76 }); | 77 regex.hasMatch(results.stdout[0])); |
| 77 | 78 }) |
| 78 future.handleException((err) { | 79 .catchError((err) { |
| 79 // If the process failed, they probably don't have it. | 80 // If the process failed, they probably don't have it. |
| 80 completer.complete(false); | 81 completer.complete(false); |
| 81 return true; | 82 }); |
| 82 }); | |
| 83 | 83 |
| 84 return completer.future; | 84 return completer.future; |
| 85 } | 85 } |
| OLD | NEW |