Skip to content

Instantly share code, notes, and snippets.

@flashwave
Last active September 10, 2023 02:10
Show Gist options
  • Save flashwave/998fb7456cfc62b5b53c01a34a551ad5 to your computer and use it in GitHub Desktop.
Save flashwave/998fb7456cfc62b5b53c01a34a551ad5 to your computer and use it in GitHub Desktop.
<?php
define('SCRATCH_DIR', __DIR__ . DIRECTORY_SEPARATOR . '_scratch2');
define('SRC_MARIE', __DIR__ . DIRECTORY_SEPARATOR . 'marie' . DIRECTORY_SEPARATOR . '.git');
define('SRC_NERU', __DIR__ . DIRECTORY_SEPARATOR . 'srv-neru' . DIRECTORY_SEPARATOR . '.git');
define('TGT_MARIE', 'C:\Users\flash\Desktop\target\db-marie\\.git');
define('TGT_NERU', 'C:\Users\flash\Desktop\target\db-neru\\.git');
$sourceGit = SRC_NERU;
$targetGit = TGT_NERU;
function run_git(string $workDir, string $cmd): string {
return shell_exec(sprintf('git --git-dir="%s" -C "%s" %s', $workDir, SCRATCH_DIR, $cmd)) ?? '';
}
$history = (function() use ($sourceGit) {
$lines = array_reverse(explode("\n", trim(run_git($sourceGit, 'log --format="%H %s"'))));
$history = [];
foreach($lines as $line) {
$line = trim($line);
if($line === '')
continue;
$line = explode(' ', $line, 2);
if(count($line) !== 2)
continue;
$history[] = $item = new stdClass;
$item->hash = $line[0];
$item->comment = $line[1];
}
return $history;
})();
//$lastCommit = '';
foreach($history as $commit) {
printf('=> Switching to %s %s%s', substr($commit->hash, 0, 8), $commit->comment, PHP_EOL);
run_git($sourceGit, sprintf('checkout %s', $commit->hash));
run_git($sourceGit, 'clean -fd');
run_git($sourceGit, 'reset HEAD --hard');
if(is_dir(SCRATCH_DIR . DIRECTORY_SEPARATOR . 'misuzu'))
shell_exec(sprintf('rd /s /q "%s\\misuzu"', SCRATCH_DIR));
run_git($targetGit, 'add .');
run_git($targetGit, sprintf('commit -m "%s"', $commit->comment));
//if($lastCommit === '')
// run_git($targetGit, 'push -u origin master');
//else
// run_git($targetGit, 'push');
//$lastCommit = $commit->hash;
}
<?php
define('ZIP_DIR', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Raw Backups');
define('SRC_MARIE', __DIR__ . DIRECTORY_SEPARATOR . 'db-marie' . DIRECTORY_SEPARATOR . '.git');
define('SRC_NERU', __DIR__ . DIRECTORY_SEPARATOR . 'db-neru' . DIRECTORY_SEPARATOR . '.git');
define('SRC_MIKU', __DIR__ . DIRECTORY_SEPARATOR . 'db-miku' . DIRECTORY_SEPARATOR . '.git');
define('SRC_TETO', __DIR__ . DIRECTORY_SEPARATOR . 'db-teto' . DIRECTORY_SEPARATOR . '.git');
$sourceGit = SRC_NERU;
function run_git(string $gitDir, string $workDir, string $cmd): string {
return shell_exec(sprintf('git --git-dir="%s" -C "%s" %s', $gitDir, $workDir, $cmd)) ?? '';
}
$history = (function() use ($sourceGit) {
$lines = array_reverse(explode("\n", trim(run_git($sourceGit, '/tmp', 'log --format="%H %s"'))));
$history = [];
foreach($lines as $line) {
$line = trim($line);
if($line === '')
continue;
$line = explode(' ', $line, 2);
if(count($line) !== 2)
continue;
$history[] = $item = new stdClass;
$item->hash = $line[0];
$item->comment = $line[1];
}
return $history;
})();
foreach($history as $commit) {
if($commit->comment === 'Initial commit')
continue;
$targetDirName = strtolower($commit->comment);
$targetDir = ZIP_DIR . DIRECTORY_SEPARATOR . $targetDirName;
if(!is_dir($targetDir))
mkdir($targetDir);
$targetDir = realpath($targetDir);
var_dump($targetDir);
printf('=> Switching to %s %s to %s%s', substr($commit->hash, 0, 8), $commit->comment, $targetDir, PHP_EOL);
run_git($sourceGit, $targetDir, sprintf('checkout %s', $commit->hash));
run_git($sourceGit, $targetDir, 'clean -fd');
run_git($sourceGit, $targetDir, 'reset HEAD --hard');
if(is_dir($targetDir . DIRECTORY_SEPARATOR . 'misuzu'))
shell_exec(sprintf('rm -rf "%s/misuzu"', $targetDir));
if(is_file($targetDir . DIRECTORY_SEPARATOR . '.gitattributes'))
shell_exec(sprintf('rm -rf "%s/.gitattributes"', $targetDir));
if(is_file($targetDir . DIRECTORY_SEPARATOR . '.gitignore'))
shell_exec(sprintf('rm -rf "%s/.gitignore"', $targetDir));
shell_exec(sprintf('tar -cjvf "%s.tar.bz2" -C "%s" "%s"', $targetDir, ZIP_DIR, $targetDirName));
shell_exec(sprintf('rm -rf "%s"', $targetDir));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment