| 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 pub.git; | 6 library pub.git; |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 GitException(Iterable<String> args, this.stderr) | 28 GitException(Iterable<String> args, this.stderr) |
| 29 : args = args.toList(); | 29 : args = args.toList(); |
| 30 | 30 |
| 31 String toString() => message; | 31 String toString() => message; |
| 32 } | 32 } |
| 33 | 33 |
| 34 /// Tests whether or not the git command-line app is available for use. | 34 /// Tests whether or not the git command-line app is available for use. |
| 35 bool get isInstalled { | 35 bool get isInstalled { |
| 36 if (_isInstalledCache != null) return _isInstalledCache; | 36 if (_isInstalledCache != null) return _isInstalledCache; |
| 37 _isInstalledCache = _gitCommand != null; | 37 _isInstalledCache = command != null; |
| 38 return _isInstalledCache; | 38 return _isInstalledCache; |
| 39 } | 39 } |
| 40 bool _isInstalledCache; | 40 bool _isInstalledCache; |
| 41 | 41 |
| 42 /// Run a git process with [args] from [workingDir]. | 42 /// Run a git process with [args] from [workingDir]. |
| 43 /// | 43 /// |
| 44 /// Returns the stdout as a list of strings if it succeeded. Completes to an | 44 /// Returns the stdout as a list of strings if it succeeded. Completes to an |
| 45 /// exception if it failed. | 45 /// exception if it failed. |
| 46 Future<List<String>> run(List<String> args, | 46 Future<List<String>> run(List<String> args, |
| 47 {String workingDir, Map<String, String> environment}) { | 47 {String workingDir, Map<String, String> environment}) { |
| 48 if (!isInstalled) { | 48 if (!isInstalled) { |
| 49 fail("Cannot find a Git executable.\n" | 49 fail("Cannot find a Git executable.\n" |
| 50 "Please ensure Git is correctly installed."); | 50 "Please ensure Git is correctly installed."); |
| 51 } | 51 } |
| 52 | 52 |
| 53 log.muteProgress(); | 53 log.muteProgress(); |
| 54 return runProcess(_gitCommand, args, workingDir: workingDir, | 54 return runProcess(command, args, workingDir: workingDir, |
| 55 environment: environment).then((result) { | 55 environment: environment).then((result) { |
| 56 if (!result.success) throw new GitException(args, result.stderr.join("\n")); | 56 if (!result.success) throw new GitException(args, result.stderr.join("\n")); |
| 57 | 57 |
| 58 return result.stdout; | 58 return result.stdout; |
| 59 }).whenComplete(() { | 59 }).whenComplete(() { |
| 60 log.unmuteProgress(); | 60 log.unmuteProgress(); |
| 61 }); | 61 }); |
| 62 } | 62 } |
| 63 | 63 |
| 64 /// Like [run], but synchronous. | 64 /// Like [run], but synchronous. |
| 65 List<String> runSync(List<String> args, {String workingDir, | 65 List<String> runSync(List<String> args, {String workingDir, |
| 66 Map<String, String> environment}) { | 66 Map<String, String> environment}) { |
| 67 if (!isInstalled) { | 67 if (!isInstalled) { |
| 68 fail("Cannot find a Git executable.\n" | 68 fail("Cannot find a Git executable.\n" |
| 69 "Please ensure Git is correctly installed."); | 69 "Please ensure Git is correctly installed."); |
| 70 } | 70 } |
| 71 | 71 |
| 72 var result = runProcessSync(_gitCommand, args, | 72 var result = runProcessSync(command, args, |
| 73 workingDir: workingDir, | 73 workingDir: workingDir, |
| 74 environment: environment); | 74 environment: environment); |
| 75 if (!result.success) throw new GitException(args, result.stderr.join("\n")); | 75 if (!result.success) throw new GitException(args, result.stderr.join("\n")); |
| 76 return result.stdout; | 76 return result.stdout; |
| 77 } | 77 } |
| 78 | 78 |
| 79 /// Starts a git process and returns it. | 79 /// Starts a git process and returns it. |
| 80 Future<PubProcess> start(List<String> args, | 80 Future<PubProcess> start(List<String> args, |
| 81 {String workingDir, Map<String, String> environment}) { | 81 {String workingDir, Map<String, String> environment}) { |
| 82 if (!isInstalled) { | 82 if (!isInstalled) { |
| 83 fail("Cannot find a Git executable.\n" | 83 fail("Cannot find a Git executable.\n" |
| 84 "Please ensure Git is correctly installed."); | 84 "Please ensure Git is correctly installed."); |
| 85 } | 85 } |
| 86 | 86 |
| 87 return startProcess(_gitCommand, args, workingDir: workingDir, | 87 return startProcess(command, args, workingDir: workingDir, |
| 88 environment: environment); | 88 environment: environment); |
| 89 } | 89 } |
| 90 | 90 |
| 91 /// Returns the name of the git command-line app, or null if Git could not be | 91 /// Returns the name of the git command-line app, or null if Git could not be |
| 92 /// found on the user's PATH. | 92 /// found on the user's PATH. |
| 93 String get _gitCommand { | 93 String get command { |
| 94 if (_commandCache != null) return _commandCache; | 94 if (_commandCache != null) return _commandCache; |
| 95 | 95 |
| 96 var command; | 96 var command; |
| 97 if (_tryGitCommand("git")) { | 97 if (_tryGitCommand("git")) { |
| 98 _commandCache = "git"; | 98 _commandCache = "git"; |
| 99 } else if (_tryGitCommand("git.cmd")){ | 99 } else if (_tryGitCommand("git.cmd")){ |
| 100 _commandCache = "git.cmd"; | 100 _commandCache = "git.cmd"; |
| 101 } else { | 101 } else { |
| 102 return null; | 102 return null; |
| 103 } | 103 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 114 var result = runProcessSync(command, ["--version"]); | 114 var result = runProcessSync(command, ["--version"]); |
| 115 var regexp = new RegExp("^git version"); | 115 var regexp = new RegExp("^git version"); |
| 116 return result.stdout.length == 1 && regexp.hasMatch(result.stdout.single); | 116 return result.stdout.length == 1 && regexp.hasMatch(result.stdout.single); |
| 117 } on ProcessException catch (error, stackTrace) { | 117 } on ProcessException catch (error, stackTrace) { |
| 118 var chain = new Chain.forTrace(stackTrace); | 118 var chain = new Chain.forTrace(stackTrace); |
| 119 // If the process failed, they probably don't have it. | 119 // If the process failed, they probably don't have it. |
| 120 log.error('Git command is not "$command": $error\n$chain'); | 120 log.error('Git command is not "$command": $error\n$chain'); |
| 121 return false; | 121 return false; |
| 122 } | 122 } |
| 123 } | 123 } |
| OLD | NEW |