11const { createMacro } = require ( 'babel-plugin-macros' ) ;
22const { execSync } = require ( 'child_process' ) ;
33
4- const parseGitLog = ( ( ) => {
5- let message = '' ;
6- let refs = '' ;
4+ const parsedGitLog = ( ( ) => {
5+ let message ;
6+ let refs ;
77 const commit = { } ;
88 // only the commit message can have multiple lines. Make sure to always add at the end:
9+ // The format is specified in https://git-scm.com/docs/git-log#_pretty_formats
910 const logResult = execSync ( 'git log --format="%D%n%h%n%H%n%cI%n%B" -n 1 HEAD' )
1011 . toString ( )
1112 . trim ( )
1213 . split ( / \r ? \n / ) ;
1314 [ refs , commit . shortHash , commit . hash , commit . date , ...message ] = logResult ;
14- commit . message = message . join ( "\n" ) ;
15+ commit . message = message . join ( '\n' ) ;
1516 return { refs, commit} ;
1617} ) ( ) ;
1718
1819const parseRefs = ( refs ) => {
19- let branch ;
20+ let branch = undefined ;
2021 const tags = [ ] ;
21- refs . split ( ", " ) . map ( ( item ) => {
22+ refs . split ( ', ' ) . map ( ( item ) => {
2223 const isBranch = item . match ( / H E A D - > ( .* ) / ) ;
2324 const isTag = item . match ( / t a g : ( .* ) / ) ;
2425
@@ -28,13 +29,18 @@ const parseRefs = (refs) => {
2829 branch = isBranch ? isBranch [ 1 ] : branch ;
2930 }
3031 } ) ;
32+
33+ if ( ! branch ) {
34+ throw Error ( 'Unable to get parse branch from the log result.' ) ;
35+ }
36+
3137 return [ branch , tags ] ;
3238} ;
3339
3440const gitInfo = ( ( ) => {
3541 const ret = { } ;
3642 try {
37- const logResult = parseGitLog ;
43+ const logResult = parsedGitLog ;
3844 [ ret . branch , ret . tags ] = parseRefs ( logResult . refs ) ;
3945 ret . commit = logResult . commit ;
4046 } catch ( e ) {
0 commit comments