refactor(git): rename stage_hunk to toggle_stage

This commit is contained in:
2026-05-20 13:19:01 +02:00
parent 1a582045f6
commit 942dbdcaa0
4 changed files with 37 additions and 37 deletions
+1 -1
View File
@@ -235,7 +235,7 @@ vim.keymap.set("n", "<leader>gc", "<Plug>(git-commit)")
vim.keymap.set("n", "<leader>ga", "<Plug>(git-commit-amend)") vim.keymap.set("n", "<leader>ga", "<Plug>(git-commit-amend)")
vim.keymap.set("n", "<leader>gl", "<Plug>(git-log)") vim.keymap.set("n", "<leader>gl", "<Plug>(git-log)")
vim.keymap.set("n", "<leader>gv", "<Plug>(git-hunk-select)") vim.keymap.set("n", "<leader>gv", "<Plug>(git-hunk-select)")
vim.keymap.set("n", "<leader>gs", "<Plug>(git-hunk-stage)") vim.keymap.set("n", "<leader>gs", "<Plug>(git-hunk-stage-toggle)")
vim.keymap.set("n", "<leader>gr", "<Plug>(git-hunk-reset)") vim.keymap.set("n", "<leader>gr", "<Plug>(git-hunk-reset)")
vim.keymap.set("n", "<C-w>g", "<Plug>(git-hunk-preview)") vim.keymap.set("n", "<C-w>g", "<Plug>(git-hunk-preview)")
vim.keymap.set("n", "<leader>go", "<Plug>(git-overlay-toggle)") vim.keymap.set("n", "<leader>go", "<Plug>(git-overlay-toggle)")
+1 -1
View File
@@ -813,7 +813,7 @@ local function apply_patch(state, buf, patch, zero_context)
end end
---@param buf? integer ---@param buf? integer
function M.stage_hunk(buf) function M.toggle_stage(buf)
buf = resolve_buf(buf) buf = resolve_buf(buf)
local state = states[buf] local state = states[buf]
if not state then if not state then
+2 -2
View File
@@ -295,8 +295,8 @@ end, { silent = true, desc = "Jump to next git hunk" })
vim.keymap.set({ "n", "x" }, "<Plug>(git-hunk-prev)", function() vim.keymap.set({ "n", "x" }, "<Plug>(git-hunk-prev)", function()
require("git.hunks").nav("prev") require("git.hunks").nav("prev")
end, { silent = true, desc = "Jump to previous git hunk" }) end, { silent = true, desc = "Jump to previous git hunk" })
vim.keymap.set("n", "<Plug>(git-hunk-stage)", function() vim.keymap.set("n", "<Plug>(git-hunk-stage-toggle)", function()
require("git.hunks").stage_hunk() require("git.hunks").toggle_stage()
end, { silent = true, desc = "Stage or unstage the hunk under cursor" }) end, { silent = true, desc = "Stage or unstage the hunk under cursor" })
vim.keymap.set("n", "<Plug>(git-hunk-reset)", function() vim.keymap.set("n", "<Plug>(git-hunk-reset)", function()
require("git.hunks").reset_hunk() require("git.hunks").reset_hunk()
+33 -33
View File
@@ -261,10 +261,10 @@ t.test("overlay: toggling swaps gutter signs for the overlay", function()
) )
end) end)
t.test("stage_hunk stages the change into the index", function() t.test("toggle_stage stages the change into the index", function()
local dir, buf = setup("a\nb\nc\n", "a\nB\nc\n") local dir, buf = setup("a\nb\nc\n", "a\nB\nc\n")
vim.api.nvim_win_set_cursor(0, { 2, 0 }) vim.api.nvim_win_set_cursor(0, { 2, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" return h.git(dir, "diff", "--cached", "--name-only").stdout ~= ""
end, "stage to land in the index") end, "stage to land in the index")
@@ -276,27 +276,27 @@ t.test("stage_hunk stages the change into the index", function()
) )
end) end)
t.test("stage_hunk stages a pure addition", function() t.test("toggle_stage stages a pure addition", function()
local dir, buf = setup("a\nb\n", "a\nb\nc\n") local dir, buf = setup("a\nb\n", "a\nb\nc\n")
vim.api.nvim_win_set_cursor(0, { 3, 0 }) vim.api.nvim_win_set_cursor(0, { 3, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" return h.git(dir, "diff", "--cached", "--name-only").stdout ~= ""
end, "stage to land in the index") end, "stage to land in the index")
t.eq(h.git(dir, "show", ":0:a.txt").stdout, "a\nb\nc") t.eq(h.git(dir, "show", ":0:a.txt").stdout, "a\nb\nc")
end) end)
t.test("stage_hunk stages a deletion", function() t.test("toggle_stage stages a deletion", function()
local dir, buf = setup("a\nb\nc\n", "a\nc\n") local dir, buf = setup("a\nb\nc\n", "a\nc\n")
vim.api.nvim_win_set_cursor(0, { 1, 0 }) vim.api.nvim_win_set_cursor(0, { 1, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" return h.git(dir, "diff", "--cached", "--name-only").stdout ~= ""
end, "stage to land in the index") end, "stage to land in the index")
t.eq(h.git(dir, "show", ":0:a.txt").stdout, "a\nc") t.eq(h.git(dir, "show", ":0:a.txt").stdout, "a\nc")
end) end)
t.test("stage_hunk stages only the hunk under the cursor", function() t.test("toggle_stage stages only the hunk under the cursor", function()
local committed = table.concat({ local committed = table.concat({
"local M = {}", "local M = {}",
"", "",
@@ -330,7 +330,7 @@ t.test("stage_hunk stages only the hunk under the cursor", function()
}, "\n") .. "\n" }, "\n") .. "\n"
local dir, buf = setup(committed, worktree) local dir, buf = setup(committed, worktree)
vim.api.nvim_win_set_cursor(0, { 9, 0 }) vim.api.nvim_win_set_cursor(0, { 9, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" return h.git(dir, "diff", "--cached", "--name-only").stdout ~= ""
end, "the mid hunk to land in the index") end, "the mid hunk to land in the index")
@@ -357,50 +357,50 @@ t.test("stage_hunk stages only the hunk under the cursor", function()
) )
end) end)
t.test("stage_hunk stages a whole-file change with no context", function() t.test("toggle_stage stages a whole-file change with no context", function()
local dir, buf = setup("a\nb\nc\n", "x\ny\nz\n") local dir, buf = setup("a\nb\nc\n", "x\ny\nz\n")
vim.api.nvim_win_set_cursor(0, { 2, 0 }) vim.api.nvim_win_set_cursor(0, { 2, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" return h.git(dir, "diff", "--cached", "--name-only").stdout ~= ""
end, "the change to land in the index") end, "the change to land in the index")
t.eq(h.git(dir, "show", ":0:a.txt").stdout, "x\ny\nz") t.eq(h.git(dir, "show", ":0:a.txt").stdout, "x\ny\nz")
end) end)
t.test("stage_hunk stages a change at the start of the file", function() t.test("toggle_stage stages a change at the start of the file", function()
local dir, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nc\nd\ne\n") local dir, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nc\nd\ne\n")
vim.api.nvim_win_set_cursor(0, { 1, 0 }) vim.api.nvim_win_set_cursor(0, { 1, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" return h.git(dir, "diff", "--cached", "--name-only").stdout ~= ""
end, "the change to land in the index") end, "the change to land in the index")
t.eq(h.git(dir, "show", ":0:a.txt").stdout, "A\nb\nc\nd\ne") t.eq(h.git(dir, "show", ":0:a.txt").stdout, "A\nb\nc\nd\ne")
end) end)
t.test("stage_hunk stages a change at the end of the file", function() t.test("toggle_stage stages a change at the end of the file", function()
local dir, buf = setup("a\nb\nc\nd\ne\n", "a\nb\nc\nd\nE\n") local dir, buf = setup("a\nb\nc\nd\ne\n", "a\nb\nc\nd\nE\n")
vim.api.nvim_win_set_cursor(0, { 5, 0 }) vim.api.nvim_win_set_cursor(0, { 5, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" return h.git(dir, "diff", "--cached", "--name-only").stdout ~= ""
end, "the change to land in the index") end, "the change to land in the index")
t.eq(h.git(dir, "show", ":0:a.txt").stdout, "a\nb\nc\nd\nE") t.eq(h.git(dir, "show", ":0:a.txt").stdout, "a\nb\nc\nd\nE")
end) end)
t.test("stage_hunk stages a deletion at the start of the file", function() t.test("toggle_stage stages a deletion at the start of the file", function()
local dir, buf = setup("a\nb\nc\nd\n", "b\nc\nd\n") local dir, buf = setup("a\nb\nc\nd\n", "b\nc\nd\n")
vim.api.nvim_win_set_cursor(0, { 1, 0 }) vim.api.nvim_win_set_cursor(0, { 1, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" return h.git(dir, "diff", "--cached", "--name-only").stdout ~= ""
end, "the deletion to land in the index") end, "the deletion to land in the index")
t.eq(h.git(dir, "show", ":0:a.txt").stdout, "b\nc\nd") t.eq(h.git(dir, "show", ":0:a.txt").stdout, "b\nc\nd")
end) end)
t.test("stage_hunk leaves an adjacent unstaged hunk in place", function() t.test("toggle_stage leaves an adjacent unstaged hunk in place", function()
local dir, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nC\nd\ne\n") local dir, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nC\nd\ne\n")
vim.api.nvim_win_set_cursor(0, { 3, 0 }) vim.api.nvim_win_set_cursor(0, { 3, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return h.git(dir, "diff", "--cached", "--name-only").stdout ~= "" return h.git(dir, "diff", "--cached", "--name-only").stdout ~= ""
end, "the line-3 hunk to land in the index") end, "the line-3 hunk to land in the index")
@@ -411,22 +411,22 @@ t.test("stage_hunk leaves an adjacent unstaged hunk in place", function()
) )
end) end)
t.test("stage_hunk unstages one of two adjacent staged hunks", function() t.test("toggle_stage unstages one of two adjacent staged hunks", function()
local dir, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nC\nd\ne\n") local dir, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nC\nd\ne\n")
vim.api.nvim_win_set_cursor(0, { 1, 0 }) vim.api.nvim_win_set_cursor(0, { 1, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return #assert(hunks.state(buf)).staged == 1 return #assert(hunks.state(buf)).staged == 1
end, "the line-1 hunk to be staged") end, "the line-1 hunk to be staged")
vim.api.nvim_win_set_cursor(0, { 3, 0 }) vim.api.nvim_win_set_cursor(0, { 3, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
local s = assert(hunks.state(buf)) local s = assert(hunks.state(buf))
return #s.hunks == 0 and #s.staged == 2 return #s.hunks == 0 and #s.staged == 2
end, "both hunks to be staged") end, "both hunks to be staged")
vim.api.nvim_win_set_cursor(0, { 3, 0 }) vim.api.nvim_win_set_cursor(0, { 3, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return #assert(hunks.state(buf)).staged == 1 return #assert(hunks.state(buf)).staged == 1
end, "the line-3 hunk to be unstaged again") end, "the line-3 hunk to be unstaged again")
@@ -437,18 +437,18 @@ t.test("stage_hunk unstages one of two adjacent staged hunks", function()
) )
end) end)
t.test("stage_hunk refreshes the gutter when status stays modified", function() t.test("toggle_stage refreshes the gutter when status stays modified", function()
local _, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nC\nd\nE\n") local _, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nC\nd\nE\n")
t.eq(#assert(hunks.state(buf)).hunks, 3) t.eq(#assert(hunks.state(buf)).hunks, 3)
vim.api.nvim_win_set_cursor(0, { 1, 0 }) vim.api.nvim_win_set_cursor(0, { 1, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return #assert(hunks.state(buf)).hunks == 2 return #assert(hunks.state(buf)).hunks == 2
end, "gutter to drop the first staged hunk") end, "gutter to drop the first staged hunk")
vim.api.nvim_win_set_cursor(0, { 3, 0 }) vim.api.nvim_win_set_cursor(0, { 3, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return #assert(hunks.state(buf)).hunks == 1 return #assert(hunks.state(buf)).hunks == 1
end, "gutter to drop the middle staged hunk") end, "gutter to drop the middle staged hunk")
@@ -457,7 +457,7 @@ end)
t.test("staged hunks show with the staged highlight", function() t.test("staged hunks show with the staged highlight", function()
local _, buf = setup("a\nb\nc\n", "a\nB\nc\n") local _, buf = setup("a\nb\nc\n", "a\nB\nc\n")
vim.api.nvim_win_set_cursor(0, { 2, 0 }) vim.api.nvim_win_set_cursor(0, { 2, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
local s = assert(hunks.state(buf)) local s = assert(hunks.state(buf))
return #s.hunks == 0 and #s.staged == 1 return #s.hunks == 0 and #s.staged == 1
@@ -470,7 +470,7 @@ end)
t.test("the gutter shows staged and unstaged hunks together", function() t.test("the gutter shows staged and unstaged hunks together", function()
local _, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nC\nd\nE\n") local _, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nC\nd\nE\n")
vim.api.nvim_win_set_cursor(0, { 1, 0 }) vim.api.nvim_win_set_cursor(0, { 1, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return #assert(hunks.state(buf)).hunks == 2 return #assert(hunks.state(buf)).hunks == 2
end, "the first hunk to leave the unstaged set") end, "the first hunk to leave the unstaged set")
@@ -481,15 +481,15 @@ t.test("the gutter shows staged and unstaged hunks together", function()
}) })
end) end)
t.test("stage_hunk toggles a staged hunk back to unstaged", function() t.test("toggle_stage toggles a staged hunk back to unstaged", function()
local _, buf = setup("a\nb\nc\n", "a\nB\nc\n") local _, buf = setup("a\nb\nc\n", "a\nB\nc\n")
vim.api.nvim_win_set_cursor(0, { 2, 0 }) vim.api.nvim_win_set_cursor(0, { 2, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
local s = assert(hunks.state(buf)) local s = assert(hunks.state(buf))
return #s.hunks == 0 and #s.staged == 1 return #s.hunks == 0 and #s.staged == 1
end, "the hunk to become staged") end, "the hunk to become staged")
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
local s = assert(hunks.state(buf)) local s = assert(hunks.state(buf))
return #s.hunks == 1 and #s.staged == 0 return #s.hunks == 1 and #s.staged == 0
@@ -499,10 +499,10 @@ t.test("stage_hunk toggles a staged hunk back to unstaged", function()
}) })
end) end)
t.test("stage_hunk unstages correctly when buffer lines are shifted", function() t.test("toggle_stage unstages correctly when buffer lines are shifted", function()
local dir, buf = setup("a\nb\nc\n", "a\nb\nC\n") local dir, buf = setup("a\nb\nc\n", "a\nb\nC\n")
vim.api.nvim_win_set_cursor(0, { 3, 0 }) vim.api.nvim_win_set_cursor(0, { 3, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return #assert(hunks.state(buf)).staged == 1 return #assert(hunks.state(buf)).staged == 1
end, "the line-3 change to be staged") end, "the line-3 change to be staged")
@@ -515,7 +515,7 @@ t.test("stage_hunk unstages correctly when buffer lines are shifted", function()
end, "the unstaged add at the top to register") end, "the unstaged add at the top to register")
vim.api.nvim_win_set_cursor(0, { 4, 0 }) vim.api.nvim_win_set_cursor(0, { 4, 0 })
hunks.stage_hunk(buf) hunks.toggle_stage(buf)
t.wait_for(function() t.wait_for(function()
return #assert(hunks.state(buf)).staged == 0 return #assert(hunks.state(buf)).staged == 0
end, "the shifted staged hunk to be unstaged") end, "the shifted staged hunk to be unstaged")