Skip to content

Instantly share code, notes, and snippets.

@hpbuniat
Last active December 15, 2015 00:39
Show Gist options
  • Save hpbuniat/5174391 to your computer and use it in GitHub Desktop.
Save hpbuniat/5174391 to your computer and use it in GitHub Desktop.
Get all versions of a specific file from svn
<?php
/**
* SVN-Version extractor of a specific file
*
* Get all versions of a specific file from svn
*/
// argument-handling
$aArgs = getopt('f:p:r:');
$iRevision = (true === empty($aArgs['r'])) ? 'HEAD' : $aArgs['r'];
if (empty($aArgs['f']) === true or empty($aArgs['p']) === true) {
print_r(sprintf('usage: %s -f file -p svn-path', $argv[0]));
exit;
}
/**
* Class o
*/
class o {
/**
* Run a command
*
* @param string $sCommand
*
* @return string
*/
public function cmd($sCommand) {
print_r(sprintf('running %s ...', $sCommand) . PHP_EOL);
$rCommand = popen($sCommand, 'r');
$sReturn = '';
while (feof($rCommand) !== true) {
$sReturn .= fread($rCommand, 4096);
}
pclose($rCommand);
return $sReturn;
}
/**
* Run the extraction
*
* @param string $sFile
* @param string $sSvn
* @param mixed $mRevision
*
* @throws RuntimeException
*/
public function run($sFile, $sSvn, $mRevision) {
print_r('reading history ...' . PHP_EOL);
$sHistory = $this->cmd(sprintf('svn log --xml %s/%s@%s', $sSvn, $sFile, $mRevision));
if (empty($sHistory) === true) {
throw new RuntimeException('no result');
}
$aFile = explode(DIRECTORY_SEPARATOR, $sFile);
$sLast = end($aFile);
$aFile = explode('.', $sLast);
$sDir = reset($aFile);
$sDir = sprintf('%s-%s', date('Y-m-d'), $sDir);
if (is_dir($sDir) === false) {
mkdir($sDir, 0777, true);
}
$oXml = simplexml_load_string($sHistory);
$iCount = count($oXml->children());
$iCounter = 0;
$aChildren = array();
foreach ($oXml->children() as $oVersion) {
++$iCounter;
$aChildren[$iCounter] = pcntl_fork();
if ($aChildren[$iCounter] === -1) {
unset($aChildren[$iCounter]);
}
elseif ($aChildren[$iCounter] === 0) {
print_r(sprintf('fetching %d/%d ...', $iCounter, $iCount) . PHP_EOL);
$this->cmd(sprintf('svn export -r %s %s/%s@%s %s/%s-%s', $oVersion->attributes()->revision, $sSvn, $sFile, $oVersion->attributes()->revision, $sDir, date('Y-m-d_Hi', strtotime($oVersion->date)), $sLast));
exit;
}
}
$iChildren = 0;
do {
$iStatus = null;
$iPid = pcntl_waitpid(-1, $iStatus, WNOHANG);
$bUnset = false;
foreach ($aChildren as $sChild => $iChild) {
if ($iChild === $iPid) {
unset($aChildren[$sChild]);
$bUnset = true;
}
}
if ($bUnset === false) {
usleep(10000);
}
$iChildren = count($aChildren);
}
while ($iChildren > 0);
}
}
$o = new o();
$o->run($aArgs['f'], trim($aArgs['p'], ' /'), $iRevision);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment