Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly strip quotes in GitStatusConsumer #26

Merged
merged 2 commits into from
Apr 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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