Skip to content

Commit

Permalink
fix: error when missing remote branch (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 authored Jun 15, 2023
1 parent 755312f commit 5b00559
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
51 changes: 34 additions & 17 deletions lua/gitlinker/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ local job = require("plenary.job")
local path = require("plenary.path")
local logger = require("gitlinker.logger")

--- @alias JobResult table<string, any>
--- @class JobResult
--- @field stdout string[]
--- @field stderr string[]

--- @param result JobResult
--- @return boolean
Expand Down Expand Up @@ -42,11 +44,11 @@ local function cmd(args, cwd)
return result
end

--- @return string[]
--- @return JobResult
local function get_remote()
local result = cmd({ "remote" })
logger.debug("[git.get_remote] result:%s", vim.inspect(result))
return result.stdout
return result
end

--- @param remote string
Expand Down Expand Up @@ -75,15 +77,15 @@ local function get_rev(revspec)
end

--- @param revspec string|nil
--- @return string|nil
--- @return JobResult
local function get_rev_name(revspec)
local result = cmd({ "rev-parse", "--abbrev-ref", revspec })
logger.debug(
"[git.get_rev_name] revspec:%s, result:%s",
vim.inspect(revspec),
vim.inspect(result)
)
return has_output(result) and result.stdout[1] or nil
return result
end

local function is_file_in_rev(file, revspec)
Expand Down Expand Up @@ -145,7 +147,7 @@ local function is_rev_in_remote(revspec, remote)
return false
end

local allowed_chars = "[_%-%w%.]+"
local UpstreamBranchAllowedChars = "[_%-%w%.]+"

local function get_closest_remote_compatible_rev(remote)
assert(remote, "remote cannot be nil")
Expand Down Expand Up @@ -182,7 +184,7 @@ local function get_closest_remote_compatible_rev(remote)
end

logger.error(
"Error! Failed to get closest revision in that exists in remote '%s'",
"fatal: failed to get closest revision in that exists in remote '%s'",
remote
)
return nil
Expand Down Expand Up @@ -210,43 +212,58 @@ end

local function get_branch_remote()
-- origin/upstream
local remotes = get_remote()
--- @type JobResult
local remote_result = get_remote()

if type(remotes) ~= "table" or #remotes == 0 then
logger.error("Error! Git repository has no remote")
if type(remote_result.stdout) ~= "table" or #remote_result.stdout == 0 then
if #remote_result.stderr > 0 then
logger.error("%s", remote_result.stderr[1])
else
logger.error("fatal: git repository has no remote")
end
return nil
end

if #remotes == 1 then
return remotes[1]
if #remote_result.stdout == 1 then
return remote_result.stdout[1]
end

-- origin/linrongbin16/add-rule2
local upstream_branch = get_rev_name("@{u}")
if not upstream_branch then
--- @type JobResult
local upstream_branch_result = get_rev_name("@{u}")
if not has_output(upstream_branch_result) then
if #upstream_branch_result.stderr > 0 then
logger.error("%s", upstream_branch_result.stderr[1])
else
logger.error("fatal: git branch has no remote")
end
return nil
end

--- @type string
local upstream_branch = upstream_branch_result.stdout[1]
-- origin
--- @type string
local remote_from_upstream_branch =
upstream_branch:match("^(" .. allowed_chars .. ")%/")
upstream_branch:match("^(" .. UpstreamBranchAllowedChars .. ")%/")

if not remote_from_upstream_branch then
logger.error(
"Error! Cannot parse remote name from remote branch '%s'",
"fatal: cannot parse remote name from remote branch '%s'",
upstream_branch
)
return nil
end

local remotes = remote_result.stdout
for _, remote in ipairs(remotes) do
if remote_from_upstream_branch == remote then
return remote
end
end

logger.error(
"Error! Parsed remote '%s' from remote branch '%s' is not a valid remote",
"fatal: parsed remote '%s' from remote branch '%s' is not a valid remote",
remote_from_upstream_branch,
upstream_branch
)
Expand Down
9 changes: 2 additions & 7 deletions lua/gitlinker/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ local function log(level, msg)
if Config.console then
vim.cmd("echohl " .. EchoHl[level])
for _, line in ipairs(msg_lines) do
vim.cmd(
string.format(
'echom "%s"',
vim.fn.escape(string.format("gitlinker: %s", line), '"')
)
)
vim.cmd(string.format('echom "%s"', vim.fn.escape(line, '"')))
end
vim.cmd("echohl None")
end
Expand All @@ -43,7 +38,7 @@ local function log(level, msg)
for _, line in ipairs(msg_lines) do
fp:write(
string.format(
"gitlinker: %s [%s] - %s\n",
"%s [%s] - %s\n",
os.date("%Y-%m-%d %H:%M:%S"),
level,
line
Expand Down

0 comments on commit 5b00559

Please sign in to comment.