mirror of
https://github.com/godotengine/godot.git
synced 2024-11-21 19:42:43 +00:00
Replace hand-written pre-commit hooks with pre-commit
Python tool
`pre-commit` can be installed with pip, and configured in the Godot repo with `pre-commit install`. It can then easily be run both locally with `pre-commit run`, and on CI, in a cross-platform way. This makes it much easier for contributors to set up pre-commit hooks, without having to manually copy files to their git folder. Co-authored-by: Rémi Verschelde <rverschelde@gmail.com>
This commit is contained in:
parent
bb6b06c813
commit
20c563de40
19
.github/workflows/static_checks.yml
vendored
19
.github/workflows/static_checks.yml
vendored
@ -23,7 +23,7 @@ jobs:
|
||||
|
||||
- name: Install Python dependencies and general setup
|
||||
run: |
|
||||
pip3 install black==23.3.0 pytest==7.1.2 mypy==0.971
|
||||
pip3 install pytest==7.1.2 mypy==0.971
|
||||
git config diff.wsErrorHighlight all
|
||||
|
||||
- name: Get changed files
|
||||
@ -46,6 +46,9 @@ jobs:
|
||||
run: |
|
||||
bash ./misc/scripts/gitignore_check.sh
|
||||
|
||||
- name: Style checks via pre-commit
|
||||
uses: pre-commit/action@v3.0.1
|
||||
|
||||
- name: File formatting checks (file_format.sh)
|
||||
run: |
|
||||
bash ./misc/scripts/file_format.sh changed.txt
|
||||
@ -54,14 +57,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 +87,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: |
|
||||
|
49
.pre-commit-config.yaml
Normal file
49
.pre-commit-config.yaml
Normal file
@ -0,0 +1,49 @@
|
||||
repos:
|
||||
- repo: https://github.com/pre-commit/mirrors-clang-format
|
||||
rev: v16.0.6
|
||||
hooks:
|
||||
- id: clang-format
|
||||
files: \.(c|h|cpp|hpp|cc|cxx|m|mm|inc|java|glsl)$
|
||||
types_or: [text]
|
||||
exclude: |
|
||||
(?x)^(
|
||||
tests/python_build.*|
|
||||
.*thirdparty.*|
|
||||
.*platform/android/java/lib/src/com.*|
|
||||
.*-so_wrap.*
|
||||
)
|
||||
|
||||
- repo: https://github.com/psf/black-pre-commit-mirror
|
||||
rev: 23.3.0
|
||||
hooks:
|
||||
- id: black
|
||||
files: (\.py$|SConstruct|SCsub)
|
||||
types_or: [text]
|
||||
exclude: .*thirdparty.*
|
||||
args:
|
||||
- --line-length=120
|
||||
|
||||
- repo: local
|
||||
hooks:
|
||||
- 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$
|
||||
|
||||
- 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.*
|
||||
)
|
@ -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
|
||||
<https://github.com/llvm/llvm-project/releases>
|
||||
- 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.
|
@ -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
|
@ -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"
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
#!/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
|
@ -1,219 +0,0 @@
|
||||
#!/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
|
@ -1,262 +0,0 @@
|
||||
#!/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
|
@ -1,12 +0,0 @@
|
||||
#!/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
|
@ -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)
|
@ -1,26 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# This script runs black on all Python files in the repo.
|
||||
|
||||
set -uo pipefail
|
||||
|
||||
# Apply black.
|
||||
echo -e "Formatting Python files..."
|
||||
PY_FILES=$(git ls-files -- '*SConstruct' '*SCsub' '*.py' ':!:.git/*' ':!:thirdparty/*')
|
||||
black -l 120 $PY_FILES
|
||||
|
||||
diff=$(git diff --color)
|
||||
|
||||
# If no diff has been generated all is OK, clean up, and exit.
|
||||
if [ -z "$diff" ] ; then
|
||||
printf "\e[1;32m*** Files in this commit comply with the black style rules.\e[0m\n"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# A diff has been created, notify the user, clean up, and exit.
|
||||
printf "\n\e[1;33m*** The following changes must be made to comply with the formatting rules:\e[0m\n\n"
|
||||
# Perl commands replace trailing spaces with `·` and tabs with `<TAB>`.
|
||||
printf "%s\n" "$diff" | perl -pe 's/(.*[^ ])( +)(\e\[m)$/my $spaces="·" x length($2); sprintf("$1$spaces$3")/ge' | perl -pe 's/(.*[^\t])(\t+)(\e\[m)$/my $tabs="<TAB>" x length($2); sprintf("$1$tabs$3")/ge'
|
||||
|
||||
printf "\n\e[1;91m*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\e[0m\n"
|
||||
exit 1
|
@ -1,53 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# This script runs clang-format and fixes copyright headers on all relevant files in the repo.
|
||||
# This is the primary script responsible for fixing style violations.
|
||||
|
||||
set -uo pipefail
|
||||
|
||||
if [ $# -eq 0 ]; then
|
||||
# Loop through all code files tracked by Git.
|
||||
files=$(git ls-files -- '*.c' '*.h' '*.cpp' '*.hpp' '*.cc' '*.hh' '*.cxx' '*.m' '*.mm' '*.inc' '*.java' '*.glsl' \
|
||||
':!:.git/*' ':!:thirdparty/*' ':!:*/thirdparty/*' ':!:platform/android/java/lib/src/com/google/*' \
|
||||
':!:*-so_wrap.*' ':!:tests/python_build/*')
|
||||
else
|
||||
# $1 should be a file listing file paths to process. Used in CI.
|
||||
files=$(cat "$1" | grep -v "thirdparty/" | grep -E "\.(c|h|cpp|hpp|cc|hh|cxx|m|mm|inc|java|glsl)$" | grep -v "platform/android/java/lib/src/com/google/" | grep -v "\-so_wrap\." | grep -v "tests/python_build/")
|
||||
fi
|
||||
|
||||
if [ ! -z "$files" ]; then
|
||||
clang-format --Wno-error=unknown -i $files
|
||||
fi
|
||||
|
||||
# Fix copyright headers, but not all files get them.
|
||||
for f in $files; do
|
||||
if [[ "$f" == *"inc" && "$f" != *"compat.inc" ]]; then
|
||||
continue
|
||||
elif [[ "$f" == *"glsl" ]]; then
|
||||
continue
|
||||
elif [[ "$f" == "platform/android/java/lib/src/org/godotengine/godot/gl/GLSurfaceView"* ]]; then
|
||||
continue
|
||||
elif [[ "$f" == "platform/android/java/lib/src/org/godotengine/godot/gl/EGLLogWrapper"* ]]; then
|
||||
continue
|
||||
elif [[ "$f" == "platform/android/java/lib/src/org/godotengine/godot/utils/ProcessPhoenix"* ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
python misc/scripts/copyright_headers.py "$f"
|
||||
done
|
||||
|
||||
diff=$(git diff --color)
|
||||
|
||||
# If no diff has been generated all is OK, clean up, and exit.
|
||||
if [ -z "$diff" ] ; then
|
||||
printf "\e[1;32m*** Files in this commit comply with the clang-format style rules.\e[0m\n"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# A diff has been created, notify the user, clean up, and exit.
|
||||
printf "\n\e[1;33m*** The following changes must be made to comply with the formatting rules:\e[0m\n\n"
|
||||
# Perl commands replace trailing spaces with `·` and tabs with `<TAB>`.
|
||||
printf "%s\n" "$diff" | perl -pe 's/(.*[^ ])( +)(\e\[m)$/my $spaces="·" x length($2); sprintf("$1$spaces$3")/ge' | perl -pe 's/(.*[^\t])(\t+)(\e\[m)$/my $tabs="<TAB>" x length($2); sprintf("$1$tabs$3")/ge'
|
||||
|
||||
printf "\n\e[1;91m*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\e[0m\n"
|
||||
exit 1
|
Loading…
Reference in New Issue
Block a user