Repository navigation
Conversation
With *.c diff=cpp, generating an ordinary patch can fail on macOS with
"illegal byte sequence" under a UTF-8 locale. The built-in word pattern
contains this C string fragment:
"[\xc0-\xff][\x80-\xbf]+"
The C compiler turns these escapes into raw bytes that are not valid UTF-8
in the pattern, so regcomp_l rejects it while loading the driver.
The word patterns were imported from Git alongside the function name
rules. The compiled word_pattern is stored and freed, but never used for
matching. Hunk headers use the separate fn_patterns list, so omitting
word-pattern compilation does not change hunk contents or headers.
Commit ab96ca5 ("Make sure we use the `C` locale for `regcomp` on
macOS.") addressed this error by switching to regcomp_l, but the failure
is still reproducible under a UTF-8 locale on macOS.
Stop compiling built-in word patterns when loading a driver and add a
regression test for diff=cpp under a UTF-8 locale.
Reproduce on macOS with pygit2 linked to libgit2 using regcomp_l:
python3 - <<'PYTHON'
import locale
from pathlib import Path
from tempfile import TemporaryDirectory
import pygit2
locale.setlocale(locale.LC_CTYPE, 'UTF-8')
with TemporaryDirectory() as directory:
repo = pygit2.init_repository(directory)
root = Path(directory)
(root / '.gitattributes').write_text('*.c diff=cpp\n')
source = root / 'test.c'
source.write_text('int value = 1;\n')
repo.index.add('test.c')
repo.index.write()
source.write_text('int value = 20;\n')
print(repo.diff().patch)
PYTHON
Before the fix, generating the patch raises "illegal byte sequence".
Signed-off-by: Gregory Herrero <gregory.herrero@oracle.com>
Diff drivers compile diff.<driver>.wordregex and store the result in word_pattern, but no matching code reads it. An invalid expression can therefore prevent an ordinary line diff even though the pattern is unused. Remove the compiled field and its compilation and disposal. Keep checking whether wordregex is set, since a wordregex-only configuration selects a custom driver. Dropping that lookup would allow fallback to a built-in driver and change the function context in hunk headers. Add tests to preserve driver selection with a valid wordregex and verify that an invalid wordregex does not prevent patch generation. Signed-off-by: Gregory Herrero <gregory.herrero@oracle.com>
herrerog
force-pushed
the
fix/macos-diff-cpp
branch
from
October 4, 2026 20:06
cbd0f38 to
837671f
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I recently had to switch to macOS and while using our tools based on libgit2 (and pygit2), I faced below issue while trying to diff a c file from the linux kernel:
Here is a simple reproducer:
When cpp driver is used, it seems that libgit2 compiles the regex pattern with this C string:
"[\xc0-\xff][\x80-\xbf]+"(for example)But it produces invalid utf-8 character that are rejected by
regcomp_lon MacOS.Given those patterns are not used, we can simply remove their compilation.
I've used AI for fixing the issue.