Skip to content

Commit abf0ec2

Browse files
committed
Add a utility function to parse tree objects.
1 parent 225d523 commit abf0ec2

File tree

1 file changed

+38
-0
lines changed
  • coder-apps/common/vcs/app

1 file changed

+38
-0
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var byline = require('byline');
44
var async = require('async');
55
var util = require('util');
66
var path = require('path');
7+
require('buffertools').extend()
78

89
var Git = function(p)
910
{
@@ -302,5 +303,42 @@ Git.prototype.parseCommit = function(sha, callback) {
302303
});
303304
};
304305

306+
Git.prototype.parseTree = function(sha, callback) {
307+
this.cat_file(sha, "tree", {encoding: 'binary'}, function(err, data) {
308+
if (err) return callback(err, null);
309+
310+
data = Buffer(data, "binary");
311+
var results = Object.create(null);
312+
313+
var i = 0;
314+
while (i < data.length) {
315+
var j = data.indexOf(" ", i);
316+
var k = data.indexOf("\0", i);
317+
318+
var mode = data.toString("utf-8", i, j);
319+
var name = data.toString("utf-8", j + 1, k);
320+
var object = data.slice(k+1, k+21).toString('hex');
321+
322+
var type;
323+
if (mode == "160000")
324+
type = "commit";
325+
else if (mode == "40000")
326+
type = "tree";
327+
else
328+
type = "blob";
329+
330+
results[name] = {
331+
mode: mode,
332+
type: type,
333+
object: object
334+
}
335+
336+
i = k + 21;
337+
}
338+
callback(null, results);
339+
});
340+
};
341+
342+
305343
module.exports = Git;
306344

0 commit comments

Comments
 (0)