@@ -34,6 +34,25 @@ function replace (s, m) {
3434 return s
3535}
3636
37+ async function spawnPromisified ( ...args ) {
38+ const callback = args . pop ( )
39+ const cp = spawn ( ...args )
40+
41+ const closeEv = once ( cp , 'close' )
42+ closeEv . catch ( ( ) => { } )
43+
44+ const result = await Promise . race ( [
45+ once ( cp , 'error' ) . then ( e => Promise . reject ( e [ 0 ] ) ) ,
46+ callback ( cp . stdout )
47+ ] )
48+ const [ code , signal ] = await closeEv
49+ if ( code !== 0 ) {
50+ throw new Error ( `Command "${ args [ 0 ] } ${ args [ 1 ] . join ( ' ' ) } " exited with ${ signal || code } ` )
51+ }
52+
53+ return result
54+ }
55+
3756export async function branchDiff ( branch1 , branch2 , options ) {
3857 if ( ! branch1 || ! branch2 ) {
3958 throw new Error ( 'Must supply two branch names to compare' )
@@ -48,21 +67,10 @@ export async function branchDiff (branch1, branch2, options) {
4867}
4968
5069async function findMergeBase ( repoPath , branch1 , branch2 ) {
51- const cp = spawn ( 'git' , [ 'merge-base' , branch1 , branch2 ] , {
70+ const chunks = await spawnPromisified ( 'git' , [ 'merge-base' , branch1 , branch2 ] , {
5271 cwd : repoPath ,
5372 stdio : [ 'ignore' , 'pipe' , 'inherit' ]
54- } )
55- const closeEv = once ( cp , 'close' )
56- closeEv . catch ( ( ) => { } )
57-
58- const chunks = await Promise . race ( [
59- once ( cp , 'error' ) . then ( e => Promise . reject ( e [ 0 ] ) ) ,
60- cp . stdout . toArray ( )
61- ] )
62- const [ code , signal ] = await closeEv
63- if ( code !== 0 ) {
64- throw new Error ( `Command "git merge-base ${ branch1 } ${ branch2 } " exited with code ${ code } and signal ${ signal } ` )
65- }
73+ } , stdout => stdout . toArray ( ) )
6674 return Buffer . concat ( chunks ) . toString ( 'utf-8' , 0 , 10 )
6775}
6876
@@ -121,33 +129,21 @@ async function collect (repoPath, branch, startCommit, endRef) {
121129 const untilcmd = endRef ? replace ( commitdatecmd , { refcmd : endrefcmd } ) : ''
122130 const _gitcmd = replace ( gitcmd , { branch, startCommit, untilcmd } )
123131
124- const cp = spawn ( _gitcmd , [ ] , {
132+ const commitList = [ ]
133+ await spawnPromisified ( _gitcmd , [ ] , {
125134 shell : true ,
126135 cwd : repoPath ,
127136 stdio : [ 'ignore' , 'pipe' , 'inherit' ]
128- } )
129- const closeEv = once ( cp , 'close' )
130- closeEv . catch ( ( ) => { } )
131-
132- const commitList = [ ]
133- await Promise . race ( [
134- once ( cp , 'error' ) . then ( e => Promise . reject ( e [ 0 ] ) ) ,
135- pipeline (
136- cp . stdout ,
137- split2 ( ) ,
138- commitStream ( ghId . user , ghId . repo ) ,
139- async function * ( source ) {
140- for await ( const commit of source ) {
141- commitList . push ( commit )
142- }
137+ } , ( stdout ) => pipeline (
138+ stdout ,
139+ split2 ( ) ,
140+ commitStream ( ghId . user , ghId . repo ) ,
141+ async function * ( source ) {
142+ for await ( const commit of source ) {
143+ commitList . push ( commit )
143144 }
144- )
145- ] )
146- const [ code , signal ] = await closeEv
147- if ( code !== 0 ) {
148- throw new Error ( `Command "${ _gitcmd } " exited with code ${ code } and signal ${ signal } ` )
149- }
150-
145+ }
146+ ) )
151147 return commitList
152148}
153149
0 commit comments