8000 fix(utils): allow non-GitHub URLs while still expanding the short form GitHub owner/repo paths by mbwilding · Pull Request #42 · zuqini/zpack.nvim · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/zpack/import.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ local normalize_source = function(spec)
elseif type(spec.dir) == 'string' then
return vim.fn.expand(spec.dir)
elseif type(spec[1]) == 'string' then
return utils.github_url(spec[1])
return utils.expand_src(spec[1])
else
return nil, "spec must provide one of: [1], src, dir, or url"
end
Expand Down
4 changes: 2 additions & 2 deletions lua/zpack/merge.lua
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ local function fold_rank(entry, src)
local explicit, top, dep = -1, -1, -1
for _, spec in ipairs(entry.specs) do
local order = spec._import_order or 0
if spec.dev == true and (type(spec[1]) ~= 'string' or src ~= util.github_url(spec[1])) then
if spec.dev == true and (type(spec[1]) ~= 'string' or src ~= util.expand_src(spec[1])) then
return 3, order
end
if type(spec.src) == 'string' or type(spec.url) == 'string'
Expand Down Expand Up @@ -475,7 +475,7 @@ local function named_repos(entry, src)
local repos = { [src] = true }
for _, spec in ipairs(entry.specs) do
if type(spec[1]) == 'string' then
repos[util.github_url(spec[1])] = true
repos[util.expand_src(spec[1])] = true
end
end
return repos
Expand Down
12 changes: 8 additions & 4 deletions lua/zpack/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,17 @@ M.derive_name_from_src = function(src)
return (basename:gsub('%.git$', ''))
end

---Expand a `[1]` short name ("user/repo") to its GitHub URL. Shared by
---Resolve a `[1]` value to a source. `user/repo` shorthand expands to its GitHub URL; full URLs and
---paths are returned unchanged. Shared by
---`normalize_source` and merge.lua's coalesce fold so the two sites cannot
---drift on what a shorthand resolves to.
---@param short_name string
---@param src string
---@return string
M.github_url = function(short_name)
return 'https://github.com/' .. short_name
M.expand_src = function(src)
if src:match('^%a[%w+.-]*://') or src:match('^[%w_.-]+@[^/]+:') or src:match('^[/~.]') then
return src
end
return 'https://github.com/' .. src
end

---Resolve a plugin's display/pack name. lazy.nvim parity: the name derives
Expand Down
27 changes: 27 additions & 0 deletions tests/utils_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,30 @@ describe('latch_first_call', function()
"Subsequent calls must not invoke the inner — return nil")
end)
end)

describe("expand_src", function()
local utils = require('zpack.utils')

it("expands a short name to a GitHub URL", function()
assert.are.equal('https://github.com/user/repo', utils.expand_src('user/repo'))
end)

it("returns full URLs unchanged", function()
local urls = {
'https://forge.barrettruth.com/barrettruth/canola.nvim',
'http://example.com/user/repo.git',
'ssh://git@example.com/user/repo.git',
'git@example.com:user/repo.git',
'file:///home/user/repo',
}
for _, url in ipairs(urls) do
assert.are.equal(url, utils.expand_src(url))
end
end)

it("returns local paths unchanged", function()
assert.are.equal('/home/user/repo', utils.expand_src('/home/user/repo'))
assert.are.equal('~/repo', utils.expand_src('~/repo'))
assert.are.equal('./repo', utils.expand_src('./repo'))
end)
end)
Loading
0