@@ -21,7 +21,7 @@ export class IncreaseProcess<T extends keyof Mapper> extends BaseProcess<T> {
2121 stream : { } ,
2222 } ;
2323
24- private firstGitMessage : CommitInfo = { } ;
24+ private baseDiffCommitInfo : CommitInfo | undefined ;
2525
2626 constructor ( lcovPath : string | string [ ] , opts : IncreaseProcessOpts < T > = { stream : { } } ) {
2727 super ( ) ;
@@ -45,11 +45,14 @@ export class IncreaseProcess<T extends keyof Mapper> extends BaseProcess<T> {
4545 }
4646
4747 async exec ( ) : Promise < IncreaseResult > {
48- // 得到本次 diff 信息
48+ // 如果传递了 hash,则得到该提交记录
4949 if ( this . opts . hash ) {
50- await this . getInfoByHash ( this . opts . hash ) ;
51- } else {
52- await this . getLog ( ) ;
50+ this . baseDiffCommitInfo = await this . getCommitInfoByHash ( this . opts . hash ) ;
51+ }
52+
53+ // 如果通过 hash 无法获得,则自动获取指定日期的那次代码提交
54+ if ( ! this . baseDiffCommitInfo ) {
55+ this . baseDiffCommitInfo = await this . getCommitInfoByLogSince ( ) ;
5356 }
5457
5558 if ( ! this . opts . cwd ) {
@@ -59,9 +62,6 @@ export class IncreaseProcess<T extends keyof Mapper> extends BaseProcess<T> {
5962 // 将首次提交的代码信息当做创建信息
6063 const createInfo = await this . getCreateInfo ( ) as CommitInfo ;
6164
62- // 得到创建信息
63- this . firstGitMessage = createInfo ;
64-
6565 // 得到增量合并结果
6666 await this . getLcov ( ) ;
6767
@@ -70,14 +70,14 @@ export class IncreaseProcess<T extends keyof Mapper> extends BaseProcess<T> {
7070 if ( this . opts . output ) {
7171 this . output ( {
7272 data : this . formatData ,
73- commit : this . firstGitMessage ,
73+ commit : this . baseDiffCommitInfo ,
7474 createInfo,
7575 } ) ;
7676 }
7777
7878 return {
7979 data : this . formatData ,
80- commit : this . firstGitMessage ,
80+ commit : this . baseDiffCommitInfo as CommitInfo ,
8181 createInfo,
8282 gitRepoInfo : this . getGitRepoInfo ( )
8383 } ;
@@ -90,36 +90,41 @@ export class IncreaseProcess<T extends keyof Mapper> extends BaseProcess<T> {
9090 // 解析获得 lcov.info 的信息,这里是全量覆盖率信息
9191 const res = await getLcovFile ( this . lcovPath ) ;
9292
93- this . lcov = (
94- await new IncreaseConcat ( {
95- cwd : this . opts . cwd ,
96- hash : this . firstGitMessage ?. hash ,
97- } ) . concat ( ...res )
98- ) . getRes ( ) ;
93+ // 合并多个 lcov 文件
94+ const increaseConcat = await new IncreaseConcat ( {
95+ cwd : this . opts . cwd ,
96+ hash : this . baseDiffCommitInfo ?. hash ,
97+ } ) . concat ( ...res ) ;
98+
99+ // 获得合并结果
100+ this . lcov = increaseConcat . getRes ( ) ;
99101 }
100102
101103 /**
102- * 得到 GitLog 结果
104+ * 通过 GitLog 的 since 值得到本次提交的记录
103105 */
104- private async getLog ( ) {
106+ private async getCommitInfoByLogSince ( ) : Promise < ( CommitInfo | undefined ) > {
107+ // 获得当月1号
105108 const subDate = dayjs ( this . opts . since ) . subtract ( 1 , 'day' ) . format ( 'YYYY-MM-DD' ) ;
106109
107110 const res = await new LogParser ( {
108111 repo : this . opts . cwd as string ,
109- until : subDate ,
112+
113+ // before: 该日期之前的记录,更新的排在前面
114+ // after: 该日期之后的记录,更新的排在前面
115+ before : subDate ,
110116 } ) . run ( ) ;
111117
112- [ this . firstGitMessage ] = res ;
118+ // 返回第一个值
119+ return res [ 0 ] ;
113120 }
114121
115122 /**
116123 * 通过 hash 值得到本次提交的记录
117124 */
118- private async getInfoByHash ( hash : string ) {
125+ private async getCommitInfoByHash ( hash : string ) : Promise < ( CommitInfo | undefined ) > {
119126 const result = await getGitRepoCommitInfoByHash ( hash , this . opts . cwd ) ;
120127
121- if ( result ) {
122- this . firstGitMessage = result ;
123- }
128+ return result || undefined ;
124129 }
125130}
0 commit comments