diff --git a/lua/gitlinker.lua b/lua/gitlinker.lua index 1a34f4b..157853c 100644 --- a/lua/gitlinker.lua +++ b/lua/gitlinker.lua @@ -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") @@ -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( @@ -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 {} @@ -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( @@ -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 {} @@ -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, }) diff --git a/lua/gitlinker/git.lua b/lua/gitlinker/git.lua index 7c77d76..2bc4011 100644 --- a/lua/gitlinker/git.lua +++ b/lua/gitlinker/git.lua @@ -189,6 +189,27 @@ local function _is_rev_in_remote(revspec, remote) 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 +end + --- @param host string --- @return string? local function resolve_host(host) @@ -250,8 +271,17 @@ local function get_closest_remote_compatible_rev(remote) 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 + end + end + else local head_rev = _get_rev("HEAD") if head_rev then return head_rev @@ -259,9 +289,19 @@ local function get_closest_remote_compatible_rev(remote) 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 + end + end + end + else + for i = 1, 50 do + local revspec = "HEAD~" .. i local rev = _get_rev(revspec) if rev then return rev diff --git a/lua/gitlinker/logger.lua b/lua/gitlinker/logger.lua index ec4f295..be0222b 100644 --- a/lua/gitlinker/logger.lua +++ b/lua/gitlinker/logger.lua @@ -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"), @@ -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 ) @@ -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), @@ -132,6 +114,7 @@ local function ensure(cond, fmt, ...) end local M = { + LogLevels = LogLevels, setup = setup, debug = debug, info = info, diff --git a/test/git_spec.lua b/test/git_spec.lua index c71aec9..61cbdeb 100644 --- a/test/git_spec.lua +++ b/test/git_spec.lua @@ -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, }) diff --git a/test/logger_spec.lua b/test/logger_spec.lua index ab5db8a..d305de5 100644 --- a/test/logger_spec.lua +++ b/test/logger_spec.lua @@ -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, })