Add git hash (from refs/remotes/origin/master) to error reports.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-08-31 17:54:32 -05:00
parent e535a88840
commit c70fcb296f
2 changed files with 32 additions and 0 deletions

View File

@ -171,6 +171,12 @@ public class ErrorHandler {
if (version != null && !version.equals("${pom.version}")) {
sb.append("**Version:** ");
sb.append(version);
String hash = Utils.getGitHash();
if (hash != null) {
sb.append(" (");
sb.append(hash.substring(0, 12));
sb.append(')');
}
sb.append('\n');
}
String timestamp = props.getProperty("build.date");

View File

@ -32,6 +32,7 @@ import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@ -576,4 +577,29 @@ public class Utils {
public static boolean parseBoolean(String s) {
return (Integer.parseInt(s) == 1);
}
/**
* Returns the git hash of the remote-tracking branch 'origin/master' from the
* most recent update to the working directory (e.g. fetch or successful push).
* @return the 40-character SHA-1 hash, or null if it could not be determined
*/
public static String getGitHash() {
if (isJarRunning())
return null;
File f = new File(".git/refs/remotes/origin/master");
if (!f.isFile())
return null;
try (BufferedReader in = new BufferedReader(new FileReader(f))) {
char[] sha = new char[40];
if (in.read(sha, 0, sha.length) < sha.length)
return null;
for (int i = 0; i < sha.length; i++) {
if (Character.digit(sha[i], 16) == -1)
return null;
}
return String.valueOf(sha);
} catch (IOException e) {
return null;
}
}
}