From 011a9b6a618680e223e3fffe80643c94ffb8eed2 Mon Sep 17 00:00:00 2001 From: Amolith Date: Sun, 18 Jan 2026 11:07:21 -0700 Subject: [PATCH] feat(add): default start-point to source branch When using -b from inside a worktree without specifying a start-point, defaults to the current worktree's branch. Assisted-by: Claude Opus 4.5 via Amp --- src/wt/cmd/add.lua | 4 ++++ src/wt/git.lua | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/wt/cmd/add.lua b/src/wt/cmd/add.lua index cc5d352803d53f2998889568852149210c5fd211..05b1bc23958217dd476cf55952f6209b7a932b9c 100644 --- a/src/wt/cmd/add.lua +++ b/src/wt/cmd/add.lua @@ -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( diff --git a/src/wt/git.lua b/src/wt/git.lua index 890fe081eb3eb0aaf3b01541970ba60194340415..a3d3ca493c6eef906d37452f6f3ed582a950ab03 100644 --- a/src/wt/git.lua +++ b/src/wt/git.lua @@ -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