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

Copy over git ignored files between worktrees #92

Open
Nikola-Milovic opened this issue Jul 20, 2022 · 1 comment
Open

Copy over git ignored files between worktrees #92

Nikola-Milovic opened this issue Jul 20, 2022 · 1 comment

Comments

@Nikola-Milovic
Copy link

Nikola-Milovic commented Jul 20, 2022

I am trying to symlink (should be the same as copying) my git ignored secrets and .env. I got it working for the 1st worktree created but subsequent worktree symlinks are messed up as current dir is not root anymore

local worktree = require("git-worktree")

worktree.on_tree_change(function(op, metadata)
	if op == worktree.Operations.Create then
		local worktree_path = vim.fn.getcwd() .. "/" .. metadata.path
		local secrets_path = "~/secrets"
		local copy_all_cmd = "ln -s " .. secrets_path .. "/* " .. worktree_path
		local copy_hidden_cmd = "ln -s " .. secrets_path .. "/.* " .. worktree_path
		os.execute(copy_all_cmd)
		os.execute(copy_hidden_cmd)
	end
end)

Two commands are used as 1 links the secrets and the other dotfiles (not sure why the first one doesn't link the dotfiles )

Example

  1. Nvim a fresh --bare clone
  2. Create a worktree, it will work and symlink everything
  3. Create a new worktree, symlink will be broken as the current dir is not root but the new worktree

The issue can be avoided by reopening the project but that adds a lot of friction to the workflow. The problem stems from the inability to get the project root dir

Does anyone have a similar use case and a more elegant solution? As my current idea is to hardcode the root dir

@arnevm123
Copy link

Hi,
Not sure if you still use this, but I think I have a slightly more elegant solution:
This issue helped me find the correct root:
#102

Then I have the files that are ignored by git in a folder in my bare repo root (makes more sense to me, I will have different environment variables for different repos).

And then the /{,.} makes the copy all + copy all hidden just one command.

Worktree.on_tree_change(function(op, metadata)
    local Path = require("plenary.path")
    if op == Worktree.Operations.Create then
        -- If we're dealing with create, the path is relative to the worktree and not absolute
        -- so we need to convert it to an absolute path.
        local path = metadata.path
        if not Path:new(path):is_absolute() then
            path = Path:new():absolute()
            if path:sub(-#"/") == "/" then
                path = string.sub(path, 1, string.len(path) - 1)
            end
        end
        -- local branch = branchname(metadata.path)
        local worktree_path = path .. "/" .. metadata.path .. "/"
        local gitignored_path = path .. "/gitignored"
        local link_gitignored = "ln -s " .. gitignored_path .. "/{*,.*} " .. worktree_path
        os.execute(link_gitignored)
    end
end)

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