Skip to content

Commit

Permalink
refactor(git): rewrite internal command line API (ruifm#71)
Browse files Browse the repository at this point in the history
* refactor(git): better function API

* perf(ci): add version.txt for please-release
  • Loading branch information
linrongbin16 committed Oct 20, 2023
1 parent 35aebb7 commit baed32a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
17 changes: 3 additions & 14 deletions lua/gitlinker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,12 @@ local function make_link_data(range)
vim.inspect(remote)
)

--- @type JobResult
local remote_url_result = git.get_remote_url(remote)
if not remote_url_result:has_out() then
remote_url_result:print_err(
"failed to get remote url by remote '" .. remote .. "'"
)
if not remote_url_result then
return nil
end
logger.debug(
"|make_link_data| remote_url_result(%s):%s",
vim.inspect(type(remote_url_result)),
"|make_link_data| remote_url_result:%s",
vim.inspect(remote_url_result)
)

Expand Down Expand Up @@ -254,14 +249,8 @@ local function make_link_data(range)
)
end

local remote_url = remote_url_result.stdout[1]
logger.debug(
"[make_link_data] remote_url(%s):%s",
vim.inspect(type(remote_url)),
vim.inspect(remote_url)
)
return new_linker(
remote_url,
remote_url_result,
rev,
buf_path_on_root,
range.lstart,
Expand Down
27 changes: 13 additions & 14 deletions lua/gitlinker/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,29 @@ end

--- @package
--- @return JobResult
local function get_remote()
local function _get_remote()
local result = cmd({ "git", "remote" })
logger.debug(
"|git.get_remote| result(%s):%s",
vim.inspect(type(result)),
vim.inspect(result)
)
logger.debug("|git._get_remote| result:%s", vim.inspect(result))
return result
end

--- @param remote string
--- @return JobResult
--- @return string?
local function get_remote_url(remote)
assert(remote, "remote cannot be nil")
local result = cmd({ "git", "remote", "get-url", remote })
logger.debug(
"|git.get_remote_url| remote(%s):%s, result(%s):%s",
vim.inspect(type(remote)),
"|git.get_remote_url| remote:%s, result:%s",
vim.inspect(remote),
vim.inspect(type(result)),
vim.inspect(result)
)
return result
if not result:has_out() then
result:print_err(
"failed to get remote url by remote '" .. remote .. "'"
)
return nil
end
return result.stdout[1]
end

--- @package
Expand Down Expand Up @@ -250,10 +250,10 @@ end
local function get_branch_remote()
-- origin/upstream
--- @type JobResult
local remote_result = get_remote()
local remote_result = _get_remote()

if type(remote_result.stdout) ~= "table" or #remote_result.stdout == 0 then
remote_result:print_err("git repository has no remote")
remote_result:print_err("git repo has no remote")
return nil
end

Expand All @@ -262,7 +262,6 @@ local function get_branch_remote()
end

-- origin/linrongbin16/add-rule2
--- @type JobResult
local upstream_branch_result = get_rev_name("@{u}")
if not upstream_branch_result:has_out() then
upstream_branch_result:print_err("git branch has no remote")
Expand Down
1 change: 1 addition & 0 deletions version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0

0 comments on commit baed32a

Please sign in to comment.