forked from JPeer264/node-git-commit-info
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (47 loc) · 1.57 KB
/
index.js
File metadata and controls
58 lines (47 loc) · 1.57 KB
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
import execa from 'execa';
import isGit from 'is-git-repository';
import { platform } from 'os';
import path from 'path';
import pathIsAbsolute from 'path-is-absolute';
const processCwd = process.cwd();
const regex = /\s+([\s\S]*)/g; // matches everything after the first whitespace
const gitCommitInfo = ({ cwd, commit } = {}) => {
const thisCommit = commit || '';
let thisPath = cwd || processCwd;
thisPath = pathIsAbsolute(thisPath) ? thisPath : path.join(cwd, thisPath);
if (!isGit(thisPath)) {
return {};
}
try {
let exec;
if (platform() === 'win32') {
exec = execa.shellSync(`pushd ${thisPath} & git --no-pager show ${thisCommit} --summary`);
} else {
exec = execa.shellSync(`(cd ${thisPath} ; git --no-pager show ${thisCommit} --summary)`);
}
const info = exec.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 author = new RegExp(regex).exec(info[1 + mergeIndex])[1].match(/([^<]+)/)[1].trim();
const email = new RegExp(regex).exec(info[1 + mergeIndex])[1].match(/<([^>]+)>/)[1];
const date = new RegExp(regex).exec(info[2 + mergeIndex])[1];
const message = exec.stdout.split('\n\n')[1].trim();
return {
hash,
shortHash,
commit: hash,
shortCommit: shortHash,
author,
email,
date,
message,
};
} catch (e) {
console.info(e);
return {};
}
};
export default gitCommitInfo;