Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rocks update in bare repo with GIT_DIR and GIT_WORK_TREE env vars set can lead to loss of uncomitted local changes #78

Open
CarbonChauvinist opened this issue Oct 28, 2024 · 3 comments

Comments

@CarbonChauvinist
Copy link

CarbonChauvinist commented Oct 28, 2024

So after reporting an issue with my configuration that lead to issues with updates and syncs against git repos when using rocks-git.nvim in a bare repo it turned out to be essentially user error (see for reference)

However, I think there is potentially something still to be looked at here?

Basically AFAICT, if someone is using a bare repo setup, and has the GIT_DIR and GIT_WORK_TREE env vars set for any reason and available from within nvim they might lose changes in their worktree if all of the following applies:

  • use rocks-git.nvim
  • have telescope-fzf-native.nvim listed in rocks.toml to be installed from git repo in config
  • has run :Rocks sync initially without the GIT_DIR or GIT_WORK_TREE setup in order to get the plugins installed first
  • make changes to local bare repo
  • subsequently starts nvim from their GIT_WORK_TREE as the cwd with GIT_DIR and GIT_WORK_TREE env vars set and runs :Rocks update

When this happens it seems that one can lose all uncommitted changes in the worktree (I assume related somehow to the use of "--force" checkout?)

I think this was previously noted here, but OP was unsure of how to reproduce.
Originally posted by @boydkelly in nvim-neorocks/rocks.nvim#312 (comment)

I've setup a minimal config to reproduce using a fresh NVIM_APPNAME folder and only installing rocks, rocks-config, rocks-git, a git sourced plugin baredot.nvim which simply allows me to more easily set the GIT_DIR and GIT_WORK_TREE env vars. The key, in my testing at least, was to also install telescope-fzf-native.nvim from git with a make build command in config (edit happens with or without the build key in rocks.toml).

$ tree -a ~/.config/barerepo_issue

/home/ghost/.config/barerepo_issue
├── init.lua
├── lua
│   └── plugins
│       └── baredot.lua
└── rocks.toml

3 directories, 3 files

Here's the output of the minimal files, default rocks.nvim init.lua created from pasting in register when running the install script, and a bare bones baredot.nvim config setup.

rocks.toml
[plugins]
"rocks.nvim" = "2.41.0"
"rocks-git.nvim" = "2.5.0"
"rocks-config.nvim" = "3.0.0"

[plugins."telescope-fzf-native.nvim" ]
git = "nvim-telescope/telescope-fzf-native.nvim"
rev = "cf48d4dfce44e0b9a2e19a008d6ec6ea6f01a83b"
build = "make"

[plugins."baredot.nvim" ]
git = "ejrichards/baredot.nvim"
rev = "v1.1.0"

[config]
auto_setup = false
init.lua
local rocks_config = {
    rocks_path = vim.env.HOME .. "/.local/share/barerepo_issue/rocks",
}

vim.g.rocks_nvim = rocks_config

local luarocks_path = {
    vim.fs.joinpath(rocks_config.rocks_path, "share", "lua", "5.1", "?.lua"),
    vim.fs.joinpath(rocks_config.rocks_path, "share", "lua", "5.1", "?", "init.lua"),
}
package.path = package.path .. ";" .. table.concat(luarocks_path, ";")

local luarocks_cpath = {
    vim.fs.joinpath(rocks_config.rocks_path, "lib", "lua", "5.1", "?.so"),
    vim.fs.joinpath(rocks_config.rocks_path, "lib64", "lua", "5.1", "?.so"),
}
package.cpath = package.cpath .. ";" .. table.concat(luarocks_cpath, ";")

vim.opt.runtimepath:append(vim.fs.joinpath(rocks_config.rocks_path, "lib", "luarocks", "rocks-5.1", "*", "*"))

Here's the alias I use for my bare repo:

dotz is an alias for /usr/bin/git --work-tree=$HOME --git-dir=$HOME/.config/dotz/

And here's the bare bones baredot.nvim plugin setup:

-- ~/.config/$NVIM_APPNAME/lua/plugins/baredot.lua
require("baredot").setup({git_dir = "~/.config/dotz", git_work_tree = "~"})

On first run of nvim I can :Rocks sync to install all the listed plugins from rocks.toml. I sometimes have to start nvim again and rerun :Rocks sync to make sure it installs the baredot plugin (seems like rocks-git.nvim needs to be installed first and then the git plugins can be installed subsequently?

Either way, once both rocks-git and baredot and telescope-fzf have been installed sucesfully, exit nvim and make some changes in the bare repo locally, but don't commit yet.

As you can see my GIT_WORK_TREE is simply home (~). And I now have some changes within the work tree.

Sun 27  ~ $ dotz status
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
  (use "git push" to publish your local commits)
                                                                                                                                                                              
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   .bash_aliases
        modified:   .config/foot/foot.ini

If I now start nvim from the GIT_WORK_TREE dir (~) as the cwd with either baredot plugin managing the GIT_WORK_TREE and GIT_DIR env vars (this also occurs when setting them manually and not using the baredot plugin) - and then run :Rocks update the following errors occur during the operation.

`:Rocks log` after error during `:Rocks update`
[START][2024-10-27 22:53:08] rocks.nvim logging initiated
WARN | 2024-10-27 23:00:45 | ...are/barerepo_issue/rocks/rocks_rtp/lua/rocks-git/git.lua:262 | Could not parse latest tag from: 
ERROR | 2024-10-27 23:00:47 | ...are/barerepo_issue/rocks/rocks_rtp/lua/rocks-git/git.lua:198 | {
  code = 1,
  signal = 0,
  stderr = "ssh_askpass: exec(/usr/lib/ssh/ssh-askpass): No such file or directory\r\[email protected]: Permission denied (publickey,password,keyboard-interactive).\r\nfatal: Could not read from remote repository.\n\nPlease make sure you have the correct access rights\nand the repository exists.\n",
  stdout = ""
}
ERROR | 2024-10-27 23:00:47 | ...po_issue/rocks/rocks_rtp/lua/rocks/operations/update.lua:76 | UPDATE ERROR: rocks-git: Failed to pull telescope-fzf-native.nvim
ERROR | 2024-10-27 23:00:47 | ...po_issue/rocks/rocks_rtp/lua/rocks/operations/update.lua:176 | Update completed with errors! Run ':Rocks log' for details.

Checking the status of the bare repo now shows it's lost all local changes.

Sun 27  ~ $ dotz status
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit (use -u to show untracked files)
@mrcjkb
Copy link
Member

mrcjkb commented Oct 28, 2024

Hey 👋

Thanks for the detailed report.
Since last time, it looked like a luarocks issue, could you please try a :Rocks update with rocks-git.nvim disabled? It should be enough to comment it out in your rocks.toml during the update.

@CarbonChauvinist
Copy link
Author

CarbonChauvinist commented Oct 29, 2024

Just commenting out rocks-git.nvim from rocks.toml and then running :Rocks update did reproduce the issue (when started from homedir as cwd and with GIT_DIR and GIT_WORK_TREE env vars set) with my rocks log pasted below.

[START][2024-10-29 00:40:16] rocks.nvim logging initiated
WARN | 2024-10-29 00:40:37 | ...are/barerepo_issue/rocks/rocks_rtp/lua/rocks-git/git.lua:262 | Could not parse latest tag from: 
ERROR | 2024-10-29 00:40:39 | ...are/barerepo_issue/rocks/rocks_rtp/lua/rocks-git/git.lua:198 | {
  code = 1,
  signal = 0,
  stderr = "ssh_askpass: exec(/usr/lib/ssh/ssh-askpass): No such file or directory\r\[email protected]: Permission denied (publickey,password,keyboard-interactive).\r\nfatal: Could not read from remote repository.\n\nPlease make sure you have the correct access rights\nand the repository exists.\n",
  stdout = ""
}
ERROR | 2024-10-29 00:40:39 | ...po_issue/rocks/rocks_rtp/lua/rocks/operations/update.lua:76 | UPDATE ERROR: rocks-git: Failed to pull telescope-fzf-native.nvim
ERROR | 2024-10-29 00:40:39 | ...po_issue/rocks/rocks_rtp/lua/rocks/operations/update.lua:176 | Update completed with errors! Run ':Rocks log' for details.

However, if I first removed rocks-git.nvim completely (comment out and run :Rocks sync to prune the plugin) and then ran :Rocks update I did not get the errors in log and uncommited changes were not removed from my local bare repo.

@mrcjkb
Copy link
Member

mrcjkb commented Oct 31, 2024

I've pushed a release in which I unset the two env variables when invoking git.
Please let me know if that fixes it 😄

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

No branches or pull requests

2 participants