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 option to disable fetching on branch creation #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,16 @@ edit the wrong files.

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

`fetch_on_create`: When creating a new worktree, do a git fetch. Defaults to true

```lua
require("git-worktree").setup({
change_directory_command = <str> -- default: "cd",
update_on_change = <boolean> -- default: true,
update_on_change_command = <str> -- default: "e .",
clearjumps_on_change = <boolean> -- default: true,
autopush = <boolean> -- default: false,
fetch_on_create = <boolean> -- default: true,
})
```

Expand Down
30 changes: 20 additions & 10 deletions lua/git-worktree/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,16 @@ local function create_worktree(path, branch, upstream, found_branch)
worktree_path = Path:new(git_worktree_root, path):absolute()
end

local fetch = Job:new({
'git', 'fetch', '--all',
cwd = worktree_path,
on_start = function()
status:next_status("git fetch --all (This may take a moment)")
end
})
local fetch
if M._config.fetch_on_create then
fetch = Job:new({
'git', 'fetch', '--all',
cwd = worktree_path,
on_start = function()
status:next_status("git fetch --all (This may take a moment)")
end
})
end

local set_branch_cmd = 'git'
local set_branch_args= {'branch', string.format('--set-upstream-to=%s/%s', upstream, branch)}
Expand Down Expand Up @@ -343,8 +346,12 @@ local function create_worktree(path, branch, upstream, found_branch)
})

if upstream ~= nil then
create:and_then_on_success(fetch)
fetch:and_then_on_success(set_branch)
if M._config.fetch_on_create then
create:and_then_on_success(fetch)
fetch:and_then_on_success(set_branch)
else
create:and_then_on_success(set_branch)
end

if M._config.autopush then
-- These are "optional" operations.
Expand All @@ -357,7 +364,9 @@ local function create_worktree(path, branch, upstream, found_branch)
end

create:after_failure(failure("create_worktree", create.args, git_worktree_root))
fetch:after_failure(failure("create_worktree", fetch.args, worktree_path))
if M._config.fetch_on_create then
fetch:after_failure(failure("create_worktree", fetch.args, worktree_path))
end

set_branch:after_failure(failure("create_worktree", set_branch.args, worktree_path, true))

Expand Down Expand Up @@ -543,6 +552,7 @@ M.setup = function(config)
confirm_telescope_deletions = false,
-- should this default to true or false?
autopush = false,
fetch_on_create = true,
}, config)
end

Expand Down