Repository navigation
Expand file tree
/
Copy pathcommit-stream.js
More file actions
152 lines (140 loc) · 4.17 KB
/
Copy pathcommit-stream.js
File metadata and controls
152 lines (140 loc) · 4.17 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { Transform } from 'node:stream'
import { stripVTControlCharacters } from 'node:util'
/**
* @typedef {Object} Person
* @property {string} name
* @property {string} email
*/
/**
* @typedef {Object} Changes
* @property {number} files
* @property {number} insertions
* @property {number} deletions
*/
/**
* @typedef {Object} Commit
* @property {string} sha
* @property {Person} [author]
* @property {Person[]} [authors]
* @property {string} [authorDate]
* @property {string} [commitDate]
* @property {string} [summary]
* @property {string[]} [description]
* @property {Person[]} [reviewers]
* @property {Changes} [changes]
* @property {string} [prUrl]
* @property {string} [ghUser]
* @property {string} [ghProject]
* @property {number} [ghIssue]
* @property {string} [cveId]
*/
/**
* Create an object stream that turns lines of `git log` output into commit objects.
*
* @param {string} [ghUser] - GitHub user used to expand short `#XX` PR references
* @param {string} [ghProject] - GitHub project used to expand short `#XX` PR references
* @returns {Transform}
*/
export default function commitStream (ghUser, ghProject) {
/** @type {Commit | undefined} */
let commit
return new Transform({
objectMode: true,
transform (line, _, callback) {
let done
try {
done = addLine(line)
} catch (err) {
return callback(/** @type {Error} */ (err))
}
emit(this, done)
callback()
},
flush (callback) {
emit(this, commit)
callback()
}
})
/**
* @param {string} line
* @returns {Commit | undefined} the previous commit, when `line` starts a new one
*/
function addLine (line) {
line = stripVTControlCharacters(line)
if (line.length === 0) return
let m
if ((m = line.match(/^commit (\w+)/)) !== null) {
const old = commit
commit = { sha: m[1] }
return old
}
if (!commit) {
throw new Error(`Unexpected line before first "commit" header: ${line}`)
}
if ((m = line.match(/^CommitDate:\s+(.*)$/)) !== null) {
commit.commitDate = m[1].trim()
} else if (
(m = line.match(
/^\s*(?:Author|Co[- ]?authored[- ]?by):?\s*([^<]+) <([^>]+)>\s*$/i
)) !== null
) {
(commit.authors ??= []).push({ name: m[1], email: m[2] })
} else if ((m = line.match(/^(?:AuthorDate|Date):\s+(.*)$/)) !== null) {
commit.authorDate = m[1].trim()
} else if (
(m = line.match(/^\s+Reviewed[- ]?By:?\s*([^<]+) <([^>]+)>\s*$/i)) !==
null
) {
(commit.reviewers ??= []).push({ name: m[1], email: m[2] })
} else if ((m = line.match(/\bCVE-ID:\s+(CVE-\d{4}-\d{4,})\b/)) !== null) {
commit.cveId = m[1]
} else if ((m = line.match(/^\s+PR(?:[- ]?URL)?:?\s*(.+)\s*$/) || line.match(/\(#(\d+)\)$/)) !== null) {
commit.prUrl = m[1]
if (ghUser && ghProject && (m = commit.prUrl.match(/^\s*#?(\d+)\s*$/)) !== null) {
commit.prUrl = `https://github.com/${ghUser}/${ghProject}/pull/${m[1]}`
}
if (
(m = commit.prUrl.match(
/^(https?:\/\/.+\/([^/]+)\/([^/]+))\/\w+\/(\d+)(\/)?$/i
)) !== null
) {
commit.ghIssue = Number(m[4])
commit.ghUser = m[2]
commit.ghProject = m[3]
}
if ((m = line.match(/^ {4}(.*)\s\(#\d+\)$/)) && !commit.summary) {
commit.summary = m[1]
}
} else if (/^ {4}/.test(line) && (line = line.trim()).length !== 0) {
if (commit.summary === undefined) {
commit.summary = line
} else {
(commit.description ??= []).push(line)
}
} else if (
/^ [^ ]/.test(line) &&
(line = line.trim()).length !== 0 &&
(m = line.match(
/^(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?/
)) !== null
) {
commit.changes = {
files: Number(m[1]),
insertions: Number(m[2] ?? 0),
deletions: Number(m[3] ?? 0)
}
}
}
}
/**
* Push a completed commit, skipping any without an author.
*
* @param {Transform} stream
* @param {Commit | undefined} commit
*/
function emit (stream, commit) {
if (commit?.authors?.length) {
commit.author = commit.authors[0]
stream.push(commit)
}
}