@@ -38,17 +38,67 @@ export class ClaudeSandbox {
3838 const currentBranch = await this . git . branchLocal ( ) ;
3939 console . log ( chalk . blue ( `Current branch: ${ currentBranch . current } ` ) ) ;
4040
41- // Use target branch from config or generate one
42- const branchName =
43- this . config . targetBranch ||
44- ( ( ) => {
45- const timestamp = new Date ( )
46- . toISOString ( )
47- . replace ( / [: .] / g, "-" )
48- . split ( "T" ) [ 0 ] ;
49- return `claude/${ timestamp } -${ Date . now ( ) } ` ;
50- } ) ( ) ;
51- console . log ( chalk . blue ( `Will create branch in container: ${ branchName } ` ) ) ;
41+ // Determine target branch based on config options
42+ let branchName = "" ;
43+
44+ if ( this . config . prNumber ) {
45+ // Checkout PR
46+ console . log ( chalk . blue ( `Fetching PR #${ this . config . prNumber } ...` ) ) ;
47+ try {
48+ branchName = `pr-${ this . config . prNumber } ` ;
49+
50+ // Check if PR branch already exists locally
51+ const branches = await this . git . branchLocal ( ) ;
52+ if ( branches . all . includes ( branchName ) ) {
53+ // PR branch exists, just checkout
54+ await this . git . checkout ( branchName ) ;
55+ console . log ( chalk . green ( `✓ Switched to existing PR branch: ${ branchName } ` ) ) ;
56+ } else {
57+ // Fetch and create new PR branch
58+ await this . git . fetch ( "origin" , `pull/${ this . config . prNumber } /head:${ branchName } ` ) ;
59+ await this . git . checkout ( branchName ) ;
60+ console . log ( chalk . green ( `✓ Checked out PR #${ this . config . prNumber } ` ) ) ;
61+ }
62+ } catch ( error ) {
63+ console . error ( chalk . red ( `✗ Failed to checkout PR #${ this . config . prNumber } :` ) , error ) ;
64+ throw error ;
65+ }
66+ } else if ( this . config . remoteBranch ) {
67+ // Checkout remote branch
68+ console . log ( chalk . blue ( `Checking out remote branch: ${ this . config . remoteBranch } ...` ) ) ;
69+ try {
70+ await this . git . fetch ( "origin" ) ;
71+ const localBranchName = this . config . remoteBranch . replace ( "origin/" , "" ) ;
72+
73+ // Check if local branch already exists
74+ const branches = await this . git . branchLocal ( ) ;
75+ if ( branches . all . includes ( localBranchName ) ) {
76+ // Local branch exists, just checkout
77+ await this . git . checkout ( localBranchName ) ;
78+ console . log ( chalk . green ( `✓ Switched to existing branch: ${ localBranchName } ` ) ) ;
79+ } else {
80+ // Create new local branch from remote
81+ await this . git . checkoutBranch ( localBranchName , this . config . remoteBranch ) ;
82+ console . log ( chalk . green ( `✓ Checked out remote branch: ${ this . config . remoteBranch } ` ) ) ;
83+ }
84+ branchName = localBranchName ;
85+ } catch ( error ) {
86+ console . error ( chalk . red ( `✗ Failed to checkout remote branch ${ this . config . remoteBranch } :` ) , error ) ;
87+ throw error ;
88+ }
89+ } else {
90+ // Use target branch from config or generate one
91+ branchName =
92+ this . config . targetBranch ||
93+ ( ( ) => {
94+ const timestamp = new Date ( )
95+ . toISOString ( )
96+ . replace ( / [: .] / g, "-" )
97+ . split ( "T" ) [ 0 ] ;
98+ return `claude/${ timestamp } -${ Date . now ( ) } ` ;
99+ } ) ( ) ;
100+ console . log ( chalk . blue ( `Will create branch in container: ${ branchName } ` ) ) ;
101+ }
52102
53103 // Discover credentials (optional - don't fail if not found)
54104 const credentials = await this . credentialManager . discover ( ) ;
0 commit comments