-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2352 from Agenta-AI/add-pre-commit-hook
Add pre commit hook
- Loading branch information
Showing
4 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Define the directories to check | ||
DIRECTORIES=("agenta-backend" "agenta-cli") | ||
ORIGINAL_DIR=$(pwd) | ||
|
||
# Check for changes in frontend directory | ||
if git diff --cached --name-only | grep -q '^agenta-web/'; then | ||
cd agenta-web || exit | ||
|
||
# Run Prettier check | ||
if ! npm run format; then | ||
echo '⚠️ Formatting issues detected. Running Prettier to fix them...' | ||
npm run format-fix | ||
|
||
echo '✅ Formatting issues fixed. Please stage the changes and commit the code again' | ||
exit 1 | ||
fi | ||
|
||
cd "$ORIGINAL_DIR" || exit | ||
fi | ||
|
||
# Check for changes in backend directory | ||
for DIR in "${DIRECTORIES[@]}"; do | ||
if git diff --cached --name-only | grep -q "^$DIR/"; then | ||
cd "$DIR" || exit | ||
|
||
# Run black for formatting | ||
if ! black --check .; then | ||
echo "⚠️ Formatting issues detected in $DIR. Running black to fix them..." | ||
black . | ||
|
||
echo "✅ Formatting issues fixed Please stage the changes and commit the code again $DIR." | ||
exit 1 | ||
fi | ||
|
||
cd "$ORIGINAL_DIR" || exit | ||
fi | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Define the directories to check | ||
DIRECTORIES=("agenta-backend" "agenta-cli") | ||
ORIGINAL_DIR=$(pwd) | ||
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) | ||
FIRST_PUSH_FILE=".git/first_push_detected" | ||
|
||
# Function to run frontend checks | ||
run_frontend_checks() { | ||
cd agenta-web || exit | ||
|
||
# Run ESLint check | ||
if ! npm run lint; then | ||
echo '❌ ESLint issues detected. Please fix them before pushing.' | ||
exit 1 | ||
fi | ||
|
||
# Run TypeScript type check | ||
if ! npm run types:check; then | ||
echo '❌ TypeScript type check failed.' | ||
exit 1 | ||
fi | ||
|
||
echo '🎉 Frontend checks passed!' | ||
cd "$ORIGINAL_DIR" || exit | ||
} | ||
|
||
# Function to run backend checks | ||
run_backend_checks() { | ||
local DIR=$1 | ||
cd "$DIR" || exit | ||
|
||
# Run pylint checks | ||
if ! pylint --recursive=y --errors-only .; then | ||
echo "❌ pylint issues detected in $DIR. Please fix them before pushing." | ||
exit 1 | ||
fi | ||
|
||
echo "🎉 Backend checks passed for $DIR." | ||
cd "$ORIGINAL_DIR" || exit | ||
} | ||
|
||
# Check if this is the first push | ||
is_first_push() { | ||
# Check if the branch exists locally | ||
if ! git rev-parse --verify "$BRANCH_NAME" >/dev/null 2>&1; then | ||
return 0 # First push, since the branch doesn't exist locally | ||
fi | ||
|
||
# Check if the branch exists remotely | ||
if ! git ls-remote --heads origin "$BRANCH_NAME" | grep -q "$BRANCH_NAME"; then | ||
return 0 # First push, since the branch doesn't exist remotely | ||
fi | ||
|
||
return 1 # Not the first push | ||
} | ||
|
||
# If: First-time push: Run all checks | ||
# Else: Check directory-specific changes for existing branch | ||
if is_first_push; then | ||
echo "🚀 First-time push detected for branch: $BRANCH_NAME" | ||
run_frontend_checks | ||
|
||
# for DIR in "${DIRECTORIES[@]}"; do | ||
# run_backend_checks "$DIR" | ||
# done | ||
else | ||
echo "🔍 Checking directory changes for existing branch: $BRANCH_NAME" | ||
if git diff --name-only --cached origin/"$BRANCH_NAME" | grep -q '^agenta-web/'; then | ||
run_frontend_checks | ||
fi | ||
|
||
# for DIR in "${DIRECTORIES[@]}"; do | ||
# if git diff --name-only --cached origin/"$BRANCH_NAME" | grep -q "^$DIR/"; then | ||
# run_backend_checks "$DIR" | ||
# fi | ||
# done | ||
fi |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters