Add option to hide whitepace changes in diffs

Co-authored-by: Florian Scholdei<florian.scholdei@cloudogu.com>
Co-authored-by: René Pfeuffer<rene.pfeuffer@cloudogu.com>


Reviewed-by: Florian Scholdei <florian.scholdei@cloudogu.com>
This commit is contained in:
Viktor Egorov
2024-05-14 11:33:48 +02:00
parent e5e2fd151c
commit c8ef99cf07
37 changed files with 688 additions and 498 deletions

View File

@@ -25,6 +25,7 @@
package sonia.scm.repository.spi;
import org.junit.Test;
import sonia.scm.repository.api.IgnoreWhitespaceLevel;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -77,6 +78,20 @@ public class GitDiffCommandTest extends AbstractGitCommandTestBase {
" b\n" +
"+change\n";
public static final String DIFF_IGNORE_WHITESPACE = "diff --git a/a.txt b/a.txt\n" +
"index 2f8bc28..fc3f0ba 100644\n" +
"--- a/a.txt\n" +
"+++ b/a.txt\n";
public static final String DIFF_WITH_WHITESPACE = "diff --git a/a.txt b/a.txt\n" +
"index 2f8bc28..fc3f0ba 100644\n" +
"--- a/a.txt\n" +
"+++ b/a.txt\n" +
"@@ -1,2 +1,2 @@\n" +
" a\n" +
"-line for blame\n" +
"+line for blame\n";
@Test
public void diffForOneRevisionShouldCreateDiff() throws IOException {
GitDiffCommand gitDiffCommand = new GitDiffCommand(createContext());
@@ -97,6 +112,28 @@ public class GitDiffCommandTest extends AbstractGitCommandTestBase {
assertEquals(DIFF_FILE_A + DIFF_FILE_B, output.toString());
}
@Test
public void shouldIgnoreWhiteSpace() throws IOException {
GitDiffCommand gitDiffCommand = new GitDiffCommand(createContext());
DiffCommandRequest diffCommandRequest = new DiffCommandRequest();
diffCommandRequest.setIgnoreWhitespaceLevel(IgnoreWhitespaceLevel.ALL);
diffCommandRequest.setRevision("whitespace");
ByteArrayOutputStream output = new ByteArrayOutputStream();
gitDiffCommand.getDiffResult(diffCommandRequest).accept(output);
assertEquals(DIFF_IGNORE_WHITESPACE, output.toString());
}
@Test
public void shouldNotIgnoreWhiteSpace() throws IOException {
GitDiffCommand gitDiffCommand = new GitDiffCommand(createContext());
DiffCommandRequest diffCommandRequest = new DiffCommandRequest();
diffCommandRequest.setIgnoreWhitespaceLevel(IgnoreWhitespaceLevel.NONE);
diffCommandRequest.setRevision("whitespace");
ByteArrayOutputStream output = new ByteArrayOutputStream();
gitDiffCommand.getDiffResult(diffCommandRequest).accept(output);
assertEquals(DIFF_WITH_WHITESPACE, output.toString());
}
@Test
public void diffForPathShouldCreateLimitedDiff() throws IOException {
GitDiffCommand gitDiffCommand = new GitDiffCommand(createContext());
@@ -156,4 +193,9 @@ public class GitDiffCommandTest extends AbstractGitCommandTestBase {
.contains("rename from b.txt")
.contains("rename to b-copy.txt");
}
@Override
protected String getZippedRepositoryResource() {
return "sonia/scm/repository/spi/scm-git-spi-whitespace-test.zip";
}
}

View File

@@ -28,11 +28,14 @@ import org.junit.Test;
import sonia.scm.repository.api.DiffFile;
import sonia.scm.repository.api.DiffResult;
import sonia.scm.repository.api.Hunk;
import sonia.scm.repository.api.IgnoreWhitespaceLevel;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
public class GitDiffResultCommandTest extends AbstractGitCommandTestBase {
@@ -162,6 +165,48 @@ public class GitDiffResultCommandTest extends AbstractGitCommandTestBase {
assertThat(diffResult.getOffset()).isZero();
}
@Test
public void shouldIgnoreWhiteSpace() throws IOException {
GitDiffResultCommand gitDiffResultCommand = new GitDiffResultCommand(createContext());
DiffResultCommandRequest diffCommandRequest = new DiffResultCommandRequest();
diffCommandRequest.setRevision("whitespace");
diffCommandRequest.setIgnoreWhitespaceLevel(IgnoreWhitespaceLevel.ALL);
DiffResult diffResult = gitDiffResultCommand.getDiffResult(diffCommandRequest);
Iterator<DiffFile> iterator = diffResult.iterator();
DiffFile a = iterator.next();
Iterator<Hunk> hunks = a.iterator();
assertThat(hunks).isExhausted();
}
@Test
public void shouldNotIgnoreWhiteSpace() throws IOException {
GitDiffResultCommand gitDiffResultCommand = new GitDiffResultCommand(createContext());
DiffResultCommandRequest diffCommandRequest = new DiffResultCommandRequest();
diffCommandRequest.setRevision("whitespace");
diffCommandRequest.setIgnoreWhitespaceLevel(IgnoreWhitespaceLevel.NONE);
DiffResult diffResult = gitDiffResultCommand.getDiffResult(diffCommandRequest);
Iterator<DiffFile> iterator = diffResult.iterator();
DiffFile a = iterator.next();
Iterator<Hunk> hunks = a.iterator();
Hunk hunk = hunks.next();
assertThat(hunk.getOldStart()).isEqualTo(1);
assertThat(hunk.getOldLineCount()).isEqualTo(2);
assertThat(hunk.iterator())
.toIterable()
.extracting("content")
.containsExactly(
"a",
"line for blame",
"line for blame"
);
}
private DiffResult createDiffResult(String s) throws IOException {
return createDiffResult(s, null, null);
}
@@ -175,4 +220,9 @@ public class GitDiffResultCommandTest extends AbstractGitCommandTestBase {
return gitDiffResultCommand.getDiffResult(diffCommandRequest);
}
@Override
protected String getZippedRepositoryResource() {
return "sonia/scm/repository/spi/scm-git-spi-whitespace-test.zip";
}
}