diff --git a/docs/release_notes/891-refactor-pre-commit b/docs/release_notes/891-refactor-pre-commit new file mode 100644 index 00000000..0a032f43 --- /dev/null +++ b/docs/release_notes/891-refactor-pre-commit @@ -0,0 +1,7 @@ +### Patch Updates + +- Refactored the git pre-commit to make sure the latest version is always used. Issue [#891](https://github.com/semanticarts/gist/issues/891) + - Updated setup.cmd to install tools/pre-commit-hook as .git/hooks/pre-commit + - New file tools/pre-commit-hook which just calls ./tools/pre-commit-code. + - Renamed tools/pre-commit to tools/pre-commit-code + diff --git a/docs/release_notes/898-remove-default-prefix b/docs/release_notes/898-remove-default-prefix new file mode 100644 index 00000000..041085c1 --- /dev/null +++ b/docs/release_notes/898-remove-default-prefix @@ -0,0 +1,4 @@ +### Patch Updates + +- Added a test in the git pre-commit hook to check for default PREFIX declarations in ontology files and cause the commit to fail if it finds one. For this to work, the setup.cmd command needs to be re-run to update the git hook. Issue [#898](https://github.com/semanticarts/gist/issues/898). + diff --git a/ontologies/gistCore.ttl b/ontologies/gistCore.ttl index d1fa95ab..7e13780c 100644 --- a/ontologies/gistCore.ttl +++ b/ontologies/gistCore.ttl @@ -1,4 +1,3 @@ -@prefix : . @prefix gist: . @prefix gistd: . @prefix owl: . diff --git a/tools/check_default_prefix/check_default_prefix.py b/tools/check_default_prefix/check_default_prefix.py new file mode 100644 index 00000000..70d91de5 --- /dev/null +++ b/tools/check_default_prefix/check_default_prefix.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +import re, sys, pathlib + +# Get command line arguments +verbose = False +if "--verbose" in sys.argv: + sys.argv.remove("--verbose") + verbose = True + +if len(sys.argv) != 2: + print("INVALID ARGUMENTS! Usage: ", sys.argv[0], " path/to/ontology/files [--verbose]") + sys.exit(1) + +# Regex matches both Turtle (@prefix) and SPARQL (PREFIX) default declarations. +pattern = re.compile( + r'(?mi)^\s*(?:@prefix|prefix)\s*:\s*<[^>]+>\s*(?:\.\s*)?(?:#.*)?$' +) + +changed = False +for filePath in pathlib.Path(sys.argv[1]).glob("*.ttl"): + if verbose: + print("Checking for a default prefix in file: ", filePath) + currentText = filePath.read_text(encoding="utf-8") + newText = pattern.sub("", currentText).strip() + # Preserve the original file's trailing newlines + if currentText.endswith("\n\n"): + newText += "\n\n" + elif currentText.endswith("\n"): + newText += "\n" + if newText != currentText: + print("ERROR: there is a default PREFIX statement in file: ", filePath) + print("It must be removed from the file before you can commit your other changes.") + changed = True + # The following line would fix the file, then you would need to "git add filename" + # filePath.write_text(newText, encoding="utf-8") + # Exit 1 if anything changed, so you re-stage before committing. + +# Exit 1 to indicate there are default PREFIX to remove +# If no change was needed, exit 0 and let the commit proceed. +sys.exit(1 if changed else 0) diff --git a/tools/pre-commit b/tools/pre-commit-code similarity index 53% rename from tools/pre-commit rename to tools/pre-commit-code index edf98a18..105c7a52 100644 --- a/tools/pre-commit +++ b/tools/pre-commit-code @@ -1,6 +1,14 @@ #!/usr/bin/env sh -# This file should be copied into the .git/hooks directory of the project. +# This file performs all the actual git pre-commit checks. +# It should be called by .git/hooks/pre-commit, which should be a copy of tools/pre-commit-hook. + +# Get absolute path to this script +# SCRIPT=$(readlink -f "$0") +# SCRIPTPATH=$(dirname "$SCRIPT") + +# Get root directory of this git repository +base_dir=$(git rev-parse --show-toplevel) # Stops accidental commits to branches master, main, or develop. BRANCH=`git rev-parse --abbrev-ref HEAD` @@ -11,20 +19,24 @@ then exit 1 fi -# Get absolute path to this script -# SCRIPT=$(readlink -f "$0") -# SCRIPTPATH=$(dirname "$SCRIPT") +# Verify that there are no default PREFIX statements in the ontology files +"${base_dir}/tools/check_default_prefix/check_default_prefix.py" "${base_dir}/ontologies/" +rc=$? +if [ ! ${rc} -eq 0 ] ; then + exit 1 +fi -# Get root directory of this git repository -base_dir=$(git rev-parse --show-toplevel) +# For repositories that have their ontology files in the project root directory +"${base_dir}/tools/check_default_prefix/check_default_prefix.py" "${base_dir}/" +rc=$? +if [ ! ${rc} -eq 0 ] ; then + exit 1 +fi # Run the serializer -set -x - "${base_dir}/tools/serializer/pre-commit" rc=$? -set +x # echo "RESULT == ${rc}" if [ ! ${rc} -eq 0 ] ; then @@ -32,3 +44,4 @@ if [ ! ${rc} -eq 0 ] ; then fi exit 0 + diff --git a/tools/pre-commit-hook b/tools/pre-commit-hook new file mode 100644 index 00000000..bbc9036e --- /dev/null +++ b/tools/pre-commit-hook @@ -0,0 +1,18 @@ +#!/usr/bin/env sh + +# This file should be copied into the .git/hooks directory of the project. +# All it does is call ./tools/pre-commit-code + +# Get root directory of this git repository +base_dir=$(git rev-parse --show-toplevel) + +# Run the pre-commit code from the ./tools/ directory +"${base_dir}/tools/pre-commit-code" +rc=$? + +# Return the response code from previous command +if [ ! ${rc} -eq 0 ] ; then + exit 1 +fi + +exit 0 diff --git a/tools/setup.cmd b/tools/setup.cmd index 2ca47d93..d7223325 100644 --- a/tools/setup.cmd +++ b/tools/setup.cmd @@ -16,17 +16,20 @@ base_dir=$(git rev-parse --show-toplevel) # Print out commands so user can see what is being done set -x -# Copy pre-commit to the git hooks directory -cp "${base_dir}/tools/pre-commit" "${base_dir}/.git/hooks/" +# Copy pre-commit-hook to the git hooks directory +cp "${base_dir}/tools/pre-commit-hook" "${base_dir}/.git/hooks/pre-commit" -# Make pre-commit hook executable. +# Make pre-commit hook executable. chmod +x "${base_dir}/.git/hooks/pre-commit" +# Ensure that tools/pre-commit-code is executable. +chmod +x "${base_dir}/tools/pre-commit-code" + # Ensure that the serializer pre-commit hook is executable. chmod +x "${base_dir}/tools/serializer/pre-commit" -# Don't track executable flags on files in this repository (this is not a global setting). -git config core.filemode false +# Ensure that check_default_prefix.py is executable. +chmod +x "${base_dir}/tools/check_default_prefix/check_default_prefix.py" # Exit linux shell exit @@ -35,8 +38,8 @@ exit :WINDOWS CHDIR "%~dp0" IF EXIST "tools" chdir tools -IF EXIST "pre-commit" ( - copy "pre-commit" ..\.git\hooks\ +IF EXIST "pre-commit-hook" ( + copy "pre-commit-hook" ..\.git\hooks\pre-commit ) ELSE ( - echo Could not find the "pre-commit" file in %cd%. + echo ERROR: Could not find the "pre-commit-hook" file in %cd%. )