diff --git a/scripts/updateModHistory.php b/scripts/updateModHistory.php index 6ca06bc9a..83c853137 100644 --- a/scripts/updateModHistory.php +++ b/scripts/updateModHistory.php @@ -2,35 +2,55 @@ $modHistoryFile = 'fileModHistory.php'; +$runningInGithubActions = (getenv("GITHUB_ACTIONS") !== false); + +$head = $runningInGithubActions ? "\$GITHUB_SHA" : "HEAD"; + $modHistoryArray = []; if (file_exists($modHistoryFile)) { + echo timeStamp() . " - Loading modification history file... "; $modHistoryArray = include $modHistoryFile; if (!is_array($modHistoryArray)) { - exit("Corrupted modificiation history file\n"); + echo "file is corrupted (not an array)\n"; + exit(1); } + echo "done\n"; +} else { + echo timeStamp() . " - Modification history file doesn't exist\n"; } if (isset($modHistoryArray["last commit hash"]) && $modHistoryArray["last commit hash"] !== "") { - $cmd = "git rev-parse --quiet --verify " . $modHistoryArray["last commit hash"]; - if (exec($cmd, $verifiedHash) === false) { - exit("Could not retrieve hash of the last commit\n"); + echo timeStamp() . " - Found last commit hash: " . $modHistoryArray["last commit hash"] . "\n"; + echo timeStamp() . " - Retrieving hash of the common ancestor of HEAD and the last commit... "; + $cmd = "git merge-base " . $modHistoryArray["last commit hash"] . " $head"; + if (exec($cmd, $commonAncestor) === false) { + echo "failed\n"; + exit(1); } - if (implode("", $verifiedHash) !== $modHistoryArray["last commit hash"]) { - // we cannot handle reverted commits as we don't know what changes to roll back - exit("Modification history file's commit hash is not in this branch's commit history\n"); - } - $lastCommitHash = $modHistoryArray["last commit hash"]; + $commonAncestorHash = implode("", $commonAncestor); + echo "done: "; } else { + echo timeStamp() . " - Last commit hash not found. Using empty git tree hash: "; // since there is no modification history, generate it for all commits since the inital one // 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is the SHA1 of the empty git tree - $lastCommitHash = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"; + $commonAncestorHash = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"; } +echo $commonAncestorHash . "\n"; + +echo timeStamp() . " - Retrieving number of files with a diff... "; +$cmd = "git diff --name-only $commonAncestorHash $head | wc -l"; +if (exec($cmd, $numOfFilesWithDiff) === false) { + echo "failed\n"; + exit(1); +} +$numOfFilesWithDiff = implode("", $numOfFilesWithDiff); +echo "done (" . $numOfFilesWithDiff . ")\n"; $modifiedFilescommand = << $fileProps) { + if ($fileName === "last commit hash") { + fwrite($fp, " \"last commit hash\" => \"" . implode("", $fileProps) . "\",\n"); + continue; + } + $newModHistoryString = ' "' . $fileName . "\" => [\n"; + $newModHistoryString .= " \"modified\" => \"" . ($fileProps["modified"] ?? "") . "\",\n"; + $newModHistoryString .= " \"contributors\" => [\n"; + if (isset($fileProps["contributors"])) { + if (!is_array($fileProps["contributors"])) { + exit("Non-array contributors list\n"); + } + foreach ($fileProps["contributors"] as $contributor) { + $newModHistoryString .= " \"" . $contributor . "\",\n"; + } + } + $newModHistoryString .= " ],\n"; + $newModHistoryString .= " ],\n"; + fwrite($fp, $newModHistoryString); +} +fwrite($fp, "];\n"); +fclose($fp); + +echo "done at " . date('H:i:s') . "\n"; + +function timeStamp(): string { + return "[" . date('H:i:s') . "]"; +} + +function processGitDiffLine($line, &$modifiedFiles): void { + static $currentType = ""; + static $currentFile = ""; + global $fileCounter; + switch ($line) { case "filename:": $currentType = "filename"; - continue 2; + $fileCounter++; + return; case "modified:": $currentType = "modDateTime"; - continue 2; + return; case "contributors:": $currentType = "contributors"; - continue 2; + return; case "last commit hash:": $currentType = "commitHash"; - continue 2; + return; } if ($currentType === "") { - continue; + return; } switch ($currentType) { @@ -71,13 +150,13 @@ break; case "modDateTime": if ($currentFile === "") { - continue 2; + return; } $modifiedFiles[$currentFile]["modified"] = $line; break; case "contributors": if ($currentFile === "") { - continue 2; + return; } $modifiedFiles[$currentFile]["contributors"][] = htmlspecialchars($line, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401); break; @@ -86,38 +165,3 @@ break; } } - -if (count($modifiedFiles) === 1) { - // there will always be 1 entry with the last commit hash - exit("No files have been modified\n"); -} - -$mergedModHistory = array_merge($modHistoryArray, $modifiedFiles); - -$newModHistoryString = " $fileProps) { - if ($fileName === "last commit hash") { - $newModHistoryString .= " \"last commit hash\" => \"" . implode("", $fileProps) . "\",\n"; - continue; - } - $newModHistoryString .= ' "' . $fileName . "\" => [\n"; - $newModHistoryString .= " \"modified\" => \"" . ($fileProps["modified"] ?? "") . "\",\n"; - $newModHistoryString .= " \"contributors\" => [\n"; - if (isset($fileProps["contributors"])) { - if (!is_array($fileProps["contributors"])) { - exit("Non-array contributors list\n"); - } - foreach ($fileProps["contributors"] as $contributor) { - $newModHistoryString .= " \"" . $contributor . "\",\n"; - } - } - $newModHistoryString .= " ],\n"; - $newModHistoryString .= " ],\n"; -} -$newModHistoryString .= "];\n"; - -if (file_put_contents($modHistoryFile, $newModHistoryString) === false) { - exit("Could not write modification history file\n"); -} - -echo "Modification history updated\n";