1import { spawnSync } from 'node:child_process' 9const lsRemoteWithRetry = async (gitsshRemoteUrl, maxRetries = 3) => { 10 for (let i = 0; i < maxRetries; i++) { 11 const result = spawnSync('git', ['ls-remote', gitsshRemoteUrl], {stdio: 'pipe', timeout: 10000}) 12 if (result.status === 0) return {ok: true, hasContent: !!result.stdout?.toString().trim()} 13 if (i < maxRetries - 1) { 14 console.log(`gitssh not reachable yet, retrying in 3s...`) 18 return {ok: false, hasContent: false} 21const pushWithRetry = async (gitsshRemoteUrl, maxRetries = 3) => { 22 for (let i = 0; i < maxRetries; i++) { 23 const result = spawnSync('git', ['push', gitsshRemoteUrl, 'HEAD:main'], {stdio: 'inherit', timeout: 120000}) 24 if (result.status === 0) return true 25 if (i < maxRetries - 1) { 26 console.log(`Push failed, retrying in 3s...`) 33const ensureGitsshHasContent = async ({action, gitsshRemoteUrl}) => { 34 if (action !== 'apply') return true 36 const {ok, hasContent} = await lsRemoteWithRetry(gitsshRemoteUrl) 37 if (ok && hasContent) return true 39 if (!ok) console.log('\n gitssh not reachable, will push to init') 40 else console.log('\n gitssh repo is empty, pushing current branch...') 41 const pushOk = await pushWithRetry(gitsshRemoteUrl) 43 console.error(`Push failed. Run setup action for gitssh first to init the repo.`) 46 // Verify push propagated (brief delay can occur between push completing and refs being available) 47 const {hasContent: verified} = await lsRemoteWithRetry(gitsshRemoteUrl) 49 console.error(`Push succeeded but refs not visible yet - unexpected state`) 52 console.log('Push successful!') 56export const syncCodeserv = async () => { 62 await ensureGitsshHasContent({action, gitsshRemoteUrl})