@@ -368,7 +368,9 @@ local runner = require("neogit.runner")
368368--- @field verify -commit GitCommandVerifyCommit
369369--- @field worktree GitCommandWorktree
370370--- @field write -tree GitCommandWriteTree
371- --- @field git_root fun ( dir : string ): string
371+ --- @field worktree_root fun ( dir : string ): string
372+ --- @field git_dir fun ( dir : string ): string
373+ --- @field worktree_git_dir fun ( dir : string ): string
372374--- @field is_inside_worktree fun ( dir : string ): boolean
373375--- @field history ProcessResult[]
374376
@@ -965,15 +967,34 @@ local configurations = {
965967 [" bisect" ] = config {},
966968}
967969
968- --- NOTE: Use require("neogit.lib.git").repo.git_root instead of calling this function.
969- --- repository.git_root is used by all other library functions, so it's most likely the one you want to use.
970- --- git_root_of_cwd () returns the git repo of the cwd, which can change anytime
971- --- after git_root_of_cwd () has been called.
970+ --- NOTE: Use require("neogit.lib.git").repo.worktree_root instead of calling this function.
971+ --- repository.worktree_root is used by all other library functions, so it's most likely the one you want to use.
972+ --- worktree_root_of_cwd () returns the git repo of the cwd, which can change anytime
973+ --- after worktree_root_of_cwd () has been called.
972974--- @param dir string
973- --- @return string
974- local function git_root (dir )
975- local cmd = { " git" , " -C" , dir , " rev-parse" , " --show-toplevel" }
975+ --- @return string Absolute path of current worktree
976+ local function worktree_root (dir )
977+ local cmd = { " git" , " -C" , dir , " rev-parse" , " --show-toplevel" , " --path-format=absolute " }
976978 local result = vim .system (cmd , { text = true }):wait ()
979+
980+ return Path :new (vim .trim (result .stdout )):absolute ()
981+ end
982+
983+ --- @param dir string
984+ --- @return string Absolute path of ` .git/` directory
985+ local function git_dir (dir )
986+ local cmd = { " git" , " -C" , dir , " rev-parse" , " --git-common-dir" , " --path-format=absolute" }
987+ local result = vim .system (cmd , { text = true }):wait ()
988+
989+ return Path :new (vim .trim (result .stdout )):absolute ()
990+ end
991+
992+ --- @param dir string
993+ --- @return string Absolute path of ` .git/` directory
994+ local function worktree_git_dir (dir )
995+ local cmd = { " git" , " -C" , dir , " rev-parse" , " --git-dir" , " --path-format=absolute" }
996+ local result = vim .system (cmd , { text = true }):wait ()
997+
977998 return Path :new (vim .trim (result .stdout )):absolute ()
978999end
9791000
9821003local function is_inside_worktree (dir )
9831004 local cmd = { " git" , " -C" , dir , " rev-parse" , " --is-inside-work-tree" }
9841005 local result = vim .system (cmd ):wait ()
1006+
9851007 return result .code == 0
9861008end
9871009
@@ -1152,7 +1174,7 @@ local function new_builder(subcommand)
11521174
11531175 return process .new {
11541176 cmd = cmd ,
1155- cwd = git .repo .git_root ,
1177+ cwd = git .repo .worktree_root ,
11561178 env = state .env ,
11571179 input = state .input ,
11581180 on_error = opts .on_error ,
@@ -1229,7 +1251,9 @@ local meta = {
12291251
12301252local cli = setmetatable ({
12311253 history = runner .history ,
1232- git_root = git_root ,
1254+ worktree_root = worktree_root ,
1255+ worktree_git_dir = worktree_git_dir ,
1256+ git_dir = git_dir ,
12331257 is_inside_worktree = is_inside_worktree ,
12341258}, meta )
12351259
0 commit comments