diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6fefa14ed25..490373ea632 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -34,6 +34,15 @@ repos: args: [--tab] # Run local script + # + # For instance to update the license in edited files, you could add to local.sh: + # + # ```shell + # #!/bin/bash + # MYDIR=$(dirname "$0") + # CHANGED_INTERNALS=$(git diff --name-only | grep -v includes) + # "$MYDIR/dev/tools/updatelicense.php" $CHANGED_INTERNALS + # ``` - repo: local hooks: - id: local-precommit-script diff --git a/dev/tools/updatelicense.php b/dev/tools/updatelicense.php new file mode 100755 index 00000000000..6c69785e7e0 --- /dev/null +++ b/dev/tools/updatelicense.php @@ -0,0 +1,154 @@ +#!/usr/bin/env php + + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is furnished + * to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * Script to update year periods and user/email in headers. + */ + +/** + * Retrieve Git user information + * + * @return array{name:string,email:string} + */ +function getGitUserInfo() +{ + $name = trim(shell_exec('git config user.name')); + $email = trim(shell_exec('git config user.email')); + return ['name' => $name, 'email' => $email]; +} + +const PREFIXES = [ + 'sh' => ['# ', '# ', '', '#!'], + 'php' => ['/* ', ' * ', ' */', '\d{4}))\s+{$r_name}\s*\<{$r_email}>~"; + + // Check if the lines match the pattern + if (preg_match($pattern, $lines, $matches)) { + $existingYear = $matches['year']; + + // Check if the existing year is different from the current year + if ($existingYear !== date('Y')) { + // Update the year range to include or be up to the current year + $updatedNotice = preg_replace('/(\d{4})(-\d{4})?\s+/', $existingYear . '-' . date('Y') . "\t", $matches[0]); + + // Replace the old notice with the updated one in the file + file_put_contents($filename, preg_replace($pattern, $updatedNotice, file_get_contents($filename))); + return true; // Change detected + } + // If the existing year is the same, no need to update + } else { + // Adjust tabs for proper alignment + $emailTabs = str_repeat("\t", (int) (max(0, (31 - mb_strlen($name)) / 4))); + + // No match found, add a new line to the header + $newNotice = "Copyright (C) " . date('Y') . "\t\t" . $name . $emailTabs . "<" . $email . ">"; + + // Read the file content + $fileContent = file_get_contents($filename); + + // Check if there are existing copyright notices + $pos = max(strrpos($fileContent, "{$prefix0}Copyright"), strrpos($fileContent, "{$prefix1}Copyright")); + + if ($pos !== false) { + // Add the new notice behind the last preceding copyright notices + $pos = strpos($fileContent, "\n", $pos) + 1; + $fileContent = substr_replace($fileContent, $prefix1 . $newNotice . "\n", $pos, 0); + } elseif (strpos($fileContent, $prefix3) !== false) { + // Add the new notice after the shebang or ' [ ...]" . PHP_EOL; + exit(1); +} + +// Process each filename provided +$changesDetected = false; +for ($i = 1; $i < $argc; $i++) { + $filename = $argv[$i]; + + // Determine file type based on extension + $fileType = pathinfo($filename, PATHINFO_EXTENSION); + + // Retrieve Git user information + $gitUserInfo = getGitUserInfo(); + $name = $gitUserInfo['name']; + $email = $gitUserInfo['email']; + + // Update or add copyright notice based on file type + $changeDetected = updateCopyrightNotice($filename, $fileType, $name, $email); + $changesDetected |= $changeDetected; + if ($changeDetected) { + echo "Copyright notice updated in '$filename'" . PHP_EOL; + } +} + +if (!$changesDetected) { + echo "No changes needed in any file" . PHP_EOL; +}