1212import java .util .List ;
1313import java .util .Optional ;
1414
15- public record GitVersion (String tag , String commits , String commit ) {
16- private static final GitVersion UNKNOWN = new GitVersion ("0.0" , "9999" , "unknown" );
15+ public record GitVersion (String tag , String commits , String commit , String lastCommitMessage ) {
16+ private static final GitVersion UNKNOWN = new GitVersion ("0.0" , "9999" , "unknown" , "unknown" );
1717
1818 public static GitVersion getGitVersion () {
1919 try {
@@ -41,7 +41,10 @@ public static GitVersion getGitVersion() {
4141 // Get HEAD commit short hash
4242 String commitHash = repository .resolve ("HEAD" ).getName ().substring (0 , 7 );
4343
44- return new GitVersion (tagName , String .valueOf (commitCount ), commitHash );
44+ // Get last commit message
45+ String lastCommitMessage = getLastCommitMessage (repository );
46+
47+ return new GitVersion (tagName , String .valueOf (commitCount ), commitHash , lastCommitMessage );
4548 }
4649 } catch (Exception ignored ) {}
4750 return UNKNOWN ;
@@ -67,6 +70,13 @@ private static int countCommitsSince(Repository repository, RevCommit taggedComm
6770 }
6871 }
6972
73+ private static String getLastCommitMessage (Repository repository ) throws IOException {
74+ try (RevWalk walk = new RevWalk (repository )) {
75+ RevCommit headCommit = walk .parseCommit (repository .resolve ("HEAD" ));
76+ return headCommit .getShortMessage ();
77+ }
78+ }
79+
7080 public boolean isUnknown () {
7181 return this == UNKNOWN ;
7282 }
@@ -78,4 +88,8 @@ public String getVersionAsString() {
7888 public String getVersionAsString (boolean includeCommit ) {
7989 return includeCommit ? tag + "." + commits + "-" + commit : tag + "." + commits ;
8090 }
91+
92+ public String getLastCommitMessage () {
93+ return lastCommitMessage ;
94+ }
8195}
0 commit comments