From 65e50482ad68cdb0a6076b201cd96021402a958c Mon Sep 17 00:00:00 2001 From: Hiroshi Muraoka Date: Wed, 20 Aug 2025 09:28:22 +0900 Subject: [PATCH] feat: copy path with hash Signed-off-by: Hiroshi Muraoka --- lua/cfp/config.lua | 2 ++ lua/cfp/init.lua | 51 +++++++++++++++++++++++++++++++++++++++++++++ lua/cfp/keymaps.lua | 12 +++++++++++ 3 files changed, 65 insertions(+) diff --git a/lua/cfp/config.lua b/lua/cfp/config.lua index e5b2df0..477d8d1 100644 --- a/lua/cfp/config.lua +++ b/lua/cfp/config.lua @@ -5,6 +5,8 @@ local defaults = { copy_path = "cp", copy_path_line = "cl", copy_path_url = "cu", + copy_path_hash = "cw", + copy_path_with_hash = "cW", }, } diff --git a/lua/cfp/init.lua b/lua/cfp/init.lua index 77f31f6..b627528 100644 --- a/lua/cfp/init.lua +++ b/lua/cfp/init.lua @@ -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 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 diff --git a/lua/cfp/keymaps.lua b/lua/cfp/keymaps.lua index d0a6083..17f5fee 100644 --- a/lua/cfp/keymaps.lua +++ b/lua/cfp/keymaps.lua @@ -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 \ No newline at end of file