Neovim: Instantly Find The PR That Updated a Line in GitHub

Neovim: Instantly Find The PR That Updated a Line in GitHub
Photo by Marc-Olivier Jodoin / Unsplash

The GitLens extension in vscode makes it easy to get extra blame info for each line like author, associated PR, file diff, previews, etc. You can replicate most of its functionality in neovim via the gitsigns.nvim plugin except being able to jump to a line's associated PR.

I managed to do just that by leveraging the gh cli from GitHub and a bunch of awk commands. Here's a snippet you can copy-paste:

-- Searches for the PR that last updated the current line in github
-- Depends on the `gh` cli
vim.keymap.set('n', '<leader>pr', function()
  local lineNum = vim.api.nvim__buf_stats(0).current_lnum
  local fileName = vim.fn.expand('%')
  local commitHashCommand = 'git blame -L' .. lineNum .. ',' .. lineNum .. ' ' .. fileName .. " | awk '{print $1}'"
  local repoCommand = [[git remote get-url origin | awk -F':' '{split($2, a, ".git"); print a[1]}']]

  vim.fn.system('gh search prs --web --repo' .. ' `' .. repoCommand .. '`' .. ' `' .. commitHashCommand .. '`')
end)
Neovim keymap for going to associated PR