Skip to content

Commit 3ac221a

Browse files
johnkeepinggitster
authored andcommitted
git-remote-testpy: fix path hashing on Python 3
When this change was originally made (0846b0c - git-remote-testpy: hash bytes explicitly , I didn't realise that the "hex" encoding we chose is a "bytes to bytes" encoding so it just fails with an error on Python 3 in the same way as the original code. It is not possible to provide a single code path that works on Python 2 and Python 3 since Python 2.x will attempt to decode the string before encoding it, which fails for strings that are not valid in the default encoding. Python 3.1 introduced the "surrogateescape" error handler which handles this correctly and permits a bytes -> unicode -> bytes round-trip to be lossless. As the original came from reading the filesystem path, we convert them back into the original bytes encoded in sys.getfilesystemencoding(). At this point Python 3.0 is unsupported so we don't go out of our way to try to support it. Helped-by: Michael Haggerty <[email protected]> Signed-off-by: John Keeping <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f9640ac commit 3ac221a

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

git-remote-testpy.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@
3636
sys.stderr.write("git-remote-testgit: requires Python 2.0 or later.\n")
3737
sys.exit(1)
3838

39+
40+
def encode_filepath(path):
41+
"""Encodes a Unicode file path to a byte string.
42+
43+
On Python 2 this is a no-op; on Python 3 we encode the string as
44+
suggested by [1] which allows an exact round-trip from the command line
45+
to the filesystem.
46+
47+
[1] http://docs.python.org/3/c-api/unicode.html#file-system-encoding
48+
49+
"""
50+
if sys.hexversion < 0x03000000:
51+
return path
52+
return path.encode(sys.getfilesystemencoding(), 'surrogateescape')
53+
54+
3955
def get_repo(alias, url):
4056
"""Returns a git repository object initialized for usage.
4157
"""
@@ -45,7 +61,7 @@ def get_repo(alias, url):
4561
repo.get_head()
4662

4763
hasher = _digest()
48-
hasher.update(repo.path.encode('hex'))
64+
hasher.update(encode_filepath(repo.path))
4965
repo.hash = hasher.hexdigest()
5066

5167
repo.get_base_path = lambda base: os.path.join(

0 commit comments

Comments
 (0)