Skip to content

Instantly share code, notes, and snippets.

@raouldc
Forked from williamdenton/commit-msg
Last active October 20, 2022 07:02
Show Gist options
  • Save raouldc/cbc55f65fee3a4e7e7226297996e7b22 to your computer and use it in GitHub Desktop.
Save raouldc/cbc55f65fee3a4e7e7226297996e7b22 to your computer and use it in GitHub Desktop.
Git hooks

useful Git hooks for my dev environment

TODO: Update to use GNU versions of tools such as sed so that it works on other environments

#!/bin/sh
commit_regex='(([[:upper:]]*\-[0-9]*)|fixup!)'
error_msg="Aborting commit. Your commit message is missing a JIRA ticket"
if ! grep -iqE "$commit_regex" "$1";
then
echo "$error_msg" >&2
exit 1
fi
current_branch="$(git rev-parse --abbrev-ref HEAD)"
current_branch_ticket="$(echo $current_branch | grep -oh "[[:upper:]]*-[0-9]*" | head -n 1)"
commit_matches_branch_regex="($current_branch_ticket|fixup\!)"
if ! grep -iqE "$commit_matches_branch_regex" "$1";
then
echo "your JIRA ticket number doesn't match the branch name" >&2
exit 1
fi
#!/bin/bash
# Stops accidental commits to master and develop. https://gist.github.com/stefansundin/9059706
BRANCH=`git rev-parse --abbrev-ref HEAD`
if [[ "$BRANCH" == "master" || "$BRANCH" == "develop" ]]; then
echo "You are on branch $BRANCH. Are you sure you want to commit to this branch?"
echo "If so, commit with -n to bypass this pre-commit hook."
exit 1
fi
exit 0
#!/bin/sh
#
# The "pre-rebase" hook is run just before "git rebase" starts doing
# its job, and can prevent the command from running by exiting with
# non-zero status.
#
# The hook is called with the following parameters:
#
# $1 -- the upstream the series was forked from.
# $2 -- the branch being rebased (or empty when rebasing the current branch).
#
# fix the brain dead behaviour of visual studio when reloading modified projects
find -type f -name "*.sln" -exec touch {} +
#!/bin/bash
# Adds JIRA Ticket number to commit messages
# Based on http://waiting-for-dev.github.io/blog/2014/07/19/append-issue-number-to-commit-message-automatically-with-git-hooks/
if ! [ -z ${2+x} ]; then
COMMIT_PREFIX=$(git rev-parse --abbrev-ref HEAD | grep -o '[[:upper:]]*\-[0-9]*' | head -1)
sed -i '' "1s/^/[$COMMIT_PREFIX] /" "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment