Skip to content

Instantly share code, notes, and snippets.

@nzbart
Last active March 29, 2018 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nzbart/82a368ae5e9c621bf24f98a92cccd226 to your computer and use it in GitHub Desktop.
Save nzbart/82a368ae5e9c621bf24f98a92cccd226 to your computer and use it in GitHub Desktop.
Quick and dirty script to commit a large number / size of files in smaller batches. Most Git cloud providers don't like to receive enormous pushes and will drop the connection.
git push
$deletedFiles = @(git ls-files --deleted)
if($deletedFiles.length -ne 0) {
$deletedFiles | % { git rm $_ }
git commit -m "Deleted some files"
}
$f = git status --porcelain=v1 -u
if(!$?) { throw "Failed." }
$f = $f | % { $_ -replace '^...', '' -replace '^"', '' -replace '"$', '' }
$f = $f | % { ls -Force -LiteralPath $_ } | sort FullName
function BuildCommands($batch) {
@($batch) | % {
git add $_
}
git commit -m 'Adding new batch'
git push
if(!$?) {
throw "Failed to push batch."
}
}
$maxSize = 500MB
$totalSize = 0
$thisBatch = New-Object System.Collections.ArrayList
$commands = for($i = 0; $i -ne $f.length; ++$i) {
$current = $f[$i]
Write-Host $current.FullName
$currentSize = $current.Length
$sizeIncludingThis = $totalSize + $currentSize
if($sizeIncludingThis -le $maxSize) {
$thisBatch.Add($current) | Out-Null
$totalSize = $sizeIncludingThis
if($i -eq $f.length - 1) {
# this is the final batch of stragglers
BuildCommands $thisBatch
}
} else {
if($thisBatch.Count -ne 0) {
BuildCommands $thisBatch
}
$thisBatch = New-Object System.Collections.ArrayList
$thisBatch.Add($current) | Out-Null
$totalSize = $currentSize
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment