Skip to content

Commit 1766376

Browse files
argshookRobitx
authored andcommitted
Add support for relative paths in template render
this changes the `{{filename}}` substitute in templates to point to a relative path from the `.git` repository. If `.git` repository is not found, then full path is used. With this change commands like `:GpChatPaste` would include a less verbose path without including the username. For example with config like this: ```lua template_selection = "from `{{filename}}`" ``` we now get ``` from `my-project/file.py` ``` previously we would get ``` from `/home/username/projects/repositories/my-project/file.py` ``` which is often excessive and also leaks the username.
1 parent 5b5f944 commit 1766376

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

lua/gp/init.lua

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,12 +641,26 @@ M.repo_instructions = function()
641641
return table.concat(lines, "\n")
642642
end
643643

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+
644655
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+
645659
local key_value_pairs = {
646660
["{{command}}"] = command,
647661
["{{selection}}"] = selection,
648662
["{{filetype}}"] = filetype,
649-
["{{filename}}"] = filename,
663+
["{{filename}}"] = relative_filename,
650664
}
651665
return _H.template_render(template, key_value_pairs)
652666
end

0 commit comments

Comments
 (0)