Skip to content

Commit

Permalink
Add option to disable fetching on branch creation
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaMcGlinn committed Jan 8, 2024
1 parent f247308 commit c43933b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
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

0 comments on commit c43933b

Please sign in to comment.