Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jgit-dev] Ignore White Space in DiffFormatter

Hi all,

I have been experimenting with the DiffFormatter and the RawTextComparator.WS_IGNORE_ALL diff comparator. However, the results are not what I would expect.

I wrote a test case that diffs two commits, containing a single file with the same name whose content differs only by a trailing white space:

public class DiffIgnoreWhitespaceTest {

  @Rule
  public TemporaryFolder tempFolder = new TemporaryFolder();

  private Git git;

  @Before
  public void setUp() throws GitAPIException {
    git = Git.init().setDirectory( tempFolder.getRoot() ).call();
  }

  @After
  public void tearDown() {
    git.getRepository().close();
  }
  @Test
  public void ignoreWhitespace() throws Exception {
    RevCommit oldCommit = commitFile( "file.txt", "first line\n" );
    RevCommit newCommit = commitFile( "file.txt", "first line \n" );
    AbstractTreeIterator oldTreeIterator = getCanonicalTreeParser( oldCommit );
    AbstractTreeIterator newTreeIterator = getCanonicalTreeParser( newCommit );

    DiffFormatter diffFormatter = new DiffFormatter( NullOutputStream.INSTANCE );
    diffFormatter.setRepository( git.getRepository() );
    diffFormatter.setDiffComparator( RawTextComparator.WS_IGNORE_ALL );
    List<DiffEntry> diffEntries = diffFormatter.scan( oldTreeIterator, newTreeIterator );
    diffFormatter.close();

    assertEquals( 0, diffEntries.size() );
  }

  private RevCommit commitFile( String name, String content ) throws IOException, GitAPIException {
    writeFile( name, content );
    git.add().addFilepattern( name ).call();
    return git.commit().setMessage( "commit message" ).call();
  }

  private File writeFile( String name, String content ) throws IOException {
    File file = new File( git.getRepository().getWorkTree(), name );
    try( FileOutputStream outputStream = new FileOutputStream( file ) ) {
      outputStream.write( content.getBytes( UTF_8 ) );
    }
    return file;
  }

  private AbstractTreeIterator getCanonicalTreeParser( ObjectId commitId ) throws IOException {
    try( RevWalk walk = new RevWalk( git.getRepository() ) ) {
      RevCommit commit = walk.parseCommit( commitId );
      ObjectId treeId = commit.getTree().getId();
      try( ObjectReader reader = git.getRepository().newObjectReader() ) {
        return new CanonicalTreeParser( null, reader, treeId );
      }
    }
  }
  
}

I would expect that no diff entries are returned, but obviously, one diff entry is returned. Obtaining the edit list, reveals an empty list:
    EditList editList = diffFormatter.toFileHeader( diffEntry ).toEditList();

I haven't found a matching test case in the JGit test suite either. Could you clarify what is wrong here? Or is this behavior intended?

Thanks in advance,
Rüdiger

Back to the top