@@ -17,9 +17,18 @@ export interface ICommitFormatOptions {
1717
1818export class CommitFormatter {
1919
20+ private _commit : GitCommit ;
2021 private _options : ICommitFormatOptions ;
2122
22- constructor ( private commit : GitCommit , options ?: ICommitFormatOptions ) {
23+ constructor ( commit : GitCommit , options ?: ICommitFormatOptions ) {
24+ this . reset ( commit , options ) ;
25+ }
26+
27+ reset ( commit : GitCommit , options ?: ICommitFormatOptions ) {
28+ this . _commit = commit ;
29+
30+ if ( options === undefined && this . _options !== undefined ) return ;
31+
2332 options = options || { } ;
2433 if ( options . tokenOptions == null ) {
2534 options . tokenOptions = { } ;
@@ -33,31 +42,31 @@ export class CommitFormatter {
3342 }
3443
3544 get ago ( ) {
36- const ago = moment ( this . commit . date ) . fromNow ( ) ;
45+ const ago = moment ( this . _commit . date ) . fromNow ( ) ;
3746 return this . _padOrTruncate ( ago , this . _options . tokenOptions ! . ago ) ;
3847 }
3948
4049 get author ( ) {
41- const author = this . commit . author ;
50+ const author = this . _commit . author ;
4251 return this . _padOrTruncate ( author , this . _options . tokenOptions ! . author ) ;
4352 }
4453
4554 get authorAgo ( ) {
46- const authorAgo = `${ this . commit . author } , ${ moment ( this . commit . date ) . fromNow ( ) } ` ;
55+ const authorAgo = `${ this . _commit . author } , ${ moment ( this . _commit . date ) . fromNow ( ) } ` ;
4756 return this . _padOrTruncate ( authorAgo , this . _options . tokenOptions ! . authorAgo ) ;
4857 }
4958
5059 get date ( ) {
51- const date = moment ( this . commit . date ) . format ( this . _options . dateFormat ! ) ;
60+ const date = moment ( this . _commit . date ) . format ( this . _options . dateFormat ! ) ;
5261 return this . _padOrTruncate ( date , this . _options . tokenOptions ! . date ) ;
5362 }
5463
5564 get id ( ) {
56- return this . commit . shortSha ;
65+ return this . _commit . shortSha ;
5766 }
5867
5968 get message ( ) {
60- const message = this . commit . isUncommitted ? 'Uncommitted change' : this . commit . message ;
69+ const message = this . _commit . isUncommitted ? 'Uncommitted change' : this . _commit . message ;
6170 return this . _padOrTruncate ( message , this . _options . tokenOptions ! . message ) ;
6271 }
6372
@@ -113,6 +122,18 @@ export class CommitFormatter {
113122 return s ;
114123 }
115124
125+ private static _formatter : CommitFormatter | undefined = undefined ;
126+
127+ static fromCommit ( commit : GitCommit , options ?: ICommitFormatOptions ) : CommitFormatter {
128+ if ( CommitFormatter . _formatter === undefined ) {
129+ CommitFormatter . _formatter = new CommitFormatter ( commit , options ) ;
130+ }
131+ else {
132+ CommitFormatter . _formatter . reset ( commit , options ) ;
133+ }
134+ return CommitFormatter . _formatter ;
135+ }
136+
116137 static fromTemplate ( template : string , commit : GitCommit , dateFormat : string | null ) : string ;
117138 static fromTemplate ( template : string , commit : GitCommit , options ?: ICommitFormatOptions ) : string ;
118139 static fromTemplate ( template : string , commit : GitCommit , dateFormatOrOptions ?: string | null | ICommitFormatOptions ) : string ;
0 commit comments