Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pre-commit hook checking spotless #1881

Merged
merged 10 commits into from
Aug 26, 2024
5 changes: 5 additions & 0 deletions docs/3.-Contributing-to-JPlag.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ Please try to make well-documented and clear structured submissions:
Run `mvn clean package assembly:single` instead if you need the full jar which includes all dependencies.
5. You will find the generated JARs in the subdirectory `jplag.cli/target`.

### Git hooks

The repository contains a pre-commit hook, that prevents commits if fail spotless.
To set up the hooks, call `git config --local core.hooksPath gitHooks/hooks` once within your local repository.

42 changes: 42 additions & 0 deletions gitHooks/hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/sh
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved

echo "Running pre commit checks"

hasJavaFiles=0
hasJsFiles=0

files=`git diff --name-only --cached`
while read name
do
if [[ $name == report-viewer* ]]
then
hasJsFiles=1
fi

if [[ $name == *.java ]]
then
hasJavaFiles=1
fi
done <<< "$files"

if [[ $hasJsFiles -gt 0 ]]
then
echo "Running report viewer pre commit checks"
cd report-viewer
../gitHooks/scripts/reportViewerPreCommit
if [ $? -gt 0 ]
then
exit 1
fi
cd ..
fi

if [[ $hasJavaFiles -gt 0 ]]
then
echo "Running java pre commit checks"
gitHooks/scripts/javaPreCommit
if [ $? -gt 0 ]
then
exit 1
fi
fi
19 changes: 19 additions & 0 deletions gitHooks/scripts/javaPreCommit
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

if ! command -v mvn &> /dev/null #checks if maven is installed
then
echo "Maven is not installed. Spotless will not be checked"
exit 0
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
fi

#prevents the shell from aborting if maven returns a non zero exit code
set +e
echo Checking spotless
mvn spotless:check &> /dev/null
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
exitCode=$?

if [ $exitCode -gt 0 ]
then
echo "Spotless failed. Please run 'mvn spotless:apply' to fix."
fi
exit $exitCode
9 changes: 9 additions & 0 deletions gitHooks/scripts/reportViewerPreCommit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

if ! command -v npm &> /dev/null #checks if npm is installed
then
echo "Npm is not installed. Linter check will be skipped"
exit 0
fi

npx lint-staged
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 0 additions & 2 deletions report-viewer/.husky/pre-commit

This file was deleted.

9 changes: 1 addition & 8 deletions report-viewer/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions report-viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"type-check": "vue-tsc --noEmit -p tsconfig.vitest.json --composite false",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore --max-warnings 0",
"format": "prettier --write src/",
"prepare": "cd .. && husky report-viewer/.husky"
"prepare": "git config --local core.hooksPath gitHooks/hooks"
},
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.6.0",
Expand Down Expand Up @@ -49,7 +49,6 @@
"autoprefixer": "^10.4.20",
"eslint": "^8.57.0",
"eslint-plugin-vue": "^9.27.0",
"husky": "^9.1.5",
"jsdom": "^24.1.1",
"lint-staged": "^15.2.9",
"npm-run-all": "^4.1.5",
Expand Down
Loading