@@ -609,9 +609,13 @@ M.file_to_table = function(file_path)
609
609
end
610
610
611
611
-- helper function to find the root directory of the current git repository
612
+ --- @param path string | nil # optional path to start searching from
612
613
--- @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 )
614
615
local cwd = vim .fn .expand (" %:p:h" )
616
+ if path then
617
+ cwd = vim .fn .fnamemodify (path , " :p:h" )
618
+ end
615
619
while cwd ~= " /" do
616
620
local files = vim .fn .readdir (cwd )
617
621
if vim .tbl_contains (files , " .git" ) then
@@ -641,26 +645,20 @@ M.repo_instructions = function()
641
645
return table.concat (lines , " \n " )
642
646
end
643
647
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
-
655
648
M .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
+
659
657
local key_value_pairs = {
660
658
[" {{command}}" ] = command ,
661
659
[" {{selection}}" ] = selection ,
662
660
[" {{filetype}}" ] = filetype ,
663
- [" {{filename}}" ] = relative_filename ,
661
+ [" {{filename}}" ] = filename ,
664
662
}
665
663
return _H .template_render (template , key_value_pairs )
666
664
end
0 commit comments