Repository: maven-scm
Updated Branches:
  refs/heads/master 9dfde5c62 -> e0afea497


Correctly strip quotes in GitStatusConsumer

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.


Project: http://git-wip-us.apache.org/repos/asf/maven-scm/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-scm/commit/f9e39a02
Tree: http://git-wip-us.apache.org/repos/asf/maven-scm/tree/f9e39a02
Diff: http://git-wip-us.apache.org/repos/asf/maven-scm/diff/f9e39a02

Branch: refs/heads/master
Commit: f9e39a02a7a63da744045ebc1035eadd9bde83d7
Parents: 06c3954
Author: David Samuelson <[email protected]>
Authored: Thu Jan 1 21:16:53 2015 -0800
Committer: David Samuelson <[email protected]>
Committed: Thu Jan 1 21:31:56 2015 -0800

----------------------------------------------------------------------
 .../command/status/GitStatusConsumer.java       | 28 ++++++++++++--------
 .../command/status/GitStatusConsumerTest.java   | 12 +++++++++
 2 files changed, 29 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-scm/blob/f9e39a02/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/status/GitStatusConsumer.java
----------------------------------------------------------------------
diff --git 
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/status/GitStatusConsumer.java
 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/status/GitStatusConsumer.java
index 6fea6f8..cb11c0d 100644
--- 
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/status/GitStatusConsumer.java
+++ 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/status/GitStatusConsumer.java
@@ -222,15 +222,13 @@ public class GitStatusConsumer
         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;
         }
     }
 
@@ -245,8 +243,7 @@ public class GitStatusConsumer
         // 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")));
     }
 
 
@@ -254,4 +251,13 @@ public class GitStatusConsumer
     {
         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;
+    }
 }

http://git-wip-us.apache.org/repos/asf/maven-scm/blob/f9e39a02/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/status/GitStatusConsumerTest.java
----------------------------------------------------------------------
diff --git 
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/status/GitStatusConsumerTest.java
 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/status/GitStatusConsumerTest.java
index 4758038..a2537f4 100644
--- 
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/status/GitStatusConsumerTest.java
+++ 
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/status/GitStatusConsumerTest.java
@@ -353,6 +353,18 @@ public class GitStatusConsumerTest
 
         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 )

Reply via email to