Skip to content

Instantly share code, notes, and snippets.

@Programie
Created January 30, 2015 14:54
Show Gist options
  • Select an option

  • Save Programie/044e506f223f368f5c5d to your computer and use it in GitHub Desktop.

Select an option

Save Programie/044e506f223f368f5c5d to your computer and use it in GitHub Desktop.
Easily switch between trunk, tags and branches in SVN
<?php
function getSvnInfo($path = "")
{
$info = array();
foreach (explode("\n", shell_exec("svn info " . $path)) as $line)
{
$line = trim($line);
if (!$line)
{
continue;
}
list($key, $value) = explode(":", $line, 2);
$value = trim($value);
$info[$key] = $value;
}
return $info;
}
function parseSvnUrl($url)
{
$data = new StdClass;
$url = explode("/", $url);
$url = array_reverse($url);
if ($url[0] == "trunk" and $url[1] != "branches" and $url[1] != "tags")
{
$baseUrl = array_reverse($url);
array_pop($baseUrl);
$data->type = "trunk";
$data->baseUrl = implode("/", $baseUrl);
return $data;
}
if ($url[1] == "branches")
{
$baseUrl = array_reverse($url);
array_pop($baseUrl);
array_pop($baseUrl);
$data->type = "branch";
$data->folder = $url[0];
$data->baseUrl = implode("/", $baseUrl);
return $data;
}
if ($url[1] == "tags")
{
$baseUrl = array_reverse($url);
array_pop($baseUrl);
array_pop($baseUrl);
$data->type = "tag";
$data->folder = $url[0];
$data->baseUrl = implode("/", $baseUrl);
return $data;
}
}
$svnInfo = getSvnInfo();
$workingCopyRootPath = $svnInfo["Working Copy Root Path"];
$svnInfo = getSvnInfo($workingCopyRootPath);
$url = $svnInfo["URL"];
$parsedUrl = parseSvnUrl($url);
if (!$parsedUrl)
{
echo "Unable to parse SVN URL!";
exit;
}
if (count($argv) < 2)
{
echo "Now target specified!";
exit;
}
$newTarget = $argv[1];
$newUrl = $parsedUrl->baseUrl . "/" . $newTarget;
passthru("svn switch " . $newUrl . " " . $workingCopyRootPath);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment