How to automatically set cwd to submodule ? #1148
-
Hi ! I love neogit, but I have run into a thing that is a big issue for me after having migrated from vim-fugitive. If I have a git project A which has a submodule registered at include/mysubmodule, then, with neogit, I have to manually (it seems to me, but I could be wrong) run Any ideas? Thanks ! <3 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I solved it like this for myself. I added the following code to my setup, and that registers the command local function find_git_root(filepath)
local function get_folder_from_filepath(fp)
return fp:match("(.*/)")
end
filepath = get_folder_from_filepath(filepath)
-- The Git command to find the top-level directory
local git_cmd = "git -C " .. filepath .. " rev-parse --show-toplevel"
-- Execute the Git command
local git_root = vim.fn.system(git_cmd)
-- Handling errors (if the file is not in a Git repository or other issues)
if vim.v.shell_error ~= 0 then
print("NeogitRel: Error finding Git root: " .. git_root)
return nil
end
-- Trim any newlines from the output
git_root = git_root:gsub("%s+", "")
return git_root
end
local function open_neogit_from_current_file(opts)
opts = opts or {}
local gitroot = find_git_root(vim.fn.expand("%:p"))
if gitroot == nil then
print("NeogitRel: Not in a git repo")
return
end
-- Prepare options
opts = vim.tbl_extend("force", { cwd = gitroot }, opts)
require "neogit".open(opts)
end
-- Register excommand that opens Neogit from the current file
-- The command takes one argument which is passed to Neogit
vim.api.nvim_create_user_command(
'NeogitRel', -- Name of the command
function(opts)
-- Call the local function with the argument
open_neogit_from_current_file(opts.args[1])
end,
{ nargs = '?' } -- Specifies that the command takes one argument
) |
Beta Was this translation helpful? Give feedback.
I solved it like this for myself. I added the following code to my setup, and that registers the command
NeogitRel
which is a a drop-in replacement for:Neogit
but it always setscwd
to the git root.