@@ -70,6 +70,10 @@ function M.cmd_add(args)
local output, code
if create_branch then
+ -- Default start-point to source worktree's branch if inside one
+ if not start_point and source_worktree then
+ start_point = git.get_worktree_branch(git_dir, source_worktree)
+ end
-- Create new branch with worktree
if start_point then
output, code = shell.run_cmd(
@@ -193,6 +193,24 @@ function M.parse_worktree_list(output)
return worktrees
end
+---Get the branch checked out in a specific worktree
+---@param git_dir string
+---@param worktree_path string
+---@return string|nil branch name, or nil if detached/not found
+function M.get_worktree_branch(git_dir, worktree_path)
+ local output, code = run_cmd("GIT_DIR=" .. git_dir .. " git worktree list --porcelain")
+ if code ~= 0 then
+ return nil
+ end
+ local worktrees = M.parse_worktree_list(output)
+ for _, wt in ipairs(worktrees) do
+ if wt.path == worktree_path then
+ return wt.branch
+ end
+ end
+ return nil
+end
+
---Check if branch is checked out in any worktree
---@param git_dir string
---@param branch string