-
Notifications
You must be signed in to change notification settings - Fork 5
/
commit.js
121 lines (94 loc) · 2.69 KB
/
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Create a sign commit object
function createSignedCommit({
author,
message,
parents,
treeHash
},
callback
) {
let commit = formCommit({
treeHash,
author,
parents,
message
});
signContent(commit, (result) => {
let signature = isolateSignature(result);
callback(formSignedCommit(commit, signature));
});
}
// Form the commit
function formCommit({
treeHash,
author,
parents,
message
}) {
let {
timestamp,
timezoneOffset
} = determineTime();
// First commit has no parent
if (!parents) {
parents = [];
}
let commit = {
tree: treeHash,
parent: parents,
author: {
name: author.name,
email: author.email,
timestamp: timestamp,
timezoneOffset: timezoneOffset
},
message
};
return `${formCommitHeaders(commit)}\n${normalize(commit.message)}`;
}
// Form the commit header
function formCommitHeaders(commit) {
let headers = '';
if (commit.tree) headers += `${TYPE_TREE} ${commit.tree}\n`;
else headers += `${TYPE_TREE} ${NULL_TREE}\n`; //null tree
// Check if the commit has parent(s)
if (commit.parent && commit.parent.length) {
for (let p of commit.parent) {
headers += 'parent';
headers += ' ' + p + '\n';
}
}
let author = commit.author;
headers += `author ${author.name} <${author.email}> ${author.timestamp} ${formatTimezoneOffset(author.timezoneOffset)}\n`;
// Committer and author of GUI commits are the same
let committer = commit.committer || commit.author;
headers += `committer ${committer.name} <${committer.email}> ${committer.timestamp} ${formatTimezoneOffset(committer.timezoneOffset)}\n`;
return headers;
}
// Form signed commit
function formSignedCommit(commit, signature) {
let headers = comHeader(commit);
let message = comMessage(commit);
signature = normalize(signature);
let signedCommit = `${headers}\n${PGP_SIG}${indent(signature)}\n${message}`;
return signedCommit
}
// Extract the commit's signature
function isolateSignature(commit) {
// Take the last signature as the commit signature
// TODO: Make sure it does not go wrong
let signature = commit.slice(
commit.lastIndexOf(`${PGP_START}`),
commit.lastIndexOf(`${PGP_END}`) +
`${PGP_END}`.length
)
return outdent(signature)
}
// Get commit message
function comMessage(commit) {
return normalize(commit.slice(commit.indexOf('\n\n') + 2))
}
// Get commit header
function comHeader(commit) {
return commit.slice(0, commit.indexOf('\n\n'))
}