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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
import { Commit as NodeGitCommit, Oid as NodeGitOid } from "nodegit";
import { Author } from "api";
import { Diff } from "./diff";
import { Repository } from "./repository";
import { Tree } from "./tree";
import { createMessage, readKey, readSignature, verify } from "openpgp";
import { promisify } from "util";
import { exec } from "child_process";
import { findAsync } from "./misc";
import { CommitError, createError } from "./error";
const pExec = promisify(exec);
export type CommitSummary = {
id: string,
message: string,
date: number
}
type DiffStats = {
insertions: number,
deletions: number,
files_changed: number
}
/**
* A author of a commit
*/
export class CommitAuthor implements Author {
private _ng_commit: NodeGitCommit;
constructor(ng_commit: NodeGitCommit) {
this._ng_commit = ng_commit;
}
public get name(): string {
return this._ng_commit.author().name();
}
public get email(): string {
return this._ng_commit.author().email();
}
/**
* Returns the public key fingerprint of the commit's signer.
*/
public async fingerprint(): Promise<string> {
const basic_signature = await this._ng_commit.getSignature().catch(() => {
throw(createError(CommitError, 500, "Commit isn't signed!"));
});
const message = await createMessage({ text: basic_signature.signedData });
const pub_keys_list = await pExec("gpg --list-public-keys");
if(pub_keys_list.stderr) {
throw(createError(CommitError, 500, "Failed to get public keys from gpg!"));
}
const pub_keys = pub_keys_list.stdout
.split("\n")
.slice(2, -1)
.join("\n")
.split(/^\n/gm);
// Find a public key that matches the signature
const pub_key = await findAsync(pub_keys, async key => {
// Make sure the UID is the same as the commit author
const uid = key
.split("\n")[2]
.replace(/^uid\s*\[.*\]\s/, "");
if(uid !== `${this.name} <${this.email}>`) {
return false;
}
// Get the public key as an armored key
const fingerprint = key.split("\n")[1].replace(/^\s*/, "");
const key_export = await pExec(`gpg --armor --export ${fingerprint}`);
if(key_export.stderr) {
throw(createError(CommitError, 500, "Failed to export a public key from gpg!"));
}
const signature = await readSignature({ armoredSignature: basic_signature.signature });
const verification = await verify({
message: message,
verificationKeys: await readKey({ armoredKey: key_export.stdout }),
expectSigned: true,
signature: signature
})
.then(result => result.signatures[0].verified)
.catch(() => Promise.resolve(false));
return verification;
});
if(!pub_key) {
throw(createError(CommitError, 500, "Failed to find a public key matching the commit signature!"));
}
return pub_key
.split("\n")[1]
.replace(/^\s*/, "");
}
}
/**
* A representation of a commit
*/
export class Commit {
private _ng_commit: NodeGitCommit;
private _owner: Repository;
public id: string;
public date: number;
public message: string;
/**
* @param owner - The repository which the commit is in
* @param commit - An instance of a Nodegit commit
*/
constructor(owner: Repository, commit: NodeGitCommit) {
this._ng_commit = commit;
this._owner = owner;
this.id = commit.sha();
this.date = commit.time();
this.message = commit.message();
}
/**
* Returns the commit's author
*
* @returns An instance of a commit author
*/
public author(): CommitAuthor {
return new CommitAuthor(this._ng_commit);
}
/**
* Returns the commit's diff
*
* @returns An instance of a diff
*/
public async diff(): Promise<Diff> {
return new Diff((await this._ng_commit.getDiff())[0]);
}
/**
* Returns the commit's stats
*
* @returns A diff stats instance
*/
public async stats(): Promise<DiffStats> {
const stats = await (await this._ng_commit.getDiff())[0].getStats();
return {
insertions: <number>stats.insertions(),
deletions: <number>stats.deletions(),
files_changed: <number>stats.filesChanged()
};
}
/**
* Returns the commit's tree
*
* @returns An instance of a tree
*/
public async tree(): Promise<Tree> {
return new Tree(this._owner, await this._ng_commit.getTree());
}
/**
* Returns whether or not the commit is signed
*/
public isSigned(): Promise<boolean> {
return this._ng_commit.getSignature()
.then(() => true)
.catch(() => false);
}
/**
* Lookup a commit
*
* @param repository - The repository which the commit is in
* @param id - The SHA of a commit
* @returns An instance of a commit
*/
public static async lookup(repository: Repository, id: string | NodeGitOid): Promise<Commit> {
const commit = await NodeGitCommit.lookup(repository.ng_repository, id instanceof NodeGitOid ? id : NodeGitOid.fromString(id));
return new Commit(repository, commit);
}
/**
* Returns if an commit exists or not
*
* @param repository - The repository which the commit is in
* @param id - The sha of a commit
* @returns Whether or not the commit exists
*/
public static lookupExists(repository: Repository, id: string): Promise<boolean> {
return NodeGitCommit.lookup(repository.ng_repository, NodeGitOid.fromString(id))
.then(() => true)
.catch(() => false);
}
/**
* Returns the most recent commit of the repository's branch
*
* @param owner - A repository
* @returns An instance of a commit
*/
public static async branchCommit(owner: Repository): Promise<Commit> {
return new Commit(owner, await owner.ng_repository.getBranchCommit(owner.branch_name));
}
}
|