Patchwork convert: unescape Perforce-escaped special characters in filenames

login
register
mail settings
Submitter Eugene Baranov
Date July 14, 2015, 3:25 p.m.
Message ID <3908871b636316c592c2.1436887531@ADNADTX6400256.eng.citrite.net>
Download mbox | patch
Permalink /patch/9976/
State Superseded
Headers show

Comments

Eugene Baranov - July 14, 2015, 3:25 p.m.
# HG changeset patch
# User Eugene Baranov <eug.baranov@gmail.com>
# Date 1436887437 -3600
#      Tue Jul 14 16:23:57 2015 +0100
# Node ID 3908871b636316c592c2b911b7f1e7ad5ae764b4
# Parent  f516ac1d4b9bbbcd45763c1b008c805e60b8bfbf
convert: unescape Perforce-escaped special characters in filenames
Simon King - July 14, 2015, 3:37 p.m.
On Tue, Jul 14, 2015 at 4:25 PM, Eugene Baranov <eug.baranov@gmail.com> wrote:
> # HG changeset patch
> # User Eugene Baranov <eug.baranov@gmail.com>
> # Date 1436887437 -3600
> #      Tue Jul 14 16:23:57 2015 +0100
> # Node ID 3908871b636316c592c2b911b7f1e7ad5ae764b4
> # Parent  f516ac1d4b9bbbcd45763c1b008c805e60b8bfbf
> convert: unescape Perforce-escaped special characters in filenames
>
> diff -r f516ac1d4b9b -r 3908871b6363 hgext/convert/p4.py
> --- a/hgext/convert/p4.py       Tue Jul 14 14:40:56 2015 +0100
> +++ b/hgext/convert/p4.py       Tue Jul 14 16:23:57 2015 +0100
> @@ -23,6 +23,14 @@
>      except EOFError:
>          pass
>
> +def decodefilename(filename):
> +    '''Perforce escapes special characters @, #, *, or %
> +    with %40, %23, %2A, or %25 respectively'''
> +    replacements = { '%2A': '*', '%23' : '#', '%40' : '@', '%25' : '%'}
> +    for k, v in replacements.iteritems():
> +        filename = filename.replace(k, v)

The order of iteration over dictionaries is undefined. This means you
could potentially replace "%25" before one of the other characters,
which would cause an issue with a filename containing the sequence
"%252A" for example. Making "replacements" a list of tuples instead
would probably be safer.

Simon

Patch

diff -r f516ac1d4b9b -r 3908871b6363 hgext/convert/p4.py
--- a/hgext/convert/p4.py	Tue Jul 14 14:40:56 2015 +0100
+++ b/hgext/convert/p4.py	Tue Jul 14 16:23:57 2015 +0100
@@ -23,6 +23,14 @@ 
     except EOFError:
         pass
 
+def decodefilename(filename):
+    '''Perforce escapes special characters @, #, *, or %
+    with %40, %23, %2A, or %25 respectively'''
+    replacements = { '%2A': '*', '%23' : '#', '%40' : '@', '%25' : '%'}
+    for k, v in replacements.iteritems():
+        filename = filename.replace(k, v)
+    return filename
+
 class p4_source(converter_source):
     def __init__(self, ui, path, revs=None):
         super(p4_source, self).__init__(ui, path, revs=revs)
@@ -138,7 +146,7 @@ 
                 filename = None
                 for v in vieworder:
                     if oldname.lower().startswith(v.lower()):
-                        filename = views[v] + oldname[len(v):]
+                        filename = decodefilename(views[v] + oldname[len(v):])
                         break
                 if filename:
                     files.append((filename, d["rev%d" % i]))