git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
DGDavid Racine via GitGitGadget <gitgitgadget@gmail.com>
Date
Dec 4, 2020, 23:19 UTC
Message-ID
<pull.928.git.git.1607123962304.gitgitgadget@gmail.com>
From: David Racine <bass_dr@hotmail.com>

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 <bass_dr@hotmail.com>
---
    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
Message 1 of 1 in “Support having non-utf-8 characters returned by p4”
  1. Support having non-utf-8 characters returned by p4David Racine via GitGitGadget, Dec 4, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.