Skip to content

Instantly share code, notes, and snippets.

@etng
Created September 17, 2012 02:00
Show Gist options
  • Save etng/3735165 to your computer and use it in GitHub Desktop.
Save etng/3735165 to your computer and use it in GitHub Desktop.
use this script to export changeset from the last time you pack it, also you can ignore some files by keyword, and filter by author name
<?php
if(!file_exists($changeset_config_file = 'changeset.conf.json'))
{
$prefix = '/path/to/workcopy/of/svn/';
$stop_keywords = array('.gitignore', '.bak', '.swp');
$authors = array();
$base_path = dirname(__file__);
$repo_username = 'svn_username';
$repo_password = 'svn_pass';
$repo_url = 'http://domain/path/svn/root/';
$last_date = date('Y-m-d', strtotime('-1 year'));
file_put_contents($changeset_config_file, pretty_json(json_encode(compact('prefix', 'stop_keywords', 'authors', 'base_path', 'repo_username', 'repo_password', 'repo_url', 'last_date'))));
echo "please edit {$changeset_config_file} to config this script" . PHP_EOL;
die();
}
else
{
$config=json_decode(file_get_contents($changeset_config_file), true);
extract($config);
}
$cache_file_name = '_svnlog.xml';
$command = "svn log -v --username {$repo_username} --password {$repo_password} -r {{$last_date}}:HEAD --xml {$repo_url}> {$cache_file_name}";
exec($command);
if(!file_exists($cache_file_name))
{
echo "`{$command}` dit not run right" . PHP_EOL;
die();
}
libxml_use_internal_errors(true);
$xml = simplexml_load_file($cache_file_name);
@unlink($cache_file_name);
if (!$xml) {
echo "try to run `{$command}` and find why the following errors:" . PHP_EOL;
$errors = libxml_get_errors();
foreach ($errors as $error) {
echo "Error $error->code: " .
trim($error->message) .
"\n Line: {$error->line}" .
"\n Column: {$error->column}";
}
libxml_clear_errors();
die();
}
$last_date = date("Y-m-d");
file_put_contents($changeset_config_file, pretty_json(json_encode(compact('prefix', 'stop_keywords', 'authors', 'base_path', 'repo_username', 'repo_password', 'repo_url', 'last_date'))));
$quoted_stop_keywords = array();
foreach($stop_keywords as $stop_keyword)
{
$quoted_stop_keywords []= preg_quote($stop_keyword);
}
$stop_pattern = '!('.implode('|', $quoted_stop_keywords).')!sim';
$log_entries = $xml->xpath("/log/logentry");
$filenames = array();
foreach($log_entries as $log_entry)
{
$author = (string)$log_entry->author;
if(!$authors || in_array($author, $authors))
{
$changes = array();
foreach($log_entry->paths->path as $path)
{
$action = (string)$path['action'];
$filename = substr((string)$path, strlen($prefix));
if(!preg_match($stop_pattern, $filename))
{
if($action=='D')
{
while(($idx=array_search($filename, $filenames))!==false)
{
unset($filenames[$idx]);
}
}
else
{
$filenames []= $filename;
}
}
else
{
echo "ignore {$filename}" . PHP_EOL;
}
}
}
}
$filenames = array_unique($filenames);
sort($filenames);
file_put_contents('changeset.txt', implode(PHP_EOL, $filenames));
$zip = new ZipArchive;
$zip_filename = $base_path . '/changeset.zip';
$res = $zip->open($zip_filename, ZipArchive::CREATE);
if ($res === TRUE)
{
foreach($filenames as $filename)
{
$full_path = $base_path.'/'.$filename;
if(!is_dir($full_path) && is_file($filename))
{
$zip->addFile($full_path = $base_path.'/'.$filename, $filename);
}
}
$zip->close();
echo realpath($zip_filename) . PHP_EOL;
echo "ok" . PHP_EOL;
}
else
{
$zip_err_dict = array(
ZIPARCHIVE::ER_EXISTS =>"File already exists. ",
ZIPARCHIVE::ER_INCONS =>"Zip archive inconsistent. ",
ZIPARCHIVE::ER_INVAL =>"Invalid argument. ",
ZIPARCHIVE::ER_MEMORY =>"Malloc failure. ",
ZIPARCHIVE::ER_NOENT =>"No such file. ",
ZIPARCHIVE::ER_NOZIP =>"Not a zip archive. ",
ZIPARCHIVE::ER_OPEN =>"Can't open file. ",
ZIPARCHIVE::ER_READ =>"Read error. ",
ZIPARCHIVE::ER_SEEK =>"Seek error. ",
);
trigger_error("can not open zip file:" . $zip_err_dict[$res]);
}
/**
* Indents a flat JSON string to make it more human-readable.
*
* @param string $json The original JSON string to process.
*
* @return string Indented version of the original JSON string.
*/
function pretty_json($json) {
$result = '';
$pos = 0;
$strLen = strlen($json);
$indentStr = ' ';
$newLine = "\n";
$prevChar = '';
$outOfQuotes = true;
for ($i=0; $i<=$strLen; $i++) {
// Grab the next character in the string.
$char = substr($json, $i, 1);
// Are we inside a quoted string?
if ($char == '"' && $prevChar != '\\') {
$outOfQuotes = !$outOfQuotes;
// If this character is the end of an element,
// output a new line and indent the next line.
} else if(($char == '}' || $char == ']') && $outOfQuotes) {
$result .= $newLine;
$pos --;
for ($j=0; $j<$pos; $j++) {
$result .= $indentStr;
}
}
// Add the character to the result string.
$result .= $char;
// If the last character was the beginning of an element,
// output a new line and indent the next line.
if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
$result .= $newLine;
if ($char == '{' || $char == '[') {
$pos ++;
}
for ($j = 0; $j < $pos; $j++) {
$result .= $indentStr;
}
}
$prevChar = $char;
}
return $result;
}
@etng
Copy link
Author

etng commented Feb 28, 2013

do not use this one any more.
use this: https://gist.github.com/etng/5054579

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment