From 7cef67c02e5c3742a1dde54f1a7ef71d3c054de0 Mon Sep 17 00:00:00 2001 From: haszi Date: Mon, 22 Apr 2024 21:42:37 +0200 Subject: [PATCH 1/6] Add file modification history update script --- scripts/updateModHistory.php | 123 +++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 scripts/updateModHistory.php diff --git a/scripts/updateModHistory.php b/scripts/updateModHistory.php new file mode 100644 index 000000000..6ca06bc9a --- /dev/null +++ b/scripts/updateModHistory.php @@ -0,0 +1,123 @@ + $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"; From ee8e71cd710d3090cdc1df6937fe8fd3d5a20afe Mon Sep 17 00:00:00 2001 From: haszi Date: Thu, 1 Aug 2024 19:24:09 +0200 Subject: [PATCH 2/6] Update script --- scripts/updateModHistory.php | 162 ++++++++++++++++++++++------------- 1 file changed, 103 insertions(+), 59 deletions(-) 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"; From eeab3a2503df63ac24b5b705c013c51780797ea3 Mon Sep 17 00:00:00 2001 From: haszi Date: Tue, 1 Oct 2024 10:40:17 +0200 Subject: [PATCH 3/6] Make documentation and history file paths command line arguments --- scripts/updateModHistory.php | 64 ++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/scripts/updateModHistory.php b/scripts/updateModHistory.php index 83c853137..461d7dbb6 100644 --- a/scripts/updateModHistory.php +++ b/scripts/updateModHistory.php @@ -1,6 +1,11 @@ 0) { echo "failed\n"; exit(1); } @@ -39,13 +49,19 @@ echo timeStamp() . " - Retrieving number of files with a diff... "; $cmd = "git diff --name-only $commonAncestorHash $head | wc -l"; -if (exec($cmd, $numOfFilesWithDiff) === false) { +if (exec($cmd, $numOfFilesWithDiff, $exitCode) === false + || $exitCode > 0) { echo "failed\n"; exit(1); } $numOfFilesWithDiff = implode("", $numOfFilesWithDiff); echo "done (" . $numOfFilesWithDiff . ")\n"; +if ($numOfFilesWithDiff === "0") { + echo timeStamp() . " - No changes since last commit. Exiting...\n"; + exit(0); +} + $modifiedFilescommand = << 2) ? "s" : ""; +echo timeStamp() . " - Retrieved author$s and last commit date$s/time$s for " . (count($modifiedFiles) - 1) . " file$s\n"; if (count($modifiedFiles) === 1) { // there will always be at least 1 entry with the last commit hash exit(1); @@ -165,3 +182,38 @@ function processGitDiffLine($line, &$modifiedFiles): void { break; } } + +function verifyCommandLineOptions($commandLineOptions): void { + echo timeStamp() . " - Parsing command line arguments... "; + if ($commandLineOptions === false + || !isset($commandLineOptions["docs-path"])) { + echo "\"--docs-path\" is a required argument\n"; + exit(1); + } + echo "documentation path supplied\n"; + if (isset($commandLineOptions["history-path"])) { + echo " mod history file path supplied\n"; + } + + echo timeStamp() . " - Verifying command line arguments... "; + if (!file_exists($commandLineOptions["docs-path"])) { + echo "documentation path \"" . $commandLineOptions["docs-path"] . "\" doesn't exist\n"; + exit(1); + } else if (!is_dir($commandLineOptions["docs-path"])) { + echo "documentation path \"" . $commandLineOptions["docs-path"] . "\" is not a directory\n"; + exit(1); + } + echo "documentation path verified"; + + if (isset($commandLineOptions["history-path"])) { + if (!file_exists($commandLineOptions["history-path"])) { + echo "\n mod history file path \"" . $commandLineOptions["history-path"] . "\" doesn't exist\n"; + exit(1); + } else if (!is_file($commandLineOptions["history-path"])) { + echo "\n mod history file path \"" . $commandLineOptions["history-path"] . "\" is not a file\n"; + exit(1); + } + echo "\n mod history file path verified"; + } + echo "\n"; +} From c15cfb6cbba6fcf1a611cd6f33635ba72a9a8524 Mon Sep 17 00:00:00 2001 From: haszi Date: Sat, 12 Oct 2024 15:33:02 +0200 Subject: [PATCH 4/6] Remove unnecessary variable --- scripts/updateModHistory.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/scripts/updateModHistory.php b/scripts/updateModHistory.php index 461d7dbb6..8fe97596e 100644 --- a/scripts/updateModHistory.php +++ b/scripts/updateModHistory.php @@ -78,13 +78,13 @@ echo timeStamp() . " - Retrieving commit authors and last commit date/time of modified files... \n"; -$fileCounter = 0; $modifiedFiles = []; $proc = popen($modifiedFilescommand, 'rb'); while (($line = fgets($proc)) !== false) { processGitDiffLine(rtrim($line, "\n\r"), $modifiedFiles); if (! $runningInGithubActions) { + $fileCounter = max(count($modifiedFiles) - 1, 0); fwrite( STDERR, sprintf("\033[0G{$fileCounter} of {$numOfFilesWithDiff} files read...", "", "") @@ -140,12 +140,10 @@ function timeStamp(): string { function processGitDiffLine($line, &$modifiedFiles): void { static $currentType = ""; static $currentFile = ""; - global $fileCounter; switch ($line) { case "filename:": $currentType = "filename"; - $fileCounter++; return; case "modified:": $currentType = "modDateTime"; From b0dc0cce93387c75ee77dbc7eb502943c5c9ae55 Mon Sep 17 00:00:00 2001 From: haszi Date: Sat, 12 Oct 2024 16:13:38 +0200 Subject: [PATCH 5/6] Address review comment --- scripts/updateModHistory.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/scripts/updateModHistory.php b/scripts/updateModHistory.php index 8fe97596e..328ba0714 100644 --- a/scripts/updateModHistory.php +++ b/scripts/updateModHistory.php @@ -182,7 +182,8 @@ function processGitDiffLine($line, &$modifiedFiles): void { } function verifyCommandLineOptions($commandLineOptions): void { - echo timeStamp() . " - Parsing command line arguments... "; + $output = timeStamp() . " - Parsing command line arguments... "; + echo $output; if ($commandLineOptions === false || !isset($commandLineOptions["docs-path"])) { echo "\"--docs-path\" is a required argument\n"; @@ -190,10 +191,11 @@ function verifyCommandLineOptions($commandLineOptions): void { } echo "documentation path supplied\n"; if (isset($commandLineOptions["history-path"])) { - echo " mod history file path supplied\n"; + echo str_repeat(" ", strlen($output)) . "mod history file path supplied\n"; } - echo timeStamp() . " - Verifying command line arguments... "; + $output = timeStamp() . " - Verifying command line arguments... "; + echo $output; if (!file_exists($commandLineOptions["docs-path"])) { echo "documentation path \"" . $commandLineOptions["docs-path"] . "\" doesn't exist\n"; exit(1); @@ -201,17 +203,17 @@ function verifyCommandLineOptions($commandLineOptions): void { echo "documentation path \"" . $commandLineOptions["docs-path"] . "\" is not a directory\n"; exit(1); } - echo "documentation path verified"; + echo "documentation path verified\n"; if (isset($commandLineOptions["history-path"])) { + echo "\n" . str_repeat(" ", strlen($output)) . "mod history file path "; if (!file_exists($commandLineOptions["history-path"])) { - echo "\n mod history file path \"" . $commandLineOptions["history-path"] . "\" doesn't exist\n"; + echo "\"" . $commandLineOptions["history-path"] . "\" doesn't exist\n"; exit(1); } else if (!is_file($commandLineOptions["history-path"])) { - echo "\n mod history file path \"" . $commandLineOptions["history-path"] . "\" is not a file\n"; + echo "\"" . $commandLineOptions["history-path"] . "\" is not a file\n"; exit(1); } - echo "\n mod history file path verified"; + echo "verified\n"; } - echo "\n"; } From a45033f9cbb8fcb39b85f1f763970a9653518b66 Mon Sep 17 00:00:00 2001 From: haszi Date: Mon, 14 Oct 2024 08:30:44 +0200 Subject: [PATCH 6/6] Use printf instead of echo and str_repeat --- scripts/updateModHistory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/updateModHistory.php b/scripts/updateModHistory.php index 328ba0714..ece548bd8 100644 --- a/scripts/updateModHistory.php +++ b/scripts/updateModHistory.php @@ -191,7 +191,7 @@ function verifyCommandLineOptions($commandLineOptions): void { } echo "documentation path supplied\n"; if (isset($commandLineOptions["history-path"])) { - echo str_repeat(" ", strlen($output)) . "mod history file path supplied\n"; + printf("%*smod history file path supplied\n", strlen($output), " "); } $output = timeStamp() . " - Verifying command line arguments... "; @@ -206,7 +206,7 @@ function verifyCommandLineOptions($commandLineOptions): void { echo "documentation path verified\n"; if (isset($commandLineOptions["history-path"])) { - echo "\n" . str_repeat(" ", strlen($output)) . "mod history file path "; + printf("\n%*smod history file path ", strlen($output), " "); if (!file_exists($commandLineOptions["history-path"])) { echo "\"" . $commandLineOptions["history-path"] . "\" doesn't exist\n"; exit(1);