Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/release_notes/891-refactor-pre-commit
Original file line number Diff line number Diff line change
@@ -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

4 changes: 4 additions & 0 deletions docs/release_notes/898-remove-default-prefix
Original file line number Diff line number Diff line change
@@ -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).
Copy link
Collaborator

@rjyounes rjyounes Oct 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we have the pre-commit hook remove the prefix declaration, in the same way that the serializer reformats and makes some modifications?

Copy link
Collaborator

@rjyounes rjyounes Oct 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more issue: the pre-commit hook issues an error and blocks the commit when I have a file either in the ontologies directory or the root directory that is not being committed. It should be looking only at the files being committed, like the serializer.

This is another reason not to include the base directory (comment below), but that will not fully address it, since such a file could exist in my ontologies directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly because it was faster/easier to just block the commit and I was trying to get this done so the release could get done.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. That's why I want to postpone this to the next release when we can take the time to do it right. There's no need for it to go into 14.0.0.


1 change: 0 additions & 1 deletion ontologies/gistCore.ttl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@prefix : <https://w3id.org/semanticarts/ontology/gistCore#> .
@prefix gist: <https://w3id.org/semanticarts/ns/ontology/gist/> .
@prefix gistd: <https://w3id.org/semanticarts/ns/data/gist/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
Expand Down
40 changes: 40 additions & 0 deletions tools/check_default_prefix/check_default_prefix.py
Original file line number Diff line number Diff line change
@@ -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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The serializer modifies files without having to re-add them.

# 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)
31 changes: 22 additions & 9 deletions tools/pre-commit → tools/pre-commit-code
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -11,24 +19,29 @@ 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that you want to make this general, but since in this repository the ontologies are stored in the ontologies subdirectory, I don't think we should include this. Furthermore, other configurations are possible - e.g., for one client, we had models/ontologies. The point being it's not going to be all-purpose, and users are possibly going to have to modify the path if they copy this to another repo.

"${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
exit 1
fi

exit 0

18 changes: 18 additions & 0 deletions tools/pre-commit-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env sh

# This file should be copied into the .git/hooks directory of the project.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# This file should be copied into the .git/hooks directory of the project.
# This file should be copied into the .git/hooks directory of the project. This is done automatically by running setup.cmd.

# 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
19 changes: 11 additions & 8 deletions tools/setup.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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%.
)