Last active
March 29, 2018 20:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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