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

The pre-commit.protect-master should protect more than just master #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions bin/ka-clone
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def protect_master():
_install_git_hook('pre-commit.protect-master', 'pre-commit')
_install_git_hook('pre-push.protect-master', 'pre-push.protect-master')
_install_git_hook('pre-rebase.protect-master', 'pre-rebase')

# Make a note in the local git config -- e.g. git-review-branch uses this
# to check if it should allow you to branch off master.
# TODO(benkraft): Read this value as a default for the commandline
Expand Down
43 changes: 33 additions & 10 deletions templates/pre-commit.protect-master
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
#!/bin/sh

# A simple pre-commit hook that makes it illegal to commit to master
# on the repo. An exception is made for the automated process
# which is the only thing allowed to merge into master.
# A simple pre-commit hook that makes it illegal to commit to master (and other
# deploy/target branches) on the repo. An exception is made for the automated process
# which is the only thing allowed to merge into master/etc.
#
# For emergencies, you can override this hook by using 'git commit -n'.

# IF YOU UPDATE THESE VARS, be sure to update pre-push.protect-master as well.
# If you want to whitelist other users, just add another "-e <email>".
SUPERUSERS="-e [email protected]"

if git config --get user.email | grep -x $SUPERUSERS >/dev/null; then

# Exit if user email matches a SUPERUSER
if git config --get user.email | grep -x "$SUPERUSERS" >/dev/null; then
exit 0
fi

if [ "`git rev-parse --abbrev-ref HEAD`" = "master" ]; then
echo "FATAL ERROR: You cannot commit directly to the master branch."
echo "Commit to a deploy branch instead:"
echo " https://khanacademy.org/r/git-at-ka"
Copy link
Member Author

Choose a reason for hiding this comment

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

this link went nowhere...

exit 1
fi
# Get the current branch name
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)

# Get the known deploy branches from the git config (comma-separated list)
KNOWN_DEPLOY_BRANCHES=$(git config --get ka.olc.targetBranches)

# Check if the current branch is protected
case "$CURRENT_BRANCH" in
master|main|next|deploy*)
echo "FATAL ERROR: You cannot commit directly to the $CURRENT_BRANCH branch."
echo "More info: https://khanacademy.org/r/gitfaq#id-9e3a"
exit 1
;;
esac

# Check if the current branch matches any known deploy branches
IFS=','
for branch in $KNOWN_DEPLOY_BRANCHES; do
if [ "$CURRENT_BRANCH" = "$branch" ]; then
echo "FATAL ERROR: You cannot commit directly to the $CURRENT_BRANCH branch."
echo "Make a pull-request (or audit) and land it to this branch instead"
Copy link
Member

Choose a reason for hiding this comment

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

Maybe: "You cannot commit directly to the ... branch because you've told OLC it's a deploy branch." And also give instructions on how to tell OLC it's not a deploy branch, if that was in error.

echo "More info: https://khanacademy.org/r/gitfaq#id-9e3a"
exit 1
fi
done

exit 0