-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathget-messages.js
60 lines (51 loc) · 1.53 KB
/
get-messages.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
import {join} from 'path';
import exists from 'path-exists';
import gitRawCommits from 'git-raw-commits';
import gitToplevel from 'git-toplevel';
import {readFile} from 'mz/fs';
export default getCommitMessages;
const SHALLOW_MESSAGE = [
'Could not get git history from shallow clone.',
'Use git fetch --shallow before linting.',
'Original issue: https://git.io/vyKMq\n Refer to https://git.io/vyKMv for details.'
].join('\n');
// Get commit messages
// Object => Promise<Array<String>>
async function getCommitMessages(settings) {
const {from, to, edit} = settings;
if (edit) {
return getEditCommit();
}
if (await isShallow()) {
throw new Error(SHALLOW_MESSAGE);
}
return await getHistoryCommits({from, to});
}
// Get commit messages from history
// Object => Promise<Array<String>>
function getHistoryCommits(options) {
return new Promise((resolve, reject) => {
const data = [];
gitRawCommits(options)
.on('data', chunk => data.push(chunk.toString('utf-8')))
.on('error', reject)
.on('end', () => {
resolve(data);
});
});
}
// Check if the current repository is shallow
// () => Promise<Boolean>
async function isShallow() {
const top = await gitToplevel();
const shallow = join(top, '.git/shallow');
return await exists(shallow);
}
// Get recently edited commit message
// () => Promise<Array<String>>
async function getEditCommit() {
const top = await gitToplevel();
const editFilePath = join(top, '.git/COMMIT_EDITMSG');
const editFile = await readFile(editFilePath);
return [`${editFile.toString('utf-8')}\n`];
}