Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix repository root detection #232

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 49 additions & 36 deletions lua/gitlinker/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
--- NOTE: async functions can't have optional parameters so wrap it into another function without '_'
local _run_cmd = async.wrap(function(args, cwd, callback)
local result = CmdResult:new()
local logger = logging.get("gitlinker")
logger:debug(string.format("|_run_cmd| args:%s, cwd:%s", vim.inspect(args), vim.inspect(cwd)))

spawn.run(args, {
cwd = cwd,
Expand All @@ -70,10 +72,11 @@
end

--- @package
--- @param cwd string?
--- @return string[]|nil
local function _get_remote()
local function _get_remote(cwd)
local args = { "git", "remote" }
local result = run_cmd(args)
local result = run_cmd(args, cwd)
if type(result.stdout) ~= "table" or #result.stdout == 0 then
result:print_err("fatal: git repo has no remote")
return nil
Expand All @@ -87,11 +90,12 @@
end

--- @param remote string
--- @param cwd string?
--- @return string?
local function get_remote_url(remote)
local function get_remote_url(remote, cwd)
assert(remote, "remote cannot be nil")
local args = { "git", "remote", "get-url", remote }
local result = run_cmd(args)
local result = run_cmd(args, cwd)
if not result:has_out() then
result:print_err("fatal: failed to get remote url by remote '" .. remote .. "'")
return nil
Expand All @@ -106,10 +110,11 @@

--- @package
--- @param revspec string?
--- @param cwd string?
--- @return string?
local function _get_rev(revspec)
local function _get_rev(revspec, cwd)
local args = { "git", "rev-parse", revspec }
local result = run_cmd(args)
local result = run_cmd(args, cwd)
-- logger.debug(
-- "|git._get_rev| running %s: %s (error:%s)",
-- vim.inspect(args),
Expand All @@ -121,10 +126,11 @@

--- @package
--- @param revspec string
--- @param cwd string?
--- @return string?
local function _get_rev_name(revspec)
local function _get_rev_name(revspec, cwd)
local args = { "git", "rev-parse", "--abbrev-ref", revspec }
local result = run_cmd(args)
local result = run_cmd(args, cwd)
if not result:has_out() then
result:print_err("fatal: git branch has no remote")
return nil
Expand All @@ -139,10 +145,11 @@

--- @param file string
--- @param revspec string
--- @param cwd string?
--- @return boolean
local function is_file_in_rev(file, revspec)
local function is_file_in_rev(file, revspec, cwd)
local args = { "git", "cat-file", "-e", revspec .. ":" .. file }
local result = run_cmd(args)
local result = run_cmd(args, cwd)

Check warning on line 152 in lua/gitlinker/git.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/git.lua#L152

Added line #L152 was not covered by tests
if result:has_err() then
result:print_err("fatal: '" .. file .. "' does not exist in remote '" .. revspec .. "'")
return false
Expand All @@ -157,10 +164,11 @@

--- @param file string
--- @param rev string
--- @param cwd string?
--- @return boolean
local function file_has_changed(file, rev)
local function file_has_changed(file, rev, cwd)
local args = { "git", "diff", rev, "--", file }
local result = run_cmd(args)
local result = run_cmd(args, cwd)

Check warning on line 171 in lua/gitlinker/git.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/git.lua#L171

Added line #L171 was not covered by tests
-- logger.debug(
-- "|git.has_file_changed| running %s: %s",
-- vim.inspect(args),
Expand All @@ -172,10 +180,11 @@
--- @package
--- @param revspec string
--- @param remote string
--- @param cwd string?
--- @return boolean
local function _is_rev_in_remote(revspec, remote)
local function _is_rev_in_remote(revspec, remote, cwd)
local args = { "git", "branch", "--remotes", "--contains", revspec }
local result = run_cmd(args)
local result = run_cmd(args, cwd)
-- logger.debug(
-- "|git._is_rev_in_remote| running %s: %s (error:%s)",
-- vim.inspect(args),
Expand All @@ -193,10 +202,11 @@

--- @package
--- @param remote string
--- @param cwd string?
--- @return boolean
local function _has_remote_fetch_config(remote)
local function _has_remote_fetch_config(remote, cwd)
local args = { "git", "config", string.format("remote.%s.fetch", remote) }
local result = run_cmd(args)
local result = run_cmd(args, cwd)
-- logger.debug(
-- "|git._has_remote_fetch_config| running %s: %s (error:%s)",
-- vim.inspect(args),
Expand Down Expand Up @@ -258,13 +268,14 @@
end

--- @param remote string
--- @param cwd string?
--- @return string?
local function get_closest_remote_compatible_rev(remote)
local function get_closest_remote_compatible_rev(remote, cwd)
local logger = logging.get("gitlinker")
assert(remote, "remote cannot be nil")

-- try upstream branch HEAD (a.k.a @{u})
local upstream_rev = _get_rev("@{u}")
local upstream_rev = _get_rev("@{u}", cwd)
-- logger.debug(
-- "|git.get_closest_remote_compatible_rev| running _get_rev:%s",
-- vim.inspect(upstream_rev)
Expand All @@ -277,14 +288,14 @@

-- try HEAD
if remote_fetch_configured then
if _is_rev_in_remote("HEAD", remote) then
local head_rev = _get_rev("HEAD")
if _is_rev_in_remote("HEAD", remote, cwd) then
local head_rev = _get_rev("HEAD", cwd)

Check warning on line 292 in lua/gitlinker/git.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/git.lua#L292

Added line #L292 was not covered by tests
if head_rev then
return head_rev
end
end
else
local head_rev = _get_rev("HEAD")
local head_rev = _get_rev("HEAD", cwd)
if head_rev then
return head_rev
end
Expand All @@ -294,8 +305,8 @@
if remote_fetch_configured then
for i = 1, 50 do
local revspec = "HEAD~" .. i
if _is_rev_in_remote(revspec, remote) then
local rev = _get_rev(revspec)
if _is_rev_in_remote(revspec, remote, cwd) then
local rev = _get_rev(revspec, cwd)

Check warning on line 309 in lua/gitlinker/git.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/git.lua#L309

Added line #L309 was not covered by tests
if rev then
return rev
end
Expand All @@ -304,15 +315,15 @@
else
for i = 1, 50 do
local revspec = "HEAD~" .. i
local rev = _get_rev(revspec)
local rev = _get_rev(revspec, cwd)

Check warning on line 318 in lua/gitlinker/git.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/git.lua#L318

Added line #L318 was not covered by tests
if rev then
return rev
end
end
end

-- try remote HEAD
local remote_rev = _get_rev(remote)
local remote_rev = _get_rev(remote, cwd)
if remote_rev then
return remote_rev
end
Expand All @@ -321,12 +332,11 @@
return nil
end

--- @param cwd string?
--- @return string?
local function get_root()
local buf_path = vim.api.nvim_buf_get_name(0)
local buf_dir = vim.fn.fnamemodify(buf_path, ":p:h")
local function get_root(cwd)
local args = { "git", "rev-parse", "--show-toplevel" }
local result = run_cmd(args, buf_dir)
local result = run_cmd(args, cwd)
-- logger.debug(
-- "|git.get_root| buf_path:%s, buf_dir:%s, result:%s",
-- vim.inspect(buf_path),
Expand All @@ -346,11 +356,12 @@
return result.stdout[1]
end

--- @param cwd string?
--- @return string?
local function get_branch_remote()
local function get_branch_remote(cwd)
local logger = logging.get("gitlinker")
-- origin/upstream
local remotes = _get_remote()
local remotes = _get_remote(cwd)
if not remotes then
return nil
end
Expand All @@ -360,7 +371,7 @@
end

-- origin/linrongbin16/add-rule2
local upstream_branch = _get_rev_name("@{u}")
local upstream_branch = _get_rev_name("@{u}", cwd)

Check warning on line 374 in lua/gitlinker/git.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/git.lua#L374

Added line #L374 was not covered by tests
if not upstream_branch then
return nil
end
Expand Down Expand Up @@ -393,11 +404,12 @@
end

--- @param remote string
--- @param cwd string?
--- @return string?
local function get_default_branch(remote)
local function get_default_branch(remote, cwd)
local logger = logging.get("gitlinker")
local args = { "git", "rev-parse", "--abbrev-ref", string.format("%s/HEAD", remote) }
local result = run_cmd(args)
local result = run_cmd(args, cwd)
if type(result.stdout) ~= "table" or #result.stdout == 0 then
return nil
end
Expand All @@ -412,11 +424,12 @@
return splits[#splits]
end

--- @param cwd string?
--- @return string?
local function get_current_branch()
local function get_current_branch(cwd)
local logger = logging.get("gitlinker")
local args = { "git", "rev-parse", "--abbrev-ref", "HEAD" }
local result = run_cmd(args)
local result = run_cmd(args, cwd)
if type(result.stdout) ~= "table" or #result.stdout == 0 then
return nil
end
Expand Down
39 changes: 30 additions & 9 deletions lua/gitlinker/linker.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
local logging = require("gitlinker.commons.logging")
local str = require("gitlinker.commons.str")
local git = require("gitlinker.git")
local path = require("gitlinker.path")
local giturlparser = require("gitlinker.giturlparser")
local async = require("gitlinker.commons.async")
local uri = require("gitlinker.commons.uri")

--- @return string?
local function _get_buf_dir()
local logger = logging.get("gitlinker")
local buf_path = vim.api.nvim_buf_get_name(0)
local buf_dir = vim.fn.fnamemodify(buf_path, ":p:h")
logger:debug(
string.format(
"|_get_buf_dir| buf_path:%s, buf_dir:%s",
vim.inspect(buf_path),
vim.inspect(buf_dir)
)
)
if str.empty(buf_dir) or vim.fn.isdirectory(buf_dir or "") <= 0 then
return nil

Check warning on line 22 in lua/gitlinker/linker.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/linker.lua#L22

Added line #L22 was not covered by tests
end
return buf_dir
end

--- @alias gitlinker.Linker {remote_url:string,protocol:string?,username:string?,password:string?,host:string,port:string?,org:string?,user:string?,repo:string,rev:string,file:string,lstart:integer,lend:integer,file_changed:boolean,default_branch:string?,current_branch:string?}
--- @param remote string?
--- @return gitlinker.Linker?
local function make_linker(remote)
local logger = logging.get("gitlinker")
local cwd = _get_buf_dir()

local root = git.get_root()
local root = git.get_root(cwd)
if not root then
return nil
end

remote = (type(remote) == "string" and string.len(remote) > 0) and remote
or git.get_branch_remote()
if str.empty(remote) then
remote = git.get_branch_remote(cwd)
end
if not remote then
return nil
end
-- logger.debug("|linker - Linker:make| remote:%s", vim.inspect(remote))

local remote_url = git.get_remote_url(remote)
local remote_url = git.get_remote_url(remote, cwd)
if not remote_url then
return nil
end
Expand Down Expand Up @@ -55,7 +76,7 @@
-- vim.inspect(remote_url)
-- )

local rev = git.get_closest_remote_compatible_rev(remote)
local rev = git.get_closest_remote_compatible_rev(remote, cwd)
if not rev then
return nil
end
Expand All @@ -70,7 +91,7 @@
-- vim.inspect(buf_path_on_root)
-- )

local file_in_rev_result = git.is_file_in_rev(buf_path_on_root, rev)
local file_in_rev_result = git.is_file_in_rev(buf_path_on_root, rev, cwd)

Check warning on line 94 in lua/gitlinker/linker.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/linker.lua#L94

Added line #L94 was not covered by tests
if not file_in_rev_result then
return nil
end
Expand All @@ -81,14 +102,14 @@

async.scheduler()
local buf_path_on_cwd = path.buffer_relpath() --[[@as string]]
local file_changed = git.file_has_changed(buf_path_on_cwd, rev)
local file_changed = git.file_has_changed(buf_path_on_cwd, rev, cwd)

Check warning on line 105 in lua/gitlinker/linker.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/linker.lua#L105

Added line #L105 was not covered by tests
-- logger.debug(
-- "|linker - Linker:make| buf_path_on_cwd:%s",
-- vim.inspect(buf_path_on_cwd)
-- )

local default_branch = git.get_default_branch(remote)
local current_branch = git.get_current_branch()
local default_branch = git.get_default_branch(remote, cwd)
local current_branch = git.get_current_branch(cwd)

Check warning on line 112 in lua/gitlinker/linker.lua

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/linker.lua#L111-L112

Added lines #L111 - L112 were not covered by tests

local o = {
remote_url = remote_url,
Expand Down
Loading