forked from kareemaly/git-change-date
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitLogConverter.js
73 lines (62 loc) · 1.45 KB
/
gitLogConverter.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
/**
* Gets a commit log string and return an object describing
* the commit
* @param commitStr
* @return Object
*/
export const convertCommit = (commitStr) => {
const lines = commitStr.split(/\r?\n/);
const commit = {};
let re;
lines.forEach((line) => {
if (!commit.hash) {
re = line.match(/commit\s([a-zA-Z0-9]*)/);
if (re) {
[, commit.hash] = re;
}
} else if (!commit.name) {
re = line.match(/Author:[\s]*([^<]*)<(.*)>/);
if (re) {
[, commit.name, commit.email] = re;
}
} else if (!commit.date) {
re = line.match(/Date:[\s]*(.*)/);
if (re) {
[, commit.date] = re;
}
} else if (!commit.subject) {
re = line.match(/\s*(.*)/);
if (re && re[1]) {
[, commit.subject] = re;
}
}
});
return {
hash: commit.hash.trim(),
name: commit.name.trim(),
email: commit.email.trim(),
date: commit.date.trim(),
subject: commit.subject.trim(),
};
};
/**
* Gets a commits log string and return an array of objects
* describing the commits
* @param str
* @return Array
*/
export default (str) => {
const lines = str.split(/\r?\n/);
const commits = [];
let index = -1;
lines.forEach((line) => {
if (line.indexOf('commit ') === 0) {
index += 1;
commits[index] = '';
}
if (index > -1) {
commits[index] = commits[index].concat(`${line}\n`);
}
});
return commits.map(convertCommit);
};