Subject: [PATCH] Support having non-utf-8 characters returned by p4

From: "David Racine via GitGitGadget" <[email-masked]>
Date: Fri, 04 Dec 2020 23:19:21 +0000

From: David Racine <[email-masked]>

When perforce server is not configured for Unicode, and commands like
`p4 users` returns a string with non-ascii characters (eg. one of the
user's FullName has a french `é` in it), git-p4 was giving
`Exception: failure accessing depot: could not connect`.
With this patch, if such character is encountered, it will honor the new
`git-p4.textEncoding` config option, or silently replace the erronous
character and continue.

Signed-off-by: David Racine <[email-masked]>
---
    git-p4: Support having non-utf-8 characters returned by p4
    
    When perforce server is not configured for Unicode, and commands like p4
    users returns a string with non-ascii characters (eg. one of the user's
    FullName has a french é in it), git-p4 was giving Exception: failure
    accessing depot: could not connect. With this patch, if such character
    is encountered, it will honor the new git-p4.textEncoding config option,
    or silently replace the erronous character and continue.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-928%2Fbassdr%2Fpatch-1-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-928/bassdr/patch-1-v1
Pull-Request: https://github.com/git/git/pull/928

 git-p4.py | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/git-p4.py b/git-p4.py
index 6ae5bbfe99..6cbd153419 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -195,16 +195,27 @@ def decode_path(path):
     """Decode a given string (bytes or otherwise) using configured path encoding options
     """
     encoding = gitConfig('git-p4.pathEncoding') or 'utf_8'
+    return p4_decode_stream(path, encoding)
+
+def p4_decode_text(user):
+    """Decode a given string (bytes or otherwise) using configured text encoding options
+    """
+    encoding = gitConfig('git-p4.textEncoding') or 'utf-8'
+    return p4_decode_stream(s, encoding)
+    
+def p4_decode_stream(s, encoding):
+    """Decode a given string (bytes or otherwise) using encoding argument
+    """
     if bytes is not str:
-        return path.decode(encoding, errors='replace') if isinstance(path, bytes) else path
+        return s.decode(encoding, errors='replace') if isinstance(s, bytes) else s
     else:
         try:
-            path.decode('ascii')
+            s.decode('ascii')
         except:
-            path = path.decode(encoding, errors='replace')
+            s = s.decode(encoding, errors='replace')
             if verbose:
-                print('Path with non-ASCII characters detected. Used {} to decode: {}'.format(encoding, path))
-        return path
+                print('Text with non-ASCII characters detected. Used {} to decode: {}'.format(encoding, s))
+        return s
 
 def run_git_hook(cmd, param=[]):
     """Execute a hook if the hook exists."""
@@ -771,7 +782,7 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
                 for key, value in entry.items():
                     key = key.decode()
                     if isinstance(value, bytes) and not (key in ('data', 'path', 'clientFile') or key.startswith('depotFile')):
-                        value = value.decode()
+                        value = p4_decode_text(value)
                     decoded_entry[key] = value
                 # Parse out data if it's an error response
                 if decoded_entry.get('code') == 'error' and 'data' in decoded_entry:

base-commit: 3a0b884caba2752da0af626fb2de7d597c844e8b
-- 
gitgitgadget