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

pre-push: Don't block pushes to canary for forked repos #67946

Merged
merged 3 commits into from
Jul 22, 2024
Merged
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
46 changes: 42 additions & 4 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
#!/usr/bin/env bash

main_branch="canary"
protected_branch='canary'

branch="$(git rev-parse --abbrev-ref HEAD)"
protected_remote_urls=(
'git@github.com:vercel/next.js.git'
'https://github.com/vercel/next.js.git' # github blocks password-based auth, but still usable via API token
)

if [ "$branch" = "$main_branch" ]; then
echo "You probably didn't intend to push directly to '$main_branch'." >&2

# 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
Expand Down
Loading