Skip to content

Commit 988562d

Browse files
committed
Add utility to find blob by path.
1 parent abf0ec2 commit 988562d

File tree

1 file changed

+26
-2
lines changed
  • coder-apps/common/vcs/app

1 file changed

+26
-2
lines changed

coder-apps/common/vcs/app/git.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ var spawn = require('child_process').spawn;
33
var byline = require('byline');
44
var async = require('async');
55
var util = require('util');
6-
var path = require('path');
6+
var pathutil = require('path');
77
require('buffertools').extend()
88

99
var Git = function(p)
1010
{
11-
this.path = path.resolve(p);
11+
this.path = pathutil.resolve(p);
1212
};
1313

1414
Git.prototype.git = function(/* command, args, options, callback */)
@@ -339,6 +339,30 @@ Git.prototype.parseTree = function(sha, callback) {
339339
});
340340
};
341341

342+
Git.prototype.findBlob = function(sha, path, callback) {
343+
var self = this;
344+
if (typeof path === "string")
345+
path = pathutil.resolve('/', path).split('/');
346+
347+
while (path[0] == "" || path[0] == ".")
348+
path.shift();
349+
350+
351+
var name = path.shift();
352+
this.parseTree(sha, function(err, files) {
353+
if (err) return callback(err, null);
354+
355+
if (!files[name])
356+
return callback("Path not found", null);
357+
358+
if (files[name].type == "blob")
359+
return callback(null, files[name]);
360+
else if (files[name].type == "tree")
361+
return self.findBlob(files[name].object, path, callback);
362+
else
363+
return callback("Unsupported object type", null);
364+
});
365+
};
342366

343367
module.exports = Git;
344368

0 commit comments

Comments
 (0)