From 9d9a1974cc4147822ce95fede650d99afa2bab2a Mon Sep 17 00:00:00 2001 From: madebyly Date: Wed, 18 Feb 2026 00:38:58 -0500 Subject: [PATCH 1/2] fix: stage hunk with existing index file endings Previously, patches would be generated for hunks the user wanted to stage with lines concatenated by either: - a specific platform line ending (currently only Windows) if no file exists at the path the hunk came from, or; - reading the file and determining the line endings of the file by checking the line ending of the first line, only. While both of these worked for entirely new files and / or on platforms where the environment default line ending is the same as the indexed file's line endings, it would clobber the indexed file's line endings when the environment's default line endings differed from the index. The reason why this only clobbers the indexed files line endings is because the patch is eventually applied only to the index, not the working copy. This would result in the entire file being considered changed in the diff without additional options to filter out the noise. This usually only affects Windows due to it using `CRLF` by default, and only when you work on a repository that primarily commits in `LF` line endings, but could've probably affected it in the reverse if it was also vice-versa, with the system being in `LF` while the repository was in `CRLF`, a setup typical for Windows-specific code repositories being contributed to cross-platform by developers on other platforms. (NOTE: I say probably since I haven't had a chance to test that specific reverse scenario yet, I'm only working off of the core line ending mismatch problem) This patch fixes the problem by leveraging Git to determine the existing indexed file's line endings and using those to concatenate the patch's lines with, falling back to the existing logic if the file isn't currently in the repository's index OR if the file has mixed line endings. This keeps the line endings consistent as to what would be written to the repository index on commits. --- lua/neogit/lib/git/cli.lua | 2 ++ lua/neogit/lib/git/index.lua | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lua/neogit/lib/git/cli.lua b/lua/neogit/lib/git/cli.lua index c128bc24b..f12abcf45 100644 --- a/lua/neogit/lib/git/cli.lua +++ b/lua/neogit/lib/git/cli.lua @@ -312,6 +312,7 @@ end ---@field exclude_standard self ---@field full_name self ---@field error_unmatch self +---@field eol self ---@class GitCommandLsTree: GitCommandBuilder ---@field full_tree self @@ -950,6 +951,7 @@ local configurations = { exclude_standard = "--exclude-standard", full_name = "--full-name", error_unmatch = "--error-unmatch", + eol = "--eol", }, }, diff --git a/lua/neogit/lib/git/index.lua b/lua/neogit/lib/git/index.lua index 6c4d0f100..d07d32172 100644 --- a/lua/neogit/lib/git/index.lua +++ b/lua/neogit/lib/git/index.lua @@ -1,18 +1,36 @@ local git = require("neogit.lib.git") local Path = require("neogit.lib.path") local util = require("neogit.lib.util") +local crlf = "\r\n" +local lf = "\n" ---@param path string ---@return string local function eol_character(path) + local _, output = next(git.cli["ls-files"].eol.files(path).call { await = true }.stdout) + + if output ~= nil then + local _, _, match = output:find("i/(%w+)") + + if match ~= nil then + if match == crlf then + return crlf + elseif match == lf then + return lf + end + end + end + + -- Handle the `mixed` line ending case OR lack of a file in the index by deferring to platform defaults for EOL + -- or by reading the current working file and determining its line ending that way if not Path:new(path):exists() then - return (vim.fn.has("win32") == 1 or vim.fn.has("win64") == 1) and "\r\n" or "\n" + return (vim.fn.has("win32") == 1 or vim.fn.has("win64") == 1) and crlf or lf end local lines = vim.fn.readfile(path, "b", 1) if lines[1] and lines[1]:find("\r") then - return "\r\n" + return crlf else - return "\n" + return lf end end From bdfb0768bf3f45044045ae55c33e7bc78c8cb357 Mon Sep 17 00:00:00 2001 From: Ly Date: Mon, 21 Sep 2026 19:57:54 -0400 Subject: [PATCH 2/2] fix: check exact match result This was an erroneous change where the variable was used even though the value returned from `git ls-files --eol` doesn't include the leading slash. Using the "constant" doesn't actually perform the match correctly. --- lua/neogit/lib/git/index.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/neogit/lib/git/index.lua b/lua/neogit/lib/git/index.lua index d07d32172..abdabaf5c 100644 --- a/lua/neogit/lib/git/index.lua +++ b/lua/neogit/lib/git/index.lua @@ -13,9 +13,9 @@ local function eol_character(path) local _, _, match = output:find("i/(%w+)") if match ~= nil then - if match == crlf then + if match == 'crlf' then return crlf - elseif match == lf then + elseif match == 'lf' then return lf end end