organize git functions in git.lua
This commit is contained in:
@@ -151,7 +151,7 @@ require("nvim-autopairs").setup({})
|
|||||||
require("user.file-tree")
|
require("user.file-tree")
|
||||||
require("user.treesitter")
|
require("user.treesitter")
|
||||||
require("user.telescope")
|
require("user.telescope")
|
||||||
require("user.git")
|
require("user.gitsigns")
|
||||||
require("user.comp")
|
require("user.comp")
|
||||||
require("user.lsp")
|
require("user.lsp")
|
||||||
require("user.comment")
|
require("user.comment")
|
||||||
|
|||||||
@@ -1,53 +1,55 @@
|
|||||||
local a = require("gitsigns.actions")
|
local u = require("user.utils")
|
||||||
local reload_nvimtree = require("nvim-tree.actions.reloaders").reload_explorer
|
|
||||||
|
|
||||||
require("gitsigns").setup({
|
local git = {}
|
||||||
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()
|
--- @return string commit_hash
|
||||||
a.stage_hunk()
|
function git.get_commit_hash_for_current_line()
|
||||||
reload_nvimtree()
|
local fullpath = vim.api.nvim_buf_get_name(0)
|
||||||
end)
|
local cwd = vim.loop.cwd()
|
||||||
vim.keymap.set("v", "gu", function()
|
local relative_path = string.gsub(fullpath, cwd .. "/", "")
|
||||||
a.undo_stage_hunk()
|
|
||||||
reload_nvimtree()
|
local line = unpack(vim.api.nvim_win_get_cursor(0))
|
||||||
end)
|
|
||||||
vim.keymap.set("v", "gr", function()
|
local f = io.popen("git blame -L " .. line .. ",+1 " .. relative_path, "r")
|
||||||
a.reset_hunk()
|
local data = f:read("*a")
|
||||||
reload_nvimtree()
|
f:close()
|
||||||
end)
|
|
||||||
end,
|
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
|
||||||
|
|||||||
53
neovim/.config/nvim/lua/user/gitsigns.lua
Normal file
53
neovim/.config/nvim/lua/user/gitsigns.lua
Normal 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,
|
||||||
|
})
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
local ts = require("telescope.builtin")
|
local ts = require("telescope.builtin")
|
||||||
|
local u = require("user.utils")
|
||||||
|
local git = require("user.git")
|
||||||
|
|
||||||
-- General keymaps
|
-- General keymaps
|
||||||
vim.keymap.set("n", "<C-c>", ":nohl<CR>")
|
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" })
|
vim.diagnostic.open_float(nil, { focus = false, scope = "line" })
|
||||||
end)
|
end)
|
||||||
vim.keymap.set("n", "<leader>dy", function()
|
vim.keymap.set("n", "<leader>dy", function()
|
||||||
require("user.utils").copy_diagnostic_message()
|
u.copy_diagnostic_message()
|
||||||
end)
|
end)
|
||||||
vim.keymap.set("n", "<leader>dn", function()
|
vim.keymap.set("n", "<leader>dn", function()
|
||||||
vim.diagnostic.goto_next({ float = false })
|
vim.diagnostic.goto_next({ float = false })
|
||||||
@@ -105,10 +107,10 @@ vim.keymap.set("n", "<leader>do", ":lopen<CR>")
|
|||||||
-- Git
|
-- Git
|
||||||
vim.keymap.set("n", "<leader>gg", ":G<CR>")
|
vim.keymap.set("n", "<leader>gg", ":G<CR>")
|
||||||
vim.keymap.set("n", "<leader>go", function()
|
vim.keymap.set("n", "<leader>go", function()
|
||||||
require("user.utils").open_commit_on_github()
|
git.open_commit_on_github()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- Refactoring
|
-- Refactoring
|
||||||
vim.keymap.set("n", "<leader>ri", function()
|
vim.keymap.set("n", "<leader>ri", function()
|
||||||
require("user.utils").lsp_organize_imports()
|
u.lsp_organize_imports()
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
local Job = require("plenary.job")
|
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
M.yank = function(message)
|
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)
|
vim.lsp.buf_request_sync(bufnr, "workspace/executeCommand", params, timeout or 500)
|
||||||
end
|
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)
|
M.open_url_in_browser = function(url)
|
||||||
|
-- TODO: implement windows support
|
||||||
if vim.fn.has("win32") == 1 then
|
if vim.fn.has("win32") == 1 then
|
||||||
print("windows not supported, sorry")
|
print("windows not supported, sorry")
|
||||||
return
|
return
|
||||||
@@ -97,18 +62,4 @@ M.open_url_in_browser = function(url)
|
|||||||
f:close()
|
f:close()
|
||||||
end
|
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
|
return M
|
||||||
|
|||||||
Reference in New Issue
Block a user