-
Notifications
You must be signed in to change notification settings - Fork 42
/
git_commit.js
93 lines (70 loc) · 3.13 KB
/
git_commit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var util = require('util'),
UserInfo = require('../user_info').UserInfo;
var GitCommit = exports.GitCommit = function(tree, parent, author, committer, message, headers, repository) {
var _tree = tree, _parent = parent, _author = author, _committer = committer, _message = message, _headers = headers, _repository = repository;
Object.defineProperty(this, "tree", { get: function() { return _tree; }, enumerable: true});
Object.defineProperty(this, "parent", { get: function() { return _parent; }, enumerable: true});
Object.defineProperty(this, "author", { get: function() { return _author; }, enumerable: true});
Object.defineProperty(this, "committer", { get: function() { return _committer; }, enumerable: true});
Object.defineProperty(this, "message", { get: function() { return _message; }, enumerable: true});
Object.defineProperty(this, "headers", { get: function() { return _headers; }, enumerable: true});
Object.defineProperty(this, "repository", { get: function() { return _repository; }, enumerable: true});
Object.defineProperty(this, "type", { get: function() { return "commit"; }, enumerable: true});
// Raw content of commit
Object.defineProperty(this, "raw_content", { get: function() {
return "tree " + _tree + "\n"
+ _parent.map(function(i) { return "parent " + i +"\n"; }).join('')
+ "author " + _author + "\ncommitter " + _committer + "\n\n" + _message;
}, enumerable: true});
}
// Create a commit from a raw object
GitCommit.from_raw = function(raw_object, repository) {
var parent = [];
var tree = null, author = null, committer = null;
// Split the text but only grab the 2 first blocks
var split_result = raw_object.content.split(/\n\n/);
var headers = split_result.shift();
var message = split_result.join("\n\n");
// get all the headers
var all_headers = headers.split(/\n/).map(function(header) {
var parts = header.split(/ /);
return [parts.shift(), parts.join(" ")];
})
// Iterate over all the headers
all_headers.forEach(function(header) {
var key = header[0];
var value = header[1];
if(key == "tree") {
tree = value;
} else if(key == "parent") {
parent.push(value);
} else if(key == "author") {
author = new UserInfo(value);
} else if(key == "committer") {
committer = new UserInfo(value);
} else {
// Unknow header
util.puts("unknow header '" + key + "' in commit " + raw_object.sha_hex())
}
})
if(!tree && !author && !committer) {
throw "incomplete raw commit object";
}
// Return the git commit object
return new GitCommit(tree, parent, author, committer, message, headers, repository);
}
GitCommit.prototype.raw_log = function(sha1) {
var output = "commit " + sha1 + "\n";
output = output + this.headers + "\n\n";
var lines = this.message.split("\n");
var length = lines.length;
// drop last line if it's blank
if (length > 0 && lines[length - 1].length === 0) {
length--;
}
for(var i = 0; i < length; i++) {
output += ' ' + lines[i] + '\n';
}
// Return the output
return output + '\n';
}