Spaces:
Sleeping
Sleeping
File size: 5,554 Bytes
db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce db26343 578f8ce |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
param(
[string]$Message = "",
[switch]$PullOnly,
[switch]$Verbose
)
if ($Message -eq "" -and -not $PullOnly) {
$Message = "Update " + (Get-Date -Format "yyyy-MM-dd HH:mm:ss")
}
# Check HF_TOKEN
if (-not $env:HF_TOKEN -or $env:HF_TOKEN.Trim().Length -lt 10) {
Write-Host "Error: HF_TOKEN not set or invalid"
Write-Host "Set with: [Environment]::SetEnvironmentVariable('HF_TOKEN','YOUR_TOKEN','User')"
exit 1
}
$repoRoot = git rev-parse --show-toplevel 2>$null
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: Not a git repository"
exit 1
}
Set-Location $repoRoot
# Get HF info from remote
$hfRemote = git remote get-url huggingface 2>$null
if ($LASTEXITCODE -ne 0) {
Write-Host "Error: Hugging Face remote not configured"
exit 1
}
if ($hfRemote -match 'huggingface\.co/spaces/([^/]+)/(.+?)(?:\.git)?$') {
$hfUser = $Matches[1]
$hfSpace = $Matches[2]
} else {
Write-Host "Error: Cannot parse HF remote"
exit 1
}
Write-Host "`n=== 3-Way Sync: Local <-> GitHub <-> HF ===`n"
function Get-RemoteCommit($remote) {
$commit = git rev-parse "$remote/main" 2>$null
if ($LASTEXITCODE -eq 0) { return $commit }
return $null
}
function Get-RemoteDate($remote) {
$date = git log $remote/main -1 --format=%ci 2>$null
if ($LASTEXITCODE -eq 0) { return [datetime]$date }
return [datetime]::MinValue
}
# Stage any local changes
git add -A
# Fetch from both remotes
Write-Host "Fetching from remotes..."
git fetch github 2>$null
$ghExists = $LASTEXITCODE -eq 0
git fetch huggingface 2>$null
$hfExists = $LASTEXITCODE -eq 0
if (-not $ghExists -and -not $hfExists) {
Write-Host "No remotes found (first push)"
$isFirstPush = $true
} else {
$isFirstPush = $false
}
# Check for local uncommitted changes
git diff HEAD --quiet
$hasLocalChanges = $LASTEXITCODE -ne 0
# TRUE 3-WAY SYNC: Merge both remotes
if (-not $isFirstPush) {
$localCommit = git rev-parse HEAD 2>$null
$ghCommit = Get-RemoteCommit "github"
$hfCommit = Get-RemoteCommit "huggingface"
$ghDate = Get-RemoteDate "github"
$hfDate = Get-RemoteDate "huggingface"
Write-Host "Status:"
if ($ghExists) { Write-Host " GitHub: $($ghDate.ToString('yyyy-MM-dd HH:mm'))" }
if ($hfExists) { Write-Host " HuggingFace: $($hfDate.ToString('yyyy-MM-dd HH:mm'))" }
Write-Host ""
# Check if we need to merge GitHub
if ($ghExists -and $ghCommit -and $localCommit -ne $ghCommit) {
Write-Host "Merging from GitHub..."
if ($hasLocalChanges) {
git commit -m "Local changes before merge" 2>$null | Out-Null
}
git merge github/main --no-edit 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "[!] Merge conflict with GitHub. Keeping local version..."
git merge --abort 2>$null
} else {
Write-Host "[OK] Merged from GitHub"
}
}
# Check if we need to merge HuggingFace
$localCommit = git rev-parse HEAD 2>$null
if ($hfExists -and $hfCommit -and $localCommit -ne $hfCommit) {
Write-Host "Merging from HuggingFace..."
git diff HEAD --quiet
if ($LASTEXITCODE -ne 0) {
git commit -m "Local changes before HF merge" 2>$null | Out-Null
}
git merge huggingface/main --no-edit 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "[!] Merge conflict with HuggingFace. Keeping local version..."
git merge --abort 2>$null
} else {
Write-Host "[OK] Merged from HuggingFace"
}
}
# Check if everything is in sync
$localCommit = git rev-parse HEAD 2>$null
if ($ghCommit -and $hfCommit -and $localCommit -eq $ghCommit -and $localCommit -eq $hfCommit) {
Write-Host "[OK] All locations in sync"
}
}
if ($PullOnly) {
Write-Host "`n[OK] Pull complete`n"
exit 0
}
# Commit any remaining changes
git diff HEAD --quiet
if ($LASTEXITCODE -ne 0) {
git commit -m $Message
Write-Host "[OK] Committed: $Message"
} else {
# Check if we have commits to push
$localCommit = git rev-parse HEAD 2>$null
$needsPush = $false
if ($ghExists) {
$ghCommit = Get-RemoteCommit "github"
if ($localCommit -ne $ghCommit) { $needsPush = $true }
} else {
$needsPush = $true
}
if ($hfExists) {
$hfCommit = Get-RemoteCommit "huggingface"
if ($localCommit -ne $hfCommit) { $needsPush = $true }
} else {
$needsPush = $true
}
if (-not $needsPush) {
Write-Host "`n[OK] Already up-to-date, nothing to push`n"
exit 0
}
}
# Push to both remotes
Write-Host "`nPushing to remotes..."
# Push to GitHub
git push github main 2>&1 | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host "[OK] GitHub"
} else {
git push github main --force 2>&1 | Out-Null
if ($LASTEXITCODE -eq 0) {
Write-Host "[OK] GitHub (forced)"
} else {
Write-Host "[ERROR] GitHub failed"
}
}
# Push to HuggingFace
$hfUrl = "https://${hfUser}:$($env:HF_TOKEN)@huggingface.co/spaces/${hfUser}/${hfSpace}"
if ($Verbose) {
Write-Host "Debug: Pushing to spaces/${hfUser}/${hfSpace}"
git push $hfUrl main --force 2>&1
} else {
git push $hfUrl main --force 2>&1 | Out-Null
}
if ($LASTEXITCODE -eq 0) {
Write-Host "[OK] Hugging Face: spaces/${hfUser}/${hfSpace}"
} else {
Write-Host "[ERROR] Hugging Face FAILED!"
Write-Host "`nTroubleshooting:"
Write-Host "1. Does the space exist? https://huggingface.co/spaces/${hfUser}/${hfSpace}"
Write-Host "2. Is your token valid? (HF Settings > Access Tokens)"
Write-Host "3. Does your token have write permission?"
Write-Host "4. Run with -Verbose flag for details"
exit 1
}
Write-Host "`n[OK] Sync complete`n" |