Skip to content

Instantly share code, notes, and snippets.

@Lukas238
Forked from kiang/gist:9224e139320c230fedc5
Last active January 21, 2022 19:51
Show Gist options
  • Save Lukas238/302df76e97fa8c877809a250d403428c to your computer and use it in GitHub Desktop.
Save Lukas238/302df76e97fa8c877809a250d403428c to your computer and use it in GitHub Desktop.
Batch push files into git repo with size limitation in each commit
<?php
/**
* This script will list all modified/untracked/deleted git files, with their relative path,
* and add them to a commit group until the group pass 25mb in size, and then will push it.
*
* To run this script on the repository root folder by using this command `php gitbackup.php`.
*
* Requirements: PHP need to be able to run `exec`.
*/
// Variables
$limit = 26214400;// Aprox. 25mb
$lineCount = $currentFileSize = $pushCount = $added = 0;
// Get list of files to commit (modified, deleted, and untracked
exec('git status -s', $lines);
// Loop results
foreach ($lines AS $line) {
++$lineCount;
if (!empty($line)) {
$path = trim(substr($line, 3)); // Remove git status leading letter
addFile($path);
}
}
// Push any remaining files
if($added){
commit_push();
}
function addFile($file) {
global $limit, $currentFileSize, $pushCount, $added;
$added = true;
$currentFileSize += filesize($file);
exec("git add {$file}");
if ($currentFileSize >= $limit) {
commit_push();
}
}
function commit_push(){
global $currentFileSize, $pushCount, $added;
$added = false;
++$pushCount;
exec("git commit -m 'batch push files part {$pushCount}'");
exec("git push");
$currentFileSize = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment