Skip to content

Commit

Permalink
fix: eslint pre-commit hook now supports partially staged files
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-sanderson committed Jan 2, 2025
1 parent 0ad8dc0 commit 21ed04a
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions .husky/eslint.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/bin/bash

GREEN='\033[0;32m'
RED='\033[31m'
NC='\033[0m' # No Color


if [ "$TREZOR_PRE_COMMIT_ESLINT_SKIP" == "true" ]; then
echo "Skipping eslint pre-commit hook, do: 'export TREZOR_PRE_COMMIT_ESLINT_SKIP=false' to re-enable it."
exit 0
Expand All @@ -24,14 +26,48 @@ else
echo "$STAGED_FILES"
echo ""

# This will stashed all changes that are not added (the red files), but keeps added (the green files)
echo -e "Stashing the ${GREEN}staged${NC} files."
git stash push --staged -q # Stash staged changes (green files)
git status

UNSTAGED_FILES=$(git diff --name-only)

if [ -z "$UNSTAGED_FILES" ]; then
echo -e "There are no ${RED}unstaged${NC} files to stash."
git stash pop -q # In case no unstaged files are present, we can pop the stash
git add . # And we need to stage them as well (from stash pop they became unstaged)

git status

else
echo -e "Stashing the ${RED}unstaged${NC} files."
git stash -q # If there are some unstaged (red) files, we need to stash them into second stash
git status

echo -e "Restoring the ${GREEN}staged${NC} files."
git stash pop stash@\{1\} -q # And then restore the first stash with the staged (green), files
git add . # And we need to stage them as well (from stash pop they became unstaged)
git status
fi

echo -e "Running lint..."

# No quotes to pass as separate arguments (files)
# shellcheck disable=SC2086
if ! yarn lint:js:fix-files $STAGED_FILES; then
echo "Eslint failed! Please fix the errors and try again. You can also use --no-verify to skip pre-commit checks."

if [ -n "$UNSTAGED_FILES" ]; then
git stash pop -q # Restore the stash of the unstaged files if there were any
fi
exit 1
fi

git update-index --again
fi
git update-index --again # Stage all changes (made by ESlint)

if [ -n "$UNSTAGED_FILES" ]; then
git stash pop -q # Restore the stash of the unstaged files if there were any
fi
fi

0 comments on commit 21ed04a

Please sign in to comment.