From d2c52a41dad1b57f120519366cfeb33ed3e90967 Mon Sep 17 00:00:00 2001 From: Lilli Szafranski Date: Fri, 8 Nov 2024 15:24:18 -0800 Subject: [PATCH 1/4] [FEI-5966] something --- templates/pre-commit.protect-master | 42 ++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/templates/pre-commit.protect-master b/templates/pre-commit.protect-master index 593b1f9..f38001a 100755 --- a/templates/pre-commit.protect-master +++ b/templates/pre-commit.protect-master @@ -1,8 +1,8 @@ #!/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'. @@ -10,13 +10,35 @@ # If you want to whitelist other users, just add another "-e ". SUPERUSERS="-e jenkins@khanacademy.org" -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" - 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 "Make a pull-request or audit to a deploy branch instead: https://khanacademy.org/r/git-at-ka" + 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 to a deploy branch instead: https://khanacademy.org/r/git-at-ka" + exit 1 + fi +done + +exit 0 From 8fe095f05ffe89dd0acf215644910bb7b713eb5a Mon Sep 17 00:00:00 2001 From: Lilli Szafranski Date: Fri, 8 Nov 2024 15:35:48 -0800 Subject: [PATCH 2/4] [FEI-5966] The pre-commit.protect-master should protect more than just master --- bin/ka-clone | 1 + templates/pre-commit.protect-master | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/ka-clone b/bin/ka-clone index 2b83af3..2523dba 100755 --- a/bin/ka-clone +++ b/bin/ka-clone @@ -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 diff --git a/templates/pre-commit.protect-master b/templates/pre-commit.protect-master index f38001a..5ce78b4 100755 --- a/templates/pre-commit.protect-master +++ b/templates/pre-commit.protect-master @@ -26,7 +26,6 @@ KNOWN_DEPLOY_BRANCHES=$(git config --get ka.olc.targetBranches) case "$CURRENT_BRANCH" in master|main|next|deploy*) echo "FATAL ERROR: You cannot commit directly to the $CURRENT_BRANCH branch." - echo "Make a pull-request or audit to a deploy branch instead: https://khanacademy.org/r/git-at-ka" exit 1 ;; esac @@ -36,7 +35,7 @@ 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 to a deploy branch instead: https://khanacademy.org/r/git-at-ka" + echo "Make a pull-request or audit and land it to this branch instead" exit 1 fi done From 93487a000f0c55a3aede8da4bce50c6a87bbb97d Mon Sep 17 00:00:00 2001 From: Lilli Szafranski Date: Fri, 8 Nov 2024 15:41:06 -0800 Subject: [PATCH 3/4] [FEI-5966] parens --- templates/pre-commit.protect-master | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/pre-commit.protect-master b/templates/pre-commit.protect-master index 5ce78b4..a33901d 100755 --- a/templates/pre-commit.protect-master +++ b/templates/pre-commit.protect-master @@ -35,7 +35,7 @@ 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" + echo "Make a pull-request (or audit) and land it to this branch instead" exit 1 fi done From 2f4f5f4d188575884ba59008bcd27fcc519690e0 Mon Sep 17 00:00:00 2001 From: Lilli Szafranski Date: Tue, 12 Nov 2024 16:32:15 -0800 Subject: [PATCH 4/4] [FEI-5966] better output --- templates/pre-commit.protect-master | 2 ++ 1 file changed, 2 insertions(+) diff --git a/templates/pre-commit.protect-master b/templates/pre-commit.protect-master index a33901d..fa5d352 100755 --- a/templates/pre-commit.protect-master +++ b/templates/pre-commit.protect-master @@ -26,6 +26,7 @@ KNOWN_DEPLOY_BRANCHES=$(git config --get ka.olc.targetBranches) 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 @@ -36,6 +37,7 @@ 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" + echo "More info: https://khanacademy.org/r/gitfaq#id-9e3a" exit 1 fi done