Skip to content

Instantly share code, notes, and snippets.

@mlask
Created June 27, 2016 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlask/a4a4de59c1952dcd29db57e0128f005b to your computer and use it in GitHub Desktop.
Save mlask/a4a4de59c1952dcd29db57e0128f005b to your computer and use it in GitHub Desktop.
Export changed (and, optionally, uncommited) files from SVN repository
#!/usr/bin/php
<?php
/**
* Options:
* -r rev_from:rev_to -- revision range to export
* -u username -- svn username (optional)
* -p password -- svn password (optional)
* -n -- export with uncommited files (optional)
*/
$opts = getopt('r:u:p:n');
$odir = sprintf('svnexport-%s', date('Ymd'));
if (isset($opts['r']))
{
$outfile = [];
// files changed in specified revision range
$svndiff = shell_exec('svn diff -r ' . escapeshellarg($opts['r']) . (isset($opts['u']) ? ' --username ' . escapeshellarg($opts['u']) : '') . (isset($opts['p']) ? ' --password ' . escapeshellarg($opts['p']) : '') . ' --summarize');
foreach (explode("\n", trim($svndiff)) as $item)
if (preg_match('/^[AM]\s+(.+)$/', trim($item), $match))
$outfile[md5($match[1])] = $match[1];
// uncommited files
if (isset($opts['n']))
{
$svnstat = shell_exec('svn status --ignore-externals' . (isset($opts['u']) ? ' --username ' . escapeshellarg($opts['u']) : '') . (isset($opts['p']) ? ' --password ' . escapeshellarg($opts['p']) : ''));
foreach (explode("\n", trim($svnstat)) as $item)
if (preg_match('/^[AM\?]\s+(.+)$/', trim($item), $match))
$outfile[md5($match[1])] = $match[1];
}
// remove output dir from filelist
if (isset($outfile[md5($odir)]))
unset($outfile[md5($odir)]);
$outfile = array_values($outfile);
if (count($outfile) > 0)
{
printf("Exporting %d file(s) to \"%s\"...\n", count($outfile), $odir);
if (!file_exists($odir))
if (!mkdir($odir, 0777, true))
die(sprintf("\nFailed to create output directory: %s\n\n", $odir));
foreach ($outfile as $path)
{
printf(" -- %s\n", $path);
if (is_file($path))
{
if (!file_exists($odir . '/' . dirname($path)))
if (!mkdir($odir . '/' . dirname($path), 0777, true))
die(sprintf("Failed to create export directory: %s\n\n", $odir . '/' . dirname($path)));
if (!copy($path, $odir . '/' . $path))
die(sprintf("Failed to export file: %s\n\n", $path));
}
elseif (is_dir($path))
{
if (!file_exists($odir . '/' . $path))
if (!mkdir($odir . '/' . $path, 0777, true))
die(sprintf("Failed to create export directory: %s\n\n", $odir . '/' . $path));
}
}
echo("Done!\n\n");
}
else
die("Nothing to export!\n\n");
}
else
die("Usage:\n\tsvn-changes <options>\nOptions:\n\t-r rev_from:rev_to\n\t[-u username]\n\t[-p password]\n\t[-n]\n\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment