ci(post-commit): updated to track post-commit

This commit is contained in:
2025-03-17 13:31:54 -04:00
parent 4d12b6beb2
commit 7568eb1a46
3 changed files with 64 additions and 3 deletions

View File

@@ -4,12 +4,18 @@ All 4DSSE files should have a header specifying authors, last edits, copyright,
Hook should be setup automatically to run on each commit.
## Dependencies
- GitPython
- Python (>=3.10)
## Usage
This script assumes that an enviromental variable has been set which points to the root of the 4DSSE project. This is used to retreive the git log so that author information and last update date information may be read. By default this uses the enviromental variable `_4DSSE_ROOT`; however, this may be changed with the `--root` command line flag. Whatever is set as `--root` will be the name of the enviromental variable this script will search for.
## Installation
Generally one is intended to use this by copying `post-commit` into the `.git/hooks` directory. This can be done simply by running the install script (note that the `_4DSEE_ROOT` enviromental variable **must** be set.)
```bash
install.sh
```
## Manual Usage
The python script `formatHeader.py` assumes that an enviromental variable has been set which points to the root of the 4DSSE project. This is used to retreive the git log so that author information and last update date information may be read. By default this uses the enviromental variable `_4DSSE_ROOT`; however, this may be changed with the `--root` command line flag. Whatever is set as `--root` will be the name of the enviromental variable this script will search for.
General usage might look like
```bash

19
commit-config/hooks/install.sh Executable file
View File

@@ -0,0 +1,19 @@
#!/bin/bash
PROJECT_ROOT=$_4DSSE_ROOT
# Check if the project root is set
if [ -z "$PROJECT_ROOT" ]; then
echo "The 4DSSE root is not set. Please set the _4DSSE_ROOT environment variable."
exit 1
fi
cp $PROJECT_ROOT/commit-config/hooks/post-commit $PROJECT_ROOT/.git/hooks/post-commit
# Make sure the post-commit hook is executable
chmod +x $PROJECT_ROOT/.git/hooks/post-commit
# Check if the post-commit hook was copied successfully
if [ ! -f $PROJECT_ROOT/.git/hooks/post-commit ]; then
echo "ERROR! The post-commit hook was not copied successfully."
exit 1
fi

36
commit-config/hooks/post-commit Executable file
View File

@@ -0,0 +1,36 @@
#!/bin/bash
# Do not run the commit hook a second time when the headers have been added
if [[ "$SKIP_POST_COMMIT" == "1" ]]; then
exit 0
fi
# Define the formatter script
FORMATTER_SCRIPT="python ./commit-config/hooks/formatHeader.py"
# File extensions to match
EXTENSIONS="py|h|hpp|c|cpp|f|f90|build"
# Find all matching files in src which are currently staged
files=$(git diff --name-only HEAD^ | grep -E '\.('"$EXTENSIONS"')$')
# Check if any files were found
if [ -z "$files" ]; then
echo "No matching files found."
exit 0
fi
# Run the formatter on each file
for file in $files; do
echo "Updating header for: $file"
$FORMATTER_SCRIPT "$file"
done
# # Automatically stage the updated files
# git add $files
# # Optionally, commit the updated files (creates a second commit)
# SKIP_POST_COMMIT=1 git commit -m "(docs/headers) Update headers after commit" || echo "No changes to commit"
echo "Post-commit hook executed."