Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Clean up the code a little and add a stream reader for files.

  • Loading branch information...
commit 513a279da0974633779d9cd7ff579be96677a573 1 parent cd106fc
@creationix authored
Showing with 36 additions and 10 deletions.
  1. +36 −10 lib/git.js
View
46 lib/git.js
@@ -34,7 +34,7 @@ var Git = module.exports = function (repo) {
} catch (e) {
throw new Error("Bad repo path: " + repo);
}
-
+
try {
// Check is this is a working repo
gitDir = Path.join(repo, ".git")
@@ -45,10 +45,33 @@ var Git = module.exports = function (repo) {
gitDir = repo;
gitCommands = ["--git-dir=" + gitDir];
}
-
+
};
+function createExecStream(commands) {
+ commands = gitCommands.concat(commands);
+ var child = ChildProcess.spawn("git", commands);
+
+ // Collect error messages and emit an error event on exit if the status
+ // code is non-zero.
+ var stderr = "";
+ child.stderr.addListener('data', function (data) {
+ stderr += data
+ });
+ child.addListener('exit', function (code) {
+ if (code > 0) {
+ child.stdout.emit("error", new Error(stderr));
+ }
+ })
+
+ // Close the stdin stream so git know we're ready for it to start.
+ child.stdin.end();
+
+ return child.stdout;
+}
+
+
var execQueue = {};
// Internal helper to talk to the git subprocess
function gitExec(commands, callback) {
@@ -85,6 +108,16 @@ function gitExec(commands, callback) {
child.stdin.end();
}
+Git.createReadStream = function readFileStream(path, version) {
+ if (!version) {
+ if (workTree) {
+ return fs.createReadStream(Path.join(workTree, path));
+ }
+ version = "HEAD";
+ }
+ return createExecStream(["show", version + ":" + path]);
+}
+
// Loads a file from a git repo
Git.readFile = function readFile(path, version, callback) {
// version defaults to HEAD
@@ -97,14 +130,7 @@ Git.readFile = function readFile(path, version, callback) {
}
version = "HEAD";
}
- path = version + ":" + path;
- gitExec(["show", path], function (err, text) {
- if (err) {
- callback(err);
- return;
- }
- callback(null, text);
- });
+ gitExec(["show", version + ":" + path], callback);
};
// Reads a directory at a given version and returns an objects with two arrays
Please sign in to comment.
Something went wrong with that request. Please try again.