From c43933bb2b27401411735b7ca1b2de0499cae815 Mon Sep 17 00:00:00 2001 From: Tama McGlinn Date: Thu, 15 Sep 2022 16:57:50 +0200 Subject: [PATCH] Add option to disable fetching on branch creation --- README.md | 3 +++ lua/git-worktree/init.lua | 30 ++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index aaa8970..c990d04 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,8 @@ 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 = -- default: "cd", @@ -94,6 +96,7 @@ require("git-worktree").setup({ update_on_change_command = -- default: "e .", clearjumps_on_change = -- default: true, autopush = -- default: false, + fetch_on_create = -- default: true, }) ``` diff --git a/lua/git-worktree/init.lua b/lua/git-worktree/init.lua index 0fb5295..f4ad818 100644 --- a/lua/git-worktree/init.lua +++ b/lua/git-worktree/init.lua @@ -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)} @@ -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. @@ -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)) @@ -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