From d7ba2a165497120ed909158102d8bc05e22e9205 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 9 Jan 2024 14:47:16 +0200 Subject: [PATCH 1/6] Replace hand-written pre-commit-clang-format hook with pre-commit --- .pre-commit-config.yaml | 15 ++ misc/hooks/clang-format-wrap | 27 +++ misc/hooks/pre-commit-clang-format | 264 +---------------------------- 3 files changed, 45 insertions(+), 261 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100755 misc/hooks/clang-format-wrap diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000000..1d2605d5286c --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,15 @@ +repos: + - repo: local + hooks: + - id: clang-format + name: clang-format + entry: ./misc/hooks/clang-format-wrap + language: system + files: \.(c|h|cpp|hpp|cc|cxx|m|mm|inc|java|glsl)$ + exclude: | + (?x)^( + tests/python_build.*| + .*thirdparty.*| + .*platform/android/java/lib/src/com.*| + .*-so_wrap.* + ) diff --git a/misc/hooks/clang-format-wrap b/misc/hooks/clang-format-wrap new file mode 100755 index 000000000000..b7efe65f566d --- /dev/null +++ b/misc/hooks/clang-format-wrap @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +CLANG_FORMAT=$(which clang-format 2>/dev/null) + +# To get consistent formatting, we recommend contributors to use the same +# clang-format version as CI. +RECOMMENDED_CLANG_FORMAT_MAJOR_MIN="13" +RECOMMENDED_CLANG_FORMAT_MAJOR_MAX="16" + +if [ ! -x "$CLANG_FORMAT" ]; then + message="Error: clang-format executable not found. Please install clang-format $RECOMMENDED_CLANG_FORMAT_MAJOR_MAX." + exit 1 +fi + +# The returned string can be inconsistent depending on where clang-format comes from. +# Example output strings reported by `clang-format --version`: +# - Ubuntu: "Ubuntu clang-format version 11.0.0-2" +# - Fedora: "clang-format version 11.0.0 (Fedora 11.0.0-2.fc33)" +CLANG_FORMAT_VERSION="$(clang-format --version | sed "s/[^0-9\.]*\([0-9\.]*\).*/\1/")" +CLANG_FORMAT_MAJOR="$(echo "$CLANG_FORMAT_VERSION" | cut -d. -f1)" + +if [[ "$CLANG_FORMAT_MAJOR" -lt "$RECOMMENDED_CLANG_FORMAT_MAJOR_MIN" || "$CLANG_FORMAT_MAJOR" -gt "$RECOMMENDED_CLANG_FORMAT_MAJOR_MAX" ]]; then + echo "Warning: Your clang-format binary is the wrong version ($CLANG_FORMAT_VERSION, expected between $RECOMMENDED_CLANG_FORMAT_MAJOR_MIN and $RECOMMENDED_CLANG_FORMAT_MAJOR_MAX)." + echo " Consider upgrading or downgrading clang-format as formatting may not be applied correctly." +fi + +exec "$CLANG_FORMAT" -style=file --Wno-error=unknown -i "$@" diff --git a/misc/hooks/pre-commit-clang-format b/misc/hooks/pre-commit-clang-format index eddbc593640e..cc3917b0dede 100755 --- a/misc/hooks/pre-commit-clang-format +++ b/misc/hooks/pre-commit-clang-format @@ -1,262 +1,4 @@ #!/usr/bin/env bash - -# git pre-commit hook that runs a clang-format stylecheck. -# Features: -# - abort commit when commit does not comply with the style guidelines -# - create a patch of the proposed style changes -# Modifications for clang-format by rene.milk@wwu.de - -# This file is part of a set of unofficial pre-commit hooks available -# at github. -# Link: https://github.com/githubbrowser/Pre-commit-hooks -# Contact: David Martin, david.martin.mailbox@googlemail.com - -# Some quality of life modifications made for Godot Engine. - -################################################################## -# SETTINGS -# Set path to clang-format binary. -CLANG_FORMAT=`which clang-format 2>/dev/null` - -# Remove any older patches from previous commits. Set to true or false. -DELETE_OLD_PATCHES=false - -# Only parse files with the extensions in FILE_EXTS. Set to true or false. -# If false every changed file in the commit will be parsed with clang-format. -# If true only files matching one of the extensions are parsed with clang-format. -PARSE_EXTS=true - -# File types to parse. Only effective when PARSE_EXTS is true. -FILE_EXTS=".c .h .cpp .hpp .cc .hh .cxx .m .mm .inc .java .glsl" - -# Use pygmentize instead of cat to parse diff with highlighting. -# Install it with `pip install pygments` (Linux) or `easy_install Pygments` (Mac) -PYGMENTIZE=`which pygmentize 2>/dev/null` -if [ ! -z "$PYGMENTIZE" ]; then - READER="pygmentize -l diff" -else - READER=cat -fi - -# Path to zenity -ZENITY=`which zenity 2>/dev/null` - -# Path to xmessage -XMSG=`which xmessage 2>/dev/null` - -# Path to powershell (Windows only) -PWSH=`which powershell 2>/dev/null` - -# Path to osascript (macOS only) -OSA=`which osascript 2>/dev/null` - -################################################################## -# There should be no need to change anything below this line. - -. "$(dirname -- "$0")/canonicalize_filename.sh" - -# exit on error -set -e - -# check whether the given file matches any of the set extensions -matches_extension() { - local filename=$(basename "$1") - local extension=".${filename##*.}" - local ext - - for ext in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done - - return 1 -} - -# necessary check for initial commit -if git rev-parse --verify HEAD >/dev/null 2>&1 ; then - against=HEAD -else - # Initial commit: diff against an empty tree object - against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 -fi - -# To get consistent formatting, we recommend contributors to use the same -# clang-format version as CI. -RECOMMENDED_CLANG_FORMAT_MAJOR_MIN="13" -RECOMMENDED_CLANG_FORMAT_MAJOR_MAX="16" - -if [ ! -x "$CLANG_FORMAT" ] ; then - message="Error: clang-format executable not found. Please install clang-format $RECOMMENDED_CLANG_FORMAT_MAJOR_MAX." - - if [ ! -t 1 ] ; then - if [ -x "$ZENITY" ] ; then - $ZENITY --error --title="Error" --text="$message" - exit 1 - elif [ -x "$XMSG" ] ; then - $XMSG -center -title "Error" "$message" - exit 1 - elif [ -x "$OSA" ] ; then - asmessage="$(canonicalize_filename "$(dirname -- "$0")/asmessage.applescript")" - $OSA "$asmessage" -center -title "Error" --text "$message" - exit 1 - elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then - winmessage="$(canonicalize_filename "$(dirname -- "$0")/winmessage.ps1")" - $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -center -title "Error" --text "$message" - exit 1 - fi - fi - printf "$message\n" - printf "Set the correct path in $(canonicalize_filename "$0").\n" - exit 1 -fi - -# The returned string can be inconsistent depending on where clang-format comes from. -# Example output strings reported by `clang-format --version`: -# - Ubuntu: "Ubuntu clang-format version 11.0.0-2" -# - Fedora: "clang-format version 11.0.0 (Fedora 11.0.0-2.fc33)" -CLANG_FORMAT_VERSION="$(clang-format --version | sed "s/[^0-9\.]*\([0-9\.]*\).*/\1/")" -CLANG_FORMAT_MAJOR="$(echo "$CLANG_FORMAT_VERSION" | cut -d. -f1)" - -if [[ "$CLANG_FORMAT_MAJOR" -lt "$RECOMMENDED_CLANG_FORMAT_MAJOR_MIN" || "$CLANG_FORMAT_MAJOR" -gt "$RECOMMENDED_CLANG_FORMAT_MAJOR_MAX" ]]; then - echo "Warning: Your clang-format binary is the wrong version ($CLANG_FORMAT_VERSION, expected between $RECOMMENDED_CLANG_FORMAT_MAJOR_MIN and $RECOMMENDED_CLANG_FORMAT_MAJOR_MAX)." - echo " Consider upgrading or downgrading clang-format as formatting may not be applied correctly." -fi - -# create a random filename to store our generated patch -prefix="pre-commit-clang-format" -suffix="$(date +%s)" -patch="/tmp/$prefix-$suffix.patch" - -# clean up any older clang-format patches -$DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch - -# create one patch containing all changes to the files -git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file; -do - # ignore thirdparty files - if grep -q "thirdparty" <<< $file; then - continue; - fi - if grep -q "platform/android/java/lib/src/com" <<< $file; then - continue; - fi - if grep -q "\-so_wrap." <<< $file; then - continue; - fi - if grep -q "tests/python_build" <<< $file; then - continue; - fi - - # ignore file if we do check for file extensions and the file - # does not match any of the extensions specified in $FILE_EXTS - if $PARSE_EXTS && ! matches_extension "$file"; then - continue; - fi - - # clang-format our sourcefile, create a patch with diff and append it to our $patch - # The sed call is necessary to transform the patch from - # --- $file timestamp - # +++ - timestamp - # to both lines working on the same file and having a/ and b/ prefix. - # Else it can not be applied with 'git apply'. - "$CLANG_FORMAT" -style=file "$file" --Wno-error=unknown | \ - diff -u "$file" - | \ - sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch" -done - -# if no patch has been generated all is ok, clean up the file stub and exit -if [ ! -s "$patch" ] ; then - printf "Files in this commit comply with the clang-format rules.\n" - rm -f "$patch" - exit 0 -fi - -# a patch has been created, notify the user and exit -printf "\nThe following differences were found between the code to commit " -printf "and the clang-format rules:\n\n" - -if [ -t 1 ] ; then - $READER "$patch" - printf "\n" - # Allows us to read user input below, assigns stdin to keyboard - exec < /dev/tty - terminal="1" -else - cat "$patch" - printf "\n" - # Allows non zero zenity/powershell output - set +e - terminal="0" -fi - -while true; do - if [ $terminal = "0" ] ; then - if [ -x "$ZENITY" ] ; then - choice=$($ZENITY --text-info --filename="$patch" --width=800 --height=600 --title="Do you want to apply that patch?" --ok-label="Apply" --cancel-label="Do not apply" --extra-button="Apply and stage") - if [ "$?" = "0" ] ; then - yn="Y" - else - if [ "$choice" = "Apply and stage" ] ; then - yn="S" - else - yn="N" - fi - fi - elif [ -x "$XMSG" ] ; then - $XMSG -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?" - choice=$? - if [ "$choice" = "100" ] ; then - yn="Y" - elif [ "$choice" = "200" ] ; then - yn="S" - else - yn="N" - fi - elif [ -x "$OSA" ] ; then - asmessage="$(canonicalize_filename "$(dirname -- "$0")/asmessage.applescript")" - choice=`$OSA "$asmessage" -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?"` - if [ "$choice" = "100" ] ; then - yn="Y" - elif [ "$choice" = "200" ] ; then - yn="S" - else - yn="N" - fi - elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then - winmessage="$(canonicalize_filename "$(dirname -- "$0")/winmessage.ps1")" - $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?" - choice=$? - if [ "$choice" = "100" ] ; then - yn="Y" - elif [ "$choice" = "200" ] ; then - yn="S" - else - yn="N" - fi - else - printf "Error: zenity, xmessage, osascript, or powershell executable not found.\n" - exit 1 - fi - else - read -p "Do you want to apply that patch (Y - Apply, N - Do not apply, S - Apply and stage files)? [Y/N/S] " yn - fi - case $yn in - [Yy] ) git apply $patch; - printf "The patch was applied. You can now stage the changes and commit again.\n\n"; - break - ;; - [Nn] ) printf "\nYou can apply these changes with:\n git apply $patch\n"; - printf "(may need to be called from the root directory of your repository)\n"; - printf "Aborting commit. Apply changes and commit again or skip checking with"; - printf " --no-verify (not recommended).\n\n"; - break - ;; - [Ss] ) git apply $patch; - git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file; - do git add $file; - done - printf "The patch was applied and the changed files staged. You can now commit.\n\n"; - break - ;; - * ) echo "Please answer yes or no." - ;; - esac -done -exit 1 # we don't commit in any case +# This script does not do anything anymore; +# it is kept to avoid breaking existing git hooks. +exit 0 From 5c97c470a033d643c22018341b760269fbf5a81a Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 9 Jan 2024 14:48:58 +0200 Subject: [PATCH 2/6] Replace hand-written pre-commit-make-rst hook with pre-commit --- .pre-commit-config.yaml | 7 +++++++ misc/hooks/pre-commit-make-rst | 14 +++----------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1d2605d5286c..106d482a24ba 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,3 +13,10 @@ repos: .*platform/android/java/lib/src/com.*| .*-so_wrap.* ) + + - id: make-rst + name: make-rst + entry: python3 doc/tools/make_rst.py doc/classes modules platform --dry-run --color + pass_filenames: false + language: python + files: ^(doc|modules|platform).*xml$ diff --git a/misc/hooks/pre-commit-make-rst b/misc/hooks/pre-commit-make-rst index 3737272a6dc0..cc3917b0dede 100755 --- a/misc/hooks/pre-commit-make-rst +++ b/misc/hooks/pre-commit-make-rst @@ -1,12 +1,4 @@ #!/usr/bin/env bash - -# Git pre-commit hook that checks the class reference syntax using make_rst.py. - -# Workaround because we can't execute the .py file directly on windows -PYTHON=python -py_ver=$($PYTHON -c "import sys; print(sys.version_info.major)") -if [[ "$py_ver" != "3" ]]; then - PYTHON+=3 -fi - -$PYTHON doc/tools/make_rst.py doc/classes modules platform --dry-run --color +# This script does not do anything anymore; +# it is kept to avoid breaking existing git hooks. +exit 0 From 5a0084c7f42bde24055aabf1ebd265ee87d96413 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 9 Jan 2024 14:50:13 +0200 Subject: [PATCH 3/6] Replace hand-written pre-commit-black hook with pre-commit --- .pre-commit-config.yaml | 13 +++ misc/hooks/pre-commit-black | 221 +----------------------------------- 2 files changed, 16 insertions(+), 218 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 106d482a24ba..a9465c8e6349 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,3 +20,16 @@ repos: pass_filenames: false language: python files: ^(doc|modules|platform).*xml$ + + - repo: https://github.com/psf/black-pre-commit-mirror # mypyc-compiled black + rev: 23.3.0 + hooks: + - id: black + exclude: .*thirdparty.* + args: + - --line-length=120 + + - id: black + files: SConstruct|SCsub + args: + - --line-length=120 diff --git a/misc/hooks/pre-commit-black b/misc/hooks/pre-commit-black index bbad6a690a92..cc3917b0dede 100755 --- a/misc/hooks/pre-commit-black +++ b/misc/hooks/pre-commit-black @@ -1,219 +1,4 @@ #!/usr/bin/env bash - -# git pre-commit hook that runs a black stylecheck. -# Based on pre-commit-clang-format. - -################################################################## -# SETTINGS -# Set path to black binary. -BLACK=`which black 2>/dev/null` -BLACK_OPTIONS="-l 120" - -# Remove any older patches from previous commits. Set to true or false. -DELETE_OLD_PATCHES=false - -# File types to parse. -FILE_NAMES="SConstruct SCsub" -FILE_EXTS=".py" - -# Use pygmentize instead of cat to parse diff with highlighting. -# Install it with `pip install pygments` (Linux) or `easy_install Pygments` (Mac) -PYGMENTIZE=`which pygmentize 2>/dev/null` -if [ ! -z "$PYGMENTIZE" ]; then - READER="pygmentize -l diff" -else - READER=cat -fi - -# Path to zenity -ZENITY=`which zenity 2>/dev/null` - -# Path to xmessage -XMSG=`which xmessage 2>/dev/null` - -# Path to powershell (Windows only) -PWSH=`which powershell 2>/dev/null` - -# Path to osascript (macOS only) -OSA=`which osascript 2>/dev/null` - -################################################################## -# There should be no need to change anything below this line. - -. "$(dirname -- "$0")/canonicalize_filename.sh" - -# exit on error -set -e - -# check whether the given file matches any of the set extensions -matches_name_or_extension() { - local filename=$(basename "$1") - local extension=".${filename##*.}" - - for name in $FILE_NAMES; do [[ "$name" == "$filename" ]] && return 0; done - for ext in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done - - return 1 -} - -# necessary check for initial commit -if git rev-parse --verify HEAD >/dev/null 2>&1 ; then - against=HEAD -else - # Initial commit: diff against an empty tree object - against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 -fi - -if [ ! -x "$BLACK" ] ; then - if [ ! -t 1 ] ; then - if [ -x "$ZENITY" ] ; then - $ZENITY --error --title="Error" --text="Error: black executable not found." - exit 1 - elif [ -x "$XMSG" ] ; then - $XMSG -center -title "Error" "Error: black executable not found." - exit 1 - elif [ -x "$OSA" ] ; then - asmessage="$(canonicalize_filename "$(dirname -- "$0")/asmessage.applescript")" - $OSA "$asmessage" -center -title "Error" --text "Error: black executable not found." - exit 1 - elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then - winmessage="$(canonicalize_filename "$(dirname -- "$0")/winmessage.ps1")" - $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -center -title "Error" --text "Error: black executable not found." - exit 1 - fi - fi - printf "Error: black executable not found.\n" - printf "Set the correct path in $(canonicalize_filename "$0").\n" - exit 1 -fi - -# create a random filename to store our generated patch -prefix="pre-commit-black" -suffix="$(date +%s)" -patch="/tmp/$prefix-$suffix.patch" - -# clean up any older black patches -$DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch - -# create one patch containing all changes to the files -git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file; -do - # ignore thirdparty files - if grep -q "thirdparty" <<< $file; then - continue; - fi - - # ignore file if not one of the names or extensions we handle - if ! matches_name_or_extension "$file"; then - continue; - fi - - # format our file with black, create a patch with diff and append it to our $patch - # The sed call is necessary to transform the patch from - # --- $file timestamp - # +++ $file timestamp - # to both lines working on the same file and having a/ and b/ prefix. - # Else it can not be applied with 'git apply'. - "$BLACK" "$BLACK_OPTIONS" --diff "$file" | \ - sed -e "1s|--- |--- a/|" -e "2s|+++ |+++ b/|" >> "$patch" -done - -# if no patch has been generated all is ok, clean up the file stub and exit -if [ ! -s "$patch" ] ; then - printf "Files in this commit comply with the black formatter rules.\n" - rm -f "$patch" - exit 0 -fi - -# a patch has been created, notify the user and exit -printf "\nThe following differences were found between the code to commit " -printf "and the black formatter rules:\n\n" - -if [ -t 1 ] ; then - $READER "$patch" - printf "\n" - # Allows us to read user input below, assigns stdin to keyboard - exec < /dev/tty - terminal="1" -else - cat "$patch" - printf "\n" - # Allows non zero zenity/powershell output - set +e - terminal="0" -fi - -while true; do - if [ $terminal = "0" ] ; then - if [ -x "$ZENITY" ] ; then - choice=$($ZENITY --text-info --filename="$patch" --width=800 --height=600 --title="Do you want to apply that patch?" --ok-label="Apply" --cancel-label="Do not apply" --extra-button="Apply and stage") - if [ "$?" = "0" ] ; then - yn="Y" - else - if [ "$choice" = "Apply and stage" ] ; then - yn="S" - else - yn="N" - fi - fi - elif [ -x "$XMSG" ] ; then - $XMSG -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?" - choice=$? - if [ "$choice" = "100" ] ; then - yn="Y" - elif [ "$choice" = "200" ] ; then - yn="S" - else - yn="N" - fi - elif [ -x "$OSA" ] ; then - asmessage="$(canonicalize_filename "$(dirname -- "$0")/asmessage.applescript")" - choice=`$OSA "$asmessage" -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?"` - if [ "$choice" = "100" ] ; then - yn="Y" - elif [ "$choice" = "200" ] ; then - yn="S" - else - yn="N" - fi - elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then - winmessage="$(canonicalize_filename "$(dirname -- "$0")/winmessage.ps1")" - $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?" - choice=$? - if [ "$choice" = "100" ] ; then - yn="Y" - elif [ "$choice" = "200" ] ; then - yn="S" - else - yn="N" - fi - else - printf "Error: zenity, xmessage, osascript, or powershell executable not found.\n" - exit 1 - fi - else - read -p "Do you want to apply that patch (Y - Apply, N - Do not apply, S - Apply and stage files)? [Y/N/S] " yn - fi - case $yn in - [Yy] ) git apply $patch; - printf "The patch was applied. You can now stage the changes and commit again.\n\n"; - break - ;; - [Nn] ) printf "\nYou can apply these changes with:\n git apply $patch\n"; - printf "(may need to be called from the root directory of your repository)\n"; - printf "Aborting commit. Apply changes and commit again or skip checking with"; - printf " --no-verify (not recommended).\n\n"; - break - ;; - [Ss] ) git apply $patch; - git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file; - do git add $file; - done - printf "The patch was applied and the changed files staged. You can now commit.\n\n"; - break - ;; - * ) echo "Please answer yes or no." - ;; - esac -done -exit 1 # we don't commit in any case +# This script does not do anything anymore; +# it is kept to avoid breaking existing git hooks. +exit 0 From 8f292289f6b11afb8da33cf75ff9f2951d92e0a6 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 9 Jan 2024 14:54:12 +0200 Subject: [PATCH 4/6] Remove vestigial bits from misc/hooks --- misc/hooks/README.md | 42 ------------ misc/hooks/asmessage.applescript | 59 ---------------- misc/hooks/canonicalize_filename.sh | 48 ------------- misc/hooks/pre-commit | 52 +------------- misc/hooks/winmessage.ps1 | 103 ---------------------------- 5 files changed, 3 insertions(+), 301 deletions(-) delete mode 100644 misc/hooks/README.md delete mode 100644 misc/hooks/asmessage.applescript delete mode 100755 misc/hooks/canonicalize_filename.sh delete mode 100644 misc/hooks/winmessage.ps1 diff --git a/misc/hooks/README.md b/misc/hooks/README.md deleted file mode 100644 index 573f8fe350b6..000000000000 --- a/misc/hooks/README.md +++ /dev/null @@ -1,42 +0,0 @@ -# Git hooks for Godot Engine - -This folder contains Git hooks meant to be installed locally by Godot Engine -contributors to make sure they comply with our requirements. - -## List of hooks - -- Pre-commit hook for `clang-format`: Applies `clang-format` to the staged - files before accepting a commit; blocks the commit and generates a patch if - the style is not respected. - You may need to edit the file if your `clang-format` binary is not in the - `PATH`, or if you want to enable colored output with `pygmentize`. -- Pre-commit hook for `black`: Applies `black` to the staged Python files - before accepting a commit. -- Pre-commit hook for `make_rst`: Checks the class reference syntax using - `make_rst.py`. - -## Installation - -Copy all the files from this folder into your `.git/hooks` folder, and make -sure the hooks and helper scripts are executable. - -#### Linux/macOS - -The hooks rely on bash scripts and tools which should be in the system `PATH`, -so they should work out of the box on Linux/macOS. - -#### Windows - -##### clang-format -- Download LLVM for Windows (version 13 or later) from - -- Make sure LLVM is added to the `PATH` during installation - -##### black -- Python installation: make sure Python is added to the `PATH` -- Install `black` - in any console: `pip3 install black` - -## Custom hooks - -The pre-commit hook will run any other script in `.git/hooks` whose filename -matches `pre-commit-custom-*`, after the Godot ones. diff --git a/misc/hooks/asmessage.applescript b/misc/hooks/asmessage.applescript deleted file mode 100644 index 15ba94dc37a4..000000000000 --- a/misc/hooks/asmessage.applescript +++ /dev/null @@ -1,59 +0,0 @@ -on run argv - set vButtons to { "OK" } - set vButtonCodes to { 0 } - set vDbutton to "OK" - set vText to "" - set vTitle to "" - set vTimeout to -1 - - repeat with i from 1 to length of argv - try - set vArg to item i of argv - if vArg = "-buttons" then - set vButtonsAndCodes to my fSplit(item (i + 1) of argv, ",") - set vButtons to {} - set vButtonCodes to {} - repeat with j from 1 to length of vButtonsAndCodes - set vBtn to my fSplit(item j of vButtonsAndCodes, ":") - copy (item 1 of vBtn) to the end of the vButtons - copy (item 2 of vBtn) to the end of the vButtonCodes - end repeat - else if vArg = "-title" then - set vTitle to item (i + 1) of argv - else if vArg = "-center" then - -- not supported - else if vArg = "-default" then - set vDbutton to item (i + 1) of argv - else if vArg = "-geometry" then - -- not supported - else if vArg = "-nearmouse" then - -- not supported - else if vArg = "-timeout" then - set vTimeout to item (i + 1) of argv as integer - else if vArg = "-file" then - set vText to read (item (i + 1) of argv) as string - else if vArg = "-text" then - set vText to item (i + 1) of argv - end if - end try - end repeat - - set vDlg to display dialog vText buttons vButtons default button vDbutton with title vTitle giving up after vTimeout with icon stop - set vRet to button returned of vDlg - repeat with i from 1 to length of vButtons - set vBtn to item i of vButtons - if vBtn = vRet - return item i of vButtonCodes - end if - end repeat - - return 0 -end run - -on fSplit(vString, vDelimiter) - set oldDelimiters to AppleScript's text item delimiters - set AppleScript's text item delimiters to vDelimiter - set vArray to every text item of vString - set AppleScript's text item delimiters to oldDelimiters - return vArray -end fSplit diff --git a/misc/hooks/canonicalize_filename.sh b/misc/hooks/canonicalize_filename.sh deleted file mode 100755 index fe66999d8ca8..000000000000 --- a/misc/hooks/canonicalize_filename.sh +++ /dev/null @@ -1,48 +0,0 @@ -#!/bin/sh - -# Provide the canonicalize filename (physical filename with out any symlinks) -# like the GNU version readlink with the -f option regardless of the version of -# readlink (GNU or BSD). - -# This file is part of a set of unofficial pre-commit hooks available -# at github. -# Link: https://github.com/githubbrowser/Pre-commit-hooks -# Contact: David Martin, david.martin.mailbox@googlemail.com - -########################################################### -# There should be no need to change anything below this line. - -# Canonicalize by recursively following every symlink in every component of the -# specified filename. This should reproduce the results of the GNU version of -# readlink with the -f option. -# -# Reference: https://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac -canonicalize_filename () { - local target_file="$1" - local physical_directory="" - local result="" - - # Need to restore the working directory after work. - local working_dir="`pwd`" - - cd -- "$(dirname -- "$target_file")" - target_file="$(basename -- "$target_file")" - - # Iterate down a (possible) chain of symlinks - while [ -L "$target_file" ] - do - target_file="$(readlink -- "$target_file")" - cd -- "$(dirname -- "$target_file")" - target_file="$(basename -- "$target_file")" - done - - # Compute the canonicalized name by finding the physical path - # for the directory we're in and appending the target file. - physical_directory="`pwd -P`" - result="$physical_directory/$target_file" - - # restore the working directory after work. - cd -- "$working_dir" - - echo "$result" -} diff --git a/misc/hooks/pre-commit b/misc/hooks/pre-commit index 63591612607d..dcd98ed4ae81 100755 --- a/misc/hooks/pre-commit +++ b/misc/hooks/pre-commit @@ -1,50 +1,4 @@ #!/bin/sh -# Git pre-commit hook that runs multiple hooks specified in $HOOKS. -# Make sure this script is executable. Bypass hooks with git commit --no-verify. - -# This file is part of a set of unofficial pre-commit hooks available -# at github. -# Link: https://github.com/githubbrowser/Pre-commit-hooks -# Contact: David Martin, david.martin.mailbox@googlemail.com - - -########################################################### -# CONFIGURATION: -# pre-commit hooks to be executed. They should be in the same .git/hooks/ folder -# as this script. Hooks should return 0 if successful and nonzero to cancel the -# commit. They are executed in the order in which they are listed. -HOOKS="pre-commit-clang-format pre-commit-black pre-commit-make-rst" -HOOKS="$HOOKS $(find $(dirname -- "$0") -type f -name 'pre-commit-custom-*' -exec basename {} \;)" -########################################################### -# There should be no need to change anything below this line. - -. "$(dirname -- "$0")/canonicalize_filename.sh" - -# exit on error -set -e - -# Absolute path to this script, e.g. /home/user/bin/foo.sh -SCRIPT="$(canonicalize_filename "$0")" - -# Absolute path this script is in, thus /home/user/bin -SCRIPTPATH="$(dirname -- "$SCRIPT")" - - -for hook in $HOOKS -do - echo "Running hook: $hook" - # run hook if it exists - # if it returns with nonzero exit with 1 and thus abort the commit - if [ -f "$SCRIPTPATH/$hook" ]; then - "$SCRIPTPATH/$hook" - if [ $? != 0 ]; then - exit 1 - fi - else - echo "Error: file $hook not found." - echo "Aborting commit. Make sure the hook is in $SCRIPTPATH and executable." - echo "You can disable it by removing it from the list in $SCRIPT." - echo "You can skip all pre-commit hooks with --no-verify (not recommended)." - exit 1 - fi -done +# This script does not do anything anymore; +# please follow https://pre-commit.com/#installation instructions. +exit 0 diff --git a/misc/hooks/winmessage.ps1 b/misc/hooks/winmessage.ps1 deleted file mode 100644 index 3672579544e2..000000000000 --- a/misc/hooks/winmessage.ps1 +++ /dev/null @@ -1,103 +0,0 @@ -Param ( - [string]$file = "", - [string]$text = "", - [string]$buttons = "OK:0", - [string]$default = "", - [switch]$nearmouse = $false, - [switch]$center = $false, - [string]$geometry = "", - [int32]$timeout = 0, - [string]$title = "Message" -) -Add-Type -assembly System.Windows.Forms - -$global:Result = 0 - -$main_form = New-Object System.Windows.Forms.Form -$main_form.Text = $title - -$geometry_data = $geometry.Split("+") -if ($geometry_data.Length -ge 1) { - $size_data = $geometry_data[0].Split("x") - if ($size_data.Length -eq 2) { - $main_form.Width = $size_data[0] - $main_form.Height = $size_data[1] - } -} -if ($geometry_data.Length -eq 3) { - $main_form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual - $main_form.Location = New-Object System.Drawing.Point($geometry_data[1], $geometry_data[2]) -} -if ($nearmouse) { - $main_form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual - $main_form.Location = System.Windows.Forms.Cursor.Position -} -if ($center) { - $main_form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen -} - -$main_form.SuspendLayout() - -$button_panel = New-Object System.Windows.Forms.FlowLayoutPanel -$button_panel.SuspendLayout() -$button_panel.FlowDirection = [System.Windows.Forms.FlowDirection]::RightToLeft -$button_panel.Dock = [System.Windows.Forms.DockStyle]::Bottom -$button_panel.Autosize = $true - -if ($file -ne "") { - $text = [IO.File]::ReadAllText($file).replace("`n", "`r`n") -} - -if ($text -ne "") { - $text_box = New-Object System.Windows.Forms.TextBox - $text_box.Multiline = $true - $text_box.ReadOnly = $true - $text_box.Autosize = $true - $text_box.Text = $text - $text_box.Select(0,0) - $text_box.Dock = [System.Windows.Forms.DockStyle]::Fill - $main_form.Controls.Add($text_box) -} - -$buttons_array = $buttons.Split(",") -foreach ($button in $buttons_array) { - $button_data = $button.Split(":") - $button_ctl = New-Object System.Windows.Forms.Button - if ($button_data.Length -eq 2) { - $button_ctl.Tag = $button_data[1] - } else { - $button_ctl.Tag = 100 + $buttons_array.IndexOf($button) - } - if ($default -eq $button_data[0]) { - $main_form.AcceptButton = $button_ctl - } - $button_ctl.Autosize = $true - $button_ctl.Text = $button_data[0] - $button_ctl.Add_Click( - { - Param($sender) - $global:Result = $sender.Tag - $main_form.Close() - } - ) - $button_panel.Controls.Add($button_ctl) -} -$main_form.Controls.Add($button_panel) - -$button_panel.ResumeLayout($false) -$main_form.ResumeLayout($false) - -if ($timeout -gt 0) { - $timer = New-Object System.Windows.Forms.Timer - $timer.Add_Tick( - { - $global:Result = 0 - $main_form.Close() - } - ) - $timer.Interval = $timeout - $timer.Start() -} -$dlg_res = $main_form.ShowDialog() - -[Environment]::Exit($global:Result) From e8aa958e89405c9ea8621a1eb7becb56ce5d83aa Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 9 Jan 2024 15:00:01 +0200 Subject: [PATCH 5/6] Add copyright header check to pre-commit-config --- .pre-commit-config.yaml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a9465c8e6349..32a60783709a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -21,6 +21,22 @@ repos: language: python files: ^(doc|modules|platform).*xml$ + - id: copyright-headers + name: copyright-headers + language: python + files: \.(c|h|cpp|hpp|cc|cxx|m|mm|inc|java)$ + entry: python3 misc/scripts/copyright_headers.py + exclude: | + (?x)^( + tests/python_build.*| + .*thirdparty.*| + .*platform/android/java/lib/src/com.*| + .*-so_wrap.*| + platform/android/java/lib/src/org/godotengine/godot/gl/GLSurfaceView.*| + platform/android/java/lib/src/org/godotengine/godot/gl/EGLLogWrapper.*| + platform/android/java/lib/src/org/godotengine/godot/utils/ProcessPhoenix.* + ) + - repo: https://github.com/psf/black-pre-commit-mirror # mypyc-compiled black rev: 23.3.0 hooks: From 0e9f3ec1c369682d5f07f85da18a67caf8e09c37 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 9 Jan 2024 15:04:06 +0200 Subject: [PATCH 6/6] CI: Use `pre-commit/action` to run checks in GitHub Actions --- .github/workflows/static_checks.yml | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/.github/workflows/static_checks.yml b/.github/workflows/static_checks.yml index e7ebd2fb64e3..3a2a37a696e3 100644 --- a/.github/workflows/static_checks.yml +++ b/.github/workflows/static_checks.yml @@ -54,14 +54,6 @@ jobs: run: | bash ./misc/scripts/header_guards.sh changed.txt - - name: Python style checks via black (black_format.sh) - run: | - if grep -qE '\.py$|SConstruct|SCsub' changed.txt || [ -z "$(cat changed.txt)" ]; then - bash ./misc/scripts/black_format.sh - else - echo "Skipping Python formatting as no Python files were changed." - fi - - name: Python scripts static analysis (mypy_check.sh) run: | if grep -qE '\.py$|SConstruct|SCsub' changed.txt || [ -z "$(cat changed.txt)" ]; then @@ -92,12 +84,6 @@ jobs: - name: Documentation checks run: | doc/tools/doc_status.py doc/classes modules/*/doc_classes platform/*/doc_classes - doc/tools/make_rst.py --dry-run --color doc/classes modules platform - - - name: Style checks via clang-format (clang_format.sh) - run: | - clang-format --version - bash ./misc/scripts/clang_format.sh changed.txt - name: Style checks via dotnet format (dotnet_format.sh) run: | @@ -114,3 +100,6 @@ jobs: skip: "./bin,./thirdparty,*.desktop,*.gen.*,*.po,*.pot,*.rc,./AUTHORS.md,./COPYRIGHT.txt,./DONORS.md,./core/input/gamecontrollerdb.txt,./core/string/locales.h,./editor/project_converter_3_to_4.cpp,./misc/scripts/codespell.sh,./platform/android/java/lib/src/com,./platform/web/node_modules,./platform/web/package-lock.json" ignore_words_list: "breaked,curvelinear,doubleclick,expct,findn,gird,hel,inout,lod,mis,nd,numer,ot,requestor,te,vai" path: ${{ env.CHANGED_FILES }} + + - name: Other checks via pre-commit + uses: pre-commit/action@v3.0.0