Skip to content

Commit

Permalink
git-remote-testpy: fix path hashing on Python 3
Browse files Browse the repository at this point in the history
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 <mhagger@alum.mit.edu>
Signed-off-by: John Keeping <john@keeping.me.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
John Keeping authored and Junio C Hamano committed Jan 28, 2013
1 parent f9640ac commit 3ac221a
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion git-remote-testpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@
sys.stderr.write("git-remote-testgit: requires Python 2.0 or later.\n")
sys.exit(1)


def encode_filepath(path):
"""Encodes a Unicode file path to a byte string.
On Python 2 this is a no-op; on Python 3 we encode the string as
suggested by [1] which allows an exact round-trip from the command line
to the filesystem.
[1] http://docs.python.org/3/c-api/unicode.html#file-system-encoding
"""
if sys.hexversion < 0x03000000:
return path
return path.encode(sys.getfilesystemencoding(), 'surrogateescape')


def get_repo(alias, url):
"""Returns a git repository object initialized for usage.
"""
Expand All @@ -45,7 +61,7 @@ def get_repo(alias, url):
repo.get_head()

hasher = _digest()
hasher.update(repo.path.encode('hex'))
hasher.update(encode_filepath(repo.path))
repo.hash = hasher.hexdigest()

repo.get_base_path = lambda base: os.path.join(
Expand Down

0 comments on commit 3ac221a

Please sign in to comment.