Skip to content

Commit 7c5058a

Browse files
committed
Add option to disable fetching on branch creation
1 parent d7f4e25 commit 7c5058a

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,16 @@ edit the wrong files.
8787

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

90+
`fetch_on_create`: When creating a new worktree, do a git fetch. Defaults to true
91+
9092
```lua
9193
require("git-worktree").setup({
9294
change_directory_command = <str> -- default: "cd",
9395
update_on_change = <boolean> -- default: true,
9496
update_on_change_command = <str> -- default: "e .",
9597
clearjumps_on_change = <boolean> -- default: true,
9698
autopush = <boolean> -- default: false,
99+
fetch_on_create = <boolean> -- default: true,
97100
})
98101
```
99102

lua/git-worktree/init.lua

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,16 @@ local function create_worktree(path, branch, upstream, found_branch)
302302
worktree_path = Path:new(git_worktree_root, path):absolute()
303303
end
304304

305-
local fetch = Job:new({
306-
'git', 'fetch', '--all',
307-
cwd = worktree_path,
308-
on_start = function()
309-
status:next_status("git fetch --all (This may take a moment)")
310-
end
311-
})
305+
local fetch
306+
if M._config.fetch_on_create then
307+
fetch = Job:new({
308+
'git', 'fetch', '--all',
309+
cwd = worktree_path,
310+
on_start = function()
311+
status:next_status("git fetch --all (This may take a moment)")
312+
end
313+
})
314+
end
312315

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

345348
if upstream ~= nil then
346-
create:and_then_on_success(fetch)
347-
fetch:and_then_on_success(set_branch)
349+
if M._config.fetch_on_create then
350+
create:and_then_on_success(fetch)
351+
fetch:and_then_on_success(set_branch)
352+
else
353+
create:and_then_on_success(set_branch)
354+
end
348355

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

359366
create:after_failure(failure("create_worktree", create.args, git_worktree_root))
360-
fetch:after_failure(failure("create_worktree", fetch.args, worktree_path))
367+
if M._config.fetch_on_create then
368+
fetch:after_failure(failure("create_worktree", fetch.args, worktree_path))
369+
end
361370

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

@@ -543,6 +552,7 @@ M.setup = function(config)
543552
confirm_telescope_deletions = false,
544553
-- should this default to true or false?
545554
autopush = false,
555+
fetch_on_create = true,
546556
}, config)
547557
end
548558

0 commit comments

Comments
 (0)