feat: unified diff conflict resolution for unmerged files (#99)
## Problem Pressing `du` on a `UU` (unmerged) file in the fugitive status buffer had no effect. There was no way to see a proper ours-vs-theirs diff with syntax highlighting and intra-line changes, or to resolve conflicts from within a unified diff view. Additionally, pressing `du` on a section header containing only unmerged files showed "no changes in section" because `git diff` produces combined (`diff --cc`) output for unmerged files, which was stripped entirely. ## Solution Fetch `:2:` (ours) and `:3:` (theirs) from the git index and generate a standard unified diff. The existing highlight pipeline (treesitter + intra-line) applies automatically. Resolution keymaps (`doo`/`dot`/`dob`/`don`) on hunks in the diff view write changes back to the working file's conflict markers. Navigation (`]x`/`[x`) jumps between unresolved conflict hunks. For section diffs, combined diff entries are now replaced with generated ours-vs-theirs unified diffs instead of being stripped. Works for merge, cherry-pick, and rebase conflicts — git populates `:2:`/`:3:` the same way for all three. Closes #61
This commit is contained in:
parent
49fc446aae
commit
a2053a132b
8 changed files with 1287 additions and 28 deletions
|
|
@ -27,19 +27,19 @@ function M.get_section_at_line(bufnr, lnum)
|
|||
end
|
||||
|
||||
---@param line string
|
||||
---@return string?, string?
|
||||
---@return string?, string?, string?
|
||||
local function parse_file_line(line)
|
||||
local old, new = line:match('^R%d*%s+(.-)%s+->%s+(.+)$')
|
||||
if old and new then
|
||||
return vim.trim(new), vim.trim(old)
|
||||
return vim.trim(new), vim.trim(old), 'R'
|
||||
end
|
||||
|
||||
local filename = line:match('^[MADRCU?][MADRCU%s]*%s+(.+)$')
|
||||
if filename then
|
||||
return vim.trim(filename), nil
|
||||
local status, filename = line:match('^([MADRCU?])[MADRCU%s]*%s+(.+)$')
|
||||
if status and filename then
|
||||
return vim.trim(filename), nil, status
|
||||
end
|
||||
|
||||
return nil, nil
|
||||
return nil, nil, nil
|
||||
end
|
||||
|
||||
---@param line string
|
||||
|
|
@ -57,34 +57,34 @@ end
|
|||
|
||||
---@param bufnr integer
|
||||
---@param lnum integer
|
||||
---@return string?, diffs.FugitiveSection, boolean, string?
|
||||
---@return string?, diffs.FugitiveSection, boolean, string?, string?
|
||||
function M.get_file_at_line(bufnr, lnum)
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||
local current_line = lines[lnum]
|
||||
|
||||
if not current_line then
|
||||
return nil, nil, false, nil
|
||||
return nil, nil, false, nil, nil
|
||||
end
|
||||
|
||||
local section_header = parse_section_header(current_line)
|
||||
if section_header then
|
||||
return nil, section_header, true, nil
|
||||
return nil, section_header, true, nil, nil
|
||||
end
|
||||
|
||||
local filename, old_filename = parse_file_line(current_line)
|
||||
local filename, old_filename, status = parse_file_line(current_line)
|
||||
if filename then
|
||||
local section = M.get_section_at_line(bufnr, lnum)
|
||||
return filename, section, false, old_filename
|
||||
return filename, section, false, old_filename, status
|
||||
end
|
||||
|
||||
local prefix = current_line:sub(1, 1)
|
||||
if prefix == '+' or prefix == '-' or prefix == ' ' then
|
||||
for i = lnum - 1, 1, -1 do
|
||||
local prev_line = lines[i]
|
||||
filename, old_filename = parse_file_line(prev_line)
|
||||
filename, old_filename, status = parse_file_line(prev_line)
|
||||
if filename then
|
||||
local section = M.get_section_at_line(bufnr, i)
|
||||
return filename, section, false, old_filename
|
||||
return filename, section, false, old_filename, status
|
||||
end
|
||||
if prev_line:match('^%w+ %(') or prev_line == '' then
|
||||
break
|
||||
|
|
@ -92,7 +92,7 @@ function M.get_file_at_line(bufnr, lnum)
|
|||
end
|
||||
end
|
||||
|
||||
return nil, nil, false, nil
|
||||
return nil, nil, false, nil, nil
|
||||
end
|
||||
|
||||
---@class diffs.HunkPosition
|
||||
|
|
@ -150,7 +150,7 @@ function M.diff_file_under_cursor(vertical)
|
|||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
local lnum = vim.api.nvim_win_get_cursor(0)[1]
|
||||
|
||||
local filename, section, is_header, old_filename = M.get_file_at_line(bufnr, lnum)
|
||||
local filename, section, is_header, old_filename, status = M.get_file_at_line(bufnr, lnum)
|
||||
|
||||
local repo_root = get_repo_root_from_fugitive(bufnr)
|
||||
if not repo_root then
|
||||
|
|
@ -192,6 +192,7 @@ function M.diff_file_under_cursor(vertical)
|
|||
vertical = vertical,
|
||||
staged = section == 'staged',
|
||||
untracked = section == 'untracked',
|
||||
unmerged = status == 'U',
|
||||
old_filepath = old_filepath,
|
||||
hunk_position = hunk_position,
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue