@@ -609,9 +609,13 @@ M.file_to_table = function(file_path)
609609end
610610
611611-- helper function to find the root directory of the current git repository
612+ --- @param path string | nil # optional path to start searching from
612613--- @return string # returns the path of the git root dir or an empty string if not found
613- _H .find_git_root = function ()
614+ _H .find_git_root = function (path )
614615 local cwd = vim .fn .expand (" %:p:h" )
616+ if path then
617+ cwd = vim .fn .fnamemodify (path , " :p:h" )
618+ end
615619 while cwd ~= " /" do
616620 local files = vim .fn .readdir (cwd )
617621 if vim .tbl_contains (files , " .git" ) then
@@ -641,26 +645,20 @@ M.repo_instructions = function()
641645 return table.concat (lines , " \n " )
642646end
643647
644- local function get_git_root ()
645- local handle = io.popen (" git rev-parse --show-toplevel 2>/dev/null" )
646- local result = handle :read (" *a" ):gsub (" \n " , " " )
647- handle :close ()
648- return result ~= " " and result or nil
649- end
650-
651- local function get_relative_path (full_path , git_root )
652- return git_root and full_path :sub (# git_root + 2 ) or full_path
653- end
654-
655648M .template_render = function (template , command , selection , filetype , filename )
656- local git_root = get_git_root ()
657- local relative_filename = get_relative_path (filename , git_root )
658-
649+ local git_root = _H .find_git_root (filename )
650+ if git_root ~= " " then
651+ local git_root_plus_one = vim .fn .fnamemodify (git_root , " :h" )
652+ if git_root_plus_one ~= " " then
653+ filename = filename :sub (# git_root_plus_one + 2 )
654+ end
655+ end
656+
659657 local key_value_pairs = {
660658 [" {{command}}" ] = command ,
661659 [" {{selection}}" ] = selection ,
662660 [" {{filetype}}" ] = filetype ,
663- [" {{filename}}" ] = relative_filename ,
661+ [" {{filename}}" ] = filename ,
664662 }
665663 return _H .template_render (template , key_value_pairs )
666664end
0 commit comments