Skip to content

Commit acf4464

Browse files
authored
fix: support git remotes other than 'origin' (#97)
1 parent 809643b commit acf4464

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

lua/rocks-git/git.lua

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,15 @@ end
234234
---@return string | nil head The remote HEAD branch name
235235
function git.get_head_branch(pkg)
236236
local git_dir = vim.fs.joinpath(pkg.dir, ".git")
237-
local remote_head_ref = read_line(vim.fs.joinpath(git_dir, "refs", "remotes", "origin", "HEAD"))
238-
if not remote_head_ref then
239-
return
240-
end
241-
remote_head_ref = remote_head_ref:gsub("ref: refs/remotes/origin/", "")
242-
return remote_head_ref
237+
local remotes_dir = vim.fs.joinpath(git_dir, "refs", "remotes")
238+
local remotes = vim.fn.globpath(remotes_dir, "*", false, true)
239+
return vim.iter(remotes)
240+
:map(function(remote_dir)
241+
return read_line(vim.fs.joinpath(remote_dir, "HEAD"))
242+
end)
243+
:find(function(head_file_content)
244+
return head_file_content:gsub("ref: refs/remotes/.+/", "")
245+
end)
243246
end
244247

245248
---@param url string

spec/git_spec.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
local git = require("rocks-git.git")
2+
3+
local origin_pkg_dir = vim.fn.tempname()
4+
local origin_pkg_remote_dir = vim.fs.joinpath(origin_pkg_dir, ".git", "refs", "remotes", "origin")
5+
6+
local upstream_pkg_dir = vim.fn.tempname()
7+
local upstream_pkg_remote_dir = vim.fs.joinpath(upstream_pkg_dir, ".git", "refs", "remotes", "upstream")
8+
9+
setup(function()
10+
vim.system({ "mkdir", "-p", origin_pkg_remote_dir }, {}):wait()
11+
vim.system({ "mkdir", "-p", upstream_pkg_remote_dir }, {}):wait()
12+
local origin_head = vim.fs.joinpath(origin_pkg_remote_dir, "HEAD")
13+
local fd = assert(io.open(origin_head, "w"), "could not open " .. origin_head)
14+
fd:write("foo")
15+
fd:close()
16+
local upstream_head = vim.fs.joinpath(upstream_pkg_remote_dir, "HEAD")
17+
fd = assert(io.open(upstream_head, "w"), "could not open " .. upstream_head)
18+
fd:write("bar")
19+
fd:close()
20+
end)
21+
22+
describe("git", function()
23+
it("Can get head branch from 'origin' remote", function()
24+
local head_branch = git.get_head_branch({
25+
dir = origin_pkg_dir,
26+
url = "https://github.com/lumen-oss/luarocks-stub.git",
27+
})
28+
assert.Same("foo", head_branch)
29+
end)
30+
it("Can get head branch from 'upstream' remote", function()
31+
local head_branch = git.get_head_branch({
32+
dir = upstream_pkg_dir,
33+
url = "https://github.com/lumen-oss/luarocks-stub.git",
34+
})
35+
assert.Same("bar", head_branch)
36+
end)
37+
end)

0 commit comments

Comments
 (0)