|
| 1 | +<?php |
| 2 | + |
| 3 | +$modHistoryFile = 'fileModHistory.php'; |
| 4 | + |
| 5 | +$modHistoryArray = []; |
| 6 | +if (file_exists($modHistoryFile)) { |
| 7 | + $modHistoryArray = include $modHistoryFile; |
| 8 | + if (!is_array($modHistoryArray)) { |
| 9 | + exit("Corrupted modificiation history file\n"); |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +if (isset($modHistoryArray["last commit hash"]) && $modHistoryArray["last commit hash"] !== "") { |
| 14 | + $cmd = "git rev-parse --quiet --verify " . $modHistoryArray["last commit hash"]; |
| 15 | + if (exec($cmd, $verifiedHash) === false) { |
| 16 | + exit("Could not retrieve hash of the last commit\n"); |
| 17 | + } |
| 18 | + if (implode("", $verifiedHash) !== $modHistoryArray["last commit hash"]) { |
| 19 | + // we cannot handle reverted commits as we don't know what changes to roll back |
| 20 | + exit("Modification history file's commit hash is not in this branch's commit history\n"); |
| 21 | + } |
| 22 | + $lastCommitHash = $modHistoryArray["last commit hash"]; |
| 23 | +} else { |
| 24 | + // since there is no modification history, generate it for all commits since the inital one |
| 25 | + // 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is the SHA1 of the empty git tree |
| 26 | + $lastCommitHash = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"; |
| 27 | +} |
| 28 | + |
| 29 | +$modifiedFilescommand = <<<COMMAND |
| 30 | +#!/usr/bin/env bash |
| 31 | +echo "last commit hash:" |
| 32 | +echo "$(git rev-parse HEAD)" |
| 33 | +git diff --name-only HEAD $lastCommitHash | while read -r filename; do |
| 34 | + echo "filename:" |
| 35 | + echo "\$filename" |
| 36 | + echo "modified:" |
| 37 | + echo "$(git log -1 --format='%aI' -- \$filename)" |
| 38 | + echo "contributors:" |
| 39 | + git log --format='%an' -- \$filename|awk '!a[$0]++' |
| 40 | +done |
| 41 | +COMMAND; |
| 42 | + |
| 43 | +if (exec($modifiedFilescommand, $output) === false) { |
| 44 | + exit("Could not retrieve info from last commit\n"); |
| 45 | +} |
| 46 | + |
| 47 | +$modifiedFiles = []; |
| 48 | +$currentType = ""; |
| 49 | +foreach ($output as $line) { |
| 50 | + switch ($line) { |
| 51 | + case "filename:": |
| 52 | + $currentType = "filename"; |
| 53 | + continue 2; |
| 54 | + case "modified:": |
| 55 | + $currentType = "modDateTime"; |
| 56 | + continue 2; |
| 57 | + case "contributors:": |
| 58 | + $currentType = "contributors"; |
| 59 | + continue 2; |
| 60 | + case "last commit hash:": |
| 61 | + $currentType = "commitHash"; |
| 62 | + continue 2; |
| 63 | + } |
| 64 | + if ($currentType === "") { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + switch ($currentType) { |
| 69 | + case "filename": |
| 70 | + $currentFile = $line; |
| 71 | + break; |
| 72 | + case "modDateTime": |
| 73 | + if ($currentFile === "") { |
| 74 | + continue 2; |
| 75 | + } |
| 76 | + $modifiedFiles[$currentFile]["modified"] = $line; |
| 77 | + break; |
| 78 | + case "contributors": |
| 79 | + if ($currentFile === "") { |
| 80 | + continue 2; |
| 81 | + } |
| 82 | + $modifiedFiles[$currentFile]["contributors"][] = htmlspecialchars($line, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401); |
| 83 | + break; |
| 84 | + case "commitHash": |
| 85 | + $modifiedFiles["last commit hash"][] = $line; |
| 86 | + break; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +if (count($modifiedFiles) === 1) { |
| 91 | + // there will always be 1 entry with the last commit hash |
| 92 | + exit("No files have been modified\n"); |
| 93 | +} |
| 94 | + |
| 95 | +$mergedModHistory = array_merge($modHistoryArray, $modifiedFiles); |
| 96 | + |
| 97 | +$newModHistoryString = "<?php\n\n/* This is a generated file */\n\nreturn [\n"; |
| 98 | +foreach ($mergedModHistory as $fileName => $fileProps) { |
| 99 | + if ($fileName === "last commit hash") { |
| 100 | + $newModHistoryString .= " \"last commit hash\" => \"" . implode("", $fileProps) . "\",\n"; |
| 101 | + continue; |
| 102 | + } |
| 103 | + $newModHistoryString .= ' "' . $fileName . "\" => [\n"; |
| 104 | + $newModHistoryString .= " \"modified\" => \"" . ($fileProps["modified"] ?? "") . "\",\n"; |
| 105 | + $newModHistoryString .= " \"contributors\" => [\n"; |
| 106 | + if (isset($fileProps["contributors"])) { |
| 107 | + if (!is_array($fileProps["contributors"])) { |
| 108 | + exit("Non-array contributors list\n"); |
| 109 | + } |
| 110 | + foreach ($fileProps["contributors"] as $contributor) { |
| 111 | + $newModHistoryString .= " \"" . $contributor . "\",\n"; |
| 112 | + } |
| 113 | + } |
| 114 | + $newModHistoryString .= " ],\n"; |
| 115 | + $newModHistoryString .= " ],\n"; |
| 116 | +} |
| 117 | +$newModHistoryString .= "];\n"; |
| 118 | + |
| 119 | +if (file_put_contents($modHistoryFile, $newModHistoryString) === false) { |
| 120 | + exit("Could not write modification history file\n"); |
| 121 | +} |
| 122 | + |
| 123 | +echo "Modification history updated\n"; |
0 commit comments