Skip to content

Commit 6906231

Browse files
cvdubclaude
andcommitted
Simplify worktree support to delegate to Claude CLI
The Claude CLI already handles worktree creation and cleanup via its --worktree flag. Remove the Emacs-side worktree management code and pass --worktree [name] as a command-line switch instead. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e86b259 commit 6906231

1 file changed

Lines changed: 11 additions & 81 deletions

File tree

claude-code.el

Lines changed: 11 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,6 @@ This must be set to the path of your Claude sandbox binary before use."
110110
:type '(choice (const :tag "Not configured" nil) string)
111111
:group 'claude-code)
112112

113-
(defcustom claude-code-worktree-directory ".claude/worktrees/"
114-
"Subdirectory under git root where worktrees are created."
115-
:type 'string
116-
:group 'claude-code)
117-
118-
(defcustom claude-code-worktree-confirm-cleanup t
119-
"Whether to prompt before removing the worktree on buffer kill."
120-
:type 'boolean
121-
:group 'claude-code)
122-
123113
(defcustom claude-code-newline-keybinding-style 'newline-on-shift-return
124114
"Key binding style for entering newlines and sending messages.
125115
@@ -957,56 +947,6 @@ If not in a project and no buffer file return `default-directory'."
957947
;; Case 3: No project and no buffer file
958948
(t default-directory))))
959949

960-
(defvar-local claude-code--worktree-path nil
961-
"Path to the git worktree for this Claude buffer.
962-
963-
Set by `claude-code-worktree' so that the worktree can be cleaned up
964-
when the buffer is killed.")
965-
966-
(defun claude-code--git-root ()
967-
"Return the git repository root for the current directory.
968-
969-
Return nil if not in a git repository."
970-
(condition-case nil
971-
(car (process-lines "git" "rev-parse" "--show-toplevel"))
972-
(error nil)))
973-
974-
(defun claude-code--create-worktree (git-root &optional name)
975-
"Create a git worktree under GIT-ROOT.
976-
977-
NAME is the worktree name; if nil a timestamp-based name is generated.
978-
Return the worktree path on success, signal an error on failure."
979-
(let* ((wt-name (or name (format-time-string "wt-%Y%m%d-%H%M%S")))
980-
(wt-path (expand-file-name
981-
(concat claude-code-worktree-directory wt-name)
982-
git-root))
983-
(branch (concat "claude-worktree-" wt-name))
984-
(exit-code (call-process "git" nil nil nil
985-
"worktree" "add" wt-path "-b" branch)))
986-
(unless (zerop exit-code)
987-
(error "Failed to create git worktree at %s" wt-path))
988-
wt-path))
989-
990-
(defun claude-code--remove-worktree (worktree-path)
991-
"Remove the git worktree at WORKTREE-PATH.
992-
993-
Errors are reported as messages rather than signaled."
994-
(let ((exit-code (call-process "git" nil nil nil
995-
"worktree" "remove" worktree-path)))
996-
(if (zerop exit-code)
997-
(message "Removed git worktree at %s" worktree-path)
998-
(message "Failed to remove git worktree at %s" worktree-path))))
999-
1000-
(defun claude-code--maybe-cleanup-worktree ()
1001-
"Remove the git worktree associated with this buffer if confirmed.
1002-
1003-
Called from `kill-buffer-hook' in worktree Claude buffers."
1004-
(when claude-code--worktree-path
1005-
(when (or (not claude-code-worktree-confirm-cleanup)
1006-
(y-or-n-p (format "Remove git worktree at %s? "
1007-
claude-code--worktree-path)))
1008-
(claude-code--remove-worktree claude-code--worktree-path))))
1009-
1010950
(defun claude-code--find-all-claude-buffers ()
1011951
"Find all active Claude buffers across all directories.
1012952
@@ -1458,29 +1398,19 @@ for the project directory."
14581398
(defun claude-code-worktree (&optional arg)
14591399
"Start Claude in a new git worktree for isolated editing.
14601400
1461-
Creates a git worktree under the directory specified by
1462-
`claude-code-worktree-directory' and starts Claude there. When the
1463-
Claude buffer is killed, the worktree is optionally cleaned up
1464-
\(controlled by `claude-code-worktree-confirm-cleanup').
1401+
Passes --worktree to the Claude CLI, which handles worktree creation
1402+
and cleanup. Prompts for an optional worktree name.
14651403
1466-
With prefix ARG (\\[universal-argument]), switch to buffer after creating."
1404+
With prefix ARG (\\[universal-argument]), switch to buffer after creating.
1405+
1406+
With double prefix ARG (\\[universal-argument] \\[universal-argument]),
1407+
prompt for the project directory."
14671408
(interactive "P")
1468-
(let* ((git-root (claude-code--git-root))
1469-
(_ (unless git-root (error "Not in a git repository")))
1470-
(name (read-string "Worktree name: "
1471-
(format-time-string "wt-%Y%m%d-%H%M%S")))
1472-
(worktree-path (claude-code--create-worktree git-root name))
1473-
buf-name)
1474-
(cl-letf (((symbol-function 'claude-code--directory)
1475-
(lambda () worktree-path)))
1476-
(claude-code--start (when arg '(4)) nil)
1477-
(setq buf-name (claude-code--buffer-name "default")))
1478-
(let ((buf (get-buffer buf-name)))
1479-
(when buf
1480-
(with-current-buffer buf
1481-
(setq claude-code--worktree-path worktree-path)
1482-
(add-hook 'kill-buffer-hook
1483-
#'claude-code--maybe-cleanup-worktree nil t))))))
1409+
(let* ((name (read-string "Worktree name (empty for auto): "))
1410+
(extra-switches (if (string-empty-p name)
1411+
'("--worktree")
1412+
(list "--worktree" name))))
1413+
(claude-code--start arg extra-switches)))
14841414

14851415
;;;###autoload
14861416
(defun claude-code-sandbox (&optional arg)

0 commit comments

Comments
 (0)