Skip to content

Commit

Permalink
[SCM-991] GitDiffConsumer cannot parse moved files
Browse files Browse the repository at this point in the history
This closes #151
  • Loading branch information
JnRouvignac authored and michael-o committed Jul 6, 2022
1 parent b04525f commit 07d51d3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public class GitDiffConsumer

private static final String INDEX_LINE_TOKEN = "index ";

private static final String SIMILARITY_INDEX_LINE_TOKEN = "similarity index ";

private static final String RENAME_FROM_LINE_TOKEN = "rename from ";

private static final String RENAME_TO_LINE_TOKEN = "rename to ";

private static final String NEW_FILE_MODE_TOKEN = "new file mode ";

private static final String DELETED_FILE_MODE_TOKEN = "deleted file mode ";
Expand Down Expand Up @@ -149,6 +155,16 @@ else if ( line.startsWith( END_REVISION_TOKEN ) )
// skip, though could parse to verify filename, end revision
patch.append( line ).append( "\n" );
}
else if ( line.startsWith( SIMILARITY_INDEX_LINE_TOKEN ) )
{
// skip
patch.append( line ).append( "\n" );
}
else if ( line.startsWith( RENAME_FROM_LINE_TOKEN ) || line.startsWith( RENAME_TO_LINE_TOKEN ) )
{
// skip, though could parse to verify filename
patch.append( line ).append( "\n" );
}
else if ( line.startsWith( ADDED_LINE_TOKEN ) || line.startsWith( REMOVED_LINE_TOKEN )
|| line.startsWith( UNCHANGED_LINE_TOKEN ) || line.startsWith( CHANGE_SEPARATOR_TOKEN )
|| line.equals( NO_NEWLINE_TOKEN ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,54 @@ public void testLog2Consumer()
assertTrue( addDiffs.indexOf( "+maven-scm git provider works fine :-)" ) >= 0 );
}

@Test
public void testLog3Consumer()
throws Exception
{
GitDiffConsumer consumer = new GitDiffConsumer( null );

File f = getTestFile( "src/test/resources/git/diff/git-diff3.log" );

ConsumerUtils.consumeFile( f, consumer );

List<ScmFile> changedFiles = consumer.getChangedFiles();

assertEquals( 1, changedFiles.size() );

testScmFile( changedFiles.get( 0 ), "pom.xml", ScmFileStatus.MODIFIED );

Map<String,CharSequence> differences = consumer.getDifferences();
assertNotNull( differences );

CharSequence addDiffs = differences.get( "pom.xml" );
assertNotNull( addDiffs );
assertEquals( "", addDiffs.toString() );
}

@Test
public void testLog4Consumer()
throws Exception
{
GitDiffConsumer consumer = new GitDiffConsumer( null );

File f = getTestFile( "src/test/resources/git/diff/git-diff4.log" );

ConsumerUtils.consumeFile( f, consumer );

List<ScmFile> changedFiles = consumer.getChangedFiles();

assertEquals( 1, changedFiles.size() );

testScmFile( changedFiles.get( 0 ), "pom.xml", ScmFileStatus.MODIFIED );

Map<String,CharSequence> differences = consumer.getDifferences();
assertNotNull( differences );

StringBuilder addDiffs = new StringBuilder( differences.get( "pom.xml" ) );
assertNotNull( addDiffs );
assertTrue( addDiffs.indexOf( "+ <!-- test -->" ) >= 0 );
}

private void testScmFile( ScmFile fileToTest, String expectedFilePath, ScmFileStatus expectedStatus )
{
assertEquals( expectedFilePath, fileToTest.getPath() );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
diff --git a/pom.xml b/pom_moved.xml
similarity index 100%
rename from pom.xml
rename to pom_moved.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/pom.xml b/pom_moved.xml
similarity index 99%
rename from pom.xml
rename to pom_moved.xml
index 122a94d8..1e5a80ce 100644
--- a/pom.xml
+++ b/pom_moved.xml
@@ -585,5 +585,5 @@
</snapshots>
</repository>
</repositories>
-
+ <!-- test -->
</project>

0 comments on commit 07d51d3

Please sign in to comment.