Skip to content
Merged
Changes from all commits
Commits
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
34 changes: 12 additions & 22 deletions git_hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
#!/bin/sh

# Function to run Dart command and check exit code
run_command() {
directory="$1"
command="$2"

cd "$directory" || exit 1

result=$(eval "$command")
echo "$result"
}

# Ensure git hooks path is set only once
# Ensure git hooks path is set correctly
if ! git config --get core.hooksPath | grep -q "git_hooks"; then
echo "Setting git hooks path..."
git config core.hooksPath git_hooks
Expand All @@ -20,17 +9,18 @@ else
echo "Git hooks path already set. Skipping..."
fi

# Change directory to app/lib and run dart format
run_command "app/lib" "dart format ." "dart format"
# Get staged Dart files
STAGED_DART_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.dart$')

echo "Finished running dart format command."
if [ -n "$STAGED_DART_FILES" ]; then
echo "Analyzing staged Dart files..."
echo "$STAGED_DART_FILES" | xargs -n1 dart analyze --fatal-infos

# Run dart analyze in the current directory
run_command "." "dart analyze ." "dart analyze"
echo "Applying fixes..."
echo "$STAGED_DART_FILES" | xargs -n1 dart fix --apply

echo "Finished running dart analyze command."

# Run dart fix --apply and add updated files to git
run_command "." "dart fix --apply && git add ." "dart fix"
echo "Re-adding fixed Dart files to the commit..."
echo "$STAGED_DART_FILES" | xargs -n1 git add
fi

echo "Finished running dart fix command."
exit 0