Copy over git ignored files between worktrees
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
- Nvim a fresh
--bareclone - Create a worktree, it will work and symlink everything
- 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
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: https://github.com/ThePrimeagen/git-worktree.nvim/issues/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)