Skip to content

Commit

Permalink
Correctly strip quotes in GitStatusConsumer
Browse files Browse the repository at this point in the history
Before this, `GitStatusConsumer.resolvePath()` and
`GitStatusConsumer.resolveURI()` got into trouble with
files that include spaces.

To reproduce failure condition (in a git repo):

    echo "I'll fail" > "some nice file"
    git add "some nice file"
    git status --porcelain

The output of the `git status` is:

    A  "some nice file"

This causes at least
[JENKINS-24686](https://issues.jenkins-ci.org/browse/JENKINS-24686)
downstream.
  • Loading branch information
sralmai committed Jan 2, 2015
1 parent 06c3954 commit f9e39a0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,13 @@ private boolean isFile( String file )
return targetFile.isFile();
}

protected static String resolvePath( String fileEntry, URI path )
{
if ( path != null )
{
return resolveURI( fileEntry, path ).getPath();
}
else
{
return fileEntry;
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(cleanedEntry, path).getPath();
} else {
return cleanedEntry;
}
}

Expand All @@ -245,13 +243,21 @@ 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 f9e39a0

Please sign in to comment.