Comments on deleted lines

This commit is contained in:
Harrison Cramer
2023-04-23 17:21:13 -04:00
parent b0cdc5dd88
commit 182575d95a
4 changed files with 157 additions and 50 deletions

View File

@@ -19,11 +19,43 @@ M.create_comment = function()
keymaps.set_popup_keymaps(commentPopup, M.confirm_create_comment)
end
M.find_deletion_commit = function(file)
local current_line = vim.api.nvim_get_current_line()
local command = string.format("git log -S '%s' %s", current_line, file)
local handle = io.popen(command)
local output = handle:read("*line")
if output == nil then
notify("Error reading SHA of deletion commit", "error")
return ""
end
handle:close()
local words = {}
for word in output:gmatch("%S+") do
table.insert(words, word)
end
return words[2]
end
-- Sends the comment to Gitlab
M.confirm_create_comment = function(text)
if u.base_invalid() then return end
local relative_file_path = u.get_relative_file_path()
local current_line_number = u.get_current_line_number()
if relative_file_path == nil then return end
-- If leaving a comment on a deleted line, get hash value + proper filename
local sha = ""
local is_base_file = relative_file_path:find(".git")
if is_base_file then -- We are looking at a deletion.
local _, path = u.split_diff_view_filename(relative_file_path)
sha = M.find_deletion_commit(path)
if sha == "" then
return
end
end
Job:new({
command = state.BIN,
args = {
@@ -32,6 +64,7 @@ M.confirm_create_comment = function(text)
current_line_number,
relative_file_path,
text,
sha
},
-- TODO: Render the tree after comment creation. Refresh?
on_stdout = u.print_success,

View File

@@ -215,6 +215,16 @@ local read_file = function(file_path)
return file_contents
end
local split_diff_view_filename = function(filename)
local hash, path = filename:match("://%.git/(/?[0-9a-f]+)(/.*)$")
if hash and path then
path = path:gsub("%.git/", ""):gsub("^/", "")
hash = hash:gsub("^/", "")
end
return hash, path
end
M.get_relative_file_path = get_relative_file_path
M.get_current_line_number = get_current_line_number
M.get_buffer_text = get_buffer_text
@@ -233,5 +243,6 @@ M.print_error = print_error
M.create_popup_state = create_popup_state
M.exit = exit
M.read_file = read_file
M.split_diff_view_filename = split_diff_view_filename
M.P = P
return M