Skip to content

Commit a8e7c38

Browse files
committed
feat: Enable user to create detached worktree
1 parent f445fb8 commit a8e7c38

File tree

3 files changed

+44
-18
lines changed

3 files changed

+44
-18
lines changed

lua/git-worktree/git.lua

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function M.has_branch(branch, opts, cb)
135135
end
136136

137137
--- @param path string
138-
--- @param branch string
138+
--- @param branch string?
139139
--- @param found_branch boolean
140140
--- @param upstream string
141141
--- @param found_upstream boolean
@@ -144,18 +144,23 @@ function M.create_worktree_job(path, branch, found_branch, upstream, found_upstr
144144
local worktree_add_cmd = 'git'
145145
local worktree_add_args = { 'worktree', 'add' }
146146

147-
if not found_branch then
148-
table.insert(worktree_add_args, '-b')
149-
table.insert(worktree_add_args, branch)
147+
if branch == nil then
148+
table.insert(worktree_add_args, '-d')
150149
table.insert(worktree_add_args, path)
151-
152-
if found_upstream and branch ~= upstream then
153-
table.insert(worktree_add_args, '--track')
154-
table.insert(worktree_add_args, upstream)
155-
end
156150
else
157-
table.insert(worktree_add_args, path)
158-
table.insert(worktree_add_args, branch)
151+
if not found_branch then
152+
table.insert(worktree_add_args, '-b')
153+
table.insert(worktree_add_args, branch)
154+
table.insert(worktree_add_args, path)
155+
156+
if found_upstream and branch ~= upstream then
157+
table.insert(worktree_add_args, '--track')
158+
table.insert(worktree_add_args, upstream)
159+
end
160+
else
161+
table.insert(worktree_add_args, path)
162+
table.insert(worktree_add_args, branch)
163+
end
159164
end
160165

161166
return Job:new {

lua/git-worktree/worktree.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ function M.create(path, branch, upstream)
113113
return
114114
end
115115

116+
if branch == '' then
117+
-- detached head
118+
local create_wt_job = Git.create_worktree_job(path, nil, false, nil, false)
119+
create_wt_job:after(function()
120+
vim.schedule(function()
121+
Hooks.emit(Hooks.type.CREATE, path, branch, upstream)
122+
M.switch(path)
123+
end)
124+
end)
125+
create_wt_job:start()
126+
return
127+
end
128+
116129
Git.has_branch(branch, { '--remotes' }, function(found_remote_branch)
117130
Log.debug('Found remote branch %s? %s', branch, found_remote_branch)
118131
if found_remote_branch then

lua/telescope/_extensions/git_worktree.lua

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local conf = require('telescope.config').values
99
local git_worktree = require('git-worktree')
1010
local Config = require('git-worktree.config')
1111
local Git = require('git-worktree.git')
12+
local Log = require('git-worktree.logger')
1213

1314
local force_next_deletion = false
1415

@@ -93,6 +94,9 @@ local delete_success_handler = function(opts)
9394
if confirm_branch_deletion() and opts.branch ~= nil then
9495
local delete_branch_job = Git.delete_branch_job(opts.branch)
9596
if delete_branch_job ~= nil then
97+
delete_branch_job:after_success(vim.schedule_wrap(function()
98+
print('Branch deleted')
99+
end))
96100
delete_branch_job:start()
97101
end
98102
end
@@ -133,6 +137,15 @@ local create_input_prompt = function(opts, cb)
133137
opts.pattern = nil -- show all branches that can be tracked
134138

135139
local path = vim.fn.input('Path to subtree > ', opts.branch)
140+
if path == '' then
141+
Log.error("No worktree path provided")
142+
return
143+
end
144+
145+
if opts.branch == '' then
146+
cb(path, nil)
147+
return
148+
end
136149

137150
local branches = vim.fn.systemlist('git branch --all')
138151
if #branches == 0 then
@@ -172,17 +185,11 @@ local telescope_create_worktree = function(opts)
172185
git_worktree.switch_worktree(nil)
173186
opts = opts or {}
174187

175-
-- TODO: Enable detached HEAD worktree creation, but for this the telescope
176-
-- picker git_branches must show refs/tags.
177-
178188
local create_branch = function(prompt_bufnr, _)
179189
-- if current_line is still not enough to filter everything but user
180190
-- still wants to use it as the new branch name, without selecting anything
181191
local branch = action_state.get_current_line()
182192
actions.close(prompt_bufnr)
183-
if branch == nil then
184-
return
185-
end
186193
opts.branch = branch
187194
create_input_prompt(opts, function(path, upstream)
188195
git_worktree.create_worktree(path, branch, upstream)
@@ -196,7 +203,8 @@ local telescope_create_worktree = function(opts)
196203
-- selected_entry can be null if current_line filters everything
197204
-- and there's no branch shown
198205
local branch = selected_entry ~= nil and selected_entry.value or current_line
199-
if branch == nil then
206+
if branch == nil or branch == '' then
207+
Log.error("No branch selected")
200208
return
201209
end
202210
opts.branch = branch

0 commit comments

Comments
 (0)