Skip to content

Commit

Permalink
fix(githubUrl): allow opening repos when there are unpushed commits
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Dec 24, 2024
1 parent 6201d83 commit 1843f96
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lua/tinygit/commands/github.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,23 @@ function M.githubUrl(what)
if u.notInGitRepo() then return end
local repo = M.getGithubRemote()
if not repo then return end -- not on github

-- just open repo url
if what == "repo" then
local url = "https://github.com/" .. repo
vim.ui.open(url)
vim.fn.setreg("+", url)
return
end

-- GUARD unpushed commits
local alreadyPushed = u.syncShellCmd { "git", "branch", "--remote", "--contains", "HEAD" } ~= ""
if not alreadyPushed then
u.notify("Cannot open at GitHub, current commit has not been pushed yet.", "warn")
return
end

-- PARAMETERS
if not what then what = "file" end
local filepath = vim.api.nvim_buf_get_name(0)
local gitroot = u.syncShellCmd { "git", "rev-parse", "--show-toplevel" }
local pathInRepo = filepath:sub(#gitroot + 2)
Expand All @@ -46,7 +55,7 @@ function M.githubUrl(what)
local location = ""
local mode = vim.fn.mode()

if what ~= "repo" and mode:find("[Vv]") then
if mode:find("[Vv]") then
vim.cmd.normal { mode, bang = true } -- leave visual mode, so marks are set
local startLn = vim.api.nvim_buf_get_mark(0, "<")[1]
local endLn = vim.api.nvim_buf_get_mark(0, ">")[1]
Expand All @@ -58,10 +67,8 @@ function M.githubUrl(what)
location = "#L" .. endLn .. "-L" .. startLn
end
end
if what ~= "repo" then
local type = what == "blame" and "blame" or "blob"
url = url .. ("/%s/%s/%s%s"):format(type, hash, pathInRepoEncoded, location)
end
local type = what == "blame" and "blame" or "blob"
url = url .. ("/%s/%s/%s%s"):format(type, hash, pathInRepoEncoded, location)

vim.ui.open(url)
vim.fn.setreg("+", url) -- copy to clipboard
Expand Down

0 comments on commit 1843f96

Please sign in to comment.