11import { Transform } from 'node:stream'
22import { stripVTControlCharacters } from 'node:util'
33
4+ /**
5+ * @typedef {Object } Person
6+ * @property {string } name
7+ * @property {string } email
8+ */
9+
10+ /**
11+ * @typedef {Object } Changes
12+ * @property {number } files
13+ * @property {number } insertions
14+ * @property {number } deletions
15+ */
16+
17+ /**
18+ * @typedef {Object } Commit
19+ * @property {string } sha
20+ * @property {Person } [author]
21+ * @property {Person[] } [authors]
22+ * @property {string } [authorDate]
23+ * @property {string } [commitDate]
24+ * @property {string } [summary]
25+ * @property {string[] } [description]
26+ * @property {Person[] } [reviewers]
27+ * @property {Changes } [changes]
28+ * @property {string } [prUrl]
29+ * @property {string } [ghUser]
30+ * @property {string } [ghProject]
31+ * @property {number } [ghIssue]
32+ * @property {string } [cveId]
33+ */
34+
35+ /**
36+ * Create an object stream that turns lines of `git log` output into commit objects.
37+ *
38+ * @param {string } [ghUser] - GitHub user used to expand short `#XX` PR references
39+ * @param {string } [ghProject] - GitHub project used to expand short `#XX` PR references
40+ * @returns {Transform }
41+ */
442export default function commitStream ( ghUser , ghProject ) {
43+ /** @type {Commit | undefined } */
544 let commit
645
7- return new Transform ( { objectMode : true , transform : onLine , flush : onEnd } )
46+ return new Transform ( {
47+ objectMode : true ,
48+ transform ( line , _ , callback ) {
49+ emit ( this , addLine ( line ) )
50+ callback ( )
51+ } ,
52+ flush ( callback ) {
53+ emit ( this , commit )
54+ callback ( )
55+ }
56+ } )
857
58+ /**
59+ * @param {string } line
60+ * @returns {Commit | undefined } the previous commit, when `line` starts a new one
61+ */
962 function addLine ( line ) {
1063 line = stripVTControlCharacters ( line )
1164
1265 if ( line . length === 0 ) return
1366
14- let old , m
15-
1667 if ( / ^ c o m m i t \w + $ / . test ( line ) ) {
17- old = commit
68+ const old = commit
1869 commit = { sha : line . split ( ' ' ) [ 1 ] }
19- } else if ( ( m = line . match ( / ^ C o m m i t D a t e : \s + ( .* ) $ / ) ) !== null ) {
20- if ( commit === undefined || commit === null ) {
21- throw new Error ( 'Something unexpected occurred' )
22- }
70+ return old
71+ }
72+
73+ let m
74+
75+ if ( ! commit ) {
76+ throw new Error ( 'Something unexpected occurred' )
77+ }
78+
79+ if ( ( m = line . match ( / ^ C o m m i t D a t e : \s + ( .* ) $ / ) ) !== null ) {
2380 commit . commitDate = m [ 1 ] . trim ( )
2481 } else if (
2582 ( m = line . match (
2683 / ^ \s * (?: A u t h o r | C o [ - ] ? a u t h o r e d [ - ] ? b y ) : ? \s * ( [ ^ < ] + ) < ( [ ^ > ] + ) > \s * $ / i
2784 ) ) !== null
2885 ) {
29- if ( commit === undefined || commit === null ) {
30- throw new Error ( 'Something unexpected occurred' )
31- }
32- if ( commit . authors === undefined || commit . authors === null ) {
33- commit . authors = [ ]
34- }
35- commit . authors . push ( { name : m [ 1 ] , email : m [ 2 ] } )
86+ ( commit . authors ??= [ ] ) . push ( { name : m [ 1 ] , email : m [ 2 ] } )
3687 } else if ( ( m = line . match ( / ^ (?: A u t h o r D a t e | D a t e ) : \s + ( .* ) $ / ) ) !== null ) {
37- if ( commit === undefined || commit === null ) {
38- throw new Error ( 'Something unexpected occurred' )
39- }
4088 commit . authorDate = m [ 1 ] . trim ( )
4189 } else if (
4290 ( m = line . match ( / ^ \s + R e v i e w e d [ - ] ? B y : ? \s * ( [ ^ < ] + ) < ( [ ^ > ] + ) > \s * $ / i) ) !==
4391 null
4492 ) {
45- if ( commit . reviewers === undefined || commit . reviewers === null ) {
46- commit . reviewers = [ ]
47- }
48- commit . reviewers . push ( { name : m [ 1 ] , email : m [ 2 ] } )
93+ ( commit . reviewers ??= [ ] ) . push ( { name : m [ 1 ] , email : m [ 2 ] } )
4994 } else if ( ( m = line . match ( / \b C V E - I D : \s + ( C V E - \d { 4 } - \d { 4 , } ) \b / ) ) !== null ) {
5095 commit . cveId = m [ 1 ]
5196 } else if ( ( m = line . match ( / ^ \s + P R (?: [ - ] ? U R L ) ? : ? \s * ( .+ ) \s * $ / ) || line . match ( / \( # ( \d + ) \) $ / ) ) !== null ) {
5297 commit . prUrl = m [ 1 ]
53- if (
54- typeof ghUser === 'string' &&
55- ghUser . length !== 0 &&
56- typeof ghProject === 'string' &&
57- ghProject . length !== 0 &&
58- ( m = commit . prUrl . match ( / ^ \s * # ? ( \d + ) \s * $ / ) ) !== null
59- ) {
98+ if ( ghUser && ghProject && ( m = commit . prUrl . match ( / ^ \s * # ? ( \d + ) \s * $ / ) ) !== null ) {
6099 commit . prUrl = `https://github.com/${ ghUser } /${ ghProject } /pull/${ m [ 1 ] } `
61100 }
62101 if (
63102 ( m = commit . prUrl . match (
64103 / ^ ( h t t p s ? : \/ \/ .+ \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ) ) \/ \w + \/ ( \d + ) ( \/ ) ? $ / i
65104 ) ) !== null
66105 ) {
67- commit . ghIssue = parseInt ( m [ 4 ] )
106+ commit . ghIssue = Number ( m [ 4 ] )
68107 commit . ghUser = m [ 2 ]
69108 commit . ghProject = m [ 3 ]
70109 }
71110 if ( ( m = line . match ( / ^ { 4 } ( .* ) \s \( # \d + \) $ / ) ) && ! commit . summary ) {
72111 commit . summary = m [ 1 ]
73112 }
74113 } else if ( / ^ { 4 } / . test ( line ) && ( line = line . trim ( ) ) . length !== 0 ) {
75- if ( commit . summary === undefined || commit . summary === null ) {
114+ if ( commit . summary === undefined ) {
76115 commit . summary = line
77116 } else {
78- if ( commit . description === undefined || commit . description === null ) {
79- commit . description = [ ]
80- }
81- commit . description . push ( line )
117+ ( commit . description ??= [ ] ) . push ( line )
82118 }
83119 } else if (
84120 / ^ [ ^ ] / . test ( line ) &&
@@ -88,34 +124,23 @@ export default function commitStream (ghUser, ghProject) {
88124 ) ) !== null
89125 ) {
90126 commit . changes = {
91- files : parseInt ( m [ 1 ] ) ,
92- insertions : parseInt ( m [ 2 ] ?? 0 ) ,
93- deletions : parseInt ( m [ 3 ] ?? 0 )
127+ files : Number ( m [ 1 ] ) ,
128+ insertions : Number ( m [ 2 ] ?? 0 ) ,
129+ deletions : Number ( m [ 3 ] ?? 0 )
94130 }
95131 }
96-
97- return old
98- }
99-
100- function onLine ( line , _ , callback ) {
101- const commit = addLine ( line )
102-
103- if ( commit ?. authors ?. length > 0 ) {
104- commit . author = commit . authors [ 0 ]
105-
106- this . push ( commit )
107- }
108-
109- callback ( )
110132 }
133+ }
111134
112- function onEnd ( callback ) {
113- if ( commit ?. authors ?. length > 0 ) {
114- commit . author = commit . authors [ 0 ]
115-
116- this . push ( commit )
117- }
118-
119- callback ( )
135+ /**
136+ * Push a completed commit, skipping any without an author.
137+ *
138+ * @param {Transform } stream
139+ * @param {Commit | undefined } commit
140+ */
141+ function emit ( stream , commit ) {
142+ if ( commit ?. authors ?. length ) {
143+ commit . author = commit . authors [ 0 ]
144+ stream . push ( commit )
120145 }
121146}
0 commit comments