Skip to content
Snippets Groups Projects
Commit 6a89c494 authored by Georg Seibt's avatar Georg Seibt :nerd:
Browse files

correctly parse the message (by finding the first empty line in the cat-file...

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
parent 3d578f5b
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,8 @@ public class Commit extends Reference { ...@@ -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 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 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 COMMITTER_INFO = Pattern.compile("committer (.*?)<(.*?)> (\\d+) ([+-]\\d{4})\\n");
private static final Pattern EMPTY_LINE = Pattern.compile("^\\s*$", Pattern.MULTILINE);
private String message; private String message;
...@@ -110,6 +112,10 @@ public class Commit extends Reference { ...@@ -110,6 +112,10 @@ public class Commit extends Reference {
* @return the committer * @return the committer
*/ */
public String getCommitter() { public String getCommitter() {
if (committer == null) {
getCommitInfo();
}
return committer; return committer;
} }
...@@ -128,6 +134,10 @@ public class Commit extends Reference { ...@@ -128,6 +134,10 @@ public class Commit extends Reference {
* @return the committer mail * @return the committer mail
*/ */
public String getCommitterMail() { public String getCommitterMail() {
if (committerMail == null) {
getCommitInfo();
}
return committerMail; return committerMail;
} }
...@@ -146,6 +156,10 @@ public class Commit extends Reference { ...@@ -146,6 +156,10 @@ public class Commit extends Reference {
* @return the committer time * @return the committer time
*/ */
public OffsetDateTime getCommitterTime() { public OffsetDateTime getCommitterTime() {
if (committerTime == null) {
getCommitInfo();
}
return committerTime; return committerTime;
} }
...@@ -164,6 +178,10 @@ public class Commit extends Reference { ...@@ -164,6 +178,10 @@ public class Commit extends Reference {
* @return the commit message * @return the commit message
*/ */
public String getMessage() { public String getMessage() {
if (message == null) {
getCommitInfo();
}
return message; return message;
} }
...@@ -188,11 +206,11 @@ public class Commit extends Reference { ...@@ -188,11 +206,11 @@ public class Commit extends Reference {
} }
String result = execRes.stdOut; String result = execRes.stdOut;
Matcher matcher = AUTHOR_INFO.matcher(result); Matcher matcher;
if (author != null && authorMail != null && authorTime != null) { if (author != null && authorMail != null && authorTime != null) {
LOG.finer("Skipping initialization author information. Already initialized."); 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)); LOG.warning(() -> String.format("Unexpected output while getting author info for %s.", this));
} else { } else {
author = matcher.group(1); author = matcher.group(1);
...@@ -200,11 +218,9 @@ public class Commit extends Reference { ...@@ -200,11 +218,9 @@ public class Commit extends Reference {
authorTime = OffsetDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong(matcher.group(3))), ZoneId.of(matcher.group(4))); 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) { if (committer != null && committerMail != null && committerTime != null) {
LOG.finer("Skipping initialization committer information. Already initialized."); 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)); LOG.warning(() -> String.format("Unexpected output while getting committer info for %s.", this));
} else { } else {
committer = matcher.group(1); committer = matcher.group(1);
...@@ -212,7 +228,13 @@ public class Commit extends Reference { ...@@ -212,7 +228,13 @@ public class Commit extends Reference {
committerTime = OffsetDateTime.ofInstant(Instant.ofEpochSecond(Long.parseLong(matcher.group(3))), ZoneId.of(matcher.group(4))); 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();
}
}); });
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment