-
-
Save eduardosilva/d84a6ccfb521300ae0cba7ebd2b95f39 to your computer and use it in GitHub Desktop.
Some useful git hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
protected_branch='master' | |
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') | |
RED='\033[0;31m' | |
GREEN='\033[1;32m' | |
YELLOW='\033[1;33m' | |
NC='\033[0m' # No Color | |
# only run this if you are pushing to master | |
if [[ $current_branch = $protected_branch ]] ; then | |
echo -e "${YELLOW}Running pre push to master check...${NC}" | |
echo -e "${YELLOW}Trying to build tests project...${NC}" | |
#Let's speed things up a little bit | |
DOTNET_CLI_TELEMETRY_OPTOUT=1 | |
DOTNET_SKIP_FIRST_TIME_EXPERIENCE=1 | |
# build the project | |
dotnet build | |
# $? is a shell variable which stores the return code from what we just ran | |
rc=$? | |
if [[ $rc != 0 ]] ; then | |
echo -e "${RED}Failed to build the project, please fix this and push again${NC}" | |
echo "" | |
exit $rc | |
fi | |
# navigate to the test project to run the tests | |
# TODO: change this to your test project directory | |
cd test | |
echo -e "${YELLOW}Running unit tests...${NC}" | |
echo "" | |
# run the unit tests | |
dotnet test | |
# $? is a shell variable which stores the return code from what we just ran | |
rc=$? | |
if [[ $rc != 0 ]] ; then | |
# A non-zero return code means an error occurred, so tell the user and exit | |
echo -e "${RED}Unit tests failed, please fix and push again${NC}" | |
echo "" | |
exit $rc | |
fi | |
# Everything went OK so we can exit with a zero | |
echo -e "${GREEN}Pre push check passed!${NC}" | |
echo "" | |
fi | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Inspects branch name and checks if it contains a Jira ticket number (i.e. ABC-123). | |
# If yes, commit message will be automatically prepended with [ABC-123]. | |
# | |
# Useful for looking through git history and relating a commit or group of commits | |
# back to a user story. | |
# | |
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) | |
# Ensure BRANCH_NAME is not empty and is not in a detached HEAD state (i.e. rebase). | |
# SKIP_PREPARE_COMMIT_MSG may be used as an escape hatch to disable this hook, | |
# while still allowing other githooks to run. | |
if [ ! -z "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "HEAD" ] && [ "$SKIP_PREPARE_COMMIT_MSG" != 1 ]; then | |
PREFIX_PATTERN='[A-Z]{2,5}-[0-9]{1,4}' | |
[[ $BRANCH_NAME =~ $PREFIX_PATTERN ]] | |
PREFIX=${BASH_REMATCH[0]} | |
PREFIX_IN_COMMIT=$(grep -c "\[$PREFIX\]" $1) | |
# Ensure PREFIX exists in BRANCH_NAME and is not already present in the commit message | |
if [[ -n "$PREFIX" ]] && ! [[ $PREFIX_IN_COMMIT -ge 1 ]]; then | |
sed -i.bak -e "1s~^~[$PREFIX] ~" $1 | |
fi | |
fi | |
# | |
# Resources: | |
# - https://gist.github.com/bartoszmajsak/1396344 | |
# - https://stackoverflow.com/questions/34213120/find-branch-name-during-git-rebase | |
# - https://github.com/typicode/husky/issues/311#issuecomment-580237182 | |
# - https://gmurphey.github.io/2013/02/02/ignoring-git-hooks-when-rebasing.html#.XkK1AhNKjOQ | |
# - https://mikemadisonweb.github.io/2018/12/18/git-hook-prepending-commit-message/ | |
# - https://stackoverflow.com/questions/5894946/how-to-add-gits-branch-name-to-the-commit-message | |
# - http://blog.bartoszmajsak.com/blog/2012/11/07/lazy-developers-toolbox-number-1-prepend-git-commit-messages/ | |
# - https://docs.npmjs.com/files/package.json#bin | |
# - https://www.deadcoderising.com/how-to-smoothly-develop-node-modules-locally-using-npm-link/ | |
# - https://github.com/sindresorhus/execa | |
# - https://github.com/shelljs/shelljs | |
# | |
# | |
# Alternative method for finding the branch name | |
# | |
# Note that during a rebase, this will return something like | |
# (no branch, rebasing ABC-123-feature-x) | |
# instead of | |
# HEAD | |
# | |
# BRANCH_NAME=$(git branch | grep '*' | sed 's/* //') | |
# | |
# | |
# Also, don't forget to place this inside package.json if this is part of a node/npm project | |
# | |
# "husky": { | |
# "hooks": { | |
# "prepare-commit-msg": "./prepare-commit-msg.sh $HUSKY_GIT_PARAMS" | |
# } | |
# } | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment