File tree Expand file tree Collapse file tree 3 files changed +35
-0
lines changed
Expand file tree Collapse file tree 3 files changed +35
-0
lines changed Original file line number Diff line number Diff 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" ])
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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+
You can’t perform that action at this time.
0 commit comments