@@ -2,7 +2,16 @@ local git = require("neogit.lib.git")
22local Path = require (" plenary.path" )
33local util = require (" neogit.lib.util" )
44
5- local LINE_ENDING = (vim .fn .has (" win32" ) == 1 or vim .fn .has (" win64" ) == 1 ) and " \r\n " or " \n "
5+ --- @param path string
6+ --- @return string
7+ local function eol_character (path )
8+ local lines = vim .fn .readfile (path , " b" , 1 )
9+ if (lines [1 ] and lines [1 ]:find (" \r " )) then
10+ return " \r\n "
11+ else
12+ return " \n "
13+ end
14+ end
615
716--- @class NeogitGitIndex
817local M = {}
@@ -63,12 +72,13 @@ function M.generate_patch(hunk, opts)
6372 assert (hunk .file , " hunk has no filepath" )
6473
6574 local path = Path :new (hunk .file ):make_relative (worktree_root )
75+ local eol = eol_character (path )
6676
6777 table.insert (diff_content , 1 , string.format (" +++ b/%s" , path ))
6878 table.insert (diff_content , 1 , string.format (" --- a/%s" , path ))
69- table.insert (diff_content , LINE_ENDING )
79+ table.insert (diff_content , eol )
7080
71- return table.concat (diff_content , LINE_ENDING )
81+ return table.concat (diff_content , eol )
7282end
7383
7484--- @param patch string diff generated with M.generate_patch
0 commit comments