mirror of
				https://github.com/scm-manager/scm-manager.git
				synced 2025-10-31 10:35:56 +01:00 
			
		
		
		
	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:
		| @@ -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); | ||||
|   | ||||
| @@ -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)); | ||||
|   } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user