[git] IDEA-140051 Avoid SIOOBE is the hash is too short for some reason

+ remove some obsolete code;
+ add diagnostics for hashes with unexpected length.
This commit is contained in:
Kirill Likhodedov
2015-05-09 11:39:15 +03:00
parent 5299f79be3
commit ddef5a2f92
2 changed files with 8 additions and 7 deletions
@@ -71,6 +71,7 @@ public class DvcsUtil {
private static final Logger LOGGER = Logger.getInstance(DvcsUtil.class);
private static final int IO_RETRIES = 3; // number of retries before fail if an IOException happens during file read.
private static final int SHORT_HASH_LENGTH = 8;
private static final int LONG_HASH_LENGTH = 40;
public static void installStatusBarWidget(@NotNull Project project, @NotNull StatusBarWidget widget) {
StatusBar statusBar = WindowManager.getInstance().getStatusBar(project);
@@ -163,13 +164,13 @@ public class DvcsUtil {
@NotNull
public static String getShortHash(@NotNull String hash) {
if (hash.length() == 0) return "";
if (hash.length() == 40) return hash.substring(0, SHORT_HASH_LENGTH);
if (hash.length() > 40) // revision string encoded with date too
{
return hash.substring(hash.indexOf("[") + 1, SHORT_HASH_LENGTH);
if (hash.length() < SHORT_HASH_LENGTH) {
LOG.debug("Unexpectedly short hash: [" + hash + "]");
}
return hash;
if (hash.length() > LONG_HASH_LENGTH) {
LOG.debug("Unexpectedly long hash: [" + hash + "]");
}
return hash.substring(0, Math.min(SHORT_HASH_LENGTH, hash.length()));
}
@NotNull
@@ -271,7 +271,7 @@ public class GitBranchUtil {
if (state == GitRepository.State.DETACHED) {
String currentRevision = repository.getCurrentRevision();
assert currentRevision != null : "Current revision can't be null in DETACHED state, only on the fresh repository.";
return currentRevision.substring(0, 7);
return DvcsUtil.getShortHash(currentRevision);
}
String prefix = "";