32
32
33
33
'use strict' ;
34
34
35
+ const debug = require ( 'debug' ) ( 'workflow:done' ) ;
36
+
35
37
const {
36
38
featureParent,
37
39
yesNo,
@@ -40,13 +42,21 @@ const {
40
42
inferRemote,
41
43
} = require ( '../common' ) ;
42
44
43
- /**
44
- * @param {string } diff
45
- */
45
+ /** @param {string } diff */
46
46
function stripIndexLines ( diff ) {
47
47
return diff . replace ( / ^ i n d e x .* \n / gm, '' ) ;
48
48
}
49
49
50
+ /** @param {string } diff */
51
+ function stripLineNums ( diff ) {
52
+ return diff . replace ( / ^ @ @ .* \n / gm, '' ) ;
53
+ }
54
+
55
+ /** @param {{ message: string, hash: string } } commit */
56
+ function commitDescr ( { message, hash } ) {
57
+ return `[${ hash . slice ( 0 , 7 ) } ] ${ message } ` ;
58
+ }
59
+
50
60
/**
51
61
*
52
62
* @param {import('simple-git/promise').SimpleGit } git
@@ -59,13 +69,33 @@ async function findSquashedDiff(git, feature, parent) {
59
69
const diff = stripIndexLines ( await git . diff ( [ `${ base } ..${ feature } ` ] ) ) ;
60
70
61
71
const { all : commits } = await git . log ( { from : base , to : parent } ) ;
62
- for ( const { hash, message } of commits ) {
63
- const show = await git . show ( [ hash ] ) ;
72
+
73
+ /** @type {Map<typeof commits[0], string> } */
74
+ const cachedCommitDiffs = new Map ( ) ;
75
+
76
+ for ( const commit of commits ) {
77
+ const show = await git . show ( [ commit . hash ] ) ;
64
78
// strip off the commit msg at the beginning of the show
65
79
const commitDiff = stripIndexLines ( show ) . replace ( / ^ [ \s \S ] + ?\n d i f f / , 'diff' ) ;
66
- if ( commitDiff === diff ) return `[${ hash . slice ( 0 , 7 ) } ] ${ message } ` ;
80
+ if ( commitDiff === diff ) return commitDescr ( commit ) ;
81
+ cachedCommitDiffs . set ( commit , commitDiff ) ;
67
82
}
68
83
84
+ debug ( "Couldn't find squashed diff on exact match; fudging line numbers" ) ;
85
+ const strippedDiff = stripLineNums ( diff ) ;
86
+
87
+ for ( const commit of commits ) {
88
+ let commitDiff = cachedCommitDiffs . get ( commit ) ;
89
+ if ( ! commitDiff ) {
90
+ throw new Error ( `Couldn't find cached commit diff for ${ commit . hash } ` ) ;
91
+ }
92
+ commitDiff = stripLineNums ( commitDiff ) ;
93
+ if ( commitDiff === strippedDiff ) return commitDescr ( commit ) ;
94
+ }
95
+
96
+ debug ( 'Failed to find squashed diff match for:' ) ;
97
+ debug ( strippedDiff ) ;
98
+
69
99
return null ;
70
100
}
71
101
0 commit comments