if (Test-Path$RepoDir) { if (Test-Path (Join-Path$RepoDir".git")) { Write-Host"Found existing git repo at '$RepoDir' — reusing it (no reclone)." Set-Location$RepoDir Write-Host"Fetching remote metadata..." git -c core.quotepath=false -c i18n.logOutputEncoding=utf-8 fetch --all if ($LASTEXITCODE-ne0) { Write-Host"Warning: git fetch --all returned non-zero exit code." } } else { Write-Host"Directory '$RepoDir' exists but is not a git repo. Aborting to avoid overwrite." exit1 } } else { Write-Host"Cloning (partial) into '$RepoDir' ..." git -c core.quotepath=false -c i18n.logOutputEncoding=utf-8 clone --filter=blob:none --no-checkout$RepoUrl$RepoDir if ($LASTEXITCODE-ne0) { Write-Host"Clone failed. You can try a full clone (without --filter=blob:none) as fallback." exit1 } Set-Location$RepoDir Write-Host"Fetching metadata..." git -c core.quotepath=false -c i18n.logOutputEncoding=utf-8 fetch --all }
# ---------- Get file list from HEAD ---------- # Use -z to safely handle filenames including newlines and unicode $FileListRaw = & git -c core.quotepath=false -c i18n.logOutputEncoding=utf-8ls-tree-r--name-only-z HEAD 2>$null if (-not$FileListRaw) { Write-Host"No files found in HEAD (maybe repository empty or no HEAD). Exiting." exit0 } $FileList = $FileListRaw-split"`0" | Where-Object { $_-ne"" }
Write-Host"Total files to checkout: $($FileList.Count)" Write-Host"Starting checkout in batches of $BatchSize ..."
$BatchToCheckout = @() foreach ($relin$Batch) { # Ensure we test the correct file path relative to repo root $fullPath = Join-Path (Get-Location) $rel
if (-not (Test-Path$fullPath)) { $BatchToCheckout += $rel } else { # If file exists but size == 0, re-checkout (could be placeholder) try { $length = (Get-Item$fullPath-ErrorAction Stop).Length if ($length-eq0) { Write-Host" Re-checkout (empty file): $rel" $BatchToCheckout += $rel } else { Write-Host" Skipping existing file: $rel" } } catch { Write-Host" Skipping (unable to stat): $rel" } } }
if ($BatchToCheckout.Count -eq0) { Write-Host" All files in this batch already present — skipping." continue }
# Try checkout; if fails, try fetching shallow blobs and retry once & git -c core.quotepath=false -c i18n.logOutputEncoding=utf-8 checkout HEAD --$BatchToCheckout if ($LASTEXITCODE-ne0) { Write-Host" Checkout failed for some files in this batch. Attempting conservative fetch and retry..." & git fetch --depth=1 origin & git -c core.quotepath=false -c i18n.logOutputEncoding=utf-8 checkout HEAD --$BatchToCheckout if ($LASTEXITCODE-ne0) { Write-Host" Still failed on this batch. Possible causes:" Write-Host" - missing blobs on server / partial-clone negotiation issue" Write-Host" - repository uses Git LFS (you may see pointer files)" Write-Host" These files will remain unchecked out; continuing to next batch." } else { Write-Host" Retry succeeded for this batch." } } else { Write-Host" Checkout succeeded for this batch." } }