@@ -234,10 +234,43 @@ export class RemoteClient {
234234 await CloudRunnerSystem . Run ( `git lfs install` ) ;
235235 assert ( fs . existsSync ( `.git` ) , 'git folder exists' ) ;
236236 RemoteClientLogger . log ( `${ CloudRunner . buildParameters . branch } ` ) ;
237- if ( CloudRunner . buildParameters . gitSha !== undefined ) {
238- await CloudRunnerSystem . Run ( `git checkout ${ CloudRunner . buildParameters . gitSha } ` ) ;
237+ // Ensure refs exist (tags and PR refs)
238+ await CloudRunnerSystem . Run ( `git fetch --all --tags || true` ) ;
239+ if ( ( CloudRunner . buildParameters . branch || '' ) . startsWith ( 'pull/' ) ) {
240+ await CloudRunnerSystem . Run ( `git fetch origin +refs/pull/*:refs/remotes/origin/pull/* || true` ) ;
241+ }
242+ const targetSha = CloudRunner . buildParameters . gitSha ;
243+ const targetBranch = CloudRunner . buildParameters . branch ;
244+ if ( targetSha ) {
245+ try {
246+ await CloudRunnerSystem . Run ( `git checkout ${ targetSha } ` ) ;
247+ } catch ( _error ) {
248+ try {
249+ await CloudRunnerSystem . Run ( `git fetch origin ${ targetSha } || true` ) ;
250+ await CloudRunnerSystem . Run ( `git checkout ${ targetSha } ` ) ;
251+ } catch ( _error2 ) {
252+ RemoteClientLogger . logWarning ( `Falling back to branch checkout; SHA not found: ${ targetSha } ` ) ;
253+ try {
254+ await CloudRunnerSystem . Run ( `git checkout ${ targetBranch } ` ) ;
255+ } catch ( _error3 ) {
256+ if ( ( targetBranch || '' ) . startsWith ( 'pull/' ) ) {
257+ await CloudRunnerSystem . Run ( `git checkout origin/${ targetBranch } ` ) ;
258+ } else {
259+ throw _error2 ;
260+ }
261+ }
262+ }
263+ }
239264 } else {
240- await CloudRunnerSystem . Run ( `git checkout ${ CloudRunner . buildParameters . branch } ` ) ;
265+ try {
266+ await CloudRunnerSystem . Run ( `git checkout ${ targetBranch } ` ) ;
267+ } catch ( _error ) {
268+ if ( ( targetBranch || '' ) . startsWith ( 'pull/' ) ) {
269+ await CloudRunnerSystem . Run ( `git checkout origin/${ targetBranch } ` ) ;
270+ } else {
271+ throw _error ;
272+ }
273+ }
241274 RemoteClientLogger . log ( `buildParameter Git Sha is empty` ) ;
242275 }
243276
@@ -336,10 +369,28 @@ export class RemoteClient {
336369 ) {
337370 CloudRunnerLogger . log ( `Retained Workspace Already Exists!` ) ;
338371 process . chdir ( CloudRunnerFolders . ToLinuxFolder ( CloudRunnerFolders . repoPathAbsolute ) ) ;
339- await CloudRunnerSystem . Run ( `git fetch` ) ;
372+ await CloudRunnerSystem . Run ( `git fetch --all --tags || true` ) ;
373+ if ( ( CloudRunner . buildParameters . branch || '' ) . startsWith ( 'pull/' ) ) {
374+ await CloudRunnerSystem . Run ( `git fetch origin +refs/pull/*:refs/remotes/origin/pull/* || true` ) ;
375+ }
340376 await CloudRunnerSystem . Run ( `git lfs pull` ) ;
341- await CloudRunnerSystem . Run ( `git reset --hard "${ CloudRunner . buildParameters . gitSha } "` ) ;
342- await CloudRunnerSystem . Run ( `git checkout ${ CloudRunner . buildParameters . gitSha } ` ) ;
377+ const sha = CloudRunner . buildParameters . gitSha ;
378+ const branch = CloudRunner . buildParameters . branch ;
379+ try {
380+ await CloudRunnerSystem . Run ( `git reset --hard "${ sha } "` ) ;
381+ await CloudRunnerSystem . Run ( `git checkout ${ sha } ` ) ;
382+ } catch ( _error ) {
383+ RemoteClientLogger . logWarning ( `Retained workspace: SHA not found, falling back to branch ${ branch } ` ) ;
384+ try {
385+ await CloudRunnerSystem . Run ( `git checkout ${ branch } ` ) ;
386+ } catch ( _error2 ) {
387+ if ( ( branch || '' ) . startsWith ( 'pull/' ) ) {
388+ await CloudRunnerSystem . Run ( `git checkout origin/${ branch } ` ) ;
389+ } else {
390+ throw _error2 ;
391+ }
392+ }
393+ }
343394
344395 return true ;
345396 }
0 commit comments