summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/gitblit/utils
diff options
context:
space:
mode:
authorJames Moger <james.moger@gitblit.com>2015-05-22 13:39:17 -0400
committerJames Moger <james.moger@gitblit.com>2015-05-22 13:39:17 -0400
commit310a807f07ceeaa7d8fa0890d9061021707bbeb8 (patch)
treea8c375dc296fc857338034898cf3a14f7b7beafc /src/main/java/com/gitblit/utils
parent99450c557d895c94e7eec618218d0f4bc5c16ffd (diff)
downloadgitblit-310a807f07ceeaa7d8fa0890d9061021707bbeb8.tar.gz
gitblit-310a807f07ceeaa7d8fa0890d9061021707bbeb8.zip
Implement configurable tab length support for blob views
Diffstat (limited to 'src/main/java/com/gitblit/utils')
-rw-r--r--src/main/java/com/gitblit/utils/DiffUtils.java41
-rw-r--r--src/main/java/com/gitblit/utils/GitBlitDiffFormatter.java11
-rw-r--r--src/main/java/com/gitblit/utils/StringUtils.java17
3 files changed, 48 insertions, 21 deletions
diff --git a/src/main/java/com/gitblit/utils/DiffUtils.java b/src/main/java/com/gitblit/utils/DiffUtils.java
index 09b8f801..cdebec1b 100644
--- a/src/main/java/com/gitblit/utils/DiffUtils.java
+++ b/src/main/java/com/gitblit/utils/DiffUtils.java
@@ -229,11 +229,12 @@ public class DiffUtils {
* @param commit
* @param comparator
* @param outputType
+ * @param tabLength
* @return the diff
*/
public static DiffOutput getCommitDiff(Repository repository, RevCommit commit,
- DiffComparator comparator, DiffOutputType outputType) {
- return getDiff(repository, null, commit, null, comparator, outputType);
+ DiffComparator comparator, DiffOutputType outputType, int tabLength) {
+ return getDiff(repository, null, commit, null, comparator, outputType, tabLength);
}
/**
@@ -246,11 +247,12 @@ public class DiffUtils {
* @param handler
* to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.
* May be {@code null}, resulting in the default behavior.
+ * @param tabLength
* @return the diff
*/
public static DiffOutput getCommitDiff(Repository repository, RevCommit commit,
- DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler) {
- return getDiff(repository, null, commit, null, comparator, outputType, handler);
+ DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler, int tabLength) {
+ return getDiff(repository, null, commit, null, comparator, outputType, handler, tabLength);
}
@@ -263,11 +265,12 @@ public class DiffUtils {
* @param path
* @param comparator
* @param outputType
+ * @param tabLength
* @return the diff
*/
public static DiffOutput getDiff(Repository repository, RevCommit commit, String path,
- DiffComparator comparator, DiffOutputType outputType) {
- return getDiff(repository, null, commit, path, comparator, outputType);
+ DiffComparator comparator, DiffOutputType outputType, int tabLength) {
+ return getDiff(repository, null, commit, path, comparator, outputType, tabLength);
}
/**
@@ -282,11 +285,12 @@ public class DiffUtils {
* @param handler
* to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.
* May be {@code null}, resulting in the default behavior.
+ * @param tabLength
* @return the diff
*/
public static DiffOutput getDiff(Repository repository, RevCommit commit, String path,
- DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler) {
- return getDiff(repository, null, commit, path, comparator, outputType, handler);
+ DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler, int tabLength) {
+ return getDiff(repository, null, commit, path, comparator, outputType, handler, tabLength);
}
/**
@@ -297,11 +301,13 @@ public class DiffUtils {
* @param commit
* @param comparator
* @param outputType
+ * @param tabLength
+ *
* @return the diff
*/
public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit,
- DiffComparator comparator, DiffOutputType outputType) {
- return getDiff(repository, baseCommit, commit, null, comparator, outputType);
+ DiffComparator comparator, DiffOutputType outputType, int tabLength) {
+ return getDiff(repository, baseCommit, commit, null, comparator, outputType, tabLength);
}
/**
@@ -315,11 +321,12 @@ public class DiffUtils {
* @param handler
* to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.
* May be {@code null}, resulting in the default behavior.
+ * @param tabLength
* @return the diff
*/
public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit,
- DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler) {
- return getDiff(repository, baseCommit, commit, null, comparator, outputType, handler);
+ DiffComparator comparator, DiffOutputType outputType, BinaryDiffHandler handler, int tabLength) {
+ return getDiff(repository, baseCommit, commit, null, comparator, outputType, handler, tabLength);
}
/**
@@ -335,11 +342,12 @@ public class DiffUtils {
* or folder. if unspecified, the diff is for the entire commit.
* @param outputType
* @param diffComparator
+ * @param tabLength
* @return the diff
*/
public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit,
- String path, DiffComparator diffComparator, DiffOutputType outputType) {
- return getDiff(repository, baseCommit, commit, path, diffComparator, outputType, null);
+ String path, DiffComparator diffComparator, DiffOutputType outputType, int tabLength) {
+ return getDiff(repository, baseCommit, commit, path, diffComparator, outputType, null, tabLength);
}
/**
@@ -358,10 +366,11 @@ public class DiffUtils {
* @param handler
* to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}.
* May be {@code null}, resulting in the default behavior.
+ * @param tabLength
* @return the diff
*/
public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit, String path,
- DiffComparator comparator, DiffOutputType outputType, final BinaryDiffHandler handler) {
+ DiffComparator comparator, DiffOutputType outputType, final BinaryDiffHandler handler, int tabLength) {
DiffStat stat = null;
String diff = null;
try {
@@ -370,7 +379,7 @@ public class DiffUtils {
DiffFormatter df;
switch (outputType) {
case HTML:
- df = new GitBlitDiffFormatter(commit.getName(), path, handler);
+ df = new GitBlitDiffFormatter(commit.getName(), path, handler, tabLength);
break;
case PLAIN:
default:
diff --git a/src/main/java/com/gitblit/utils/GitBlitDiffFormatter.java b/src/main/java/com/gitblit/utils/GitBlitDiffFormatter.java
index 35549d4e..86b7ca2e 100644
--- a/src/main/java/com/gitblit/utils/GitBlitDiffFormatter.java
+++ b/src/main/java/com/gitblit/utils/GitBlitDiffFormatter.java
@@ -127,6 +127,8 @@ public class GitBlitDiffFormatter extends DiffFormatter {
/** If {@link #truncated}, contains all entries skipped. */
private final List<DiffEntry> skipped = new ArrayList<DiffEntry>();
+ private int tabLength;
+
/**
* A {@link ResettableByteArrayOutputStream} that intercept the "Binary files differ" message produced
* by the super implementation. Unfortunately the super implementation has far too many things private;
@@ -162,11 +164,12 @@ public class GitBlitDiffFormatter extends DiffFormatter {
}
- public GitBlitDiffFormatter(String commitId, String path, BinaryDiffHandler handler) {
+ public GitBlitDiffFormatter(String commitId, String path, BinaryDiffHandler handler, int tabLength) {
super(new DiffOutputStream());
this.os = (DiffOutputStream) getOutputStream();
this.os.setFormatter(this, handler);
this.diffStat = new DiffStat(commitId);
+ this.tabLength = tabLength;
// If we have a full commitdiff, install maxima to avoid generating a super-long diff listing that
// will only tax the browser too much.
maxDiffLinesPerFile = path != null ? -1 : getLimit(DIFF_LIMIT_PER_FILE_KEY, 500, DIFF_LIMIT_PER_FILE);
@@ -453,14 +456,14 @@ public class GitBlitDiffFormatter extends DiffFormatter {
// Highlight trailing whitespace on deleted/added lines.
Matcher matcher = trailingWhitespace.matcher(line);
if (matcher.find()) {
- StringBuilder result = new StringBuilder(StringUtils.escapeForHtml(line.substring(0, matcher.start()), CONVERT_TABS));
+ StringBuilder result = new StringBuilder(StringUtils.escapeForHtml(line.substring(0, matcher.start()), CONVERT_TABS, tabLength));
result.append("<span class='trailingws-").append(prefix == '+' ? "add" : "sub").append("'>");
result.append(StringUtils.escapeForHtml(matcher.group(1), false));
result.append("</span>");
return result.toString();
}
}
- return StringUtils.escapeForHtml(line, CONVERT_TABS);
+ return StringUtils.escapeForHtml(line, CONVERT_TABS, tabLength);
}
/**
@@ -493,7 +496,7 @@ public class GitBlitDiffFormatter extends DiffFormatter {
} else {
sb.append("<th class='diff-state diff-state-sub'></th><td class=\"diff-cell remove2\">");
}
- line = StringUtils.escapeForHtml(line.substring(1), CONVERT_TABS);
+ line = StringUtils.escapeForHtml(line.substring(1), CONVERT_TABS, tabLength);
}
sb.append(line);
if (gitLinkDiff) {
diff --git a/src/main/java/com/gitblit/utils/StringUtils.java b/src/main/java/com/gitblit/utils/StringUtils.java
index 087de543..643c52c3 100644
--- a/src/main/java/com/gitblit/utils/StringUtils.java
+++ b/src/main/java/com/gitblit/utils/StringUtils.java
@@ -79,6 +79,19 @@ public class StringUtils {
* @return plain text escaped for html
*/
public static String escapeForHtml(String inStr, boolean changeSpace) {
+ return escapeForHtml(inStr, changeSpace, 4);
+ }
+
+ /**
+ * Prepare text for html presentation. Replace sensitive characters with
+ * html entities.
+ *
+ * @param inStr
+ * @param changeSpace
+ * @param tabLength
+ * @return plain text escaped for html
+ */
+ public static String escapeForHtml(String inStr, boolean changeSpace, int tabLength) {
StringBuilder retStr = new StringBuilder();
int i = 0;
while (i < inStr.length()) {
@@ -93,7 +106,9 @@ public class StringUtils {
} else if (changeSpace && inStr.charAt(i) == ' ') {
retStr.append("&nbsp;");
} else if (changeSpace && inStr.charAt(i) == '\t') {
- retStr.append(" &nbsp; &nbsp;");
+ for (int j = 0; j < tabLength; j++) {
+ retStr.append("&nbsp;");
+ }
} else {
retStr.append(inStr.charAt(i));
}