-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload-batches.ps1
More file actions
194 lines (161 loc) · 5.77 KB
/
Copy pathupload-batches.ps1
File metadata and controls
194 lines (161 loc) · 5.77 KB
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
param(
[string]$Remote = "origin",
[string]$Branch = "main",
[int]$BatchSizeMB = 250,
[int]$MaxFilesPerBatch = 500,
[int]$LfsThresholdMB = 95,
[int]$DelaySeconds = 2,
[int]$MaxBatches = 0,
[switch]$PlanOnly
)
$ErrorActionPreference = "Stop"
Set-Location $PSScriptRoot
$repoRoot = (Get-Location).Path
function Invoke-Git {
param([string[]]$GitArgs)
& git @GitArgs
if ($LASTEXITCODE -ne 0) {
throw "git $($GitArgs -join ' ') failed with exit code $LASTEXITCODE"
}
}
function Test-ReadableFile {
param([string]$Path)
try {
$stream = [System.IO.File]::Open($Path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite)
$stream.Close()
return $true
}
catch {
return $false
}
}
function Get-UntrackedFiles {
$paths = @(& git -c core.quotepath=false ls-files --others --exclude-standard)
if ($LASTEXITCODE -ne 0) {
throw "Unable to list untracked files."
}
$files = foreach ($relativePath in $paths) {
if ([string]::IsNullOrWhiteSpace($relativePath)) {
continue
}
$fullPath = Join-Path $repoRoot $relativePath
try {
$item = Get-Item -LiteralPath $fullPath -Force -ErrorAction Stop
}
catch {
Write-Warning "Skipping inaccessible file: $relativePath"
continue
}
if (-not $item.PSIsContainer) {
if ($item.Name -like "~$*") {
Write-Warning "Skipping Office lock file: $relativePath"
continue
}
if (-not (Test-ReadableFile -Path $item.FullName)) {
Write-Warning "Skipping unreadable file: $relativePath"
continue
}
[pscustomobject]@{
RelativePath = $relativePath.Replace("\", "/")
Length = [long]$item.Length
}
}
}
return @($files | Sort-Object RelativePath)
}
function Write-NulPathspecFile {
param(
[string]$Path,
[object[]]$Files
)
$content = [string]::Join([char]0, @($Files | ForEach-Object { $_.RelativePath })) + [char]0
[System.IO.File]::WriteAllText($Path, $content, [System.Text.UTF8Encoding]::new($false))
}
function Push-CurrentCommit {
Invoke-Git -GitArgs @("push", $Remote, "HEAD:$Branch")
}
Invoke-Git -GitArgs @("config", "core.longpaths", "true")
Invoke-Git -GitArgs @("lfs", "install", "--local")
if ($PlanOnly) {
$files = Get-UntrackedFiles
$totalBytes = ($files | Measure-Object Length -Sum).Sum
$largeFiles = @($files | Where-Object { $_.Length -gt ($LfsThresholdMB * 1MB) })
Write-Host "Untracked files : $($files.Count)"
Write-Host ("Untracked size : {0:N2} GiB" -f ($totalBytes / 1GB))
Write-Host "LFS candidates : $($largeFiles.Count)"
Write-Host "Batch limits : $BatchSizeMB MiB / $MaxFilesPerBatch files"
exit 0
}
Invoke-Git -GitArgs @("fetch", "--filter=blob:none", "--no-tags", $Remote, $Branch)
& git merge-base --is-ancestor "$Remote/$Branch" HEAD
if ($LASTEXITCODE -ne 0) {
throw "Remote branch advanced. Rebase or merge before continuing."
}
$trackedChanges = @(& git status --porcelain --untracked-files=no)
if ($LASTEXITCODE -ne 0) {
throw "Unable to inspect tracked changes."
}
if ($trackedChanges.Count -gt 0) {
throw "Tracked or staged changes already exist. Commit or restore them before running this uploader."
}
$files = Get-UntrackedFiles
$largeFiles = @($files | Where-Object { $_.Length -gt ($LfsThresholdMB * 1MB) })
foreach ($file in $largeFiles) {
$attribute = & git check-attr filter -- $file.RelativePath
if ($LASTEXITCODE -ne 0) {
throw "Unable to inspect LFS attributes for $($file.RelativePath)"
}
if ($attribute -notmatch "filter: lfs$") {
Invoke-Git -GitArgs @("lfs", "track", "--", $file.RelativePath)
}
}
& git diff --quiet -- .gitattributes
if ($LASTEXITCODE -ne 0) {
Invoke-Git -GitArgs @("add", ".gitattributes")
Invoke-Git -GitArgs @("commit", "-m", "Configure Git LFS for remaining large files")
Push-CurrentCommit
}
$batchNumber = 0
$batchLimitBytes = [long]$BatchSizeMB * 1MB
while ($true) {
$files = Get-UntrackedFiles
if ($files.Count -eq 0) {
Write-Host "All currently untracked files have been uploaded."
break
}
if ($MaxBatches -gt 0 -and $batchNumber -ge $MaxBatches) {
Write-Host "Stopped after $MaxBatches batches as requested."
break
}
$batch = [System.Collections.Generic.List[object]]::new()
[long]$batchBytes = 0
foreach ($file in $files) {
$wouldExceedBytes = $batch.Count -gt 0 -and ($batchBytes + $file.Length) -gt $batchLimitBytes
$wouldExceedCount = $batch.Count -ge $MaxFilesPerBatch
if ($wouldExceedBytes -or $wouldExceedCount) {
break
}
$batch.Add($file)
$batchBytes += $file.Length
}
$batchNumber++
$pathspecFile = Join-Path ([System.IO.Path]::GetTempPath()) "ai-2024-upload-paths-$PID.bin"
try {
Write-NulPathspecFile -Path $pathspecFile -Files $batch
Invoke-Git -GitArgs @("--literal-pathspecs", "add", "--pathspec-from-file=$pathspecFile", "--pathspec-file-nul")
Invoke-Git -GitArgs @(
"commit",
"-m",
("Upload batch {0:D3} ({1} files, {2:N1} MiB)" -f $batchNumber, $batch.Count, ($batchBytes / 1MB))
)
Push-CurrentCommit
}
finally {
Remove-Item -LiteralPath $pathspecFile -Force -ErrorAction SilentlyContinue
}
Write-Host ("Uploaded batch {0}: {1} files, {2:N1} MiB" -f $batchNumber, $batch.Count, ($batchBytes / 1MB))
if ($DelaySeconds -gt 0) {
Start-Sleep -Seconds $DelaySeconds
}
}
Invoke-Git -GitArgs @("status", "--short", "--branch")