From 6a89c49435d2ef3f9bc0f876f8575565998c73b0 Mon Sep 17 00:00:00 2001
From: Georg Seibt <seibt@fim.uni-passau.de>
Date: Thu, 20 Oct 2016 13:07:33 +0200
Subject: [PATCH] correctly parse the message (by finding the first empty line
 in the cat-file output and taking the rest of it as the message) use a very
 evil trick to avoid the Matcher initialization cost when parsing is
 unnecessary due to the fields already being initialized

---
 .../fim/seibt/gitwrapper/repo/Commit.java     | 34 +++++++++++++++----
 1 file changed, 28 insertions(+), 6 deletions(-)

diff --git a/src/de/uni_passau/fim/seibt/gitwrapper/repo/Commit.java b/src/de/uni_passau/fim/seibt/gitwrapper/repo/Commit.java
index 589f868..8a0afed 100644
--- a/src/de/uni_passau/fim/seibt/gitwrapper/repo/Commit.java
+++ b/src/de/uni_passau/fim/seibt/gitwrapper/repo/Commit.java
@@ -17,6 +17,8 @@ public class Commit extends Reference {
     private static final Pattern TREE_INFO = Pattern.compile("tree (.{40})\\nparent (.{40})\\n");
     private static final Pattern AUTHOR_INFO = Pattern.compile("author (.*?)<(.*?)> (\\d+) ([+-]\\d{4})\\n");
     private static final Pattern COMMITTER_INFO = Pattern.compile("committer (.*?)<(.*?)> (\\d+) ([+-]\\d{4})\\n");
+    private static final Pattern EMPTY_LINE = Pattern.compile("^\\s*$", Pattern.MULTILINE);
+
 
     private String message;
 
@@ -110,6 +112,10 @@ public class Commit extends Reference {
      * @return the committer
      */
     public String getCommitter() {
+        if (committer == null) {
+            getCommitInfo();
+        }
+
         return committer;
     }
 
@@ -128,6 +134,10 @@ public class Commit extends Reference {
      * @return the committer mail
      */
     public String getCommitterMail() {
+        if (committerMail == null) {
+            getCommitInfo();
+        }
+
         return committerMail;
     }
 
@@ -146,6 +156,10 @@ public class Commit extends Reference {
      * @return the committer time
      */
     public OffsetDateTime getCommitterTime() {
+        if (committerTime == null) {
+            getCommitInfo();
+        }
+
         return committerTime;
     }
 
@@ -164,6 +178,10 @@ public class Commit extends Reference {
      * @return the commit message
      */
     public String getMessage() {
+        if (message == null) {
+            getCommitInfo();
+        }
+
         return message;
     }
 
@@ -188,11 +206,11 @@ public class Commit extends Reference {
             }
 
             String result = execRes.stdOut;
-            Matcher matcher = AUTHOR_INFO.matcher(result);
+            Matcher matcher;
 
             if (author != null && authorMail != null && authorTime != null) {
                 LOG.finer("Skipping initialization author information. Already initialized.");
-            } else if (!matcher.find()) {
+            } else if (!(matcher = AUTHOR_INFO.matcher(result)).find()) {
                 LOG.warning(() -> String.format("Unexpected output while getting author info for %s.", this));
             } else {
                 author = matcher.group(1);
@@ -200,11 +218,9 @@ public class Commit extends Reference {
                 authorTime = OffsetDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong(matcher.group(3))), ZoneId.of(matcher.group(4)));
             }
 
-            matcher = COMMITTER_INFO.matcher(result);
-
             if (committer != null && committerMail != null && committerTime != null) {
                 LOG.finer("Skipping initialization committer information. Already initialized.");
-            } else if (!matcher.find()) {
+            } else if (!(matcher = COMMITTER_INFO.matcher(result)).find()) {
                 LOG.warning(() -> String.format("Unexpected output while getting committer info for %s.", this));
             } else {
                 committer = matcher.group(1);
@@ -212,7 +228,13 @@ public class Commit extends Reference {
                 committerTime = OffsetDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong(matcher.group(3))), ZoneId.of(matcher.group(4)));
             }
 
-            message = result.substring(result.lastIndexOf("\n"));
+            if (message != null) {
+                LOG.finer("Skipping initialization of commit message. Already initialized.");
+            } else if (!(matcher = EMPTY_LINE.matcher(result)).find()) {
+                LOG.warning(() -> String.format("Unexpected output while getting the message for %s.", this));
+            } else {
+                message = result.substring(matcher.end()).trim();
+            }
         });
     }
 }
-- 
GitLab