From 7568eb1a46f2f6beb2b00947b9b8ec0b3727b5ed Mon Sep 17 00:00:00 2001 From: Emily Boudreaux Date: Mon, 17 Mar 2025 13:31:54 -0400 Subject: [PATCH] ci(post-commit): updated to track post-commit --- commit-config/hooks/READEME.md | 12 ++++++++--- commit-config/hooks/install.sh | 19 +++++++++++++++++ commit-config/hooks/post-commit | 36 +++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100755 commit-config/hooks/install.sh create mode 100755 commit-config/hooks/post-commit diff --git a/commit-config/hooks/READEME.md b/commit-config/hooks/READEME.md index cac0612..068fa43 100644 --- a/commit-config/hooks/READEME.md +++ b/commit-config/hooks/READEME.md @@ -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 diff --git a/commit-config/hooks/install.sh b/commit-config/hooks/install.sh new file mode 100755 index 0000000..467205e --- /dev/null +++ b/commit-config/hooks/install.sh @@ -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 \ No newline at end of file diff --git a/commit-config/hooks/post-commit b/commit-config/hooks/post-commit new file mode 100755 index 0000000..24066e2 --- /dev/null +++ b/commit-config/hooks/post-commit @@ -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." +