@@ -16,6 +16,23 @@ import { existsSync } from "fs";
1616
1717const DEFAULT_CLONE_DIR = "/tmp/workspace" ;
1818
19+ // =============================================================================
20+ // Helpers
21+ // =============================================================================
22+
23+ /**
24+ * Checks if a branch exists on the remote using ls-remote.
25+ * This avoids masking auth/network errors as "branch not found".
26+ */
27+ function branchExistsOnRemote ( repoLocation : string , branchName : string ) : boolean {
28+ const output = execFileSync ( "git" , [ "ls-remote" , "--heads" , "origin" , branchName ] , {
29+ cwd : repoLocation ,
30+ encoding : "utf-8" ,
31+ stdio : [ "pipe" , "pipe" , "pipe" ] ,
32+ } ) . trim ( ) ;
33+ return output . length > 0 ;
34+ }
35+
1936// =============================================================================
2037// Types
2138// =============================================================================
@@ -93,20 +110,21 @@ export function cloneRepo(options: CloneRepoOptions): string {
93110 execFileSync ( "git" , [ "checkout" , "-f" , "HEAD" ] , { cwd : repoLocation } ) ;
94111
95112 if ( branchName ) {
96- // Try to fetch and reset to the remote branch; create locally if it doesn't exist
97- try {
113+ // Check if branch exists on remote before attempting fetch
114+ const remoteBranchExists = branchExistsOnRemote ( repoLocation , branchName ) ;
115+ if ( remoteBranchExists ) {
98116 execFileSync ( "git" , [ "fetch" , "--depth" , "2" , "origin" , branchName ] , { cwd : repoLocation , stdio : "pipe" } ) ;
99- execFileSync ( "git" , [ "checkout" , "-B" , branchName , `origin/ ${ branchName } ` ] , { cwd : repoLocation } ) ;
117+ execFileSync ( "git" , [ "checkout" , "-B" , branchName , "FETCH_HEAD" ] , { cwd : repoLocation } ) ;
100118 console . log ( `[Engine SDK] Checked out existing branch: ${ branchName } ` ) ;
101- } catch {
119+ } else {
102120 // Branch doesn't exist on remote — fetch default branch and create new branch from it
103121 execFileSync ( "git" , [ "fetch" , "--depth" , "2" , "origin" ] , { cwd : repoLocation , stdio : "pipe" } ) ;
104122 execFileSync ( "git" , [ "checkout" , "-B" , branchName , "FETCH_HEAD" ] , { cwd : repoLocation } ) ;
105123 console . log ( `[Engine SDK] Created new branch: ${ branchName } ` ) ;
106124 }
107125 }
108126 } else {
109- const authenticatedUrl = cloneUrl . replace ( "://" , `:// x-access-token:${ gitToken } @` ) ;
127+ const authHeader = `Authorization: basic ${ Buffer . from ( ` x-access-token:${ gitToken } ` ) . toString ( "base64" ) } ` ;
110128
111129 // Try to clone the existing remote branch directly.
112130 // If the branch doesn't exist yet, fall back to a default clone + new branch.
@@ -116,7 +134,7 @@ export function cloneRepo(options: CloneRepoOptions): string {
116134 console . log ( `[Engine SDK] Cloning ${ repository } (branch: ${ branchName } ) to ${ repoLocation } ...` ) ;
117135 execFileSync (
118136 "git" ,
119- [ "clone" , "-b" , branchName , "--single-branch" , "--depth" , "2" , authenticatedUrl , repoLocation ] ,
137+ [ "-c" , `http.extraHeader= ${ authHeader } ` , " clone", "-b" , branchName , "--single-branch" , "--depth" , "2" , cloneUrl , repoLocation ] ,
120138 { stdio : "inherit" }
121139 ) ;
122140 clonedExistingBranch = true ;
@@ -127,7 +145,7 @@ export function cloneRepo(options: CloneRepoOptions): string {
127145
128146 if ( ! clonedExistingBranch ) {
129147 console . log ( `[Engine SDK] Cloning ${ repository } to ${ repoLocation } ...` ) ;
130- execFileSync ( "git" , [ "clone" , "--depth" , "2" , authenticatedUrl , repoLocation ] , {
148+ execFileSync ( "git" , [ "-c" , `http.extraHeader= ${ authHeader } ` , " clone", "--depth" , "2" , cloneUrl , repoLocation ] , {
131149 stdio : "inherit" ,
132150 } ) ;
133151
@@ -137,7 +155,7 @@ export function cloneRepo(options: CloneRepoOptions): string {
137155 }
138156 }
139157
140- // Configure credentials and remote URL (replace embedded-token URL)
158+ // Configure credentials and remote URL
141159 configureGit ( ) ;
142160 console . log ( `[Engine SDK] Clone complete.` ) ;
143161 }
@@ -167,7 +185,7 @@ export function commitAndPush(repoLocation: string, commitMessage: string): Comm
167185 execFileSync ( "git" , [ "commit" , "-m" , commitMessage ] , { cwd : repoLocation } ) ;
168186 }
169187
170- execFileSync ( "git" , [ "push" , "--force" , "-- set-upstream", "origin" , "HEAD" ] , { cwd : repoLocation } ) ;
188+ execFileSync ( "git" , [ "push" , "--set-upstream" , "origin" , "HEAD" ] , { cwd : repoLocation } ) ;
171189
172190 return {
173191 success : true ,
0 commit comments