update GitUrl to work better with github

This commit is contained in:
JesseBot 2024-11-18 20:20:47 +01:00
parent dc5a8e9a39
commit 7c57103793
No known key found for this signature in database
2 changed files with 31 additions and 12 deletions

View file

@ -67,4 +67,4 @@ require('user.completion')
require('user.nvim-colorizer')
-- experiment to give you a remote git permalink URL for your your current line
-- require('user.git_url')
require('user.git_url')

View file

@ -6,28 +6,47 @@ local function get_git_url()
-- split into git@github.com and jessebot/onboardme.git
local base_domain, owner_repo = string.match(remote_origin, "(.*):(.*)")
print(base_domain)
-- this only works on github right now
if string.find(base_domain, "github") then
-- get the name of the current opened file
local current_file = vim.fn.expand('%:p')
if string.find(base_domain, "github.com") then
local gh_url = "https://github.com/"
-- git repo root directory
local git_root_dir = vim.fn.system("git rev-parse --show-toplevel")
remote_file_path = current_file:gsub(git_root_dir, "")
print(remote_file_path)
local git_root_dir = vim.fn.system("git rev-parse --show-prefix"):gsub("[\n\r]", " ")
-- get the name of the current opened file
local current_file = vim.fn.expand('%:t')
-- get the current line number
local current_line = unpack(vim.api.nvim_win_get_cursor(0))
file_line = remote_file_path .. "#L" .. current_line
-- combine file path, naem of file, and line number
local file_line = git_root_dir .. current_file .. "#L" .. current_line
file_line = string.gsub(file_line, "%s+", "")
-- get name of branch
local branch = vim.fn.system("git branch --show")
-- changes jessebot/onboardme.git to jessebot/onboardme/blob/main/
repo_branch_url = owner_repo:gsub(".git", "/blob/" .. branch)
local repo_branch_url = owner_repo:gsub(".git", "/blob/" .. branch)
local gh_url = "https://github.com/" .. repo_branch_url .. "/" .. file_line
-- combines https://github.com/ + jessebot/onboardme/blob/main/ + path/file.txt#1
gh_url = gh_url .. repo_branch_url .. "/" .. file_line
-- remove all new lines and white space
gh_url = string.gsub(gh_url, "%s+", "")
gh_url = string.gsub(gh_url, "[\n\r]", "")
print("External git link is:")
print(gh_url)
end
end
get_git_url()
-- ------------------------------ open bpython -------------------------------
-- install bpython beforehand: brew install bpython
vim.api.nvim_create_user_command(
'GitUrl',
function()
get_git_url()
end,
{ bang = true }
)