Kleiner Proxy für das Erstellen von SVN-Commit-Meldungen. Bezieht folgende Quellen:
- http://whatthecommit.com
- ein lokales “vommit.txt”-File
Aufruf durch:
- svnvommit.php: Liefert Random-Text
- svnvommit.php?cmd=svn: Liefert fertige SVN Commit-Kommandozeile mit Message
- svnvommit.php?help: Liefert eine Hilfe
SVN-Commit BASH-Alias:
alias svnvommit='TXT=$(curl -s http://82.197.187.164/svnvommit.php);echo "MSG ${TXT}"; svn commit -m "${TXT}"'
Beispiel unter:
http://www.alexi.ch/svnvommit.php
<?php
/**
* SVN Velocity Commit - commit without caring about a comment
*
* (c) 2012 alex schenkel
*/
$sentenceFunctions = array(
'whatthecommit' => 20,
'localvommits' => 2,
'cowsay' => 10,
'fortune' => 2
);
header('Content-Type: text/plain');
// step 0: help / list required?:
if (isset($_REQUEST['help'])) {
printHelp();
exit;
}
if (isset($_REQUEST['sentences'])) {
printSentences();
exit;
}
$func = null;
if (isset($_REQUEST['sentence'])) {
$func = $_REQUEST['sentence'];
}
// step 1: find which sentence function to use: the higher propability the more
// it would be taken:
if (!$func) {
$sumProb = 0;
foreach($sentenceFunctions as $val) {
$sumProb += max(0,(int)$val);
}
$func = null;
$rnd = rand(1,$sumProb);
$prob = 0;
foreach($sentenceFunctions as $f=>$val) {
$prob += max(0,(int)$val);
$func = $f;
if ($prob >= $rnd) {
//echo "$prob, $rnd";
break;
}
}
}
if (!function_exists($func)) {
echo "ERROR: CHECK YOUR VELOCITY COMMIT SERVICE AT {$_SERVER['SERVER_NAME']}! SOMETHING WENT WRONG!";
exit;
}
// Step 2: get the message from the sentence provider:
$text = $func();
// Step 3: output, either with or without command line wrapping:
if (isset($_REQUEST['cmd'])) {
switch ($_REQUEST['cmd']) {
case 'svn': $text = 'svn commit -m "'.addslashes($text).'"'; break;
case 'git': $text = 'git commit -m "'.addslashes($text).'"'; break;
case 'git-add': $text = 'git commit -a -m "'.$text.'"'; break;
}
}
echo $text;
exit;
function printHelp() {
echo <<<EOT
Known parameters:
help : this list
sentences : List of available sentence sources
sentence=<name>: uses only the <name> sentence source
cmd=svn : Wraps the output in a SVN commit command line
cmd=git : Wraps the output in a GIT commit command line
cmd=git-add : Wraps the output in a git -a commit command line
EOT;
}
function printSentences() {
global $sentenceFunctions;
foreach($sentenceFunctions as $f=>$val) {
echo "{$f}\n";
}
}
function whatthecommit() {
$text = file_get_contents('http://whatthecommit.com/');
try {
$xml = new SimpleXMLElement($text);
$elems = $xml->xpath("//*[@id='content']");
$text = trim(preg_replace("/[\n\"]/",'',(string)$elems[0]->p[0]));
return "WTC: ".$text;
} catch (Exception $e) {
return false;
}
return $text;
}
function localvommits() {
return "V-OMMIT: ".getVommitsTxtLine();
}
function cowsay() {
$cowsaycmd = '/opt/local/bin/cowsay';
$txt = getVommitsTxtLine();
$h = popen("{$cowsaycmd} \"{$txt}\"",'r');
if ($h) {
$ret = "The cow says:\n";
while (($line = fgets($h)) !== FALSE) {
$ret .= $line;
}
$code = pclose($h);
if ($code == 0) {
return str_replace('"','',$ret);
}
}
return $txt;
}
function getVommitsTxtLine() {
$vommits = array();
$fh = @fopen(dirname(__FILE__).'/vommits.txt','r');
if ($fh) {
while (($line = fgets($fh)) !== FALSE) {
$line = trim(preg_replace("/[\n\"]/",'',$line));
if ($line) {
$vommits[] = $line;
}
}
fclose($fh);
}
if (count($vommits) > 0) {
$text = $vommits[rand(0,count($vommits)-1)];
} else $text = '<empty vommits.txt file....>';
return $text;
}
function fortune() {
$fortunecmd = '/opt/local/bin/fortune';
$h = popen("{$fortunecmd}",'r');
$txt = "Today's Fortune:\n";
if ($h) {
$ret = "";
while (($line = fgets($h)) !== FALSE) {
$txt .= $line;
}
$code = pclose($h);
if ($code == 0) {
$txt = str_replace('"','',$txt);
}
}
return $txt;
}