Skip to content

Commit 911b106

Browse files
committed
Improve patch handling for windows: correctly determine if a specific file is dos or
unix formatted.
1 parent 96c9039 commit 911b106

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

lua/neogit/lib/git/index.lua

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ local git = require("neogit.lib.git")
22
local Path = require("plenary.path")
33
local 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
817
local 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)
7282
end
7383

7484
---@param patch string diff generated with M.generate_patch

0 commit comments

Comments
 (0)