| OLD | NEW |
| (Empty) |
| 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 | |
| 5 /// Helper functionality for invoking Git. | |
| 6 library pub.git; | |
| 7 | |
| 8 import 'dart:async'; | |
| 9 import 'dart:io'; | |
| 10 | |
| 11 import 'package:stack_trace/stack_trace.dart'; | |
| 12 | |
| 13 import 'exceptions.dart'; | |
| 14 import 'io.dart'; | |
| 15 import 'log.dart' as log; | |
| 16 import 'utils.dart'; | |
| 17 | |
| 18 /// An exception thrown because a git command failed. | |
| 19 class GitException implements ApplicationException { | |
| 20 /// The arguments to the git command. | |
| 21 final List<String> args; | |
| 22 | |
| 23 /// The standard error emitted by git. | |
| 24 final String stderr; | |
| 25 | |
| 26 String get message => 'Git error. Command: git ${args.join(" ")}\n$stderr'; | |
| 27 | |
| 28 GitException(Iterable<String> args, this.stderr) | |
| 29 : args = args.toList(); | |
| 30 | |
| 31 String toString() => message; | |
| 32 } | |
| 33 | |
| 34 /// Tests whether or not the git command-line app is available for use. | |
| 35 bool get isInstalled { | |
| 36 if (_isInstalledCache != null) return _isInstalledCache; | |
| 37 _isInstalledCache = _gitCommand != null; | |
| 38 return _isInstalledCache; | |
| 39 } | |
| 40 bool _isInstalledCache; | |
| 41 | |
| 42 /// Run a git process with [args] from [workingDir]. | |
| 43 /// | |
| 44 /// Returns the stdout as a list of strings if it succeeded. Completes to an | |
| 45 /// exception if it failed. | |
| 46 Future<List<String>> run(List<String> args, {String workingDir, Map<String, | |
| 47 String> environment}) { | |
| 48 if (!isInstalled) { | |
| 49 fail( | |
| 50 "Cannot find a Git executable.\n" "Please ensure Git is correctly instal
led."); | |
| 51 } | |
| 52 | |
| 53 log.muteProgress(); | |
| 54 return runProcess( | |
| 55 _gitCommand, | |
| 56 args, | |
| 57 workingDir: workingDir, | |
| 58 environment: environment).then((result) { | |
| 59 if (!result.success) throw new GitException(args, result.stderr.join("\n")); | |
| 60 | |
| 61 return result.stdout; | |
| 62 }).whenComplete(() { | |
| 63 log.unmuteProgress(); | |
| 64 }); | |
| 65 } | |
| 66 | |
| 67 /// Like [run], but synchronous. | |
| 68 List<String> runSync(List<String> args, {String workingDir, Map<String, | |
| 69 String> environment}) { | |
| 70 if (!isInstalled) { | |
| 71 fail( | |
| 72 "Cannot find a Git executable.\n" "Please ensure Git is correctly instal
led."); | |
| 73 } | |
| 74 | |
| 75 var result = runProcessSync( | |
| 76 _gitCommand, | |
| 77 args, | |
| 78 workingDir: workingDir, | |
| 79 environment: environment); | |
| 80 if (!result.success) throw new GitException(args, result.stderr.join("\n")); | |
| 81 return result.stdout; | |
| 82 } | |
| 83 | |
| 84 /// Returns the name of the git command-line app, or null if Git could not be | |
| 85 /// found on the user's PATH. | |
| 86 String get _gitCommand { | |
| 87 if (_commandCache != null) return _commandCache; | |
| 88 | |
| 89 var command; | |
| 90 if (_tryGitCommand("git")) { | |
| 91 _commandCache = "git"; | |
| 92 } else if (_tryGitCommand("git.cmd")) { | |
| 93 _commandCache = "git.cmd"; | |
| 94 } else { | |
| 95 return null; | |
| 96 } | |
| 97 | |
| 98 log.fine('Determined git command $command.'); | |
| 99 return _commandCache; | |
| 100 } | |
| 101 String _commandCache; | |
| 102 | |
| 103 /// Checks whether [command] is the Git command for this computer. | |
| 104 bool _tryGitCommand(String command) { | |
| 105 // If "git --version" prints something familiar, git is working. | |
| 106 try { | |
| 107 var result = runProcessSync(command, ["--version"]); | |
| 108 var regexp = new RegExp("^git version"); | |
| 109 return result.stdout.length == 1 && regexp.hasMatch(result.stdout.single); | |
| 110 } on ProcessException catch (error, stackTrace) { | |
| 111 var chain = new Chain.forTrace(stackTrace); | |
| 112 // If the process failed, they probably don't have it. | |
| 113 log.message('Git command is not "$command": $error\n$chain'); | |
| 114 return false; | |
| 115 } | |
| 116 } | |
| OLD | NEW |