Skip to content

Instantly share code, notes, and snippets.

@agalazis
Last active March 27, 2023 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agalazis/0353fc8afa753e1317f1b3d3271a6fb0 to your computer and use it in GitHub Desktop.
Save agalazis/0353fc8afa753e1317f1b3d3271a6fb0 to your computer and use it in GitHub Desktop.
gitlab ticket commit messages
#!/bin/bash
RED='\033[1;31m'
NORMAL='\033[0m'
remove_hook() {
dir=$1
rm "$dir/.git/hooks/prepare-commit-msg"
echo "Removed hook from $dir/.git/hooks/prepare-commit-msg"
}
add_hook() {
curl -s "$1" -o "./.git/hooks/${1##*/}"
echo "Added hook to $dir/.git/hooks/prepare-commit-msg"
}
usage_help(){
cat <<-EOF
Usage:
git-hooks-setup add
git-hooks-setup remove
EOF
}
error(){
echo $RED
$@
echo $NORMAL
return 1
}
if [ ! -f .githooks ]
then
error echo "Please add the applicable gihooks to a .githooks file in this directory"
fi
if [ "$1" == "add" ] || [ "$1" == "remove" ]
then
while IFS= read -r line; do ${add}_hook $line; done < .githooks
return 0
fi
error usage_help
#
# Automatically appends commit message with the branch name.
# Merge commit messages are not prepended.
# Hook is particularly useful when branches are named after gitlab tickets.
#
# Check if the MERGE_HEAD file is present, indicating that a merge is being performed
if [ -f ".git/MERGE_HEAD" ]
then
exit 0
fi
BRANCH_NAME=$(git branch | grep '*' | sed 's/* //')
BRANCH_NAME_WITHOUT_ISSUE_TYPE=${BRANCH_NAME//+([A-z])\//};
TICKET_REFERENCE=${BRANCH_NAME_WITHOUT_ISSUE_TYPE/-[A-z]+([A-z]|-|[0-9])}
TICKET_REFERENCE=${a#REF-}
TICKET_REFERENCE=${a#GL-}
# Get the commit message, removing lines that start with a #
MESSAGE=$(cat "$1" | sed '/^#.*/d')
# Check if the commit message is non-empty
if [ -n "$MESSAGE" ]
then
# Add the branch name and the commit message to the commit message file
echo "$MESSAGE Ref #$TICKET_REFERENCE" > "$1"
else
echo "Aborting commit due to empty commit message."
exit 1
fi
#!/bin/bash
RED=$(tput setab 1)
NORMAL=$(tput sgr0)
CONFLICT_MARKERS='<<<<<<<|=======|>>>>>>>'
CHECK=$(git diff --staged | grep "^+" | grep -Ei "$CONFLICT_MARKERS" -c)
if [ "$CHECK" -gt 0 ]
then
echo "$RED WARNING $NORMAL Conflict markers sill preset"
git diff --name-only -G"$CONFLICT_MARKERS"
# uncomment the below line if you need the commit to not go through at all
# exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment