Add Pre-Commit Hooks (#212)
Pre-commit hooks for:
- clang-format
- cmake-format
- copyright header check
- YAML check
- Trailing whitespace and end-of-file newline check
---------
Co-authored-by: David Galiffi <David.Galiffi@amd.com>
Co-authored-by: Peter Park <git@peterjunpark.com>
[ROCm/rocprofiler-systems commit: 54e91f1c2d]
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
ef90bab236
Коммит
07bb699993
@@ -0,0 +1,71 @@
|
||||
# MIT License
|
||||
#
|
||||
# Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
|
||||
#
|
||||
# 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.
|
||||
|
||||
# Pre-configuration file for pre-commit hooks
|
||||
# This is optional. To run pre-commit, see CONTRIBUTING.md
|
||||
|
||||
exclude: \.(svg)$|(^|/)\.gitignore$ # Exclude files with these extensions
|
||||
default_stages: [pre-commit]
|
||||
repos:
|
||||
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v5.0.0
|
||||
hooks:
|
||||
- id: check-yaml # Check YAML files for syntax errors
|
||||
- id: trailing-whitespace # Remove trailing whitespace
|
||||
- id: end-of-file-fixer # Fix files to have a newline at the end
|
||||
|
||||
- repo: https://github.com/pre-commit/mirrors-clang-format
|
||||
rev: v11.1.0 # Version 11 as specified in contributor guide
|
||||
hooks:
|
||||
- id: clang-format
|
||||
files: \.(c|cpp|h.*)$
|
||||
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: check-cmake
|
||||
name: cmake-format
|
||||
entry: ./scripts/check-cmake-format.sh
|
||||
language: script
|
||||
files: (^|/)(CMakeLists\.txt|.*\.cmake)$
|
||||
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: check-copyright
|
||||
name: copyright-detector
|
||||
require_serial: true # Slightly slower, but prevents hook running script many times
|
||||
entry: ./scripts/check-copyright.sh
|
||||
language: script
|
||||
files: \.(c|h|txt|cpp|hpp|py)$
|
||||
exclude: ^\.|^docs/|^examples/lulesh/|^examples/mpi/|^examples/openmp/|^external/|^cmake/
|
||||
|
||||
# - repo: local
|
||||
# hooks:
|
||||
# - id: check-copyright-date
|
||||
# name: Check Copyright Date
|
||||
# # Check copyright date in all files
|
||||
# # Fails if something is out of date.
|
||||
# # -u automatically updates copyright year
|
||||
# entry: ./scripts/check-copyright.sh -u
|
||||
# language: script
|
||||
# files: \.(c|h|txt|cpp|hpp|py)$
|
||||
# exclude: ^\.|^docs/|^examples/lulesh/|^examples/mpi/|^examples/openmp/|^external/|^cmake/
|
||||
@@ -79,6 +79,24 @@ By creating a pull request, you agree to the statements made in the [code licens
|
||||
* Group related files together and maintain a logical hierarchy.
|
||||
* Use `clang-format-11` and `cmake-format` formatters to ensure consistency.
|
||||
|
||||
### Using pre-commit hooks ###
|
||||
|
||||
Our project supports optional [*pre-commit hooks*](https://pre-commit.com/#introduction) which developers can leverage to verify formatting before publishing their code. Once enabled, any commits you propose to the repository will be automatically checked for formatting. Initial setup is as follows:
|
||||
|
||||
```shell
|
||||
pip install pre-commit # or: apt-get install pre-commit
|
||||
cd rocprofiler-systems
|
||||
pre-commit install
|
||||
```
|
||||
|
||||
**Note:** pre-commit version **3.0.0 or higher** is required.
|
||||
|
||||
Now, when you commit code to the repository you should see something like this:
|
||||
|
||||

|
||||
|
||||
Please see the [pre-commit documentation](https://pre-commit.com/#quick-start) for additional information.
|
||||
|
||||
## Code License ##
|
||||
|
||||
All code contributed to this project will be licensed under the license identified in the [License](LICENSE). Your contribution will be accepted under the same license.
|
||||
|
||||
Двоичный файл не отображается.
|
После Ширина: | Высота: | Размер: 18 KiB |
Исполняемый файл
+39
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# MIT License
|
||||
#
|
||||
# Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
|
||||
#
|
||||
# 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.
|
||||
|
||||
if ! command -v cmake-format &> /dev/null; then
|
||||
echo "cmake-format could not be found. Please install it with 'pip install cmake-format' or 'apt install cmake-format'."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
for file in "$@"; do
|
||||
# Run cmake-format in-place
|
||||
cmake-format -i "$file"
|
||||
# Check if the file has changes using git diff
|
||||
if ! git diff --quiet -- "$file"; then
|
||||
echo "Formatted: $file"
|
||||
fi
|
||||
done
|
||||
|
||||
exit 0
|
||||
Исполняемый файл
+91
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# MIT License
|
||||
#
|
||||
# Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
|
||||
#
|
||||
# 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.
|
||||
|
||||
expected_copyright=("Copyright (c)" ".COPYRIGHT")
|
||||
files_with_missing_copyright=()
|
||||
files=("$@")
|
||||
|
||||
if [[ "$ALLOW_MISSING_COPYRIGHT" == "1" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
for file in "${files[@]}"; do
|
||||
if [[ -f "$file" ]]; then
|
||||
found=0
|
||||
for pattern in "${expected_copyright[@]}"; do
|
||||
if grep -Fq "$pattern" "$file"; then
|
||||
found=1
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [[ $found -eq 0 ]]; then
|
||||
files_with_missing_copyright+=("$file")
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
if [ ${#files_with_missing_copyright[@]} -ne 0 ]; then
|
||||
if [[ "$ADD_COPYRIGHT" == "1" ]]; then
|
||||
for file in "${files_with_missing_copyright[@]}"; do
|
||||
# Determine the comment style based on the file extension
|
||||
if [[ "$file" == *.c || "$file" == *.cpp || "$file" == *.h || "$file" == *.hpp ]]; then
|
||||
comS="//"
|
||||
else
|
||||
comS="#"
|
||||
fi
|
||||
|
||||
# Read LICENSE file and prepend comment prefix to each line
|
||||
copyright_notice=""
|
||||
while IFS= read -r line; do
|
||||
if [[ -n "$line" ]]; then
|
||||
copyright_notice+="$comS $line"$'\n'
|
||||
else
|
||||
copyright_notice+="$comS"$'\n'
|
||||
fi
|
||||
done < LICENSE
|
||||
|
||||
# Add the notice to the beginning of the file
|
||||
temp_file=$(mktemp)
|
||||
{
|
||||
echo -e "$copyright_notice"
|
||||
cat "$file"
|
||||
} > "$temp_file"
|
||||
mv "$temp_file" "$file"
|
||||
done
|
||||
echo "Copyright notices added."
|
||||
exit 1
|
||||
fi
|
||||
echo "The following files are missing a valid copyright notice:"
|
||||
echo ""
|
||||
for file in "${files_with_missing_copyright[@]}"; do
|
||||
echo "$file"
|
||||
done
|
||||
echo ""
|
||||
echo "It may be the case that the copyright is not required by some files."
|
||||
echo "To override this check, set the environment variable ALLOW_MISSING_COPYRIGHT=1"
|
||||
echo "To add the copyright to all files listed above, set the environment variable ADD_COPYRIGHT=1"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
exit 0
|
||||
Ссылка в новой задаче
Block a user