Skip to content

fix: dont assume the remote name - #105

Open
teto wants to merge 1 commit into
lumen-oss:mainfrom
teto:teto/dont-assume-origin-v2
Open

fix: dont assume the remote name#105
teto wants to merge 1 commit into
lumen-oss:mainfrom
teto:teto/dont-assume-origin-v2

Conversation

@teto

@teto teto commented Aug 5, 2026

Copy link
Copy Markdown
Member

Problem with the function get_head_branch is that it returns "ref: refs/remotes/...."

  return vim.iter(vim.fs.dir(remotes_dir))
      :map(function(remote_subdir)
          return read_line(vim.fs.joinpath(remotes_dir, remote_subdir, "HEAD"))
      end)
      :find(function(head_file_content)
          # this bit isn't visible 
          return head_file_content:gsub("ref: refs/remotes/.+/", "")
      end)


and then rocks tries to git checkout ref: refs/remotes/... which fails here.

The official way to get the default remote branch is to use "ls-remote --symref HEAD".

Since the problem is the default remote varies depending on user config, I skipped it by passing the url:
The advantages are:

  • it doesn't depend on user config or wether the user messed up with the repo (unlikely but I sometimes managed in the past to debug plugins in place).
  • not implementation dependant

The drawback is that it's one more git call.

Alternatives considered:

  • retreive the user default remote via git config get clone.defaultRemoteName but this value can be changed after a repo was cloned so not robust enough ()
  • trying to hardcode default remote across all git invokations. Can be annoying to implement (and transition from) ?
  • fix the current code. But it still wont know what remote to choose in case there are several (it will follow vim.fs.dir order). It's unlikely to be an issue so if that's the preferred solution I can do it as well. The immediate fix that codex was suggesting is this:
diff --git a/lua/rocks-git/git.lua b/lua/rocks-git/git.lua
index 6b9bea2828..8851106fbd 100644
--- a/lua/rocks-git/git.lua
+++ b/lua/rocks-git/git.lua
@@ -237,11 +237,13 @@
     local remotes_dir = vim.fs.joinpath(git_dir, "refs", "remotes")
     return vim.iter(vim.fs.dir(remotes_dir))
         :map(function(remote_subdir)
-            return read_line(vim.fs.joinpath(remotes_dir, remote_subdir, "HEAD"))
-        end)
-        :find(function(head_file_content)
-            return head_file_content:gsub("ref: refs/remotes/.+/", "")
-        end)
+            local head_file_content = read_line(vim.fs.joinpath(remotes_dir, remote_subdir, "HEAD"))
+            if not head_file_content then
+                return
+            end
+            return head_file_content:match("^ref: refs/remotes/[^/]+/(.+)$") or head_file_content
+        end)
+        :next()
 end

As for the tests, ideally we would run them against a real repo (ie, a clone of https://github.com/lumen-oss/luarocks-stub.git) but cloning this in a _spec.lua file might have been too slow so I didnt do it. Can restore in the test setup() call (a shallow clone)?

Problem with the function get_head_branch is that it returns
"ref: refs/remotes/...."

===
  return vim.iter(vim.fs.dir(remotes_dir))
      :map(function(remote_subdir)
          return read_line(vim.fs.joinpath(remotes_dir, remote_subdir, "HEAD"))
      end)
      :find(function(head_file_content)
          return head_file_content:gsub("ref: refs/remotes/.+/", "")
      end)
===


and then rocks tries to `git checkout ref: refs/remotes/...` which fails

The official way to get the default remote branch is to use 
"ls-remote --symref <REMOTE> HEAD".
Since the problem is the default remote varies depending on user config,
I skipped it by passing the url:
The advantages are:
- it doesn't depend on user config or wether the user messed up with the repo (unlikely but I sometimes managed in the past to debug plugins in place).
- not implementation dependant

The drawback is that it's one more git call.

Alternatives considered:
- fix the current code. But it still wont know what remote to choose in
  case there are several (it will follow vim.fs.dir order). It's
  unlikely to be an issue so if that's the preferred solution I can do
  it as well.
- trying to hardcode default upstream all the way. Can be annoying to implement ?


As for the tests, ideally we would run them against a real repo (ie, a
clone of https://github.com/lumen-oss/luarocks-stub.git) but
cloning this in a _spec.lua file might have been too slow so I didnt do
it ?
Comment thread lua/rocks-git/git.lua
:find(function(head_file_content)
return head_file_content:gsub("ref: refs/remotes/.+/", "")
end)
local args = { "ls-remote", "--symref", pkg.url, "HEAD" }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: tests are failing in the nix flake check:

       > Error → ...cz4-lua5.1-nvim-nio-1.10.0-1/share/lua/5.1/nio/tests.lua @ 66
       > git Can get the remote HEAD branch
       > ...cz4-lua5.1-nvim-nio-1.10.0-1/share/lua/5.1/nio/tests.lua:48: Test task failed with message:
       > The coroutine failed with this message:
       > spec/git_spec.lua:16: Expected objects to be the same.
       > Passed in:
       > (boolean) false
       > Expected:
       > (string) 'main'
       > stack traceback:
       >  [C]: in function 'throw'
       >        ...f7ak-lua5.1-

We'll either have to implement a pure fallback mechanism or add --impure --option sandbox false to the CI run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants