Skip to content

Commit c7b424b

Browse files
committed
feat: add worktree option to ClaudeAgentOptions
Support the --worktree CLI flag for creating git worktrees per session. Accepts True for auto-generated names or a string for explicit naming.
1 parent 302ceb6 commit c7b424b

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/claude_agent_sdk/_internal/transport/subprocess_cli.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@ def _build_command(self) -> list[str]:
326326
if schema is not None:
327327
cmd.extend(["--json-schema", json.dumps(schema)])
328328

329+
# Worktree support
330+
if self._options.worktree is not None:
331+
if isinstance(self._options.worktree, str):
332+
cmd.extend(["--worktree", self._options.worktree])
333+
elif self._options.worktree:
334+
cmd.append("--worktree")
335+
329336
# Always use streaming mode with stdin (matching TypeScript SDK)
330337
# This allows agents and other large configs to be sent via initialize request
331338
cmd.extend(["--input-format", "stream-json"])

src/claude_agent_sdk/types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,10 @@ class ClaudeAgentOptions:
10961096
# When enabled, files can be rewound to their state at any user message
10971097
# using `ClaudeSDKClient.rewind_files()`.
10981098
enable_file_checkpointing: bool = False
1099+
# Create a new git worktree for this session.
1100+
# When True, creates a worktree with an auto-generated name.
1101+
# When a string, creates a worktree with the specified name.
1102+
worktree: bool | str | None = None
10991103

11001104

11011105
# SDK Control Protocol

tests/test_transport.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,3 +912,27 @@ def test_build_command_large_agents_work(self):
912912
# No @filepath references should exist
913913
cmd_str = " ".join(cmd)
914914
assert "@" not in cmd_str
915+
916+
def test_build_command_with_worktree_bool(self):
917+
"""Test building CLI command with worktree=True."""
918+
transport = SubprocessCLITransport(
919+
prompt="test",
920+
options=make_options(worktree=True),
921+
)
922+
cmd = transport._build_command()
923+
assert "--worktree" in cmd
924+
# Should not have a name argument after --worktree
925+
wt_idx = cmd.index("--worktree")
926+
assert wt_idx + 1 >= len(cmd) or cmd[wt_idx + 1].startswith("--")
927+
928+
def test_build_command_with_worktree_name(self):
929+
"""Test building CLI command with worktree as a named string."""
930+
transport = SubprocessCLITransport(
931+
prompt="test",
932+
options=make_options(worktree="my-feature"),
933+
)
934+
cmd = transport._build_command()
935+
assert "--worktree" in cmd
936+
wt_idx = cmd.index("--worktree")
937+
assert cmd[wt_idx + 1] == "my-feature"
938+

0 commit comments

Comments
 (0)