From 7a74ea1e7492cfe86f252b30a4958ddc937ed09b Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 21 Nov 2023 15:42:31 +0800 Subject: [PATCH 1/8] feat(bare/worktree): only check remote branches if remote has 'fetch' configured --- lua/gitlinker/git.lua | 46 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/lua/gitlinker/git.lua b/lua/gitlinker/git.lua index 7c77d76..9974c4d 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) @@ -251,7 +272,14 @@ local function get_closest_remote_compatible_rev(remote) end -- try HEAD - if _is_rev_in_remote("HEAD", remote) then + if _has_remote_fetch_config(remote) 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 +287,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 _has_remote_fetch_config(remote) 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 From dae832e11867218538fb159ea5293b0324f69164 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 21 Nov 2023 15:56:09 +0800 Subject: [PATCH 2/8] chore --- lua/gitlinker.lua | 3 ++- lua/gitlinker/logger.lua | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lua/gitlinker.lua b/lua/gitlinker.lua index 1a34f4b..045d872 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") @@ -402,7 +403,7 @@ local function setup(opts) -- 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/logger.lua b/lua/gitlinker/logger.lua index ec4f295..350ea6d 100644 --- a/lua/gitlinker/logger.lua +++ b/lua/gitlinker/logger.lua @@ -132,6 +132,7 @@ local function ensure(cond, fmt, ...) end local M = { + LogLevels = LogLevels, setup = setup, debug = debug, info = info, From 1d954134c1868f96f5a0fa532a6507ea7d546918 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 21 Nov 2023 15:58:02 +0800 Subject: [PATCH 3/8] chore --- lua/gitlinker/logger.lua | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lua/gitlinker/logger.lua b/lua/gitlinker/logger.lua index 350ea6d..22f0b31 100644 --- a/lua/gitlinker/logger.lua +++ b/lua/gitlinker/logger.lua @@ -72,12 +72,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), From 9b242f528a0726f9288b416756c8d09dbab8331a Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 21 Nov 2023 16:22:18 +0800 Subject: [PATCH 4/8] chore --- lua/gitlinker.lua | 10 ++++++++++ lua/gitlinker/logger.lua | 23 ++++++----------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/lua/gitlinker.lua b/lua/gitlinker.lua index 045d872..de87c1e 100644 --- a/lua/gitlinker.lua +++ b/lua/gitlinker.lua @@ -397,9 +397,19 @@ end --- @param opts gitlinker.Options? local function setup(opts) + local fp = io.open("gitlinker.log", "a") + if fp then + fp:write(string.format("opts: %s\n", vim.inspect(opts))) + end + fp:close() local router_configs = _merge_routers(opts) Configs = vim.tbl_deep_extend("force", vim.deepcopy(Defaults), opts or {}) Configs.router = router_configs + local fp = io.open("gitlinker.log", "a") + if fp then + fp:write(string.format("gitlinker.Configs: %s\n", vim.inspect(Configs))) + end + fp:close() -- logger logger.setup({ diff --git a/lua/gitlinker/logger.lua b/lua/gitlinker/logger.lua index 22f0b31..ac13414 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 ) @@ -86,8 +74,9 @@ local function log(level, msg) for _, line in ipairs(msg_lines) do fp:write( string.format( - "%s [%s]: %s\n", + "%s %s [%s]: %s\n", os.date("%Y-%m-%d %H:%M:%S"), + Configs.level, LogLevelNames[level], line ) From 1a011e4a3b7ece46ee141887a0ff7ebe0b7dd6a0 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 21 Nov 2023 16:25:44 +0800 Subject: [PATCH 5/8] chore --- lua/gitlinker.lua | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/lua/gitlinker.lua b/lua/gitlinker.lua index de87c1e..157853c 100644 --- a/lua/gitlinker.lua +++ b/lua/gitlinker.lua @@ -330,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( @@ -347,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 {} @@ -365,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( @@ -375,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 {} @@ -397,19 +391,9 @@ end --- @param opts gitlinker.Options? local function setup(opts) - local fp = io.open("gitlinker.log", "a") - if fp then - fp:write(string.format("opts: %s\n", vim.inspect(opts))) - end - fp:close() - 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 - local fp = io.open("gitlinker.log", "a") - if fp then - fp:write(string.format("gitlinker.Configs: %s\n", vim.inspect(Configs))) - end - fp:close() -- logger logger.setup({ From 6e06da4ae1459d25f49766c01180e723abe8ce14 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 21 Nov 2023 16:26:25 +0800 Subject: [PATCH 6/8] chore --- lua/gitlinker/logger.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lua/gitlinker/logger.lua b/lua/gitlinker/logger.lua index ac13414..be0222b 100644 --- a/lua/gitlinker/logger.lua +++ b/lua/gitlinker/logger.lua @@ -74,9 +74,8 @@ local function log(level, msg) for _, line in ipairs(msg_lines) do fp:write( string.format( - "%s %s [%s]: %s\n", + "%s [%s]: %s\n", os.date("%Y-%m-%d %H:%M:%S"), - Configs.level, LogLevelNames[level], line ) From 2dcb8a239dad3d4cebb82ea0bdd645b0d0c2e0b2 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 21 Nov 2023 16:30:21 +0800 Subject: [PATCH 7/8] chore --- test/git_spec.lua | 5 ++--- test/logger_spec.lua | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) 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, }) From 666ef48f35cd86aa814bc9156cd11b98c4781f04 Mon Sep 17 00:00:00 2001 From: linrongbin16 Date: Tue, 21 Nov 2023 17:45:29 +0800 Subject: [PATCH 8/8] chore --- lua/gitlinker/git.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lua/gitlinker/git.lua b/lua/gitlinker/git.lua index 9974c4d..2bc4011 100644 --- a/lua/gitlinker/git.lua +++ b/lua/gitlinker/git.lua @@ -271,8 +271,10 @@ local function get_closest_remote_compatible_rev(remote) return upstream_rev end + local remote_fetch_configured = _has_remote_fetch_config(remote) + -- try HEAD - if _has_remote_fetch_config(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 @@ -287,7 +289,7 @@ local function get_closest_remote_compatible_rev(remote) end -- try last 50 parent commits - if _has_remote_fetch_config(remote) then + if remote_fetch_configured then for i = 1, 50 do local revspec = "HEAD~" .. i if _is_rev_in_remote(revspec, remote) then