Skip to content

Commit 0f6da26

Browse files
committed
feat: Enable user to create detached worktree
1 parent 314e334 commit 0f6da26

3 files changed

Lines changed: 45 additions & 19 deletions

File tree

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: 16 additions & 8 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

@@ -91,9 +92,12 @@ end
9192
local delete_success_handler = function(opts)
9293
opts = opts or {}
9394
force_next_deletion = false
94-
if confirm_branch_deletion() and opts.branch ~= nil then
95+
if opts.branch ~= nil and opts.branch ~= 'HEAD' and confirm_branch_deletion() then
9596
local delete_branch_job = Git.delete_branch_job(opts.branch)
9697
if delete_branch_job ~= nil then
98+
delete_branch_job:after_success(vim.schedule_wrap(function()
99+
print('Branch deleted')
100+
end))
97101
delete_branch_job:start()
98102
end
99103
end
@@ -134,6 +138,15 @@ local create_input_prompt = function(opts, cb)
134138
opts.pattern = nil -- show all branches that can be tracked
135139

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

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

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

0 commit comments

Comments
 (0)