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

Post checkout hook does not honor the inputs that git provides #1546

Open
1 task done
ayush-sd opened this issue Dec 13, 2024 · 1 comment
Open
1 task done

Post checkout hook does not honor the inputs that git provides #1546

ayush-sd opened this issue Dec 13, 2024 · 1 comment

Comments

@ayush-sd
Copy link

ayush-sd commented Dec 13, 2024

Troubleshoot

If you're migrating from husky 4, see:
https://typicode.github.io/husky/migrate-from-v4.html

Context
Please describe your issue and provide some context:
Running husky on mac in zsh shell

I created post-checkout hook which receives two inputs from git -

the ref of the previous HEAD, the ref of the new HEAD

I use this to check for diff in package.json and notify if package.json has changed when you checkout

This hook runs fine if I add it in .git/hooks folder but when I try to add it to husky, husky runs the hook without providing the two inputs HEADs that git does and the hook fails.

Here is the hook:

# Checks if package.json has been changed when you checkout a branch
# If changed, then it will print a warning to nudge the user to reinstall packages.

# Define colors
RED='\033[0;31m'
YELLOW='\033[0;33m'

PACKAGE_ICON='📦'
ARROW_ICON=''

PREV_COMMIT=$1
POST_COMMIT=$2

echo $PACKAGE_ICON
echo $PREV_COMMIT # prints empty line if run via husky
echo $POST_COMMIT # prints empty line if run via husky

# Compare package.json between commits
DIFF=$(git diff --name-only "$PREV_COMMIT".."$POST_COMMIT" | grep "package.json")

# If package.json has changed, display warning
if [[ -n "$DIFF" ]]; then
    echo "${YELLOW}===============================\n"
    echo "${YELLOW}${PACKAGE_ICON} package.json changes detected ${PACKAGE_ICON}${NC}\n"
    echo "${YELLOW}==============================="

    git diff --unified=0 "$PREV_COMMIT".."$POST_COMMIT" package.json
fi

Screenshot of the error

Screenshot 2024-12-13 at 4 44 56 PM
@ayush-sd
Copy link
Author

I get the proper HEAD refs if I run the same hook without using husky by putting it inside .git/hooks
Screenshot 2024-12-13 at 4 47 04 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant