chore: implement :OpenInGHFile with its utils

This commit is contained in:
Ali Almohaya 2022-09-26 22:57:58 +03:00
parent dba902f5cc
commit b712206961
No known key found for this signature in database
GPG key ID: 4B80BC43FC6007E0
3 changed files with 38 additions and 23 deletions

View file

@ -2,8 +2,6 @@ local utils = require("openingh.utils")
local M = {}
function M.setup()
-- TODO - 1: add the repo path to global var
M.cwd = vim.fn.getcwd()
local repo_url = vim.fn.system("git config --get remote.origin.url")
if repo_url:len() == 0 then
@ -12,20 +10,34 @@ function M.setup()
return
end
-- init global variables
local username_and_reponame = utils.get_username_reponame(repo_url)
M.username = username_and_reponame.username
M.reponame = username_and_reponame.reponame
M.gh_url = "https://github.com/"
M.repo_url = M.gh_url .. M.username .. "/" .. M.reponame
end
function M.openFile()
-- TODO - 1: get the url from git folder
-- TODO - 2: get the current line in the buffer and add it to the file url
-- TODO - 3: get the selected range in the buffer and add it to the file url
if M.is_no_git_origin then
utils.print_no_remote_message()
return
end
local file_path = utils.get_current_relative_file_path()
local current_branch_name = utils.get_current_branch()
local file_page_url = M.repo_url .. "/blob/" .. current_branch_name .. file_path
local result = utils.open_url(file_page_url)
if result then
print("Opening url " .. file_page_url)
else
print("Unknown OS please open report")
end
end
function M.openRepo()
@ -34,23 +46,20 @@ function M.openRepo()
return
end
local repo_url = M.gh_url .. M.username .. "/" .. M.reponame
local repo_page_url = M.repo_url
-- check if not the default branch add it to the url
local current_branch_name = utils.get_current_branch()
local default_branch_name = utils.get_defualt_branch()
print(current_branch_name)
print(default_branch_name)
if current_branch_name ~= default_branch_name then
repo_url = repo_url .. "/tree/" .. current_branch_name
repo_page_url = M.repo_url .. "/tree/" .. current_branch_name
end
local result = utils.open_url(repo_url)
local result = utils.open_url(repo_page_url)
if result then
print("Opening url " .. repo_url)
print("Opening url " .. repo_page_url)
else
print("Unknown OS please open report")
end

View file

@ -13,7 +13,7 @@ end
-- trim extra spaces and newlines
-- useful when working with git commands returned values
function M.trim(string)
return (string:gsub("^%s*(.-)%s*$", "%1"))
return (string:gsub("^%s*(.-)%s*$", "%1"))
end
-- returns the username and the reponame form the origin url in a table
@ -39,6 +39,7 @@ function M.get_username_reponame(url)
end
end
-- get the remote default branch
function M.get_defualt_branch()
-- will return origin/[branch_name]
local branch_with_origin = vim.fn.system("git rev-parse --abbrev-ref origin/HEAD")
@ -47,15 +48,26 @@ function M.get_defualt_branch()
return M.trim(branch_name)
end
-- get the active local branch
function M.get_current_branch()
local current_branch_name = vim.fn.system("git rev-parse --abbrev-ref HEAD")
return M.trim(current_branch_name)
end
-- get the active buf relative file path form the .git
function M.get_current_relative_file_path()
-- we only want the active buffer name
local absolute_file_path = vim.api.nvim_buf_get_name(0)
local git_path = vim.fn.system("git rev-parse --show-toplevel")
local relative_file_path = "/" .. string.sub(absolute_file_path, git_path:len() + 1)
return relative_file_path
end
-- opens a url in the correct OS
function M.open_url(url)
local os = M.get_os()
local os = vim.loop.os_uname().sysname
if os == "Darwin" then
io.popen("open " .. url)
return true
@ -74,11 +86,6 @@ function M.open_url(url)
return false
end
function M.get_os()
local os_name = vim.loop.os_uname().sysname
return os_name
end
function M.print_no_remote_message()
print("There is no git origin in this repo!")
end

View file

@ -1,13 +1,12 @@
if vim.g.openingh then
return
return
end
vim.g.openingh = true
vim.api.nvim_create_user_command("OpenInGHFile", function()
require("openingh").openFile()
require("openingh").openFile()
end, {})
vim.api.nvim_create_user_command("OpenInGHRepo", function()
require("openingh"):openRepo()
require("openingh"):openRepo()
end, {})