From 01ca0025dd86c2dcae7663408640eb00396fd8e8 Mon Sep 17 00:00:00 2001 From: Oscar Wallberg Date: Wed, 20 May 2026 08:09:07 +0200 Subject: [PATCH] fix(git): refresh the gutter after staging a hunk --- lua/git/hunks.lua | 8 +++++++- test/git/hunks_test.lua | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lua/git/hunks.lua b/lua/git/hunks.lua index 1b500b8..34f741c 100644 --- a/lua/git/hunks.lua +++ b/lua/git/hunks.lua @@ -599,7 +599,7 @@ end ---@param buf? integer function M.stage_hunk(buf) - local _, state, h = cursor_hunk(buf) + local target, state, h = cursor_hunk(buf) if not state then return end @@ -616,6 +616,12 @@ function M.stage_hunk(buf) "git apply failed: %s", vim.trim(res.stderr or "") ) + return + end + local s = states[target] + if s then + s.index_sha = nil + schedule(target) end end, }) diff --git a/test/git/hunks_test.lua b/test/git/hunks_test.lua index 9410a26..1d22a60 100644 --- a/test/git/hunks_test.lua +++ b/test/git/hunks_test.lua @@ -296,6 +296,23 @@ t.test("stage_hunk stages a deletion", function() t.eq(h.git(dir, "show", ":0:a.txt").stdout, "a\nc") end) +t.test("stage_hunk refreshes the gutter when status stays modified", function() + local _, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nC\nd\nE\n") + t.eq(#assert(hunks.state(buf)).hunks, 3) + + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + hunks.stage_hunk(buf) + t.wait_for(function() + return #assert(hunks.state(buf)).hunks == 2 + end, "gutter to drop the first staged hunk") + + vim.api.nvim_win_set_cursor(0, { 3, 0 }) + hunks.stage_hunk(buf) + t.wait_for(function() + return #assert(hunks.state(buf)).hunks == 1 + end, "gutter to drop the middle staged hunk") +end) + t.test("reset_hunk restores the index content for a change", function() local _, buf, state = setup("a\nb\nc\n", "a\nB\nc\n") vim.api.nvim_win_set_cursor(0, { 2, 0 })