Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lua/cfp/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ local defaults = {
copy_path = "<leader>cp",
copy_path_line = "<leader>cl",
copy_path_url = "<leader>cu",
copy_path_hash = "<leader>cw",
copy_path_with_hash = "<leader>cW",
},
}

Expand Down
51 changes: 51 additions & 0 deletions lua/cfp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,56 @@ function M.copy_path_url()
copy_to_clipboard(github_file_url)
end

-- Copy relative path with line number as GitHub URL with commit hash
function M.copy_path_hash()
local path = get_relative_path()
if not path then
vim.notify("No file path available", vim.log.levels.WARN)
return
end

-- Get Git remote URL
local git_remote = vim.fn.system("git config --get remote.origin.url"):gsub("\n", "")
if vim.v.shell_error ~= 0 then
vim.notify("Not in a Git repository", vim.log.levels.WARN)
return
end

-- Convert SSH/HTTPS Git URL to GitHub URL
local github_url = git_remote
if github_url:match("^git@github.com:") then
github_url = github_url:gsub("^git@github.com:", "https://github.com/")
github_url = github_url:gsub("%.git$", "")
elseif github_url:match("^https://github.com/") then
github_url = github_url:gsub("%.git$", "")
else
vim.notify("Remote origin is not a GitHub repository", vim.log.levels.WARN)
return
end

-- Get current commit hash
local commit_hash = vim.fn.system("git rev-parse HEAD"):gsub("\n", "")
if vim.v.shell_error ~= 0 then
vim.notify("Failed to get current commit hash", vim.log.levels.WARN)
return
end

local line_num = vim.api.nvim_win_get_cursor(0)[1]
local github_file_url = github_url .. "/blob/" .. commit_hash .. "/" .. path .. "#L" .. line_num

copy_to_clipboard(github_file_url)
end

-- Copy relative path (for <leader>cW)
function M.copy_path_with_hash()
local path = get_relative_path()
if not path then
vim.notify("No file path available", vim.log.levels.WARN)
return
end

copy_to_clipboard(path)
end

return M

12 changes: 12 additions & 0 deletions lua/cfp/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ M.setup = function()
desc = "Copy file path as GitHub URL"
})
end

if keymaps.copy_path_hash then
vim.keymap.set("n", keymaps.copy_path_hash, cfp.copy_path_hash, {
desc = "Copy file path as GitHub URL with commit hash"
})
end

if keymaps.copy_path_with_hash then
vim.keymap.set("n", keymaps.copy_path_with_hash, cfp.copy_path_with_hash, {
desc = "Copy file path"
})
end
end

return M