Skip to content

Commit 3ed7409

Browse files
authored
fix(url): fix host ends with a colon but port is empty (#253)
1 parent 606060e commit 3ed7409

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,6 @@ jobs:
5151
cp ~/.commons.nvim/version.txt ./lua/gitlinker/commons/version.txt
5252
cd ./lua/gitlinker/commons
5353
find . -type f -name '*.lua' -exec sed -i 's/require("commons/require("gitlinker.commons/g' {} \;
54-
- name: Install giturlparser.lua
55-
if: ${{ github.ref != 'refs/heads/master' }}
56-
shell: bash
57-
run: |
58-
echo "pwd"
59-
echo $PWD
60-
git clone --depth=1 https://github.com/linrongbin16/giturlparser.lua.git ~/.giturlparser.lua
61-
cp ~/.giturlparser.lua/src/giturlparser.lua ./lua/gitlinker/giturlparser.lua
6254
- uses: stefanzweifel/git-auto-commit-action@v5
6355
if: ${{ github.ref != 'refs/heads/master' }}
6456
with:

lua/gitlinker/giturlparser.lua

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-- local inspect = require("inspect")
12
local M = {}
23

34
-- utils {
@@ -147,8 +148,6 @@ end
147148
M._parse_path = function(p, start)
148149
assert(type(start) == "number")
149150

150-
-- local inspect = require("inspect")
151-
152151
local endswith_slash = M._endswith(p, "/")
153152

154153
local org = nil
@@ -227,13 +226,24 @@ M._parse_host = function(p, start)
227226

228227
-- find first slash '/' (after second ':'), the end position of port, start position of path
229228
local first_slash_pos = M._find(p, "/", first_colon_pos + 1)
230-
if
231-
type(first_slash_pos) == "number"
232-
and first_slash_pos > first_colon_pos + 1
233-
then
234-
-- port end with '/'
235-
port, port_pos = M._make(p, first_colon_pos + 1, first_slash_pos - 1)
236-
path_obj = M._parse_path(p, first_slash_pos)
229+
-- print(
230+
-- string.format(
231+
-- "_parse_host, start:%s, first_colon_pos:%s, first_slash_pos:%s\n",
232+
-- inspect(start),
233+
-- inspect(first_colon_pos),
234+
-- inspect(first_slash_pos)
235+
-- )
236+
-- )
237+
if type(first_slash_pos) == "number" then
238+
if first_slash_pos > first_colon_pos + 1 then
239+
-- port end with '/'
240+
port, port_pos = M._make(p, first_colon_pos + 1, first_slash_pos - 1)
241+
path_obj = M._parse_path(p, first_slash_pos)
242+
else
243+
assert(first_slash_pos == first_colon_pos + 1)
244+
-- port is empty, host still end with '/'
245+
path_obj = M._parse_path(p, first_slash_pos)
246+
end
237247
else
238248
-- path not found, port end until url end
239249
port, port_pos = M._make(p, first_colon_pos + 1, plen)

spec/gitlinker_spec.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,26 @@ describe("gitlinker", function()
233233
)
234234
assert_eq(actual, routers.github_browse(lk))
235235
end)
236+
it("ssh://git@github.com:/myorg/myrepo.git", function()
237+
local lk = {
238+
remote_url = "ssh://git@github.com:/myorg/myrepo.git",
239+
username = "git",
240+
host = "github.com",
241+
org = "myorg",
242+
repo = "myrepo.git",
243+
rev = "399b1d05473c711fc5592a6ffc724e231c403486",
244+
file = "lua/gitlinker/logger.lua",
245+
lstart = 1,
246+
lend = 1,
247+
file_changed = false,
248+
}--[[@as gitlinker.Linker]]
249+
local actual = gitlinker._browse(lk)
250+
assert_eq(
251+
actual,
252+
"https://github.com/myorg/myrepo/blob/399b1d05473c711fc5592a6ffc724e231c403486/lua/gitlinker/logger.lua?plain=1#L1"
253+
)
254+
assert_eq(actual, routers.github_browse(lk))
255+
end)
236256
it("ssh://git@git.xyz.com with same lstart/lend", function()
237257
local lk = {
238258
remote_url = "ssh://git@git.xyz.com/linrongbin16/gitlinker.nvim.git",

0 commit comments

Comments
 (0)