From c70fcb296f4524211df0bba58d60f7960be09c4d Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Mon, 31 Aug 2015 17:54:32 -0500 Subject: [PATCH] Add git hash (from refs/remotes/origin/master) to error reports. Signed-off-by: Jeffrey Han --- src/itdelatrisu/opsu/ErrorHandler.java | 6 ++++++ src/itdelatrisu/opsu/Utils.java | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/itdelatrisu/opsu/ErrorHandler.java b/src/itdelatrisu/opsu/ErrorHandler.java index bad2c513..1dc4d227 100644 --- a/src/itdelatrisu/opsu/ErrorHandler.java +++ b/src/itdelatrisu/opsu/ErrorHandler.java @@ -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"); diff --git a/src/itdelatrisu/opsu/Utils.java b/src/itdelatrisu/opsu/Utils.java index e20ab7e9..87c0749b 100644 --- a/src/itdelatrisu/opsu/Utils.java +++ b/src/itdelatrisu/opsu/Utils.java @@ -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; + } + } }