Skip to content

Commit

Permalink
improv: aborting commit input saves message
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Sep 22, 2023
1 parent 201c25c commit 01e63a9
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lua/tinygit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ end

--------------------------------------------------------------------------------

---Saves the content of a `vim.ui.input` modal that was aborted with `<Esc>`,
---so it can be restored.
local lastAbortedInputText = nil

---If there are staged changes, commit them.
---If there aren't, add all changes (`git add -A`) and then commit.
---@param prefillMsg? string
Expand All @@ -230,7 +234,11 @@ function M.smartCommit(opts, prefillMsg)

vim.cmd("silent update")
if not opts then opts = {} end
if not prefillMsg then prefillMsg = "" end
if not prefillMsg and not lastAbortedInputText then
prefillMsg = ""
elseif not prefillMsg and lastAbortedInputText then
prefillMsg = lastAbortedInputText
end

setGitCommitAppearance()
vim.ui.input({ prompt = "󰊢 Commit Message", default = prefillMsg }, function(commitMsg)
Expand All @@ -249,6 +257,8 @@ function M.smartCommit(opts, prefillMsg)
local stderr = fn.system { "git", "commit", "-m", newMsg }
if nonZeroExit(stderr) then return end

lastAbortedInputText = nil -- empty it, so it isn't prefilled

local body = ('"%s"'):format(newMsg)
if opts.push then body = body .. "\nPushing…" end
notify(body, "info", "Smart-Commit")
Expand Down

0 comments on commit 01e63a9

Please sign in to comment.