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

@@ -28,8 +28,10 @@ import com.google.inject.assistedinject.Assisted;
import jakarta.inject.Inject;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.util.QuotedString;
import sonia.scm.repository.api.DiffCommandBuilder;
import sonia.scm.repository.api.IgnoreWhitespaceLevel;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
@@ -49,13 +51,16 @@ public class GitDiffCommand extends AbstractGitCommand implements DiffCommand {
@Override
public DiffCommandBuilder.OutputStreamConsumer getDiffResult(DiffCommandRequest request) throws IOException {
@SuppressWarnings("squid:S2095") // repository will be closed with the RepositoryService
org.eclipse.jgit.lib.Repository repository = open();
org.eclipse.jgit.lib.Repository repository = open();
Differ.Diff diff = Differ.diff(repository, request);
return output -> {
try (DiffFormatter formatter = new DiffFormatter(new DequoteOutputStream(output))) {
formatter.setRepository(repository);
if (request.getIgnoreWhitespaceLevel() == IgnoreWhitespaceLevel.ALL) {
formatter.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
}
for (DiffEntry e : diff.getEntries()) {
if (idOrPathChanged(e)) {

View File

@@ -26,6 +26,7 @@ package sonia.scm.repository.spi;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.lib.ObjectId;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.InternalRepositoryException;
@@ -33,6 +34,7 @@ import sonia.scm.repository.Repository;
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;
@@ -51,14 +53,21 @@ public class GitDiffResult implements DiffResult {
private final Differ.Diff diff;
private final List<DiffEntry> diffEntries;
private final IgnoreWhitespaceLevel ignoreWhitespaceLevel;
private final int offset;
private final Integer limit;
public GitDiffResult(Repository scmRepository, org.eclipse.jgit.lib.Repository repository, Differ.Diff diff, int offset, Integer limit) {
public GitDiffResult(Repository scmRepository,
org.eclipse.jgit.lib.Repository repository,
Differ.Diff diff,
IgnoreWhitespaceLevel ignoreWhitespaceLevel,
int offset,
Integer limit) {
this.scmRepository = scmRepository;
this.repository = repository;
this.diff = diff;
this.offset = offset;
this.ignoreWhitespaceLevel = ignoreWhitespaceLevel;
this.limit = limit;
this.diffEntries = diff.getEntries();
}
@@ -163,6 +172,9 @@ public class GitDiffResult implements DiffResult {
private String format(org.eclipse.jgit.lib.Repository repository, DiffEntry entry) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); DiffFormatter formatter = new DiffFormatter(baos)) {
if (ignoreWhitespaceLevel == IgnoreWhitespaceLevel.ALL) {
formatter.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
}
formatter.setRepository(repository);
formatter.format(entry);
return baos.toString(StandardCharsets.UTF_8);
@@ -170,6 +182,10 @@ public class GitDiffResult implements DiffResult {
throw new InternalRepositoryException(scmRepository, "failed to format diff entry", ex);
}
}
}
@Override
public IgnoreWhitespaceLevel getIgnoreWhitespace() {
return ignoreWhitespaceLevel;
}
}

View File

@@ -40,9 +40,16 @@ public class GitDiffResultCommand extends AbstractGitCommand implements DiffResu
super(context);
}
public DiffResult getDiffResult(DiffCommandRequest diffCommandRequest) throws IOException {
public DiffResult getDiffResult(DiffCommandRequest request) throws IOException {
org.eclipse.jgit.lib.Repository repository = open();
return new GitDiffResult(this.repository, repository, Differ.diff(repository, diffCommandRequest), 0, null);
return new GitDiffResult(
this.repository,
repository,
Differ.diff(repository, request),
request.getIgnoreWhitespaceLevel(),
0,
null
);
}
@Override
@@ -50,7 +57,14 @@ public class GitDiffResultCommand extends AbstractGitCommand implements DiffResu
org.eclipse.jgit.lib.Repository repository = open();
int offset = request.getOffset() == null ? 0 : request.getOffset();
try {
return new GitDiffResult(this.repository, repository, Differ.diff(repository, request), offset, request.getLimit());
return new GitDiffResult(
this.repository,
repository,
Differ.diff(repository, request),
request.getIgnoreWhitespaceLevel(),
offset,
request.getLimit()
);
} catch (AmbiguousObjectException ex) {
throw new NotUniqueRevisionException(Repository.class, context.getRepository().getId());
}

View File

@@ -41,6 +41,7 @@ import sonia.scm.repository.Modifications;
import sonia.scm.repository.Modified;
import sonia.scm.repository.Removed;
import sonia.scm.repository.Renamed;
import sonia.scm.repository.api.IgnoreWhitespaceLevel;
import java.io.IOException;
import java.text.MessageFormat;

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";
}
}

View File

@@ -32,6 +32,7 @@ import jakarta.inject.Inject;
import org.javahg.Repository;
import sonia.scm.repository.api.DiffCommandBuilder;
import sonia.scm.repository.api.DiffFormat;
import sonia.scm.repository.api.IgnoreWhitespaceLevel;
import sonia.scm.repository.spi.javahg.HgDiffInternalCommand;
import sonia.scm.web.HgUtil;
@@ -82,6 +83,9 @@ public class HgDiffCommand extends AbstractCommand implements DiffCommand {
cmd.git();
}
String revision = HgUtil.getRevision(request.getRevision());
if (request.getIgnoreWhitespaceLevel() == IgnoreWhitespaceLevel.ALL) {
cmd.cmdAppend("-w");
}
if (request.getAncestorChangeset() != null) {
String ancestor = HgUtil.getRevision(request.getAncestorChangeset());
cmd.cmdAppend(String.format("-r ancestor(%s,%s):%s", ancestor, revision, revision));

View File

@@ -27,6 +27,7 @@ package sonia.scm.repository.spi;
import org.junit.Test;
import sonia.scm.repository.api.DiffCommandBuilder;
import sonia.scm.repository.api.DiffFormat;
import sonia.scm.repository.api.IgnoreWhitespaceLevel;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -66,7 +67,7 @@ public class HgDiffCommandTest extends AbstractHgCommandTestBase {
}
@Test
public void shouldNotCloseInternalStream() throws IOException {
public void shouldNotCloseInternalStream() {
HgCommandContext context = spy(cmdContext);
DiffCommandRequest request = new DiffCommandRequest();
request.setRevision("3049df33fdbbded08b707bac3eccd0f7b453c58b");
@@ -106,6 +107,25 @@ public class HgDiffCommandTest extends AbstractHgCommandTestBase {
verify(context).close();
}
@Test
public void shouldNotIgnoreWhitespaceInDefaultDiff() throws IOException {
String content = diff(cmdContext, "2b6f8a90b33f");
assertThat(content).contains("""
@@ -1,2 +1,2 @@
a
-line for blame
+line for blame""");
}
@Test
public void shouldIgnoreWhitespaceInDiff() throws IOException {
DiffCommandRequest request = new DiffCommandRequest();
request.setIgnoreWhitespaceLevel(IgnoreWhitespaceLevel.ALL);
request.setRevision("2b6f8a90b33f");
String content = diff(cmdContext, request);
assertThat(content).isEmpty();
}
private String diff(HgCommandContext context, String revision) throws IOException {
DiffCommandRequest request = new DiffCommandRequest();
request.setRevision(revision);
@@ -124,4 +144,8 @@ public class HgDiffCommandTest extends AbstractHgCommandTestBase {
return baos.toString(UTF_8);
}
@Override
protected String getZippedRepositoryResource() {
return "sonia/scm/repository/spi/scm-hg-spi-diff-test.zip";
}
}

View File

@@ -34,17 +34,18 @@ import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.internal.wc2.ng.SvnNewDiffGenerator;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNDiffClient;
import org.tmatesoft.svn.core.wc.SVNDiffOptions;
import org.tmatesoft.svn.core.wc.SVNRevision;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.SvnUtil;
import sonia.scm.repository.api.DiffCommandBuilder;
import sonia.scm.repository.api.DiffFormat;
import sonia.scm.repository.api.IgnoreWhitespaceLevel;
import sonia.scm.util.Util;
public class SvnDiffCommand extends AbstractSvnCommand implements DiffCommand {
private static final Logger logger =
LoggerFactory.getLogger(SvnDiffCommand.class);
@@ -67,8 +68,11 @@ public class SvnDiffCommand extends AbstractSvnCommand implements DiffCommand {
}
clientManager = SVNClientManager.newInstance();
SVNDiffClient diffClient = clientManager.getDiffClient();
diffClient.setDiffGenerator(new SvnNewDiffGenerator(new SCMSvnDiffGenerator()));
SCMSvnDiffGenerator generator = new SCMSvnDiffGenerator();
diffClient.setDiffGenerator(new SvnNewDiffGenerator(generator));
if (request.getIgnoreWhitespaceLevel() == IgnoreWhitespaceLevel.ALL) {
generator.setDiffOptions(new SVNDiffOptions(true, true, true));
}
long currentRev = SvnUtil.getRevisionNumber(request.getRevision(), repository);
diffClient.setGitDiffFormat(request.getFormat() == DiffFormat.GIT);

View File

@@ -38,10 +38,12 @@ import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNRevision;
import sonia.scm.repository.RepositoryTestData;
import sonia.scm.repository.api.DiffFormat;
import sonia.scm.repository.api.IgnoreWhitespaceLevel;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;
import java.util.Map;
@@ -64,6 +66,47 @@ class SvnDiffCommandTest {
workingCopy = directory.resolve("working-copy").toFile();
}
@Test
void shouldCreateDiffForSimpleFile() throws SVNException, IOException {
createRepository();
Path newFile = workingCopy.toPath().resolve("a.txt");
Files.write(newFile, "Some nice content\n".getBytes());
client.getWCClient().doAdd(newFile.toFile(), false, false, false, SVNDepth.INFINITY, false, false);
commit("add a.txt");
Files.write(newFile, "Some more content\n".getBytes());
commit("modify a.txt");
String diff = gitDiff("2");
assertThat(diff).isEqualTo("""
diff --git a/a.txt b/a.txt
--- a/a.txt
+++ b/a.txt
@@ -1 +1 @@
-Some nice content
+Some more content
""");
}
@Test
void shouldIgnoreWhitespaceChanges() throws SVNException, IOException {
createRepository();
Path newFile = workingCopy.toPath().resolve("a.txt");
Files.write(newFile, "Some nice content\n".getBytes());
client.getWCClient().doAdd(newFile.toFile(), false, false, false, SVNDepth.INFINITY, false, false);
commit("add a.txt");
Files.write(newFile, "Some nice content \n".getBytes());
commit("modify a.txt");
DiffCommandRequest request = createSimpleDiffRequest("2");
request.setIgnoreWhitespaceLevel(IgnoreWhitespaceLevel.ALL);
String diff = executeDiff(request);
assertThat(diff).isEqualTo("diff --git a/a.txt b/a.txt\n");
}
@Test
void shouldCreateGitCompatibleDiffForSinglePropChanges() throws SVNException, IOException {
createRepository();
@@ -150,16 +193,25 @@ class SvnDiffCommandTest {
@Nonnull
private String gitDiff(String revision) throws IOException {
DiffCommandRequest request = createSimpleDiffRequest(revision);
return executeDiff(request);
}
private String executeDiff(DiffCommandRequest request) throws IOException {
SvnDiffCommand command = createCommand();
DiffCommandRequest request = new DiffCommandRequest();
request.setFormat(DiffFormat.GIT);
request.setRevision(revision);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
command.getDiffResult(request).accept(baos);
return baos.toString();
}
private static DiffCommandRequest createSimpleDiffRequest(String revision) {
DiffCommandRequest request = new DiffCommandRequest();
request.setFormat(DiffFormat.GIT);
request.setRevision(revision);
return request;
}
private SvnDiffCommand createCommand() {
return new SvnDiffCommand(new SvnContext(RepositoryTestData.createHeartOfGold(), repository));
}