threads / patch / 26067

patchgit-p4: Fix 'p4 opened' in git-p4 for names with spaces

Subject: [PATCH] git-p4: Fix 'p4 opened' in git-p4 for names with spaces

## tl;dr

8 messages between Dec 14, 2010 and Jan 15, 2011. Diffs are folded; open one to read it.

replies: 7people: 5as markdown or json

Jerzy Kozera· Dec 14, 2010, 20:56 UTC · lore
There is problem with git-p4 when trying to submit changes to file containing spaces in name - submit fails with "Command failed: p4 opened [name with spaces here]"
It's caused by not quoting name for p4 opened, and the patch attached fixes it.
Jerzy Kozera (1):
  Fix 'p4 opened' in git-p4 for names with spaces
 contrib/fast-import/git-p4 |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Jerzy Kozera· Dec 14, 2010, 20:56 UTC · re: Jerzy Kozera · lore
Signed-off-by: Jerzy Kozera <jerzy.kozera@gmail.com>
---
 contrib/fast-import/git-p4 |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Show changes to contrib/fast-import/git-p4 +1 −1
diff --git a/contrib/fast-import/git-p4 b/contrib/fast-import/git-p4
index 04ce7e3..a5297e7 100755
--- a/contrib/fast-import/git-p4
+++ b/contrib/fast-import/git-p4
@@ -144,7 +144,7 @@ def setP4ExecBit(file, mode):
 def getP4OpenedType(file):
     # Returns the perforce file type for the given file.
 
-    result = p4_read_pipe("opened %s" % file)
+    result = p4_read_pipe("opened \"%s\"" % file)
     match = re.match(".*\((.+)\)\r?$", result)
     if match:
         return match.group(1)
-- 
1.6.5.2
Junio C Hamano· Dec 14, 2010, 23:16 UTC · re: Jerzy Kozera · lore

Re: [PATCH] git-p4: Fix 'p4 opened' in git-p4 for names with spaces

Jerzy Kozera <jerzy.kozera@gmail.com> writes:
Show 15 quoted lines
> Signed-off-by: Jerzy Kozera <jerzy.kozera@gmail.com>
> ---
>  contrib/fast-import/git-p4 |    2 +-
>  1 files changed, 1 insertions(+), 1 deletions(-)
>
> diff --git a/contrib/fast-import/git-p4 b/contrib/fast-import/git-p4
> index 04ce7e3..a5297e7 100755
> --- a/contrib/fast-import/git-p4
> +++ b/contrib/fast-import/git-p4
> @@ -144,7 +144,7 @@ def setP4ExecBit(file, mode):
>  def getP4OpenedType(file):
>      # Returns the perforce file type for the given file.
>  
> -    result = p4_read_pipe("opened %s" % file)
> +    result = p4_read_pipe("opened \"%s\"" % file)
Don't you need a lot more than that?  What if file has " or \ in it?
Reece Dunn· Dec 14, 2010, 23:36 UTC · re: Junio C Hamano · lore

Re: [PATCH] git-p4: Fix 'p4 opened' in git-p4 for names with spaces

On 14 December 2010 23:16, Junio C Hamano <gitster@pobox.com> wrote:
Show 19 quoted lines
> Jerzy Kozera <jerzy.kozera@gmail.com> writes:
>
>> Signed-off-by: Jerzy Kozera <jerzy.kozera@gmail.com>
>> ---
>>  contrib/fast-import/git-p4 |    2 +-
>>  1 files changed, 1 insertions(+), 1 deletions(-)
>>
>> diff --git a/contrib/fast-import/git-p4 b/contrib/fast-import/git-p4
>> index 04ce7e3..a5297e7 100755
>> --- a/contrib/fast-import/git-p4
>> +++ b/contrib/fast-import/git-p4
>> @@ -144,7 +144,7 @@ def setP4ExecBit(file, mode):
>>  def getP4OpenedType(file):
>>      # Returns the perforce file type for the given file.
>>
>> -    result = p4_read_pipe("opened %s" % file)
>> +    result = p4_read_pipe("opened \"%s\"" % file)
>
> Don't you need a lot more than that?  What if file has " or \ in it?

Those are invalid characters for a filename on Windows, so cannot be entered/present in the filename. On Linux, they are accepted, but don't get put into the filename, so it all depends on where the data for file comes from (API call or user/external source). Not sure how Mac/BSD/Solaris handle those characters.

This looks fine to me, but I wonder if there are other places referencing file paths that require quoting to correctly handle spaces.

Also, escaping the quote characters can be avoided by using single quoted string literals:

+    result = p4_read_pipe('opened "%s"' % file)
- Reece
Jerzy Kozera· Jan 13, 2011, 18:51 UTC · re: Reece Dunn · lore

[PATCH] git-p4: Fixed handling of file names with spaces

Hi,
I've noticed the same issue in reopen and rm calls - not saying these three are all occurences of this problem, but I guess fixing them is a good start.
I'm using \" instead of '' quoting for consistency with rest of the file.
Regards,
Jerzy
---
 contrib/fast-import/git-p4 |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
Show changes to contrib/fast-import/git-p4 +3 −3
diff --git a/contrib/fast-import/git-p4 b/contrib/fast-import/git-p4
index 04ce7e3..2147315 100755
--- a/contrib/fast-import/git-p4
+++ b/contrib/fast-import/git-p4
@@ -139,12 +139,12 @@ def setP4ExecBit(file, mode):
         if p4Type[-1] == "+":
             p4Type = p4Type[0:-1]
 
-    p4_system("reopen -t %s %s" % (p4Type, file))
+    p4_system("reopen -t %s \"%s\"" % (p4Type, file))
 
 def getP4OpenedType(file):
     # Returns the perforce file type for the given file.
 
-    result = p4_read_pipe("opened %s" % file)
+    result = p4_read_pipe("opened \"%s\"" % file)
     match = re.match(".*\((.+)\)\r?$", result)
     if match:
         return match.group(1)
@@ -666,7 +666,7 @@ class P4Submit(Command):
                 for f in editedFiles:
                     p4_system("revert \"%s\"" % f);
                 for f in filesToAdd:
-                    system("rm %s" %f)
+                    system("rm \"%s\"" % f)
                 return
             elif response == "a":
                 os.system(applyPatchCmd)
-- 
1.7.1
Andreas Schwab· Jan 14, 2011, 22:01 UTC · re: Jerzy Kozera · lore

Re: [PATCH] git-p4: Fixed handling of file names with spaces

Jerzy Kozera <jerzy.kozera@gmail.com> writes:
> I've noticed the same issue in reopen and rm calls - not saying these three are all occurences of this problem, but I guess fixing them is a good start.

Can those file names also include a double quote or a backquote or a dollar sign?

Andreas.
-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
Jerzy Kozera· Jan 14, 2011, 22:45 UTC · re: Andreas Schwab · lore

Re: [PATCH] git-p4: Fixed handling of file names with spaces

On 14 Jan 2011, at 22:01, Andreas Schwab wrote:
> Can those file names also include a double quote or a backquote or a
> dollar sign?

Double quote and backquote get escaped by git so they are not a problem: $ git diff-tree -r HEAD^ HEAD :000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A "\" \\ $"

But as you can see above, the dollar sign remains intact, so it needs to be handled as well - patch below takes it into account.

Thanks, Jerzy

---
 contrib/fast-import/git-p4 |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
Show changes to contrib/fast-import/git-p4 +4 −4
diff --git a/contrib/fast-import/git-p4 b/contrib/fast-import/git-p4
index 04ce7e3..d930908 100755
--- a/contrib/fast-import/git-p4
+++ b/contrib/fast-import/git-p4
@@ -47,7 +47,7 @@ def p4_build_cmd(cmd):
     real_cmd += "%s" % (cmd)
     if verbose:
         print real_cmd
-    return real_cmd
+    return real_cmd.replace('$', '\\$')
 
 def chdir(dir):
     if os.name == 'nt':
@@ -139,12 +139,12 @@ def setP4ExecBit(file, mode):
         if p4Type[-1] == "+":
             p4Type = p4Type[0:-1]
 
-    p4_system("reopen -t %s %s" % (p4Type, file))
+    p4_system("reopen -t %s \"%s\"" % (p4Type, file))
 
 def getP4OpenedType(file):
     # Returns the perforce file type for the given file.
 
-    result = p4_read_pipe("opened %s" % file)
+    result = p4_read_pipe("opened \"%s\"" % file)
     match = re.match(".*\((.+)\)\r?$", result)
     if match:
         return match.group(1)
@@ -666,7 +666,7 @@ class P4Submit(Command):
                 for f in editedFiles:
                     p4_system("revert \"%s\"" % f);
                 for f in filesToAdd:
-                    system("rm %s" %f)
+                    system("rm \"%s\"" % f.replace('$', '\\$'))
                 return
             elif response == "a":
                 os.system(applyPatchCmd)
-- 
1.7.1
Pete Wyckoff· Jan 15, 2011, 14:35 UTC · re: Jerzy Kozera · lore

Re: [PATCH] git-p4: Fixed handling of file names with spaces

jerzy.kozera@gmail.com wrote on Fri, 14 Jan 2011 22:45 +0000:
Show 10 quoted lines
> On 14 Jan 2011, at 22:01, Andreas Schwab wrote:
> > Can those file names also include a double quote or a backquote or a
> > dollar sign?
> 
> 
> Double quote and backquote get escaped by git so they are not a problem:
> $ git diff-tree -r HEAD^ HEAD
> :000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A	"\" \\ $"
> 
> But as you can see above, the dollar sign remains intact, so it needs to be handled as well - patch below takes it into account.
[..]
> -    p4_system("reopen -t %s %s" % (p4Type, file))
> +    p4_system("reopen -t %s \"%s\"" % (p4Type, file))

These changes are important for correctness. Thanks for fixing them.

It is kind of ugly to have to do file escaping all over the source. I'd rather see all the os.system() calls go away, in favor of subprocess.Popen(). You can use the latter without going through the shell at all, hence no escapes are needed. If you feel ambitious, this would be a nice fix.

Spaces can happen in depot paths too. That isn't handled current. All the p4Cmd and p4CmdList calls that work on depotPaths should avoid going through the shell too.

But at least what you have done already should go in. If you feel adventurous, addressing these other space-related issues would be nice too.

		-- Pete

← back to recent threads