Skip to content

fix(parser): support git diff.mnemonicPrefix in diff header#195

Merged
barrettruth merged 2 commits intobarrettruth:mainfrom
letientai299:main
Mar 17, 2026
Merged

fix(parser): support git diff.mnemonicPrefix in diff header#195
barrettruth merged 2 commits intobarrettruth:mainfrom
letientai299:main

Conversation

@letientai299
Copy link
Contributor

When diff.mnemonicPrefix is enabled in git config, diff headers use
context-dependent single-letter prefixes instead of the default a/
and b/. For example, git commit -v produces:

diff --git c/file.lua i/file.lua

where c/ = commit (HEAD) and i/ = index (staged). Other contexts
use w/ (worktree), o/ (object), etc. The full set is documented
in git-diff(1) under diff.mnemonicPrefix.

The parser hardcoded a/ and b/, so it failed to extract the
filename from the diff header. Without a filename, filetype detection
returned nil and no syntax highlighting was applied to hunk content.

The fix replaces the literal a and b with %a (Lua pattern for
any ASCII letter). This is a strict superset of the old behavior:

  • a/…b/ still matches (the default prefix).
  • c/…i/, w/…i/, etc. now also match.
  • Non-letter characters before / still won't match, so paths like
    1/foo or ./foo are correctly rejected.
  • The rest of the pattern (/.+) is unchanged, so the capture group
    still extracts the path after the second prefix.

The diff --combined and diff --cc branches are unaffected.

@barrettruth
Copy link
Owner

Looking at this later today. Did not know about this setting - thanks for ironing it out.

letientai299 added a commit to letientai299/nvim.conf that referenced this pull request Mar 17, 2026
diffs.nvim was lazy=true and only loaded as a neogit dependency,
so it never activated during `git commit -v`.

Also switch to fork that fixes diff.mnemonicPrefix support
(upstream PR: barrettruth/diffs.nvim#195).
Problem: `git diff.mnemonicPrefix = true` produces diff headers like
`diff --git c/init.lua i/init.lua` using single-letter mnemonic
prefixes (`c/`, `i/`, `w/`) instead of the standard `a/`/`b/`.
The parser only matched `a/.+ b/(.+)`, so mnemonic-prefixed diffs
produced no highlighting.

Solution: Change the pattern to `%a/.+ %a/(.+)` (Lua `%a` matches
any letter), accepting any single-letter prefix while correctly
rejecting numeric prefixes like `1/`/`2/`. Adds tests for both
the accepted mnemonic cases and the rejected numeric case.
@barrettruth barrettruth merged commit 50918f8 into barrettruth:main Mar 17, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants