Skip to content

Switching worktrees always puts me back in the directory #41

@JamesTeague

Description

@JamesTeague

When switching worktrees if I am in an Oil buffer, I stay in an Oil buffer as expected. However when I am in a file and switch worktrees that have that file as well, I still get dropped into an oil buffer at the root of the tree. I am switching using the telescope picker.

{
		"polarmurtex/git-worktree.nvim",
		version = "^2",
		dependencies = { "nvim-telescope/telescope.nvim" },
		config = function()
			-- vim.g.git_worktree = {
			--   change_directory_command = "cd",
			--   update_on_change = true,
			--   update_on_change_command = "e .",
			--   clearjumps_on_change = true,
			--   confirm_telescope_deletions = true,
			--   autopush = false,
			-- }
			local Hooks = require("git-worktree.hooks")
			local config = require("git-worktree.config")

			Hooks.register(Hooks.type.SWITCH, function(path, prev_path)
				vim.notify("Moved from " .. prev_path .. " to " .. path)
				-- check if current buffer is an oil buffer
				if vim.fn.expand("%"):find("^oil:///") then
					-- switch to new cwd in oil
					require("oil").open(vim.fn.getcwd())
				else
					-- use built in hook for non oil buffers
					Hooks.builtins.update_current_buffer_on_switch(path, prev_path)
				end
			end)

			Hooks.register(Hooks.type.DELETE, function()
				vim.cmd(config.update_on_change_command)
			end)
			require("telescope").load_extension("git_worktree")
		end,
		keys = {
			{
				"gw",
				function()
					require("telescope").extensions.git_worktree.git_worktree()
				end,
				desc = "[G]it [W]orktree",
			},
			{
				"<leader>gwc",
				function()
					require("telescope").extensions.git_worktree.create_git_worktree()
				end,
				desc = "[G]it [W]orktree [C]reate",
			},
		},
	},

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions