-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
78 lines (64 loc) · 1.88 KB
/
index.ts
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
import execa from 'execa';
import isGit from 'is-git-repository';
import path from 'path';
export interface GitCommitInfoOptions {
cwd?: string;
commit?: string;
}
export interface GitCommitInfoResult {
hash?: string;
shortHash?: string;
commit?: string;
shortCommit?: string;
author?: string;
email?: string;
date?: string;
message?: string;
error?: Error,
}
const regex = /\s+([\s\S]*)/g; // matches everything after the first whitespace
const hashRegex = /^[0-9a-f]{7,40}$/;
const gitCommitInfo = (options: GitCommitInfoOptions = {}): GitCommitInfoResult => {
const {
cwd = process.cwd(),
commit,
} = options;
const thisCommit = commit || '';
const thisPath = path.resolve(cwd);
if ((thisCommit && !(new RegExp(hashRegex).test(thisCommit)))) {
return { error: new Error('Not a valid commit hash') };
}
if (!isGit(thisPath)) {
return {};
}
try {
const { stdout } = execa.commandSync(`git --no-pager show ${thisCommit} --summary`, { cwd });
const info = stdout
.split('\n')
.filter((entry) => entry.length !== 0);
const mergeIndex = info[1]?.indexOf('Merge') === -1 ? 0 : 1;
const hash = (new RegExp(regex).exec(info[0]) || [])[1];
const shortHash = hash.slice(0, 7);
const getInfo = (index: number): string | undefined => {
const [, extractedInfo] = (new RegExp(regex).exec(info[index]) || []);
return extractedInfo;
};
const author = (getInfo(1 + mergeIndex)?.match(/([^<]+)/) || [])[1]?.trim();
const [, email] = getInfo(1 + mergeIndex)?.match(/<([^>]+)>/) || [];
const date = getInfo(2 + mergeIndex);
const message = stdout.split('\n\n')[1].trim();
return {
hash,
shortHash,
commit: hash,
shortCommit: shortHash,
author,
email,
date,
message,
};
} catch (error) {
return { error };
}
};
export default gitCommitInfo;