@@ -24,7 +24,7 @@ export function useWorktreeActions({ showConfirm }: UseWorktreeActionsOptions) {
2424 const terminalStore = useTerminalStore ( ) ;
2525 const repoStore = useRepoStore ( ) ;
2626
27- const isCreating = ref ( false ) ;
27+ const creatingRootDirs = ref ( new Set < string > ( ) ) ;
2828
2929 function handleWorktreeSelect ( wt : WorktreeEntry ) {
3030 terminalStore . viewMode = "wt" ;
@@ -47,37 +47,43 @@ export function useWorktreeActions({ showConfirm }: UseWorktreeActionsOptions) {
4747
4848 /** タイムスタンプで即座に新規 worktree を作成する(Task なし) */
4949 async function addWorktree ( rootDir : string ) {
50- if ( isCreating . value ) return ;
51- isCreating . value = true ;
52- // default branch を起点にする。Swift 側で `origin/HEAD` を優先し、未設定
53- // (remote 無し / push 前 repo)の場合は main repo root 自身の current branch に
54- // fallback した ref を受け取り、`startPoint` に渡す。
55- const branchResult = await tryCatch ( rpcGitDefaultBranch ( { dir : rootDir } ) ) ;
56- if ( ! branchResult . ok || branchResult . value . branch === "" ) {
57- notify . error (
58- "Failed to resolve default branch" ,
59- branchResult . ok ? undefined : branchResult . error ,
50+ if ( creatingRootDirs . value . has ( rootDir ) ) return ;
51+ creatingRootDirs . value . add ( rootDir ) ;
52+ try {
53+ // default branch を起点にする。Swift 側で `origin/HEAD` を優先し、未設定
54+ // (remote 無し / push 前 repo)の場合は main repo root 自身の current branch に
55+ // fallback した ref を受け取り、`startPoint` に渡す。
56+ const branchResult = await tryCatch ( rpcGitDefaultBranch ( { dir : rootDir } ) ) ;
57+ if ( ! branchResult . ok || branchResult . value . branch === "" ) {
58+ notify . error (
59+ "Failed to resolve default branch" ,
60+ branchResult . ok ? undefined : branchResult . error ,
61+ ) ;
62+ return ;
63+ }
64+ const timestamp = generateTimestamp ( ) ;
65+ const result = await tryCatch (
66+ rpcCreateWorktree ( {
67+ dir : rootDir ,
68+ worktreeDir : timestamp ,
69+ branch : timestamp ,
70+ startPoint : branchResult . value . branch ,
71+ } ) ,
6072 ) ;
61- isCreating . value = false ;
62- return ;
73+ if ( result . ok && result . value . worktree !== undefined ) {
74+ repoStore . appendWorktree ( rootDir , result . value . worktree ) ;
75+ terminalStore . viewMode = "wt" ;
76+ worktreeStore . setOpen ( result . value . dir ) ;
77+ } else {
78+ notify . error ( "Failed to add worktree" , result . ok ? undefined : result . error ) ;
79+ }
80+ } finally {
81+ creatingRootDirs . value . delete ( rootDir ) ;
6382 }
64- const timestamp = generateTimestamp ( ) ;
65- const result = await tryCatch (
66- rpcCreateWorktree ( {
67- dir : rootDir ,
68- worktreeDir : timestamp ,
69- branch : timestamp ,
70- startPoint : branchResult . value . branch ,
71- } ) ,
72- ) ;
73- if ( result . ok && result . value . worktree !== undefined ) {
74- repoStore . appendWorktree ( rootDir , result . value . worktree ) ;
75- terminalStore . viewMode = "wt" ;
76- worktreeStore . setOpen ( result . value . dir ) ;
77- } else {
78- notify . error ( "Failed to add worktree" , result . ok ? undefined : result . error ) ;
79- }
80- isCreating . value = false ;
83+ }
84+
85+ function isCreatingFor ( rootDir : string ) : boolean {
86+ return creatingRootDirs . value . has ( rootDir ) ;
8187 }
8288
8389 /** worktree 解除: 通常削除 → 失敗時に確認の上 --force */
@@ -105,7 +111,7 @@ export function useWorktreeActions({ showConfirm }: UseWorktreeActionsOptions) {
105111 }
106112
107113 return {
108- isCreating ,
114+ isCreatingFor ,
109115 handleWorktreeSelect,
110116 addWorktree,
111117 handleWorktreeRemove,
0 commit comments