Skip to content

Instantly share code, notes, and snippets.

@etng
Created September 20, 2012 10:13
Show Gist options
  • Save etng/3755062 to your computer and use it in GitHub Desktop.
Save etng/3755062 to your computer and use it in GitHub Desktop.
put this file under your workcopy directory, run it and it will auto commit your svn workcopy to the svn repository, so there is less repeat your work!
#!/usr/bin/php
<?php
$options = getopt(implode("", array_keys($options_config = array(
"u::"=>"commit for username",
"h"=>"print help message and die",
"d"=>"just debug, do not write",
))));
$options['d'] = isset($options['d']);
$options['h'] = isset($options['h']);
if($options['h'])
{
print_r($options_config);
die();
}
if($options['d'])
{
echo "debug mode on " . PHP_EOL;
}
if(!isset($options['u']))
{
$options['u'] = "anonymous";
}
echo "working as {$options['u']}". PHP_EOL;
$base_dir = dirname(__file__);
$email_subject = sprintf("[%s]SVN Auto Commiter Report", date("Y-m-d"));
$ignore_file_patterns = array("^var\/","\.(bak|swp|tmp|log)$","^autocommit\.php$");
$ignore_file_regex = sprintf("/%s/sim", implode("|", $ignore_file_patterns));
$command_log_file = $base_dir . "/var/logs/command.log";
$recipients = array(
);
$commit_notice = "auto commit as {$options['u']}";
$cache_file_name = $base_dir . '/var/logs/status.xml';
$update_log_file_name = $base_dir . "/var/logs/up.log";
try
{
ob_start();
$command = "svn up --accept postpone --ignore-externals > {$update_log_file_name} ";
exec($command);
if(!file_exists($update_log_file_name))
{
throw new exception("`{$command}` dit not run right");
}
readfile($update_log_file_name);
$command = "svn status -v --xml > {$cache_file_name}";
exec($command);
if(!file_exists($cache_file_name))
{
throw new exception("`{$command}` dit not run right");
}
libxml_use_internal_errors(true);
$xml = simplexml_load_file($cache_file_name);
@unlink($cache_file_name);
if (!$xml)
{
ob_start();
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: ",PHP_EOL, trim($error->message) ,PHP_EOL, "Line: {$error->line}",PHP_EOL, "Column: {$error->column}",PHP_EOL;
}
libxml_clear_errors();
$xml_error = ob_get_clean();
throw new exception("can not get svn status data" . $xml_error );
}
$new_files = array();
$modified_files = array();
foreach($xml->xpath("//status/target/entry") as $entry)
{
$path = $entry['path'];
if(preg_match($ignore_file_regex, $path))
{
continue;
}
$wc_status = current($entry->xpath("wc-status"));
$status = $wc_status['item'];
switch($status)
{
case "unversioned":
$new_files []= $path;
break;
case "modified":
$modified_files []= $path;
break;
case "external":
break;
case "normal":
break;
default:
printf("unknown status: %s %s" . PHP_EOL, $status, $path);
break;
}
}
$commands = array();
if($new_files)
{
echo "new files: " , PHP_EOL , implode(PHP_EOL, $new_files).PHP_EOL;
$commands []= sprintf('svn add %s', implode(' ', $new_files));
}
if($modified_files)
{
echo "modified files: " , PHP_EOL , implode(PHP_EOL, $modified_files).PHP_EOL;
}
$commit_files = array_merge($new_files, $modified_files);
if($commit_files)
{
$commands []= sprintf('svn commit -m "%s" %s', $commit_notice, implode(' ', $commit_files));
}
if($commands)
{
echo "execute commands now " , PHP_EOL;
foreach($commands as $command)
{
printf("executing : %s" . PHP_EOL, $command);
if(!$options['d'])
{
exec($command . " > {$command_log_file}");
}
@readfile($command_log_files);
@unlink($command_log_file);
}
}
else
{
throw new Exception("nothing changed");
}
$message = "auto commit job done";
$status = true;
}
catch(Exception $e)
{
$message = $e->getMessage();
$status = false;
}
$log = ob_get_clean();
$email_body = ($status?"success":"fail")
. PHP_EOL . $message
. PHP_EOL . PHP_EOL . PHP_EOL . "The following log is FYI"
. PHP_EOL . PHP_EOL . PHP_EOL . $log;
SendNotifyEmail($recipients, $email_subject, $email_body);
echo $status?"success":"fail", PHP_EOL, $message, PHP_EOL;
function SendNotifyEmail($recipients, $subject, $message)
{
file_put_contents("var/tmp/email_".date('YmdHis') . '.json', json_encode(compact('recipients', 'subject', 'message')));
if(!$recipients)
{
return false;
}
$from_email = "mysupersite@gmail.com";
$to = implode(', ', array_keys($recipients));
$from_user = "=?UTF-8?B?".base64_encode("My Super Site")."?=";
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
$headers = array();
$headers []= sprintf("From: %s <%s>",$from_user, $from_email);
$headers []= "MIME-Version: 1.0";
$headers []= "Content-type: text/html; charset=UTF-8";
return mail($to, $subject, $message, implode("\r\n", $headers));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment