-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
106 lines (86 loc) · 2.14 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Created by hustcc.
* Contract: i@hust.cc
*/
var execSync = require('child_process').execSync;
/**
* git commit format
*
* 1. no merge
*
commit a41d55c100ae3e47a4781b7215cc2b5578fb59da
Author: hustcc <i@hust.cc>
Date: Mon Dec 18 15:22:09 2017 +0800
删除一些密码信息,防止被黑!
*
* 2. merge commit
*
commit a41d55c100ae3e47a4781b7215cc2b5578fb59da
Merge: 54df71f f8d50c8
Author: hustcc <i@hust.cc>
Date: Mon Dec 18 20:26:34 2017 +0800
Merge branch 'release_7266153_20171218' of gitlab.al
*
* 3. invalid commit
*
* fatal: ambiguous argument '12': unknown revision or path not in the working tree.
*
*/
function safeString(s) {
return s || '';
}
/**
* var tgm = new TGM(); // current dir
* var tgm = new TGM('/home/git/hustcc/git-commit');
* @param cwd
* @constructor
*/
function TGM(cwd) {
this.cwd = cwd; // current dir.
}
/**
* tgm.info(hash);
* tgm.info(); // latest commit
*
* @param hash
* @returns {string}
*/
TGM.prototype.info = function(hash) {
var baseCmd = 'git log -n 1'; // get commit log with this cmd.
var cmd = hash ? baseCmd + ' ' + hash : baseCmd;
const info = execSync(cmd, { cwd: this.cwd }).toString();
return this.parse(info);
};
/**
* parse commit log
* @param info
* @returns {{}}
*/
TGM.prototype.parse = function(info) {
var r = {};
var arr = safeString(info).trim().split('\n');
var cnt = -1;
var s = '';
// 1 th line is commit hash
s = safeString(arr[++ cnt]);
r.hash = s.substring('commit'.length).trim();
r.short = r.hash.substring(0, 7);
s = safeString(arr[++ cnt]);
// 2 th line is Merge:
if (s.indexOf('Merge:') === 0) {
r.merge = s.substring('Merge:'.length).trim().split(' ');
s = safeString(arr[++ cnt]);
}
// 3 th line is Author: author, email
var author = s.substring('Author:'.length).trim().match(/(.*)<(.*)>/);
r.author = safeString(author[1]).trim();
r.email = safeString(author[2]).trim();
s = safeString(arr[++ cnt]);
// 4 th line is Date
r.date = s.substring('Date:'.length).trim();
s = safeString(arr[++ cnt]);
// commit
r.message = arr.slice(cnt).join('').trim();
return r;
};
module.exports = TGM;