organize git functions in git.lua

This commit is contained in:
2022-03-31 23:36:27 +03:00
parent c2e873b521
commit ed6d81ef53
5 changed files with 113 additions and 105 deletions

View File

@@ -151,7 +151,7 @@ require("nvim-autopairs").setup({})
require("user.file-tree")
require("user.treesitter")
require("user.telescope")
require("user.git")
require("user.gitsigns")
require("user.comp")
require("user.lsp")
require("user.comment")

View File

@@ -1,53 +1,55 @@
local a = require("gitsigns.actions")
local reload_nvimtree = require("nvim-tree.actions.reloaders").reload_explorer
local u = require("user.utils")
require("gitsigns").setup({
signcolumn = true,
attach_to_untracked = false,
current_line_blame = true,
current_line_blame_opts = {
delay = 1000,
},
current_line_blame_formatter_opts = {
relative_time = true,
},
on_attach = function(bufnr)
vim.keymap.set("n", "<leader>gs", function()
a.stage_hunk()
reload_nvimtree()
end)
vim.keymap.set("n", "<leader>gu", function()
a.undo_stage_hunk()
reload_nvimtree()
end)
vim.keymap.set("n", "<leader>gr", function()
a.reset_hunk()
reload_nvimtree()
end)
vim.keymap.set("n", "<leader>gp", function()
a.preview_hunk()
reload_nvimtree()
end)
vim.keymap.set("n", "<leader>gn", function()
a.next_hunk()
reload_nvimtree()
end)
vim.keymap.set("n", "<leader>gN", function()
a.prev_hunk()
reload_nvimtree()
end)
local git = {}
vim.keymap.set("v", "gs", function()
a.stage_hunk()
reload_nvimtree()
end)
vim.keymap.set("v", "gu", function()
a.undo_stage_hunk()
reload_nvimtree()
end)
vim.keymap.set("v", "gr", function()
a.reset_hunk()
reload_nvimtree()
end)
end,
})
--- @return string commit_hash
function git.get_commit_hash_for_current_line()
local fullpath = vim.api.nvim_buf_get_name(0)
local cwd = vim.loop.cwd()
local relative_path = string.gsub(fullpath, cwd .. "/", "")
local line = unpack(vim.api.nvim_win_get_cursor(0))
local f = io.popen("git blame -L " .. line .. ",+1 " .. relative_path, "r")
local data = f:read("*a")
f:close()
local commit_hash = vim.split(data, " ")[1]
return commit_hash
end
--- @return string git_remote_url
function git.get_remote_url()
local f = io.popen("git remote get-url origin", "r")
local remote_url = f:read("*l")
f:close()
if string.sub(remote_url, 0, 4) == "git@" then
remote_url = string.gsub(remote_url, "git@", "")
remote_url = string.gsub(remote_url, ".git", "")
remote_url = string.gsub(remote_url, ":", "/")
end
if string.sub(remote_url, 0, 5) ~= "https" then
remote_url = "https://" .. remote_url
end
return remote_url
end
function git.open_commit_on_github()
local commit_hash = git.get_commit_hash_for_current_line()
local remote_url = git.get_remote_url()
if commit_hash == "00000000" then
print("Not committed yet")
return
end
local commit_url = remote_url .. "/commit/" .. commit_hash
u.open_url_in_browser(commit_url)
end
return git

View File

@@ -0,0 +1,53 @@
local a = require("gitsigns.actions")
local reload_nvimtree = require("nvim-tree.actions.reloaders").reload_explorer
require("gitsigns").setup({
signcolumn = true,
attach_to_untracked = false,
current_line_blame = true,
current_line_blame_opts = {
delay = 1000,
},
current_line_blame_formatter_opts = {
relative_time = true,
},
on_attach = function(bufnr)
vim.keymap.set("n", "<leader>gs", function()
a.stage_hunk()
reload_nvimtree()
end)
vim.keymap.set("n", "<leader>gu", function()
a.undo_stage_hunk()
reload_nvimtree()
end)
vim.keymap.set("n", "<leader>gr", function()
a.reset_hunk()
reload_nvimtree()
end)
vim.keymap.set("n", "<leader>gp", function()
a.preview_hunk()
reload_nvimtree()
end)
vim.keymap.set("n", "<leader>gn", function()
a.next_hunk()
reload_nvimtree()
end)
vim.keymap.set("n", "<leader>gN", function()
a.prev_hunk()
reload_nvimtree()
end)
vim.keymap.set("v", "gs", function()
a.stage_hunk()
reload_nvimtree()
end)
vim.keymap.set("v", "gu", function()
a.undo_stage_hunk()
reload_nvimtree()
end)
vim.keymap.set("v", "gr", function()
a.reset_hunk()
reload_nvimtree()
end)
end,
})

View File

@@ -1,4 +1,6 @@
local ts = require("telescope.builtin")
local u = require("user.utils")
local git = require("user.git")
-- General keymaps
vim.keymap.set("n", "<C-c>", ":nohl<CR>")
@@ -92,7 +94,7 @@ vim.keymap.set("n", "<leader>dd", function()
vim.diagnostic.open_float(nil, { focus = false, scope = "line" })
end)
vim.keymap.set("n", "<leader>dy", function()
require("user.utils").copy_diagnostic_message()
u.copy_diagnostic_message()
end)
vim.keymap.set("n", "<leader>dn", function()
vim.diagnostic.goto_next({ float = false })
@@ -105,10 +107,10 @@ vim.keymap.set("n", "<leader>do", ":lopen<CR>")
-- Git
vim.keymap.set("n", "<leader>gg", ":G<CR>")
vim.keymap.set("n", "<leader>go", function()
require("user.utils").open_commit_on_github()
git.open_commit_on_github()
end)
-- Refactoring
vim.keymap.set("n", "<leader>ri", function()
require("user.utils").lsp_organize_imports()
u.lsp_organize_imports()
end)

View File

@@ -1,5 +1,3 @@
local Job = require("plenary.job")
local M = {}
M.yank = function(message)
@@ -53,41 +51,8 @@ M.lsp_organize_imports = function(bufnr, timeout)
vim.lsp.buf_request_sync(bufnr, "workspace/executeCommand", params, timeout or 500)
end
M.get_commit_hash_for_current_line = function()
local fullpath = vim.api.nvim_buf_get_name(0)
local cwd = vim.loop.cwd()
local relative_path = string.gsub(fullpath, cwd .. "/", "")
local line = unpack(vim.api.nvim_win_get_cursor(0))
local f = io.popen("git blame -L " .. line .. ",+1 " .. relative_path, "r")
local data = f:read("*a")
f:close()
local commit_hash = vim.split(data, " ")[1]
return commit_hash
end
M.get_git_remote_url = function()
local f = io.popen("git remote get-url origin", "r")
local remote_url = f:read("*l")
f:close()
if string.sub(remote_url, 0, 4) == "git@" then
remote_url = string.gsub(remote_url, "git@", "")
remote_url = string.gsub(remote_url, ".git", "")
remote_url = string.gsub(remote_url, ":", "/")
end
if string.sub(remote_url, 0, 5) ~= "https" then
remote_url = "https://" .. remote_url
end
return remote_url
end
M.open_url_in_browser = function(url)
-- TODO: implement windows support
if vim.fn.has("win32") == 1 then
print("windows not supported, sorry")
return
@@ -97,18 +62,4 @@ M.open_url_in_browser = function(url)
f:close()
end
M.open_commit_on_github = function()
local commit_hash = M.get_commit_hash_for_current_line()
local remote_url = M.get_git_remote_url()
if commit_hash == "00000000" then
print("Not committed yet")
return
end
local commit_url = remote_url .. "/commit/" .. commit_hash
M.open_url_in_browser(commit_url)
end
return M