blob: b216735f64c94d1dc78800ff46e253b7e956a14d [file] [log] [blame]
[email protected]ddfb1642012-10-09 00:56:57 +00001// 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]cefd1f22012-12-20 02:04:28 +00005/// Helper functionality for invoking Git.
[email protected]05c9cc02012-10-09 01:25:43 +00006library git;
[email protected]ddfb1642012-10-09 00:56:57 +00007
[email protected]05c9cc02012-10-09 01:25:43 +00008import 'io.dart';
[email protected]71976ef2012-12-08 02:57:00 +00009import 'log.dart' as log;
[email protected]05c9cc02012-10-09 01:25:43 +000010import 'utils.dart';
[email protected]ddfb1642012-10-09 00:56:57 +000011
12/// Tests whether or not the git command-line app is available for use.
13Future<bool> get isInstalled {
14 if (_isGitInstalledCache != null) {
15 // TODO(rnystrom): The sleep is to pump the message queue. Can use
16 // Future.immediate() when #3356 is fixed.
17 return sleep(0).transform((_) => _isGitInstalledCache);
18 }
19
20 return _gitCommand.transform((git) => git != null);
21}
22
23/// 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.
[email protected]bc7ccb82012-10-17 20:58:19 +000025Future<List<String>> run(List<String> args, {String workingDir}) {
[email protected]ddfb1642012-10-09 00:56:57 +000026 return _gitCommand.chain((git) {
[email protected]b119eeb2012-10-16 16:03:47 +000027 return runProcess(git, args, workingDir: workingDir);
[email protected]ddfb1642012-10-09 00:56:57 +000028 }).transform((result) {
29 if (!result.success) throw new Exception(
[email protected]30a360f2012-10-09 20:54:05 +000030 'Git error. Command: git ${Strings.join(args, " ")}\n'
31 '${Strings.join(result.stderr, "\n")}');
[email protected]ddfb1642012-10-09 00:56:57 +000032
33 return result.stdout;
34 });
35}
36
37bool _isGitInstalledCache;
38
39/// The cached Git command.
40String _gitCommandCache;
41
42/// Returns the name of the git command-line app, or null if Git could not be
43/// found on the user's PATH.
44Future<String> get _gitCommand {
45 // TODO(nweiz): Just use Future.immediate once issue 3356 is fixed.
46 if (_gitCommandCache != null) {
47 return sleep(0).transform((_) => _gitCommandCache);
48 }
49
50 return _tryGitCommand("git").chain((success) {
51 if (success) return new Future.immediate("git");
52
53 // Git is sometimes installed on Windows as `git.cmd`
54 return _tryGitCommand("git.cmd").transform((success) {
55 if (success) return "git.cmd";
56 return null;
57 });
58 }).transform((command) {
[email protected]71976ef2012-12-08 02:57:00 +000059 log.fine('Determined git command $command.');
[email protected]ddfb1642012-10-09 00:56:57 +000060 _gitCommandCache = command;
61 return command;
62 });
63}
64
65/// Checks whether [command] is the Git command for this computer.
66Future<bool> _tryGitCommand(String command) {
67 var completer = new Completer<bool>();
68
69 // If "git --version" prints something familiar, git is working.
70 var future = runProcess(command, ["--version"]);
71
72 future.then((results) {
73 var regex = new RegExp("^git version");
74 completer.complete(results.stdout.length == 1 &&
75 regex.hasMatch(results.stdout[0]));
76 });
77
78 future.handleException((err) {
79 // If the process failed, they probably don't have it.
80 completer.complete(false);
81 return true;
82 });
83
84 return completer.future;
[email protected]cefd1f22012-12-20 02:04:28 +000085}