Skip to content
Snippets Groups Projects
Commit a3e4f1dd authored by Florian Heck's avatar Florian Heck
Browse files

added getter for commit information

parent f55c469a
No related branches found
No related tags found
No related merge requests found
......@@ -16,11 +16,19 @@ public class Commit {
private static final Logger LOG = Logger.getLogger(Commit.class.getCanonicalName());
// since there is no porcelain format for this command, this regex is might depend on the git version
private static final Pattern commitInfoPattern = Pattern.compile("tree (.{40})\\nparent (.{40})\\nauthor (.*?)<(.*?)> (\\d+) ([+-]\\d{4})\\ncommitter (.*?)<(.*?)> (\\d+) ([+-]\\d{4})\\n.*?\\n(.*)");
private GitWrapper git;
private Repository repo;
private String id;
private String author;
private String authorMail;
private Date authorTime;
/**
* Constructs a new {@link Commit} with the given <code>id</code> made in the <code>repo</code>.
*
......@@ -100,6 +108,67 @@ public class Commit {
return id;
}
/**
* Returns the author of this commit.
*
* @return the author
*/
public String getAuthor() {
if (author == null) {
getCommitInfo();
}
return author;
}
/**
* Returns the author mail of this commit.
*
* @return the author mail
*/
public String getAuthorMail() {
if (authorMail == null) {
getCommitInfo();
}
return authorMail;
}
/**
* Returns the author time of this commit in committer-local time
*
* @return the author time
*/
public Date getAuthorTime() {
if (authorTime == null) {
getCommitInfo();
}
return authorTime;
}
/**
* Reads the author information (author, author mail, author time) about this commit and saves it in the
* {@link Commit}.
*/
private void getCommitInfo() {
Optional<ExecRes> commitInfo = git.exec(repo.getDir(), "cat-file", "-p", id);
commitInfo.map(execRes -> {
if (git.failed(execRes)) {
LOG.warning(() -> String.format("Failed to obtain information about commit %s.", this));
return null;
}
String result = execRes.stdOut.trim();
Matcher matcher = commitInfoPattern.matcher(result);
if (!matcher.find()) {
LOG.warning(() -> String.format("Unexpected output while getting commit info for %s.", this));
} else {
author = matcher.group(3);
authorMail = matcher.group(4);
authorTime = new Date(Long.parseLong(matcher.group(5)));
}
return null;
});
}
@Override
public boolean equals(Object o) {
if (this == o) {
......
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