From ddef5a2f929240905723ca7c527b6cb7edeb9eb9 Mon Sep 17 00:00:00 2001 From: Kirill Likhodedov Date: Sat, 9 May 2015 11:38:36 +0300 Subject: [PATCH] [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. --- .../dvcs-impl/src/com/intellij/dvcs/DvcsUtil.java | 13 +++++++------ .../git4idea/src/git4idea/branch/GitBranchUtil.java | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/platform/dvcs-impl/src/com/intellij/dvcs/DvcsUtil.java b/platform/dvcs-impl/src/com/intellij/dvcs/DvcsUtil.java index 47303f694796..96bba328b8c9 100644 --- a/platform/dvcs-impl/src/com/intellij/dvcs/DvcsUtil.java +++ b/platform/dvcs-impl/src/com/intellij/dvcs/DvcsUtil.java @@ -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 diff --git a/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java b/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java index ceddd8fca5aa..e5cbd84cde27 100644 --- a/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java +++ b/plugins/git4idea/src/git4idea/branch/GitBranchUtil.java @@ -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 = "";