Skip to content

Instantly share code, notes, and snippets.

@ncuesta
Created May 16, 2012 17:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncuesta/2712235 to your computer and use it in GitHub Desktop.
Save ncuesta/2712235 to your computer and use it in GitHub Desktop.
insist-commit [php version]
#!/usr/bin/env php
<?php
/**
* Insist on committing some changes using fast:commit.
* This script takes at least one argument:
* + The changelist number.
* Optionally, you may indicate the maximum number of attempts desired.
*/
/**
* Output a $msg to stdout.
*
* @param string|array $msg A string or array of strings to output.
*/
function output($msg) {
if (is_array($msg)) {
$msg = implode("\n", $msg);
}
echo "$msg\n";
}
/**
* Run a command string and return its exit code.
*
* @param string $command The command to run.
*
* @return int
*/
function run($command) {
system($command, $exitCode);
return $exitCode;
}
$argsv = array_slice($argv, 1);
$argsc = count($argsv);
// Check required arg
if ($argsc === 0) {
output('Missing changelist number. Aborting.');
exit(1);
}
$changelist = $argsv[0];
// Check for Rakefile
if (!is_readable(__DIR__ . '/Rakefile')) {
output('There\'s no Rakefile on the current directory. Aborting.');
exit(3);
}
$limit = 0;
if ($argsc === 2) {
$limit = $argsv[1];
}
$rakeCommand = "rake fast:commit[{$changelist}]";
// Start obsessive attempts
output("Insisting on committing {$changelist}...");
$tries = 0;
$exitCode = 0;
while ($exitCode !== 1) {
$tries++;
$exitCode = run($rakeCommand);
if ($limit === $tries) {
break;
}
}
$tries--;
output(
array(
'',
"Finally! It took {$tries} attempts to check {$changelist} in.",
'Now head to to that CI server and make it go green!',
''
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment