junegunn / vim-plug

:hibiscus: Minimalist Vim Plugin Manager
https://junegunn.github.io/vim-plug/
MIT License
33.7k stars 1.9k forks source link

PlugClean fails to recover when remote renamed default branch #1253

Closed kaihowl closed 4 months ago

kaihowl commented 10 months ago

When vim-gitgutter changed the remote, default branch from master to main a while ago, my calls to :PlugClean started to fail with

Error detected while processing command line..function <SNR>2_clean[13]..<SNR>2_git_validate:
line   36:
E688: More targets than List items

The error occurs here.

Running git status in the vim-gitgutter's worktree shows

On branch master
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)

nothing to commit, working tree clean

The result from the system call on the vim-plug quoted line above is:

['''git <command> [<revision>...] -- [<file>...]''']

The underlying issue triggering this behavior is the outdated symbolic ref refs/remotes/origin/HEAD. This does not get updated on a git fetch. A git remote set-head origin -a can do the trick. But this does involve another network call to query the remote.

I run PlugClean! daily as part of this setup script.


https://github.com/kaihowl/dotfiles/blob/a01f529f8b6a938cf33369df457bb0faec999e65/nvim/config--nvim--init.vim.symlink#L100-L167

NVIM v0.9.2
Build type: Release
LuaJIT 2.1.1692716794

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/usr/local/share/nvim"

Run :checkhealth for more info
sanscore commented 5 months ago

I just noticed this issue myself, and I can enlighten the issue, but I cannot offer to fix the issue in a PR unfortunately.

So, the issue is that when the maintainer of vim-gitgutter changed the default remote from master to main, this has a knock-on effect to every existing clone of the repo. In your .git directory there is a file that records the remote's default branch, .git/refs/remotes/origin/HEAD. TMK, there is no command that maintains this file automatically. (If any command were to do this, I would expect it to be fetch; But, it does not. :shrug:) However, there is the command, git remote set-head origin -a, which will automatically update the refs/remotes/origin/HEAD file.

TBH, after the initial clone, I can't really think of any reason for this file to exist. Aside from the fact that it allows one to run a command like git checkout origin/HEAD, but that has very little utility since it will leave you in a detached-head state. :shrug: :shrug: Anyway.

If someone wants to fix this issue, then I would suggest looking into s:git_validate or s:git_origin_branch as possible locations to add this validation. i.e. Collect origin_branch, check if origin_branch exists, if so fine, but if not, then run the command, git remote set-head origin -a.

Quick Edit: I would identify this issue as a Bug and, since it's an interaction issue with git, that it affects OS: All and all versions of Vim.

junegunn commented 4 months ago

Thanks for the report. Although it's not clear how vim-plug can really fix the situation, it should at least not throw an error.

I believe #1269 will fix the problem, but I'm not sure how I can test it. Any idea?