From 3f1deb8c7dc8d40af35dfd11aa878b02709c9653 Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Tue, 28 Jan 2025 16:35:44 +0200 Subject: [PATCH 1/3] Run precommit on staged files only --- git_hooks/pre-commit | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/git_hooks/pre-commit b/git_hooks/pre-commit index 77df122b5..31e34ad39 100755 --- a/git_hooks/pre-commit +++ b/git_hooks/pre-commit @@ -1,16 +1,5 @@ #!/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 if ! git config --get core.hooksPath | grep -q "git_hooks"; then echo "Setting git hooks path..." @@ -20,17 +9,22 @@ 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_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '.dart$') -echo "Finished running dart format command." +if [-n "$STAGED_FILES"] ; then + echo "Analyzing staged Dart files..." + echo "$STAGED_DART_FILES" | xargs dart analyze --fatal-infos -# Run dart analyze in the current directory -run_command "." "dart analyze ." "dart analyze" -echo "Finished running dart analyze command." + if [ $? -ne 0 ]; then + echo "Dart analysis failed. Commit aborted." + exit 1 + fi -# Run dart fix --apply and add updated files to git -run_command "." "dart fix --apply && git add ." "dart fix" + echo "Applying fixes..." + echo "$STAGED_DART_FILES" | xargs dart fix --apply + git add $STAGED_DART_FILES +fi -echo "Finished running dart fix command." \ No newline at end of file +exit 0 \ No newline at end of file From d83b053ba2cde9bcbfdeffbb812b76ad9963dcbb Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Tue, 28 Jan 2025 16:54:16 +0200 Subject: [PATCH 2/3] Fix precommit --- git_hooks/pre-commit | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/git_hooks/pre-commit b/git_hooks/pre-commit index 31e34ad39..b474f264f 100755 --- a/git_hooks/pre-commit +++ b/git_hooks/pre-commit @@ -1,6 +1,6 @@ #!/bin/sh -# 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 @@ -10,21 +10,17 @@ else fi # Get staged Dart files -STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '.dart$') +STAGED_DART_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.dart$') -if [-n "$STAGED_FILES"] ; then +if [ -n "$STAGED_DART_FILES" ]; then echo "Analyzing staged Dart files..." echo "$STAGED_DART_FILES" | xargs dart analyze --fatal-infos - - if [ $? -ne 0 ]; then - echo "Dart analysis failed. Commit aborted." - exit 1 - fi - echo "Applying fixes..." echo "$STAGED_DART_FILES" | xargs dart fix --apply - git add $STAGED_DART_FILES + + echo "Re-adding fixed Dart files to the commit..." + echo "$STAGED_DART_FILES" | xargs git add fi exit 0 \ No newline at end of file From b529418b1192eb5505c1ad49e9ba603232051e92 Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Wed, 29 Jan 2025 16:56:49 +0200 Subject: [PATCH 3/3] Apply analyzing and fixes --- git_hooks/pre-commit | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/git_hooks/pre-commit b/git_hooks/pre-commit index b474f264f..3b4f94f3d 100755 --- a/git_hooks/pre-commit +++ b/git_hooks/pre-commit @@ -14,13 +14,13 @@ STAGED_DART_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.da if [ -n "$STAGED_DART_FILES" ]; then echo "Analyzing staged Dart files..." - echo "$STAGED_DART_FILES" | xargs dart analyze --fatal-infos + echo "$STAGED_DART_FILES" | xargs -n1 dart analyze --fatal-infos echo "Applying fixes..." - echo "$STAGED_DART_FILES" | xargs dart fix --apply + echo "$STAGED_DART_FILES" | xargs -n1 dart fix --apply echo "Re-adding fixed Dart files to the commit..." - echo "$STAGED_DART_FILES" | xargs git add + echo "$STAGED_DART_FILES" | xargs -n1 git add fi exit 0 \ No newline at end of file