fix(gh.api): get repo from upstream remote if availble. fallback to origin

This commit is contained in:
Folke Lemaitre 2025-11-02 20:04:39 +01:00
parent 7e2d710781
commit 50436373c2
No known key found for this signature in database
GPG key ID: 9B52594D560070AB
2 changed files with 13 additions and 19 deletions

View file

@ -462,14 +462,8 @@ function M.get_branch()
local branch = vim.fn.system({ "git", "branch", "--show-current" }):gsub("\n", "")
-- Get all config in one call
local git_config = vim.fn
.system({
"git",
"config",
"--get-regexp",
("^(branch\\.%s\\.|remote\\.)"):format(branch),
})
:gsub("\n$", "")
local git_config =
vim.fn.system({ "git", "config", "--get-regexp", ("^(branch\\.%s\\.|remote\\.)"):format(branch) }):gsub("\n$", "")
local cfg = {} ---@type table<string, string>
for _, line in ipairs(vim.split(git_config, "\n")) do
@ -482,23 +476,23 @@ function M.get_branch()
-- Extract values
local remote = cfg[("branch.%s.remote"):format(branch)] or ""
local merge = cfg[("branch.%s.merge"):format(branch)] or ""
local origin_url = cfg["remote.origin.url"] or ""
-- Get fork URL (either named remote or direct URL)
local url = (remote:match("^https://") or remote:match("^git@")) and remote
or cfg[("remote.%s.url"):format(remote)]
or remote
-- Parse author from fork URL
local author ---@type string?
if url ~= "" then
---@type string?
local owner_repo = url:match("github%.com[:/](.+/.+)%.git") or url:match("github%.com[:/](.+/.+)$")
author = owner_repo and owner_repo:match("^([^/]+)/") or nil
---@param u? string
---@return string?
local function parse(u)
return u and (u:match("github%.com[:/](.+/.+)%.git") or u:match("github%.com[:/](.+/.+)$")) or nil
end
-- Parse repo from origin
local repo = origin_url:match("github%.com[:/](.+/.+)%.git") or origin_url:match("github%.com[:/](.+/.+)$")
-- Parse author from fork URL
local author = (parse(url) or ""):match("^([^/]+)/")
-- Parse repo from upstream or origin
local repo = parse(cfg["remote.upstream.url"]) or parse(cfg["remote.origin.url"])
-- Parse head from merge ref
local head = merge:match("^refs/heads/(.+)$") or branch

View file

@ -179,8 +179,8 @@
---@class snacks.gh.api.Branch
---@field url string URL of the remote branch
---@field author string owner of the remote branch
---@field repo string owner/name format
---@field author? string owner of the remote branch
---@field repo? string owner/name format
---@field branch string local branch name
---@field base string branch we want to merge into
---@field head string branch we want to merge from