Skip to content

Instantly share code, notes, and snippets.

@phptek
Last active December 29, 2015 01:29
Show Gist options
  • Save phptek/7593542 to your computer and use it in GitHub Desktop.
Save phptek/7593542 to your computer and use it in GitHub Desktop.
SVN to GIT project converter.
#!/usr/bin/env php -q
<?php
/*
* Modified from an original by Sam Minnee for SilverStripe Ltd
*/
if(empty($_SERVER['argv'][2])) {
echo "Usage: {$_SERVER['argv'][0]} <svn repo> <dest folder>\n";
echo "Runs: git svn clone for the repo and then manually pulls.\n";
exit(1);
}
else {
$repo = $_SERVER['argv'][1];
$CLI_repo = escapeshellarg($repo);
$destFolder = $_SERVER['argv'][2];
$CLI_destFolder = escapeshellarg($destFolder);
}
echo_passthru("git svn clone $CLI_repo $CLI_destFolder");
chdir("$destFolder/trunk");
$externals = trim(`git svn propget svn:externals`);
if($externals) {
$externalsList = explode("\n", $externals);
foreach($externalsList as $external) {
if(preg_match("/^#/", $external)) {
continue;
}
// Sometimes you'll get the revision (-r12345569) in between the source and the location: Lose it:
$external = preg_replace("#(-r[0-9]+\s)#", '', $external);
list($externalPath, $externalSource) = preg_split('#\s+#', $external, 2);
// Clean-up $externalPath e.g. "sitemapmodule/"
$externalPath = rtrim($externalPath, '/');
$externalSource = trim($externalSource);
// Log it
$msg = "Processed svn-external: $externalSource, located at: $externalSource\n";
$dir = sys_get_temp_dir();
file_put_contents("$dir/svn2git.log", $msg);
// Add them
$CLI_externalPath = escapeshellarg($externalPath);
$CLI_externalSource = escapeshellarg($externalSource);
echo_passthru("svn export $CLI_externalSource $CLI_externalPath");
echo_passthru("git add -f $CLI_externalPath");
echo_passthru("git commit -m 'Automatic svn-external commit: $externalPath'");
// Add an assets dir, if it doesn't exist (dev/build will barf otherwise)
if(!file_exists('./assets')) {
mkdir('./assets', '0777');
}
}
}
function echo_passthru($cmd) {
echo "\x1b[32;1mEXECUTING:\x1b[37;1m $cmd\x1b[0m\n";
$returnVar = 0;
passthru($cmd, $returnVar);
if($returnVar != 0) {
echo "\x1b[31;1mERROR: command returned error level $returnVar.\x1b[0m\n\n";
exit($returnVar);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment