1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171:
<?php
define('DEBUG_MODE', false);
if (DEBUG_MODE)
debugPrint("--- DEBUG MSGS START ---");
$signedoff = find_signed_off();
if (empty($signedoff))
$signedoff = find_gpg();
if (empty($signedoff) && isset($_SERVER['argv'], $_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'travis')
$signedoff = find_signed_off_parents();
if (DEBUG_MODE)
debugPrint("--- DEBUG MSGS END ---");
if (empty($signedoff))
die('Error: Signed-off-by not found in commit message' . "\n");
elseif (DEBUG_MODE)
debugPrint('Valid signed off found');
function find_signed_off($commit = 'HEAD', $childs = array(), $level = 0)
{
$commit = trim($commit);
debugPrint('Attempting to find signed off on commit ' . $commit);
if ($level > 10)
{
debugPrint('Recusion limit exceeded on find_signed_off');
return false;
}
$stringTests = array('Signed-off-by:', 'Signed by');
$message = trim(shell_exec('git show -s --format=%B ' . $commit));
$lines = explode("\n", trim(str_replace("\r", "\n", $message)));
$lastLine = $lines[count($lines) - 1];
debugPrint('Testing line "' . $lastLine . '"');
$result = false;
foreach ($stringTests as $testedString)
{
debugPrint('Testing "' . $testedString . '"');
$result = stripos($lastLine, $testedString);
if ($result !== false)
{
debugPrint('Found "' . $testedString . '"');
break;
}
}
$debugMsgs = array(
'raw body' => '"' . rtrim(shell_exec('git show -s --format=%B ' . $commit)) . '"',
'body' => '"' . rtrim(shell_exec('git show -s --format=%b ' . $commit)) . '"',
'commit notes' => '"' . rtrim(shell_exec('git show -s --format=%N ' . $commit)) . '"',
'ref names' => '"' . rtrim(shell_exec('git show -s --format=%d ' . $commit)) . '"',
'commit hash' => '"' . rtrim(shell_exec('git show -s --format=%H ' . $commit)) . '"',
'tree hash' => '"' . rtrim(shell_exec('git show -s --format=%T ' . $commit)) . '"',
'parent hash' => '"' . rtrim(shell_exec('git show -s --format=%P ' . $commit)) . '"',
'result' => '"' . $result . '"',
'testedString' => '"' . $testedString . '"',
);
debugPrint('Commit ' . $commit . ' at time ' . time() . ": " . rtrim(print_r($debugMsgs, true)));
if ($result === false && preg_match('~Merge ([A-Za-z0-9]{40}) into ([A-Za-z0-9]{40})~i', $lastLine, $merges))
{
debugPrint('Found Merge, attempting to get more parent commit: ' . $merges[1]);
return find_signed_off($merges[1], array_merge(array($merges[1]), $childs), ++$level);
}
return $result !== false;
}
function find_gpg($commit = 'HEAD', $childs = array())
{
$commit = trim($commit);
debugPrint('Attempting to Find GPG on commit ' . $commit);
$message = trim(shell_exec('git verify-commit ' . $commit . ' -v --raw'));
$result = strlen($message) > 0;
$debugMsgs = array(
'verify-commit' => '"' . rtrim(shell_exec('git verify-commit ' . $commit . ' -v --raw')) . '"',
'result' => '"' . $result . '"',
'message' => '"' . $message . '"',
);
debugPrint('Commit ' . $commit . ' at time ' . time() . ": " . rtrim(print_r($debugMsgs, true)));
return $result;
}
function find_signed_off_parents($commit = 'HEAD')
{
$commit = trim($commit);
debugPrint('Attempting to find parents on commit ' . $commit);
$parentsRaw = rtrim(shell_exec('git show -s --format=%P ' . $commit));
$parents = explode(' ', $parentsRaw);
foreach ($parents as $p)
{
$p = trim($p);
debugPrint('Testing parent of ' . $commit . ' for signed off');
$test = find_signed_off($p);
if (empty($test))
$test = find_gpg($p);
if (!empty($test))
return $test;
}
return false;
}
function debugPrint($msg)
{
if (DEBUG_MODE)
echo $msg, "\n";
}