Skip to content

Commit 080ffaf

Browse files
authored
fix(linker): skip file and rev check when provided (#240)
1 parent 94d726e commit 080ffaf

File tree

4 files changed

+73
-52
lines changed

4 files changed

+73
-52
lines changed

lua/gitlinker.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ local _link = function(opts)
206206
local logger = logging.get("gitlinker")
207207
-- logger.debug("[link] merged opts: %s", vim.inspect(opts))
208208

209-
local lk = linker.make_linker(opts.remote)
209+
local lk = linker.make_linker(opts.remote, opts.file, opts.rev)
210210
if not lk then
211211
return nil
212212
end

lua/gitlinker/commons/str.lua

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
local M = {}
22

3+
local string_len, string_byte, string_sub, string_gsub =
4+
string.len, string.byte, string.sub, string.gsub
5+
36
--- @param s any
47
--- @return boolean
58
M.empty = function(s)
6-
return type(s) ~= "string" or string.len(s) == 0
9+
return type(s) ~= "string" or string_len(s) == 0
710
end
811

912
--- @param s any
1013
--- @return boolean
1114
M.not_empty = function(s)
12-
return type(s) == "string" and string.len(s) > 0
15+
return type(s) == "string" and string_len(s) > 0
1316
end
1417

1518
--- @param s any
1619
--- @return boolean
1720
M.blank = function(s)
18-
return type(s) ~= "string" or string.len(vim.trim(s)) == 0
21+
return type(s) ~= "string" or string_len(vim.trim(s)) == 0
1922
end
2023

2124
--- @param s any
2225
--- @return boolean
2326
M.not_blank = function(s)
24-
return type(s) == "string" and string.len(vim.trim(s)) > 0
27+
return type(s) == "string" and string_len(vim.trim(s)) > 0
2528
end
2629

2730
--- @param s string
@@ -40,8 +43,8 @@ M.find = function(s, t, start)
4043
match = false
4144
break
4245
end
43-
local a = string.byte(s, i + j - 1)
44-
local b = string.byte(t, j)
46+
local a = string_byte(s, i + j - 1)
47+
local b = string_byte(t, j)
4548
if a ~= b then
4649
match = false
4750
break
@@ -70,8 +73,8 @@ M.rfind = function(s, t, rstart)
7073
match = false
7174
break
7275
end
73-
local a = string.byte(s, i + j - 1)
74-
local b = string.byte(t, j)
76+
local a = string_byte(s, i + j - 1)
77+
local b = string_byte(t, j)
7578
if a ~= b then
7679
match = false
7780
break
@@ -93,7 +96,7 @@ M.ltrim = function(s, t)
9396

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

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

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

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

147150
if opts.ignorecase then
148-
return string.len(s) >= string.len(t) and s:sub(1, #t):lower() == t:lower()
151+
return string_len(s) >= string_len(t) and s:sub(1, #t):lower() == t:lower()
149152
else
150-
return string.len(s) >= string.len(t) and s:sub(1, #t) == t
153+
return string_len(s) >= string_len(t) and s:sub(1, #t) == t
151154
end
152155
end
153156

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

165168
if opts.ignorecase then
166-
return string.len(s) >= string.len(t) and s:sub(#s - #t + 1):lower() == t:lower()
169+
return string_len(s) >= string_len(t) and s:sub(#s - #t + 1):lower() == t:lower()
167170
else
168-
return string.len(s) >= string.len(t) and s:sub(#s - #t + 1) == t
171+
return string_len(s) >= string_len(t) and s:sub(#s - #t + 1) == t
169172
end
170173
end
171174

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

181-
local sn = string.len(s)
182-
local pn = string.len(p)
184+
local sn = string_len(s)
185+
local pn = string_len(p)
183186
local pos = 1
184187
local matched = 0
185188
local result = s
@@ -189,7 +192,7 @@ M.replace = function(s, p, r)
189192
if type(pos) ~= "number" then
190193
break
191194
end
192-
result = string.sub(result, 1, pos - 1) .. r .. string.sub(result, pos + pn)
195+
result = string_sub(result, 1, pos - 1) .. r .. string_sub(result, pos + pn)
193196
pos = pos + pn
194197
matched = matched + 1
195198
end
@@ -201,55 +204,55 @@ end
201204
--- @return boolean
202205
M.isspace = function(c)
203206
assert(type(c) == "string")
204-
assert(string.len(c) == 1)
207+
assert(string_len(c) == 1)
205208
return c:match("%s") ~= nil
206209
end
207210

208211
--- @param c string
209212
--- @return boolean
210213
M.isalnum = function(c)
211214
assert(type(c) == "string")
212-
assert(string.len(c) == 1)
215+
assert(string_len(c) == 1)
213216
return c:match("%w") ~= nil
214217
end
215218

216219
--- @param c string
217220
--- @return boolean
218221
M.isdigit = function(c)
219222
assert(type(c) == "string")
220-
assert(string.len(c) == 1)
223+
assert(string_len(c) == 1)
221224
return c:match("%d") ~= nil
222225
end
223226

224227
--- @param c string
225228
--- @return boolean
226229
M.isxdigit = function(c)
227230
assert(type(c) == "string")
228-
assert(string.len(c) == 1)
231+
assert(string_len(c) == 1)
229232
return c:match("%x") ~= nil
230233
end
231234

232235
--- @param c string
233236
--- @return boolean
234237
M.isalpha = function(c)
235238
assert(type(c) == "string")
236-
assert(string.len(c) == 1)
239+
assert(string_len(c) == 1)
237240
return c:match("%a") ~= nil
238241
end
239242

240243
--- @param c string
241244
--- @return boolean
242245
M.islower = function(c)
243246
assert(type(c) == "string")
244-
assert(string.len(c) == 1)
247+
assert(string_len(c) == 1)
245248
return c:match("%l") ~= nil
246249
end
247250

248251
--- @param c string
249252
--- @return boolean
250253
M.isupper = function(c)
251254
assert(type(c) == "string")
252-
assert(string.len(c) == 1)
255+
assert(string_len(c) == 1)
253256
return c:match("%u") ~= nil
254257
end
255258

@@ -261,18 +264,18 @@ M.setchar = function(s, pos, ch)
261264
assert(type(s) == "string")
262265
assert(type(pos) == "number")
263266
assert(type(ch) == "string")
264-
assert(string.len(ch) == 1)
267+
assert(string_len(ch) == 1)
265268

266-
local n = string.len(s)
269+
local n = string_len(s)
267270
pos = require("gitlinker.commons.tbl").list_index(pos, n)
268271

269272
local buffer = ""
270273
if pos > 1 then
271-
buffer = string.sub(s, 1, pos - 1)
274+
buffer = string_sub(s, 1, pos - 1)
272275
end
273276
buffer = buffer .. ch
274277
if pos < n then
275-
buffer = buffer .. string.sub(s, pos + 1)
278+
buffer = buffer .. string_sub(s, pos + 1)
276279
end
277280

278281
return buffer
@@ -283,9 +286,9 @@ end
283286
M.tochars = function(s)
284287
assert(type(s) == "string")
285288
local l = {}
286-
local n = string.len(s)
289+
local n = string_len(s)
287290
for i = 1, n do
288-
table.insert(l, string.sub(s, i, i))
291+
table.insert(l, string_sub(s, i, i))
289292
end
290293
return l
291294
end

lua/gitlinker/commons/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
15.0.0
1+
15.0.1

lua/gitlinker/linker.lua

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ end
2626

2727
--- @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?}
2828
--- @param remote string?
29+
--- @param file string?
30+
--- @param rev string?
2931
--- @return gitlinker.Linker?
30-
local function make_linker(remote)
32+
local function make_linker(remote, file, rev)
3133
local logger = logging.get("gitlinker")
3234
local cwd = _get_buf_dir()
3335

36+
local file_provided = str.not_empty(file)
37+
local rev_provided = str.not_empty(rev)
38+
3439
local root = git.get_root(cwd)
3540
if not root then
3641
return nil
@@ -76,37 +81,50 @@ local function make_linker(remote)
7681
-- vim.inspect(remote_url)
7782
-- )
7883

79-
local rev = git.get_closest_remote_compatible_rev(remote, cwd)
80-
if not rev then
84+
if not rev_provided then
85+
rev = git.get_closest_remote_compatible_rev(remote, cwd)
86+
end
87+
if str.empty(rev) then
8188
return nil
8289
end
8390
-- logger.debug("|linker - Linker:make| rev:%s", vim.inspect(rev))
8491

8592
async.scheduler()
86-
local buf_path_on_root = path.buffer_relpath(root) --[[@as string]]
87-
local buf_path_encoded = uri.encode(buf_path_on_root) --[[@as string]]
88-
-- logger.debug(
89-
-- "|linker - Linker:make| root:%s, buf_path_on_root:%s",
90-
-- vim.inspect(root),
91-
-- vim.inspect(buf_path_on_root)
92-
-- )
9393

94-
local file_in_rev_result = git.is_file_in_rev(buf_path_on_root, rev, cwd)
95-
if not file_in_rev_result then
96-
return nil
94+
if not file_provided then
95+
local buf_path_on_root = path.buffer_relpath(root) --[[@as string]]
96+
local buf_path_encoded = uri.encode(buf_path_on_root) --[[@as string]]
97+
-- logger.debug(
98+
-- "|linker - Linker:make| root:%s, buf_path_on_root:%s",
99+
-- vim.inspect(root),
100+
-- vim.inspect(buf_path_on_root)
101+
-- )
102+
103+
local file_in_rev_result = git.is_file_in_rev(buf_path_on_root, rev --[[@as string]], cwd)
104+
if not file_in_rev_result then
105+
return nil
106+
end
107+
file = buf_path_encoded
108+
else
109+
file = uri.encode(file)
97110
end
111+
98112
-- logger.debug(
99113
-- "|linker - Linker:make| file_in_rev_result:%s",
100114
-- vim.inspect(file_in_rev_result)
101115
-- )
102116

103117
async.scheduler()
104-
local buf_path_on_cwd = path.buffer_relpath() --[[@as string]]
105-
local file_changed = git.file_has_changed(buf_path_on_cwd, rev, cwd)
106-
-- logger.debug(
107-
-- "|linker - Linker:make| buf_path_on_cwd:%s",
108-
-- vim.inspect(buf_path_on_cwd)
109-
-- )
118+
119+
local file_changed = false
120+
if not file_provided then
121+
local buf_path_on_cwd = path.buffer_relpath() --[[@as string]]
122+
file_changed = git.file_has_changed(buf_path_on_cwd, rev --[[@as string]], cwd)
123+
-- logger.debug(
124+
-- "|linker - Linker:make| buf_path_on_cwd:%s",
125+
-- vim.inspect(buf_path_on_cwd)
126+
-- )
127+
end
110128

111129
local default_branch = git.get_default_branch(remote, cwd)
112130
local current_branch = git.get_current_branch(cwd)
@@ -123,7 +141,7 @@ local function make_linker(remote)
123141
org = parsed_url.org,
124142
repo = parsed_url.repo,
125143
rev = rev,
126-
file = buf_path_encoded,
144+
file = file,
127145
---@diagnostic disable-next-line: need-check-nil
128146
lstart = nil,
129147
---@diagnostic disable-next-line: need-check-nil

0 commit comments

Comments
 (0)