Change root dir to git root using git command

This commit is contained in:
Uthman Mohamed 2024-03-13 17:27:38 -04:00
parent ad6eb7ed0b
commit dec4c0f451

View file

@ -129,10 +129,9 @@ map("n", "<leader>ub", function() Util.toggle("background", false, {"light", "da
map("n", "<leader>gg", function() Util.terminal({ "lazygit" }, { cwd = Util.root(), esc_esc = false, ctrl_hjkl = false }) end, { desc = "Lazygit (root dir)" }) map("n", "<leader>gg", function() Util.terminal({ "lazygit" }, { cwd = Util.root(), esc_esc = false, ctrl_hjkl = false }) end, { desc = "Lazygit (root dir)" })
map("n", "<leader>gG", function() Util.terminal({ "lazygit" }, {esc_esc = false, ctrl_hjkl = false}) end, { desc = "Lazygit (cwd)" }) map("n", "<leader>gG", function() Util.terminal({ "lazygit" }, {esc_esc = false, ctrl_hjkl = false}) end, { desc = "Lazygit (cwd)" })
-- Function to get the current file path from the root directory local current_file_path_from_git_root = function()
local current_file_path_from_root_dir = function()
local path = vim.fn.expand("%:p") local path = vim.fn.expand("%:p")
local root = Util.root() local root = vim.fn.system("git -C " .. path .. " rev-parse --show-toplevel"):gsub("\n$", "")
if not path:find(root, 1, true) then if not path:find(root, 1, true) then
return path return path
end end
@ -140,7 +139,7 @@ local current_file_path_from_root_dir = function()
end end
map("n", "<leader>gf", function() map("n", "<leader>gf", function()
Util.terminal({ "lazygit", "-f", current_file_path_from_root_dir() }, { esc_esc = false, ctrl_hjkl = false }) Util.terminal({ "lazygit", "-f", current_file_path_from_git_root() }, { esc_esc = false, ctrl_hjkl = false })
end, { desc = "Lazygit current file history" }) end, { desc = "Lazygit current file history" })
-- quit -- quit