Skip to content

Instantly share code, notes, and snippets.

@kiang
Created December 24, 2014 01:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kiang/9224e139320c230fedc5 to your computer and use it in GitHub Desktop.
Save kiang/9224e139320c230fedc5 to your computer and use it in GitHub Desktop.
batch push files into git repo with size limitation in each commit
<?php
$limit = 838860800;
exec('git status ' . __DIR__, $lines);
$lineCount = $currentFileSize = $pushCount = 0;
$sw = false;
foreach ($lines AS $line) {
if ($sw) {
++$lineCount;
if ($lineCount > 2) {
if (!empty($line)) {
$line = trim($line);
$path = __DIR__ . '/' . $line;
if (is_file($path)) {
addFile($path);
} else {
foreach (glob($path . '/*') AS $file) {
addFile($file);
}
}
} else {
$sw = false;
}
}
} elseif ($line === 'Untracked files:') {
$sw = true;
}
}
function addFile($file) {
global $limit, $currentFileSize, $pushCount;
$currentFileSize += filesize($file);
if ($currentFileSize < $limit) {
exec("git add {$file}");
} else {
exec("git add {$file}");
++$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