Skip to content

Commit

Permalink
fix(linker): skip file and rev check when provided (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 authored Apr 24, 2024
1 parent 94d726e commit 080ffaf
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 52 deletions.
2 changes: 1 addition & 1 deletion lua/gitlinker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ local _link = function(opts)
local logger = logging.get("gitlinker")
-- logger.debug("[link] merged opts: %s", vim.inspect(opts))

local lk = linker.make_linker(opts.remote)
local lk = linker.make_linker(opts.remote, opts.file, opts.rev)
if not lk then
return nil
end
Expand Down
63 changes: 33 additions & 30 deletions lua/gitlinker/commons/str.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
local M = {}

local string_len, string_byte, string_sub, string_gsub =
string.len, string.byte, string.sub, string.gsub

--- @param s any
--- @return boolean
M.empty = function(s)
return type(s) ~= "string" or string.len(s) == 0
return type(s) ~= "string" or string_len(s) == 0
end

--- @param s any
--- @return boolean
M.not_empty = function(s)
return type(s) == "string" and string.len(s) > 0
return type(s) == "string" and string_len(s) > 0
end

--- @param s any
--- @return boolean
M.blank = function(s)
return type(s) ~= "string" or string.len(vim.trim(s)) == 0
return type(s) ~= "string" or string_len(vim.trim(s)) == 0
end

--- @param s any
--- @return boolean
M.not_blank = function(s)
return type(s) == "string" and string.len(vim.trim(s)) > 0
return type(s) == "string" and string_len(vim.trim(s)) > 0
end

--- @param s string
Expand All @@ -40,8 +43,8 @@ M.find = function(s, t, start)
match = false
break
end
local a = string.byte(s, i + j - 1)
local b = string.byte(t, j)
local a = string_byte(s, i + j - 1)
local b = string_byte(t, j)
if a ~= b then
match = false
break
Expand Down Expand Up @@ -70,8 +73,8 @@ M.rfind = function(s, t, rstart)
match = false
break
end
local a = string.byte(s, i + j - 1)
local b = string.byte(t, j)
local a = string_byte(s, i + j - 1)
local b = string_byte(t, j)
if a ~= b then
match = false
break
Expand All @@ -93,7 +96,7 @@ M.ltrim = function(s, t)

t = t or "%s+"
---@diagnostic disable-next-line: redundant-return-value
return string.gsub(s, "^" .. t, "")
return string_gsub(s, "^" .. t, "")
end

--- @param s string
Expand All @@ -105,7 +108,7 @@ M.rtrim = function(s, t)

t = t or "%s+"
---@diagnostic disable-next-line: redundant-return-value
return string.gsub(s, t .. "$", "")
return string_gsub(s, t .. "$", "")
end

--- @param s string
Expand Down Expand Up @@ -145,9 +148,9 @@ M.startswith = function(s, t, opts)
opts.ignorecase = type(opts.ignorecase) == "boolean" and opts.ignorecase or false

if opts.ignorecase then
return string.len(s) >= string.len(t) and s:sub(1, #t):lower() == t:lower()
return string_len(s) >= string_len(t) and s:sub(1, #t):lower() == t:lower()
else
return string.len(s) >= string.len(t) and s:sub(1, #t) == t
return string_len(s) >= string_len(t) and s:sub(1, #t) == t
end
end

Expand All @@ -163,9 +166,9 @@ M.endswith = function(s, t, opts)
opts.ignorecase = type(opts.ignorecase) == "boolean" and opts.ignorecase or false

if opts.ignorecase then
return string.len(s) >= string.len(t) and s:sub(#s - #t + 1):lower() == t:lower()
return string_len(s) >= string_len(t) and s:sub(#s - #t + 1):lower() == t:lower()
else
return string.len(s) >= string.len(t) and s:sub(#s - #t + 1) == t
return string_len(s) >= string_len(t) and s:sub(#s - #t + 1) == t
end
end

Expand All @@ -178,8 +181,8 @@ M.replace = function(s, p, r)
assert(type(p) == "string")
assert(type(r) == "string")

local sn = string.len(s)
local pn = string.len(p)
local sn = string_len(s)
local pn = string_len(p)
local pos = 1
local matched = 0
local result = s
Expand All @@ -189,7 +192,7 @@ M.replace = function(s, p, r)
if type(pos) ~= "number" then
break
end
result = string.sub(result, 1, pos - 1) .. r .. string.sub(result, pos + pn)
result = string_sub(result, 1, pos - 1) .. r .. string_sub(result, pos + pn)
pos = pos + pn
matched = matched + 1
end
Expand All @@ -201,55 +204,55 @@ end
--- @return boolean
M.isspace = function(c)
assert(type(c) == "string")
assert(string.len(c) == 1)
assert(string_len(c) == 1)
return c:match("%s") ~= nil
end

--- @param c string
--- @return boolean
M.isalnum = function(c)
assert(type(c) == "string")
assert(string.len(c) == 1)
assert(string_len(c) == 1)
return c:match("%w") ~= nil
end

--- @param c string
--- @return boolean
M.isdigit = function(c)
assert(type(c) == "string")
assert(string.len(c) == 1)
assert(string_len(c) == 1)
return c:match("%d") ~= nil
end

--- @param c string
--- @return boolean
M.isxdigit = function(c)
assert(type(c) == "string")
assert(string.len(c) == 1)
assert(string_len(c) == 1)
return c:match("%x") ~= nil
end

--- @param c string
--- @return boolean
M.isalpha = function(c)
assert(type(c) == "string")
assert(string.len(c) == 1)
assert(string_len(c) == 1)
return c:match("%a") ~= nil
end

--- @param c string
--- @return boolean
M.islower = function(c)
assert(type(c) == "string")
assert(string.len(c) == 1)
assert(string_len(c) == 1)
return c:match("%l") ~= nil
end

--- @param c string
--- @return boolean
M.isupper = function(c)
assert(type(c) == "string")
assert(string.len(c) == 1)
assert(string_len(c) == 1)
return c:match("%u") ~= nil
end

Expand All @@ -261,18 +264,18 @@ M.setchar = function(s, pos, ch)
assert(type(s) == "string")
assert(type(pos) == "number")
assert(type(ch) == "string")
assert(string.len(ch) == 1)
assert(string_len(ch) == 1)

local n = string.len(s)
local n = string_len(s)
pos = require("gitlinker.commons.tbl").list_index(pos, n)

local buffer = ""
if pos > 1 then
buffer = string.sub(s, 1, pos - 1)
buffer = string_sub(s, 1, pos - 1)
end
buffer = buffer .. ch
if pos < n then
buffer = buffer .. string.sub(s, pos + 1)
buffer = buffer .. string_sub(s, pos + 1)
end

return buffer
Expand All @@ -283,9 +286,9 @@ end
M.tochars = function(s)
assert(type(s) == "string")
local l = {}
local n = string.len(s)
local n = string_len(s)
for i = 1, n do
table.insert(l, string.sub(s, i, i))
table.insert(l, string_sub(s, i, i))
end
return l
end
Expand Down
2 changes: 1 addition & 1 deletion lua/gitlinker/commons/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15.0.0
15.0.1
58 changes: 38 additions & 20 deletions lua/gitlinker/linker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ 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?
--- @param file string?
--- @param rev string?
--- @return gitlinker.Linker?
local function make_linker(remote)
local function make_linker(remote, file, rev)
local logger = logging.get("gitlinker")
local cwd = _get_buf_dir()

local file_provided = str.not_empty(file)
local rev_provided = str.not_empty(rev)

local root = git.get_root(cwd)
if not root then
return nil
Expand Down Expand Up @@ -76,37 +81,50 @@ local function make_linker(remote)
-- vim.inspect(remote_url)
-- )

local rev = git.get_closest_remote_compatible_rev(remote, cwd)
if not rev then
if not rev_provided then
rev = git.get_closest_remote_compatible_rev(remote, cwd)
end
if str.empty(rev) then
return nil
end
-- logger.debug("|linker - Linker:make| rev:%s", vim.inspect(rev))

async.scheduler()
local buf_path_on_root = path.buffer_relpath(root) --[[@as string]]
local buf_path_encoded = uri.encode(buf_path_on_root) --[[@as string]]
-- logger.debug(
-- "|linker - Linker:make| root:%s, buf_path_on_root:%s",
-- vim.inspect(root),
-- vim.inspect(buf_path_on_root)
-- )

local file_in_rev_result = git.is_file_in_rev(buf_path_on_root, rev, cwd)
if not file_in_rev_result then
return nil
if not file_provided then
local buf_path_on_root = path.buffer_relpath(root) --[[@as string]]
local buf_path_encoded = uri.encode(buf_path_on_root) --[[@as string]]
-- logger.debug(
-- "|linker - Linker:make| root:%s, buf_path_on_root:%s",
-- vim.inspect(root),
-- vim.inspect(buf_path_on_root)
-- )

local file_in_rev_result = git.is_file_in_rev(buf_path_on_root, rev --[[@as string]], cwd)
if not file_in_rev_result then
return nil
end
file = buf_path_encoded
else
file = uri.encode(file)
end

-- logger.debug(
-- "|linker - Linker:make| file_in_rev_result:%s",
-- vim.inspect(file_in_rev_result)
-- )

async.scheduler()
local buf_path_on_cwd = path.buffer_relpath() --[[@as string]]
local file_changed = git.file_has_changed(buf_path_on_cwd, rev, cwd)
-- logger.debug(
-- "|linker - Linker:make| buf_path_on_cwd:%s",
-- vim.inspect(buf_path_on_cwd)
-- )

local file_changed = false
if not file_provided then
local buf_path_on_cwd = path.buffer_relpath() --[[@as string]]
file_changed = git.file_has_changed(buf_path_on_cwd, rev --[[@as string]], cwd)
-- logger.debug(
-- "|linker - Linker:make| buf_path_on_cwd:%s",
-- vim.inspect(buf_path_on_cwd)
-- )
end

local default_branch = git.get_default_branch(remote, cwd)
local current_branch = git.get_current_branch(cwd)
Expand All @@ -123,7 +141,7 @@ local function make_linker(remote)
org = parsed_url.org,
repo = parsed_url.repo,
rev = rev,
file = buf_path_encoded,
file = file,
---@diagnostic disable-next-line: need-check-nil
lstart = nil,
---@diagnostic disable-next-line: need-check-nil
Expand Down

0 comments on commit 080ffaf

Please sign in to comment.