diff --git a/git_hooks/pre-commit b/git_hooks/pre-commit index 77df122b5..3b4f94f3d 100755 --- a/git_hooks/pre-commit +++ b/git_hooks/pre-commit @@ -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 @@ -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." \ No newline at end of file +exit 0 \ No newline at end of file