Skip to content

Commit

Permalink
Merge branch 'fix_git_status_quotes'
Browse files Browse the repository at this point in the history
[SCM-820] Correctly strip quotes in GitStatusConsumer
fixes apache#26
  • Loading branch information
olamy committed Apr 6, 2016
2 parents 9dfde5c + 9080d85 commit e0afea4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,15 @@ private boolean isFile( String file )

protected static String resolvePath( String fileEntry, URI path )
{
/* Quotes may be included (from the git status line) when an fileEntry includes spaces */
String cleanedEntry = stripQuotes( fileEntry );
if ( path != null )
{
return resolveURI( fileEntry, path ).getPath();
return resolveURI( cleanedEntry, path ).getPath();
}
else
{
return fileEntry;
return cleanedEntry;
}
}

Expand All @@ -245,13 +247,22 @@ public static URI resolveURI( String fileEntry, URI path )
// When using URI.create, spaces need to be escaped but not the slashes, so we can't use
// URLEncoder.encode( String, String )
// new File( String ).toURI() results in an absolute URI while path is relative, so that can't be used either.
String str = fileEntry.replace( " ", "%20" );
return path.relativize( URI.create( str ) );
return path.relativize( URI.create( stripQuotes( fileEntry ).replace( " ", "%20" ) ) );
}


public List<ScmFile> getChangedFiles()
{
return changedFiles;
}

/**
* @param str the (potentially quoted) string, must not be {@code null}
* @return the string with a pair of double quotes removed (if they existed)
*/
private static String stripQuotes( String str )
{
int strLen = str.length();
return ( strLen > 0 && str.startsWith( "\"" ) && str.endsWith( "\"" ) ) ? str.substring( 1, strLen - 1 ) : str;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,18 @@ public void testResolvePath()

assertEquals( "pom.xml", GitStatusConsumer.resolvePath( "work with spaces/pom.xml", path ) );
assertEquals( "work with spaces/pom.xml", GitStatusConsumer.resolvePath( "work with spaces/pom.xml", null ) );

// spaces in path with quotes
repositoryRoot = getTestFile( "repo" );
workingDirectory = getTestFile( "repo/work with spaces and quotes" );

path = repositoryRoot.toURI().relativize( workingDirectory.toURI() );

assertEquals( "work with spaces and quotes", path.getPath() );

assertEquals( "pom.xml", GitStatusConsumer.resolvePath( "\"work with spaces and quotes/pom.xml\"", path ) );
assertEquals( "work with spaces and quotes/pom.xml",
GitStatusConsumer.resolvePath( "\"work with spaces and quotes/pom.xml\"", null ) );
}

private void testScmFile( ScmFile fileToTest, String expectedFilePath, ScmFileStatus expectedStatus )
Expand Down

0 comments on commit e0afea4

Please sign in to comment.