-
Notifications
You must be signed in to change notification settings - Fork 935
/
Copy pathread.js
82 lines (66 loc) · 1.97 KB
/
read.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
import path from 'path';
import exists from 'path-exists';
import up from 'find-up';
import gitRawCommits from 'git-raw-commits';
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 getHistoryCommits({from, to});
}
// Get commit messages from history
// Object => Promise<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 toplevel();
if (typeof top !== 'string') {
throw new TypeError(`Could not find git root - is this a git repository?`);
}
const shallow = path.join(top, '.git/shallow');
return exists(shallow);
}
// Get recently edited commit message
// () => Promise<Array<String>>
async function getEditCommit() {
const top = await toplevel();
if (typeof top !== 'string') {
throw new TypeError(`Could not find git root - is this a git repository?`);
}
const editFilePath = path.join(top, '.git/COMMIT_EDITMSG');
const editFile = await readFile(editFilePath);
return [`${editFile.toString('utf-8')}\n`];
}
// Find the next git root
// (start: string) => Promise<string | null>
async function toplevel(cwd = process.cwd()) {
const found = await up('.git', {cwd});
if (typeof found !== 'string') {
return found;
}
return path.join(found, '..');
}