Skip to content

Commit 225d523

Browse files
committed
Add command to parse git commit objects.
1 parent cc6bdfa commit 225d523

File tree

1 file changed

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

1 file changed

+49
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,5 +253,54 @@ Git.prototype.cat_file = function(/* sha, type, options, callback */) {
253253
});
254254
}
255255

256+
Git.prototype.parseCommit = function(sha, callback) {
257+
this.cat_file(sha, "commit", function(err, data) {
258+
if (err) return callback(err, null);
259+
var commit = {};
260+
commit.parents = [];
261+
262+
var i = 0;
263+
while (i < data.length) {
264+
var j = data.indexOf("\n", i);
265+
if (j == -1)
266+
j = data.length;
267+
268+
if (i == j)
269+
break;
270+
271+
var line = data.slice(i, j);
272+
273+
var match;
274+
if(match = /^tree ([0-9a-f]{40})$/.exec(line))
275+
commit.tree = match[1];
276+
else if(match = /^parent ([0-9a-f]{40})$/.exec(line))
277+
commit.parents.push(match[1]);
278+
else if(match = /^author (.+) <(.*)> (\d+) ([+-])\d{4}$/.exec(line))
279+
commit.author = {
280+
name: match[1],
281+
email: match[2],
282+
date: new Date(parseInt(match[3]))
283+
}
284+
else if(match = /^committer (.+) <(.*)> (\d+) ([+-])\d{4}$/.exec(line))
285+
commit.committer = {
286+
name: match[1],
287+
email: match[2],
288+
date: new Date(parseInt(match[3]))
289+
}
290+
else
291+
{
292+
callback("Wrong commit object format");
293+
return;
294+
}
295+
296+
i = j+1;
297+
}
298+
299+
commit.message = data.slice(i + 1);
300+
301+
callback(null, commit);
302+
});
303+
};
304+
256305
module.exports = Git;
257306

0 commit comments

Comments
 (0)