IDEA-212048 vcs: trim commit messages for enormous commits

GitOrigin-RevId: 08c2fb9d4301e77ecb3024181a2884ad2ffe425e
This commit is contained in:
Aleksey Pivovarov
2019-07-03 18:50:15 +03:00
committed by intellij-monorepo-bot
parent 80b690e748
commit cc7150b0d4
5 changed files with 41 additions and 2 deletions
@@ -291,7 +291,7 @@ public class AnnotateStackTraceAction extends DumbAwareAction {
if (revision != null) {
return XmlStringUtil.escapeString(
revision.getAuthor() + " " + DateFormatUtil.formatDateTime(revision.getDate()) + "\n" +
revision.getMessage()
VcsUtil.trimCommitMessageToSaneSize(revision.getMessage())
);
}
return null;
@@ -19,6 +19,7 @@ import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileDocumentManager;
@@ -64,6 +65,9 @@ public class VcsUtil {
@NotNull private static final VcsRoot FICTIVE_ROOT = new VcsRoot(null, null);
private static final int MAX_COMMIT_MESSAGE_LENGTH = 50000;
private static final int MAX_COMMIT_MESSAGE_LINES = 3000;
public static int getMaxVcsLoadedFileSize() {
return ourMaxLoadedFileSize;
}
@@ -603,4 +607,34 @@ public class VcsUtil {
.collect(Collectors.toSet());
}
@NotNull
public static String trimCommitMessageToSaneSize(@NotNull String message) {
int nthLine = nthIndexOf(message, '\n', MAX_COMMIT_MESSAGE_LINES);
if (nthLine != -1 && nthLine < MAX_COMMIT_MESSAGE_LENGTH) {
return trimCommitMessageAt(message, nthLine);
}
if (message.length() > MAX_COMMIT_MESSAGE_LENGTH + 50) {
return trimCommitMessageAt(message, MAX_COMMIT_MESSAGE_LENGTH);
}
return message;
}
private static String trimCommitMessageAt(@NotNull String message, int index) {
return String.format("%s\n\n... Commit message is too long and was truncated by %s ...",
message.substring(0, index),
ApplicationNamesInfo.getInstance().getProductName());
}
private static int nthIndexOf(@NotNull String text, char c, int n) {
assert n > 0;
int length = text.length();
int count = 0;
for (int i = 0; i < length; i++) {
if (text.charAt(i) == c) {
count++;
if (count == n) return i;
}
}
return -1;
}
}
@@ -21,6 +21,7 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.IssueNavigationConfiguration;
import com.intellij.util.containers.Convertor;
import com.intellij.util.ui.UIUtil;
import com.intellij.vcsUtil.VcsUtil;
import com.intellij.xml.util.XmlStringUtil;
import java.util.List;
@@ -40,7 +41,7 @@ public class IssueLinkHtmlRenderer {
@SuppressWarnings({"HardCodedStringLiteral"})
public static String formatTextWithLinks(Project project, String str, Convertor<? super String, String> convertor) {
if (StringUtil.isEmpty(str)) return "";
String comment = XmlStringUtil.escapeString(str, false);
String comment = XmlStringUtil.escapeString(VcsUtil.trimCommitMessageToSaneSize(str), false);
StringBuilder commentBuilder = new StringBuilder();
IssueNavigationConfiguration config = IssueNavigationConfiguration.getInstance(project);
@@ -20,6 +20,7 @@ import com.intellij.vcs.log.VcsCommitMetadata;
import com.intellij.vcs.log.VcsShortCommitDetails;
import com.intellij.vcs.log.VcsUser;
import com.intellij.vcs.log.util.VcsUserUtil;
import com.intellij.vcsUtil.VcsUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -128,6 +129,8 @@ public class CommitPresentationUtil {
private static String formatCommitText(@NotNull Project project,
@NotNull String fullMessage,
@NotNull Set<String> resolvedHashes) {
fullMessage = VcsUtil.trimCommitMessageToSaneSize(fullMessage);
Font font = getCommitMessageFont();
Convertor<String, String> convertor = s -> replaceHashes(s, resolvedHashes);
@@ -164,6 +164,7 @@ public class GitFileAnnotation extends FileAnnotation {
String commitMessage = getCommitMessage(revisionNumber);
if (commitMessage == null) commitMessage = lineInfo.getSubject() + "\n...";
commitMessage = VcsUtil.trimCommitMessageToSaneSize(commitMessage);
return "commit " + revisionNumber.asString() +
"\nAuthor: " + lineInfo.getAuthor() +