{"thread":{"id":"54768","subject":"[PATCH] Support having non-utf-8 characters returned by p4","startedAt":"2020-12-04T23:20:12Z","lastAt":"2020-12-04T23:20:12Z","messageCount":1,"participants":["David Racine via GitGitGadget"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"411424","messageId":"pull.928.git.git.1607123962304.gitgitgadget@gmail.com","threadId":"54768","inReplyTo":null,"subject":"[PATCH] Support having non-utf-8 characters returned by p4","fromName":"David Racine via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2020-12-04T23:19:21Z","receivedAt":"2020-12-04T23:20:12Z","isPatch":true,"sender":{"key":"name:David Racine","avatar":null},"body":"From: David Racine <bass_dr@hotmail.com>\n\nWhen perforce server is not configured for Unicode, and commands like\n`p4 users` returns a string with non-ascii characters (eg. one of the\nuser's FullName has a french `é` in it), git-p4 was giving\n`Exception: failure accessing depot: could not connect`.\nWith this patch, if such character is encountered, it will honor the new\n`git-p4.textEncoding` config option, or silently replace the erronous\ncharacter and continue.\n\nSigned-off-by: David Racine <bass_dr@hotmail.com>\n---\n    git-p4: Support having non-utf-8 characters returned by p4\n    \n    When perforce server is not configured for Unicode, and commands like p4\n    users returns a string with non-ascii characters (eg. one of the user's\n    FullName has a french é in it), git-p4 was giving Exception: failure\n    accessing depot: could not connect. With this patch, if such character\n    is encountered, it will honor the new git-p4.textEncoding config option,\n    or silently replace the erronous character and continue.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-928%2Fbassdr%2Fpatch-1-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-928/bassdr/patch-1-v1\nPull-Request: https://github.com/git/git/pull/928\n\n git-p4.py | 23 +++++++++++++++++------\n 1 file changed, 17 insertions(+), 6 deletions(-)\n\ndiff --git a/git-p4.py b/git-p4.py\nindex 6ae5bbfe99..6cbd153419 100755\n--- a/git-p4.py\n+++ b/git-p4.py\n@@ -195,16 +195,27 @@ def decode_path(path):\n     \"\"\"Decode a given string (bytes or otherwise) using configured path encoding options\n     \"\"\"\n     encoding = gitConfig('git-p4.pathEncoding') or 'utf_8'\n+    return p4_decode_stream(path, encoding)\n+\n+def p4_decode_text(user):\n+    \"\"\"Decode a given string (bytes or otherwise) using configured text encoding options\n+    \"\"\"\n+    encoding = gitConfig('git-p4.textEncoding') or 'utf-8'\n+    return p4_decode_stream(s, encoding)\n+    \n+def p4_decode_stream(s, encoding):\n+    \"\"\"Decode a given string (bytes or otherwise) using encoding argument\n+    \"\"\"\n     if bytes is not str:\n-        return path.decode(encoding, errors='replace') if isinstance(path, bytes) else path\n+        return s.decode(encoding, errors='replace') if isinstance(s, bytes) else s\n     else:\n         try:\n-            path.decode('ascii')\n+            s.decode('ascii')\n         except:\n-            path = path.decode(encoding, errors='replace')\n+            s = s.decode(encoding, errors='replace')\n             if verbose:\n-                print('Path with non-ASCII characters detected. Used {} to decode: {}'.format(encoding, path))\n-        return path\n+                print('Text with non-ASCII characters detected. Used {} to decode: {}'.format(encoding, s))\n+        return s\n \n def run_git_hook(cmd, param=[]):\n     \"\"\"Execute a hook if the hook exists.\"\"\"\n@@ -771,7 +782,7 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,\n                 for key, value in entry.items():\n                     key = key.decode()\n                     if isinstance(value, bytes) and not (key in ('data', 'path', 'clientFile') or key.startswith('depotFile')):\n-                        value = value.decode()\n+                        value = p4_decode_text(value)\n                     decoded_entry[key] = value\n                 # Parse out data if it's an error response\n                 if decoded_entry.get('code') == 'error' and 'data' in decoded_entry:\n\nbase-commit: 3a0b884caba2752da0af626fb2de7d597c844e8b\n-- \ngitgitgadget\n"}]}