Merge pull request #15 from PlaiyTiziano/tp/default-to-root-branch

This commit is contained in:
Ali Almohaya 2023-04-24 10:51:59 +03:00 committed by GitHub
commit 374c081409
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 32 deletions

View file

@ -39,7 +39,7 @@ function M.open_file(
-- if there is no buffer opened
if file_path == "/" then
print("There is no active file to open!")
utils.notify("There is no active file to open!", vim.log.levels.ERROR)
return
end
@ -49,10 +49,8 @@ function M.open_file(
file_page_url = file_page_url .. "#L" .. range_start .. "-L" .. range_end
end
local result = utils.open_url(file_page_url)
if result == false then
print("Unknown OS please open report")
if not utils.open_url(file_page_url) then
utils.notify("Could not open the built URL " .. file_page_url, vim.log.levels.ERROR)
end
end
@ -64,14 +62,9 @@ function M.open_repo()
return
end
local current_branch_name_or_commit_hash = utils.get_current_branch_or_commit()
local repo_page_url = M.repo_url .. "/tree/" .. current_branch_name_or_commit_hash
local result = utils.open_url(repo_page_url)
if result == false then
print("Unknown OS please open report")
local url = M.repo_url .. "/tree/" .. utils.get_current_branch_or_commit()
if not utils.open_url(url) then
utils.notify("Could not open the built URL " .. url, vim.log.levels.ERROR)
end
end

View file

@ -1,5 +1,11 @@
local M = {}
-- Notify the user that something went wrong
function M.notify(message, log_level)
print(message)
vim.notify({ message }, log_level, { title = "openingh.nvim" })
end
-- the missing split lua method to split a string
function M.split(string, char)
local array = {}
@ -46,17 +52,42 @@ function M.get_default_branch()
return M.trim(branch_name)
end
-- get the active local branch or commit when HEAD is detached
function M.get_current_branch_or_commit()
local current_branch_name = M.trim(vim.fn.system("git rev-parse --abbrev-ref HEAD"))
-- Checks if the supplied branch is available on the remote
function M.is_branch_upstreamed(branch)
local output = M.trim(vim.fn.system("git ls-remote --exit-code --heads origin " .. branch))
return output ~= ""
end
-- HEAD is detached
if current_branch_name ~= "HEAD" then
return current_branch_name
-- Get the current working branch
local function get_current_branch()
return M.trim(vim.fn.system("git rev-parse --abbrev-ref HEAD"))
end
-- Get the commit hash of the most recent commit
local function get_current_commit_hash()
return M.trim(vim.fn.system("git rev-parse HEAD"))
end
-- Checks if the supplied commit is available on the remote
function M.is_commit_upstreamed(commit_sha)
local output = M.trim(vim.fn.system('git log --format="%H" --remotes'))
return output:match(commit_sha) ~= nil
end
-- Returns the current branch or commit if they are available on remote
-- otherwise this will return the default branch of the repo
function M.get_current_branch_or_commit()
local current_branch = get_current_branch()
if current_branch ~= "HEAD" and M.is_branch_upstreamed(current_branch) then
return current_branch
end
local current_commit_hash = vim.fn.system("git rev-parse HEAD")
return M.trim(current_commit_hash)
local commit_hash = get_current_commit_hash()
if current_branch == "HEAD" and M.is_commit_upstreamed(commit_hash) then
return commit_hash
end
return M.get_default_branch()
end
-- get the active buf relative file path form the .git

View file

@ -25,14 +25,8 @@ describe("openingh should set user commands", function()
end)
end)
describe("openingh should open", function()
it("repo on :OpenInGHRepo", function()
vim.cmd("OpenInGHRepo")
local status = vim.g.OPENINGH_RESULT
assert.truthy(status)
end)
it("file on :OpenInGHFile", function()
describe("OpenInGHFile", function()
it("opens a URL that links to a file", function()
vim.cmd("e ./README.md")
vim.api.nvim_win_set_cursor(0, { 3, 0 })
vim.cmd("OpenInGHFile")
@ -41,7 +35,7 @@ describe("openingh should open", function()
assert.equal(expected, status:sub(-#expected))
end)
it("file range on :'<,'>OpenInGHFile", function()
it("opens a URL that links to a file on the selected line", function()
vim.cmd("e ./README.md")
vim.api.nvim_buf_set_mark(0, "<", 5, 0, {})
vim.api.nvim_buf_set_mark(0, ">", 5, 0, {})
@ -51,7 +45,7 @@ describe("openingh should open", function()
assert.equal(expected, status:sub(-#expected))
end)
it("file range on :'<,'>OpenInGHFile", function()
it("opens a URL that links to a file with a range of selected lines", function()
vim.cmd("e ./README.md")
vim.api.nvim_buf_set_mark(0, "<", 5, 0, {})
vim.api.nvim_buf_set_mark(0, ">", 10, 0, {})
@ -61,7 +55,7 @@ describe("openingh should open", function()
assert.equal(expected, status:sub(-#expected))
end)
it("file range using a keymap", function()
it("opens a URL to a file using a keymap", function()
vim.api.nvim_set_keymap("n", "ghf", ":OpenInGHFile <cr>", {})
vim.cmd("e ./README.md")
vim.api.nvim_win_set_cursor(0, { 2, 0 })
@ -71,3 +65,11 @@ describe("openingh should open", function()
assert.equal(expected, status:sub(-#expected))
end)
end)
describe("OpenInGHRepo", function()
it("opens a URL with a link to the repo", function()
vim.cmd("OpenInGHRepo")
local status = vim.g.OPENINGH_RESULT
assert.truthy(status)
end)
end)

28
tests/utils_spec.lua Normal file
View file

@ -0,0 +1,28 @@
vim.g.test = true
local utils = require("openingh.utils")
describe("is_branch_upstreamed", function()
it("returns false when the branch is not upstreamed", function()
local output = utils.is_branch_upstreamed("this branch probably does not exist")
assert.is.False(output)
end)
it("returns true when the branch is upstreamed", function()
local output = utils.is_branch_upstreamed("main")
assert.is.True(output)
end)
end)
describe("is_commit_upstreamed", function()
it("returns false when the commit is not upstreamed", function()
local output = utils.is_commit_upstreamed("2a69ced1af827535dd8124eeab19d5f6777decf1")
assert.is.False(output)
end)
it("returns true when the commit is upstreamed", function()
local commit = utils.trim(vim.fn.system("git rev-parse HEAD"))
local output = utils.is_commit_upstreamed(commit)
assert.is.True(output)
end)
end)