Skip to content

Commit

Permalink
Nathan/prevent push to main (#407)
Browse files Browse the repository at this point in the history
* Added prepush hook

* Edited

* Edited

* Edited

* Edited

* Edited

* Edited again

* Edited again

* Edited again

* Edited again

* Edited again

* Edited again

* Changed husky file

* Final prepush hook

* Removed gitignore for husky prepush
  • Loading branch information
nathanzzhang authored Feb 19, 2024
1 parent 99e095d commit 68fba83
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
!.huskyrc.js
public
2 changes: 1 addition & 1 deletion .huskyrc.js → .huskryrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ module.exports = {
'pre-commit': 'yarn lint',
'pre-push': 'yarn lint'
}
};
};
31 changes: 31 additions & 0 deletions .husky/_/husky.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh
if [ -z "$husky_skip_init" ]; then
debug () {
if [ "$HUSKY_DEBUG" = "1" ]; then
echo "husky (debug) - $1"
fi
}

readonly hook_name="$(basename "$0")"
debug "starting $hook_name..."

if [ "$HUSKY" = "0" ]; then
debug "HUSKY env variable is set to 0, skipping hook"
exit 0
fi

if [ -f ~/.huskyrc ]; then
debug "sourcing ~/.huskyrc"
. ~/.huskyrc
fi

export readonly husky_skip_init=1
sh -e "$0" "$@"
exitCode="$?"

if [ $exitCode != 0 ]; then
echo "husky - $hook_name hook exited with code $exitCode (error)"
fi

exit $exitCode
fi
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run lint
16 changes: 16 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run lint

echo "> ACM Hack Pre-push Hook: Checking branch name for push to main. \n"

BRANCH=`git rev-parse --abbrev-ref HEAD`
PROTECTED_BRANCHES="^(main|master)"

if [[ "$BRANCH" =~ $PROTECTED_BRANCHES ]]
then
echo "ERROR: Oops! You tried pushing directly to $BRANCH. Please create your own branch with \n git checkout -b <your_name/feature_name> \nand push again to create a pull request. Note: The < > is inserted for convention as a variable placeholder. Do not include < > in your actual branch name.\n" && exit 1
fi

exit 0
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1",
"lint": "eslint --ext js,jsx ."
},
"husky": {
"hooks": {
"pre-commit": "./.husky/pre-commit",
"pre-push": "./.husky/pre-push"
}
},
"repository": {
"type": "git",
"url": "https://github.com/uclaacm/hoth.uclaacm.com"
Expand Down

0 comments on commit 68fba83

Please sign in to comment.