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

Add update_on_worktree_change config option, and depreacte update_on_change_command #94

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,26 @@ Set this to `tcd` if you want to only change the `pwd` for the current vim Tab.
`update_on_change`: Updates the current buffer to point to the new work tree if
the file is found in the new project. Otherwise, the following command will be run.

`update_on_change_command`: The vim command to run during the `update_on_change` event.
Note, that this command will only be run when the current file is not found in the new worktree.
This option defaults to `e .` which opens the root directory of the new worktree.
`update_on_new_worktree`: The lua function to be run after changing the working
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now realize that update_on_worktree_change may be more appropriate. I will update the PR.

directory to the new worktree. The default function is to update the current
buffer to the equivalent file in the new worktree.

`clearjumps_on_change`: Every time you switch branches, your jumplist will be
cleared so that you don't accidentally go backward to a different branch and
edit the wrong files.

`autopush`: When creating a new worktree, it will push the branch to the upstream then perform a `git rebase`

*DEPRECATED*
`update_on_change_command`: The vim command to run during the `update_on_change` event.
Note, that this command will only be run when the current file is not found in the new worktree.
This option defaults to `e .` which opens the root directory of the new worktree.

```lua
require("git-worktree").setup({
change_directory_command = <str> -- default: "cd",
update_on_change = <boolean> -- default: true,
update_on_change_command = <str> -- default: "e .",
update_on_new_worktree = <function(op, metadata)> -- default: `update_current_buffer_with_fallback`,
clearjumps_on_change = <boolean> -- default: true,
autopush = <boolean> -- default: false,
})
Expand Down
22 changes: 16 additions & 6 deletions lua/git-worktree/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,23 @@ M.setup_git_info = function()

end

local function update_current_buffer_with_fallback(op, metadata)
local changed = M.update_current_buffer(metadata["prev_path"])
if not changed then
status:log().debug("Could not change to the file in the new worktree, running `e .`")
if M._config.update_on_change_command then
status:log().warn("`update_on_change_command` will be soon be deprecated. Use `update_on_new_worktree` instead.")
vim.cmd(M._config.update_on_change_command)
return
end
vim.cmd("e .")
end
end

local function on_tree_change_handler(op, metadata)
if M._config.update_on_change then
if op == Enum.Operations.Switch then
local changed = M.update_current_buffer(metadata["prev_path"])
if not changed then
status:log().debug("Could not change to the file in the new worktree, running the `update_on_change_command`")
vim.cmd(M._config.update_on_change_command)
end
M._config.update_on_new_worktree(op, metadata)
end
end
end
Expand Down Expand Up @@ -537,7 +546,8 @@ M.setup = function(config)
M._config = vim.tbl_deep_extend("force", {
change_directory_command = "cd",
update_on_change = true,
update_on_change_command = "e .",
update_on_change_command = nil,
update_on_new_worktree = update_current_buffer_with_fallback,
clearjumps_on_change = true,
-- default to false to avoid breaking the previous default behavior
confirm_telescope_deletions = false,
Expand Down