Skip to content

Instantly share code, notes, and snippets.

@dobrite
Created May 18, 2024 15:22
Show Gist options
  • Save dobrite/59dc5586b2dc2de120038c9b27a045d7 to your computer and use it in GitHub Desktop.
Save dobrite/59dc5586b2dc2de120038c9b27a045d7 to your computer and use it in GitHub Desktop.
local M = {}
function M.open_commit_url()
local cwd = vim.fn.getcwd()
local function is_sha()
local word = vim.fn.expand '<cword>'
return word:match '^[0-9a-fA-F]+$'
end
local function get_github_repo_url()
local handle = io.popen('git -C ' .. cwd .. ' remote get-url origin')
if handle == nil then
return nil
end
local result = handle:read '*a'
if handle:close() == nil then
return nil
end
if result then
result = result:gsub('%s+$', '')
local repo_path = result:match 'github.com[:/](.+)%.git'
return repo_path
end
return nil
end
local repo_path = get_github_repo_url()
if repo_path and is_sha() then
local sha = vim.fn.expand '<cword>'
local url = 'https://github.com/' .. repo_path .. '/commit/' .. sha
os.execute('open ' .. url)
else
local uri = vim.fn.expand '<cfile>'
vim.ui.open(uri)
end
end
vim.api.nvim_create_user_command('OpenCommitOrDefaultOpen', M.open_commit_url, {})
vim.keymap.set('n', 'gx', ':OpenCommitOrDefaultOpen<CR>', { desc = 'Open commit or default open' })
return {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment