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

feat(bare/worktree): only check remote branches if remote has 'fetch' config #143

Merged
merged 8 commits into from
Nov 22, 2023
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
21 changes: 8 additions & 13 deletions lua/gitlinker.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local range = require("gitlinker.range")
local LogLevels = require("gitlinker.logger").LogLevels
local logger = require("gitlinker.logger")
local linker = require("gitlinker.linker")
local highlight = require("gitlinker.highlight")
Expand Down Expand Up @@ -329,15 +330,14 @@ local function link(opts)
return url
end

--- @param opts gitlinker.Options?
--- @param opts gitlinker.Options
--- @return gitlinker.Options
local function _merge_routers(opts)
-- browse
local browse_routers = vim.deepcopy(Defaults.router.browse)
local browse_router_binding_opts = {}
if
type(opts) == "table"
and type(opts.router_binding) == "table"
type(opts.router_binding) == "table"
and type(opts.router_binding.browse) == "table"
then
deprecation.notify(
Expand All @@ -346,9 +346,7 @@ local function _merge_routers(opts)
browse_router_binding_opts = vim.deepcopy(opts.router_binding.browse)
end
local browse_router_opts = (
type(opts) == "table"
and type(opts.router) == "table"
and type(opts.router.browse) == "table"
type(opts.router) == "table" and type(opts.router.browse) == "table"
)
and vim.deepcopy(opts.router.browse)
or {}
Expand All @@ -364,8 +362,7 @@ local function _merge_routers(opts)
local blame_routers = vim.deepcopy(Defaults.router.blame)
local blame_router_binding_opts = {}
if
type(opts) == "table"
and type(opts.router_binding) == "table"
type(opts.router_binding) == "table"
and type(opts.router_binding.blame) == "table"
then
deprecation.notify(
Expand All @@ -374,9 +371,7 @@ local function _merge_routers(opts)
blame_router_binding_opts = vim.deepcopy(opts.router_binding.blame)
end
local blame_router_opts = (
type(opts) == "table"
and type(opts.router) == "table"
and type(opts.router.blame) == "table"
type(opts.router) == "table" and type(opts.router.blame) == "table"
)
and vim.deepcopy(opts.router.blame)
or {}
Expand All @@ -396,13 +391,13 @@ end

--- @param opts gitlinker.Options?
local function setup(opts)
local router_configs = _merge_routers(opts)
local router_configs = _merge_routers(opts or {})
Configs = vim.tbl_deep_extend("force", vim.deepcopy(Defaults), opts or {})
Configs.router = router_configs

-- logger
logger.setup({
level = Configs.debug and "DEBUG" or "INFO",
level = Configs.debug and LogLevels.DEBUG or LogLevels.INFO,
console_log = Configs.console_log,
file_log = Configs.file_log,
})
Expand Down
48 changes: 44 additions & 4 deletions lua/gitlinker/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,27 @@
return false
end

--- @package
--- @param remote string
--- @return boolean
local function _has_remote_fetch_config(remote)
local args = { "git", "config", string.format("remote.%s.fetch", remote) }
local result = cmd(args)
logger.debug(
"|git._has_remote_fetch_config| running %s: %s (error:%s)",
vim.inspect(args),
vim.inspect(result.stdout),
vim.inspect(result.stderr)
)
local output = result.stdout
for _, fetch in ipairs(output) do
if type(fetch) == "string" and string.len(vim.trim(fetch)) > 0 then
return true
end
end
return false

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

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/git.lua#L210

Added line #L210 was not covered by tests
end

--- @param host string
--- @return string?
local function resolve_host(host)
Expand Down Expand Up @@ -250,18 +271,37 @@
return upstream_rev
end

local remote_fetch_configured = _has_remote_fetch_config(remote)

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

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

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/git.lua#L279-L281

Added lines #L279 - L281 were not covered by tests
end
end
else
local head_rev = _get_rev("HEAD")
if head_rev then
return head_rev
end
end

-- try last 50 parent commits
for i = 1, 50 do
local revspec = "HEAD~" .. i
if _is_rev_in_remote(revspec, remote) then
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 rev then
return rev

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

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/git.lua#L296-L298

Added lines #L296 - L298 were not covered by tests
end
end
end
else
for i = 1, 50 do
local revspec = "HEAD~" .. i

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

View check run for this annotation

Codecov / codecov/patch

lua/gitlinker/git.lua#L303-L304

Added lines #L303 - L304 were not covered by tests
local rev = _get_rev(revspec)
if rev then
return rev
Expand Down
27 changes: 5 additions & 22 deletions lua/gitlinker/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ local PathSeperator = (vim.fn.has("win32") > 0 or vim.fn.has("win64") > 0)
and "\\"
or "/"

local Configs = {
local Defaults = {
level = LogLevels.INFO,
console_log = true,
file_log = false,
file_log_dir = vim.fn.stdpath("data"),
file_log_name = "gitlinker.log",
_file_log_path = string.format(
"%s%s%s",
vim.fn.stdpath("data"),
Expand All @@ -42,21 +40,11 @@ local Configs = {
),
}

local Configs = {}

--- @param opts gitlinker.Options?
local function setup(opts)
Configs = vim.tbl_deep_extend("force", vim.deepcopy(Configs), opts or {})
if type(Configs.level) == "string" then
Configs.level = LogLevels[Configs.level]
end

if Configs.file_log then
Configs._file_log_path = string.format(
"%s%s%s",
Configs.file_log_dir,
(vim.fn.has("win32") > 0 or vim.fn.has("win64") > 0) and "\\" or "/",
Configs.file_log_name
)
end
Configs = vim.tbl_deep_extend("force", vim.deepcopy(Defaults), opts or {})
assert(
type(Configs.level) == "number" and LogHighlights[Configs.level] ~= nil
)
Expand All @@ -72,12 +60,6 @@ local function log(level, msg)
local msg_lines = vim.split(msg, "\n", { plain = true })
if Configs.console_log and level >= LogLevels.INFO then
local msg_chunks = {}
-- local prefix = ""
-- if level == LogLevels.ERROR then
-- prefix = "error! "
-- elseif level == LogLevels.WARN then
-- prefix = "warning! "
-- end
for _, line in ipairs(msg_lines) do
table.insert(msg_chunks, {
string.format("[gitlinker] %s", --[[prefix,]] line),
Expand Down Expand Up @@ -132,6 +114,7 @@ local function ensure(cond, fmt, ...)
end

local M = {
LogLevels = LogLevels,
setup = setup,
debug = debug,
info = info,
Expand Down
5 changes: 2 additions & 3 deletions test/git_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ describe("git", function()
before_each(function()
vim.api.nvim_command("cd " .. cwd)
vim.opt.swapfile = false
local logger = require("gitlinker.logger")
logger.setup()
vim.cmd([[ edit lua/gitlinker.lua ]])
end)

local LogLevels = require("gitlinker.logger").LogLevels
local logger = require("gitlinker.logger")
logger.setup({
level = "DEBUG",
level = LogLevels.DEBUG,
console_log = true,
file_log = true,
})
Expand Down
3 changes: 2 additions & 1 deletion test/logger_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ describe("logger", function()
vim.api.nvim_command("cd " .. cwd)
end)

local LogLevels = require("gitlinker.logger").LogLevels
local logger = require("gitlinker.logger")
logger.setup({
level = "DEBUG",
level = LogLevels.DEBUG,
console_log = true,
file_log = true,
})
Expand Down
Loading