Last active
August 29, 2015 14:11
-
-
Save bgourlie/b4a88ecfef14f06d97e2 to your computer and use it in GitHub Desktop.
dart script for getting git info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:io'; | |
import 'dart:async'; | |
void main() { | |
//getCurrentBranch(r'C:\Users\wgi7748\Desktop\noBS').then((output) => print(output)); | |
getHistory(r'C:\Users\wgi7748\Desktop\noBS', 3).then((entries) { | |
assert(entries.length == 3); | |
entries.forEach((e) | |
=> print('${e.author}: ${e.commitMessage} (${e.date})')); | |
}); | |
} | |
Future<List<GitHistoryEntry>> getHistory(String directory, int limit){ | |
final completer = new Completer<List<GitHistoryEntry>>(); | |
_executeGitCommand(directory, ['log', '-n', '$limit']).then((output){ | |
final ret = new List<GitHistoryEntry>(); | |
final lines = output.split('\n'); | |
assert(lines.length % 6 == 0); | |
for(var i = 0; i < lines.length; i += 6){ | |
final logEntry = lines.skip(i).take(6).toList(); | |
final commitId = logEntry[0].substring(8); | |
final author = logEntry[1].substring(8); | |
final date = logEntry[2].substring(8); | |
final message = logEntry[4].substring(4); | |
final entry = new GitHistoryEntry(commitId, author, date, message); | |
ret.add(entry); | |
} | |
completer.complete(ret); | |
}, onError: (e) => completer.completeError(e)); | |
return completer.future; | |
} | |
Future<String> getCurrentBranch(String directory){ | |
final completer = new Completer<String>(); | |
_executeGitCommand(directory, ['branch']).then((String output){ | |
final branch = output.split('\n').firstWhere((String e) => e.startsWith('*')) | |
.substring(2); | |
completer.complete(branch); | |
}, onError: (e) => completer.completeError(e)); | |
return completer.future; | |
} | |
Future<String> _executeGitCommand(String workingDirectory, List<String> args) { | |
final completer = new Completer<String>(); | |
Process.start('git', args, workingDirectory: workingDirectory).then((process) { | |
final buf = new StringBuffer(); | |
final stdInCompleter = new Completer(); | |
final stdErrCompleter = new Completer(); | |
final stdInComplete = stdInCompleter.future; | |
final stdErrComplete = stdErrCompleter.future; | |
process.stdout.listen((chars) => chars.forEach((char) => buf.writeCharCode(char)), | |
onDone: () => stdInCompleter.complete(), onError: (e) => stdInCompleter.completeError(e)); | |
process.stderr.listen((chars) => chars.forEach((char) => buf.writeCharCode(char)), | |
onDone: () => stdErrCompleter.complete(), onError: (e) => stdErrCompleter.completeError(e)); | |
process.exitCode.then((exitCode) { | |
Future.wait([stdInComplete, stdErrComplete]).then((_) { | |
if (exitCode == 0) { | |
completer.complete(buf.toString()); | |
} else { | |
completer.completeError(buf.toString()); | |
} | |
}); | |
}); | |
}); | |
return completer.future; | |
} | |
class GitHistoryEntry { | |
final String commitId; | |
final String author; | |
final String date; | |
final String commitMessage; | |
GitHistoryEntry(this.commitId, this.author, this.date, this.commitMessage); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment