You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Possible encoding problem with Repository.file_status #687
If I try to get the status of a single file using Repository.file_status, I get a "KeyError" when the path contains non-ASCII characters like "ä" or "\u00A0" (non breaking space").
The problem seems to be that "Repository_status_file" uses "py_path_to_c_str" to convert the path. Which encodes the path using the Python file system encoding.
If I encode the string myself using "utf-8", it works fine.
Not sure whether this is a libgit2, pygit2 or Windows problem...
Using pygit-0.24.2 on Python 3.5, running on Windows 7.
sys.getfilesystemencoding() returns "mbcs".
I think I'm running into the same problem using Python 3.6.2 and pygit2 (0.26.0) on MacOS
I whipped up a test script to duplicate the issue using the official repo for Thumbor (master branch)
~ $ cat test.py
#!/usr/bin/env python3
import pygit2
git_status_flags = {
pygit2.GIT_STATUS_CURRENT: ('CURRENT', 0),
pygit2.GIT_STATUS_INDEX_NEW: ('INDEX_NEW', 1),
pygit2.GIT_STATUS_INDEX_MODIFIED: ('INDEX_MOD', 1),
pygit2.GIT_STATUS_INDEX_DELETED: ('INDEX_DEL', 1),
pygit2.GIT_STATUS_WT_NEW: ('WT_NEW', 1),
pygit2.GIT_STATUS_WT_MODIFIED: ('WT_MOD', 1),
pygit2.GIT_STATUS_WT_DELETED: ('WT_DEL', 1),
pygit2.GIT_STATUS_IGNORED: ('IGNORED', 0), # Flags for ignored files
pygit2.GIT_STATUS_CONFLICTED: ('CONFLICTED', 1),
}
def process_git_repo(path):
result = {}
repo = pygit2.Repository(pygit2.discover_repository(path))
for filepath, flags in repo.status().items():
for status_flag in git_status_flags.keys():
if git_status_flags[status_flag][1] and flags == status_flag:
print([git_status_flags[status_flag][0]], filepath)
return result
if __name__ == '__main__':
process_git_repo("./thumbor")
Running on a fresh clone of the Thumbor master branch
The original bug reported here no longer reproduces on the platforms pygit2 currently supports.
Windows: Python switched to UTF-8 filesystem encoding in Python 3.6 (PEP 529), so the old mbcs failure with characters like ä or U+00A0 is gone.
macOS: The operating system moved from HFS+ to APFS, which is normalization-preserving. The classic scenario — a Linux-created repo with NFC paths checked out on HFS+ and returned as NFD — no longer occurs.
I also added regression tests covering Repository.status_file() with non-ASCII paths in test/test_status.py (e.g. täst_é.txt, U+00A0, and both NFC/NFD forms of café.txt).
That said, there is still an underlying correctness issue: APIs like status_file() convert paths via filesystem encoding (pgit_borrow_fsdefault) rather than treating them as the raw UTF-8 bytes that Git stores. On modern Windows/macOS this happens to work, but it is semantically wrong for repository-internal paths. A proper fix would be to use UTF-8 (with surrogateescape) for string paths and accept bytes for raw path bytes, scoped only to Git-internal APIs while keeping filesystem-path APIs unchanged.
So the reported symptom is effectively fixed, but the code still has a latent correctness issue worth tracking.
The analysis and answer above were done with the help of Kimi Code. I keep the issue open to keep record of the correctness problem.
Activity
tmr232 commentedon Apr 12, 2017
@wme-at-contact-de Does it happen with pygit-0.25.0 as well?
Ari-E-S commentedon Jul 23, 2017
Hi!
I think I'm running into the same problem using Python 3.6.2 and pygit2 (0.26.0) on MacOS
I whipped up a test script to duplicate the issue using the official repo for Thumbor (master branch)
Running on a fresh clone of the Thumbor master branch
Should return empty but doesn't
The same test for encoding shows UTF-8
tests: add regression tests for status_file with non-ASCII paths
jdavid commentedon Aug 12, 2026
The original bug reported here no longer reproduces on the platforms pygit2 currently supports.
I also added regression tests covering Repository.status_file() with non-ASCII paths in test/test_status.py (e.g. täst_é.txt, U+00A0, and both NFC/NFD forms of café.txt).
That said, there is still an underlying correctness issue: APIs like status_file() convert paths via filesystem encoding (pgit_borrow_fsdefault) rather than treating them as the raw UTF-8 bytes that Git stores. On modern Windows/macOS this happens to work, but it is semantically wrong for repository-internal paths. A proper fix would be to use UTF-8 (with surrogateescape) for string paths and accept bytes for raw path bytes, scoped only to Git-internal APIs while keeping filesystem-path APIs unchanged.
So the reported symptom is effectively fixed, but the code still has a latent correctness issue worth tracking.
The analysis and answer above were done with the help of Kimi Code. I keep the issue open to keep record of the correctness problem.
Fix status_file (and index/tree path handling) for non-ASCII paths
Fix status_file (and index/tree path handling) for non-ASCII paths
Fix status_file (and index/tree path handling) for non-ASCII paths