Files
vercel__next.js/.husky/pre-push
Benjamin Woodruff 5c88a3838e Use posix-compatible syntax for pre-push hook (#68421)
### What?

Use POSIX-compatible shell script syntax for the Husky `pre-push` hook.
POSIX shell sadly doesn't support arrays, so fall back to using
IFS-based string splitting (which is fine here).

### Why?

- Husky ignores the shebang, and simply invokes `sh`:
https://github.com/typicode/husky/issues/971
- On Debian-based distributions, [`sh` is provided by
`dash`](https://wiki.archlinux.org/title/Dash), which is POSIX
compatible, but does not support `bash`-specific features.
- The Husky documentation has a workaround for using bash, but doesn't
recommend it: https://typicode.github.io/husky/how-to.html#bash

I do most of my development on a Debian VM, which is how I noticed this.

### Test Plan

On Debian with `dash`, manually invoke it like so:

```
echo local_branch_name from_commit refs/heads/canary to_commit | sh -x .husky/pre-push canary git@github.com:vercel/next.js.git
```
2024-08-02 19:27:25 -04:00

53 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
protected_branch='canary'
# github blocks password-based auth, but still usable via API token
protected_remote_urls="\
git@github.com:vercel/next.js.git
https://github.com/vercel/next.js.git"
# The pre-push hook [...] receives the name and location of the remote as parameters
# https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
remote_name="$1"
remote_url="$2"
# if we're pushing to a fork, we don't need to protect canary.
# check if the remote is one of the protected ones.
is_remote_protected=0
for protected_remote_url in $protected_remote_urls; do
if [ "$remote_url" = "$protected_remote_url" ]; then
is_remote_protected=1
break
fi
done
if [ "$is_remote_protected" = 0 ]; then
exit 0
fi
# check if the push is targeting canary on the remote
# https://stackoverflow.com/a/44156933
push_targets_protected_branch=0
protected_ref="refs/heads/$protected_branch"
while read -r _local_ref _local_sha remote_ref _remote_sha; do
if [ "$remote_ref" = "$protected_ref" ]; then
push_targets_protected_branch=1
break
fi
done
if [ "$push_targets_protected_branch" = "1" ]; then
echo "You probably didn't intend to push directly to '$protected_branch' on '$remote_name' ($remote_url)." >&2
echo "If you're sure that that's what you want to do, bypass this check via" >&2
echo "" >&2
echo " git push --no-verify" >&2
echo "" >&2
exit 1
fi