@@ -89,6 +89,15 @@ function localBranchExists(repoRoot: string, branch: string, env?: Record<string
8989 }
9090}
9191
92+ function remoteBranchExists ( repoRoot : string , branch : string , env ?: Record < string , string > ) : boolean {
93+ try {
94+ runGit ( [ "show-ref" , "--verify" , "--quiet" , `refs/remotes/origin/${ branch } ` ] , repoRoot , env ) ;
95+ return true ;
96+ } catch {
97+ return false ;
98+ }
99+ }
100+
92101function ensureDir ( path : string ) : void {
93102 if ( ! existsSync ( path ) ) {
94103 mkdirSync ( path , { recursive : true } ) ;
@@ -150,9 +159,11 @@ function ensureWorktreeConfig(repoRoot: string, env?: Record<string, string>): v
150159export async function ensureSessionWorktree ( params : {
151160 cwd : string ;
152161 worktreeId : string ;
162+ baseBranch : string ;
153163 env ?: Record < string , string > ;
154164} ) : Promise < WorktreeResult > {
155- const { cwd, worktreeId, env } = params ;
165+ const { cwd, worktreeId, baseBranch, env } = params ;
166+ const normalizedBaseBranch = baseBranch . trim ( ) || "main" ;
156167 const repoRoot = resolveRepoRoot ( cwd , env ) ;
157168 if ( ! repoRoot ) {
158169 return { worktreePath : cwd , repoRoot : null , created : false , reused : false , skipped : true } ;
@@ -180,8 +191,8 @@ export async function ensureSessionWorktree(params: {
180191
181192 const currentBranch = getCurrentBranch ( repoRoot , env ) ;
182193 const dirtyPaths = getDirtyPaths ( repoRoot , env ) ;
183- if ( currentBranch === "main" && dirtyPaths . length > 0 ) {
184- const message = "Main has uncommitted changes, skipping worktree and staying on main." ;
194+ if ( currentBranch === normalizedBaseBranch && dirtyPaths . length > 0 ) {
195+ const message = ` ${ normalizedBaseBranch } has uncommitted changes, skipping worktree and staying on ${ normalizedBaseBranch } .` ;
185196 log . warn ( message , { repoRoot } ) ;
186197 return {
187198 worktreePath : repoRoot ,
@@ -193,15 +204,52 @@ export async function ensureSessionWorktree(params: {
193204 } ;
194205 }
195206
207+ let hasLocalBaseBranch = localBranchExists ( repoRoot , normalizedBaseBranch , env ) ;
208+ let hasRemoteBaseBranch = remoteBranchExists ( repoRoot , normalizedBaseBranch , env ) ;
209+ if ( ! hasLocalBaseBranch && ! hasRemoteBaseBranch ) {
210+ try {
211+ runGit ( [ "fetch" , "origin" , normalizedBaseBranch ] , repoRoot , env ) ;
212+ } catch {
213+ // Handle branch-not-found with a user-facing message below.
214+ }
215+ hasLocalBaseBranch = localBranchExists ( repoRoot , normalizedBaseBranch , env ) ;
216+ hasRemoteBaseBranch = remoteBranchExists ( repoRoot , normalizedBaseBranch , env ) ;
217+ }
218+
219+ if ( ! hasLocalBaseBranch && ! hasRemoteBaseBranch ) {
220+ const message = `Base branch '${ normalizedBaseBranch } ' not found in this repo. Open channel settings and update Base Branch.` ;
221+ log . warn ( message , { repoRoot, baseBranch : normalizedBaseBranch } ) ;
222+ return {
223+ worktreePath : repoRoot ,
224+ repoRoot,
225+ created : false ,
226+ reused : false ,
227+ skipped : true ,
228+ message,
229+ } ;
230+ }
231+
196232 ensureDir ( worktreeDir ) ;
197- log . info ( "Pulling latest main before creating worktree" , { repoRoot } ) ;
198- runGit ( [ "pull" , "origin" , "main" ] , repoRoot , env ) ;
233+ if ( currentBranch === normalizedBaseBranch ) {
234+ log . info ( "Pulling latest base branch before creating worktree" , {
235+ repoRoot,
236+ baseBranch : normalizedBaseBranch ,
237+ } ) ;
238+ runGit ( [ "pull" , "origin" , normalizedBaseBranch ] , repoRoot , env ) ;
239+ } else {
240+ log . info ( "Fetching latest base branch before creating worktree" , {
241+ repoRoot,
242+ baseBranch : normalizedBaseBranch ,
243+ } ) ;
244+ runGit ( [ "fetch" , "origin" , normalizedBaseBranch ] , repoRoot , env ) ;
245+ }
199246
200247 log . info ( "Creating worktree for session" , { worktreePath, worktreeId } ) ;
248+ const startPoint = hasLocalBaseBranch ? normalizedBaseBranch : `origin/${ normalizedBaseBranch } ` ;
201249 if ( localBranchExists ( repoRoot , worktreeId , env ) ) {
202250 runGit ( [ "worktree" , "add" , worktreePath , worktreeId ] , repoRoot , env ) ;
203251 } else {
204- runGit ( [ "worktree" , "add" , worktreePath , "-b" , worktreeId , "main" ] , repoRoot , env ) ;
252+ runGit ( [ "worktree" , "add" , worktreePath , "-b" , worktreeId , startPoint ] , repoRoot , env ) ;
205253 }
206254 copyEnvFile ( repoRoot , worktreePath ) ;
207255
0 commit comments