Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: eslint pre-commit hook now supports partially staged files #16137

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
40 changes: 38 additions & 2 deletions .husky/eslint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
GREEN='\033[0;32m'
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 +25,49 @@ 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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--staged supported by git version 2.35+

# 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
# echo -e "Restoring the ${RED}unstaged${NC} files."
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
# echo -e "Restoring the ${RED}unstaged${NC} files."
git stash pop -q # Restore the stash of the unstaged files if there were any
fi
fi

9 changes: 8 additions & 1 deletion scripts/shellcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ set -o pipefail

shellcheck --version
# lint all *.sh files
find . -type f -name '*.sh' ! -path './node_modules/*' ! -path './yarn/*' ! -path './suite-native/app/ios/*' ! -path './submodules/*' -print0 | xargs -0 shellcheck
find . -type f -name '*.sh' \
! -path './node_modules/*' \
! -path './node_modules/*' \
! -path './yarn/*' \
! -path './suite-native/app/ios/*' \
! -path './submodules/*' \
! -path './.husky/_/*' \
-print0 | xargs -0 shellcheck
Loading