Skip to content

Commit

Permalink
fix(NeogitOrg#23): keep commit message around if commit fails
Browse files Browse the repository at this point in the history
Since the commit can fail for various reasons, we keep the commit
message file after the user closes the buffer and only remove it after
the commit was successful. Upon opening the commit buffer, we read the
commit file's contents, if it exists, and present it as the suggested
commit message
  • Loading branch information
RianFuro committed Feb 28, 2021
1 parent a22021b commit d5a230d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
23 changes: 23 additions & 0 deletions lua/neogit/async/uv.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local uv = vim.loop
local a = require('neogit.async')

local wrapper = setmetatable({}, {
__index = function (tbl, action)
if uv[action] then
return a.wrap(uv[action])
end

return nil
end
})

wrapper.read_file = a.sync(function (file)
local err, fd = a.wait(wrapper.fs_open(file, 'r', 438))
if err then return nil end
local _, stat = a.wait(wrapper.fs_fstat(fd))
local _, data = a.wait(wrapper.fs_read(fd, stat.size, 0))
a.wait(wrapper.fs_close(fd))
return data
end)

return wrapper
9 changes: 7 additions & 2 deletions lua/neogit/popups/commit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ local popup = require("neogit.lib.popup")
local cli = require("neogit.lib.git.cli")
local Buffer = require("neogit.lib.buffer")
local a = require('neogit.async')
local uv = require('neogit.async.uv')
local split = require('neogit.lib.util').split

local get_commit_message = a.wrap(function (content, cb)
Buffer.create {
Expand Down Expand Up @@ -31,7 +33,7 @@ end)
local prompt_commit_message = a.sync(function (msg)
local output = {}

if msg then
if msg and #msg > 0 then
for _, line in ipairs(msg) do
table.insert(output, line)
end
Expand Down Expand Up @@ -123,9 +125,12 @@ local function create()
description = "Commit",
callback = function(popup)
a.dispatch(function ()
a.wait(prompt_commit_message(nil))
local data = a.wait(uv.read_file('.git/COMMIT_EDITMSG'))
local old_content = split(data or '', '\n')
a.wait(prompt_commit_message(old_content))
local _, code = a.wait(cli.commit.commit_message_file('.git/COMMIT_EDITMSG').args(unpack(popup.get_arguments())).call())
if code == 0 then
a.wait(uv.fs_unlink('.git/COMMIT_EDITMSG'))
__NeogitStatusRefresh(true)
end
end)
Expand Down

0 comments on commit d5a230d

Please sign in to comment.