Skip to content

Instantly share code, notes, and snippets.

@hassansin
Last active March 2, 2023 01:06
Show Gist options
  • Save hassansin/5a7fd3ff2d4f69f4656ad84c332303c8 to your computer and use it in GitHub Desktop.
Save hassansin/5a7fd3ff2d4f69f4656ad84c332303c8 to your computer and use it in GitHub Desktop.
Clubhouse - Git Hooks
#!/bin/sh
# If commit message does not refer to any CH story number,
# this hook will parse the CH story from the current branch and
# append it to the commit message
#
# Example:
# Branch: hassansin/ch1643/custom-subdomains
# commit message: add support for EU base urls
# appended commit message: Add support for EU base urls [ch1643]
#
# Install
# curl -s -o ./.git/hooks/prepare-commit-msg https://gist.githubusercontent.com/hassansin/5a7fd3ff2d4f69f4656ad84c332303c8/raw/effbf687b261980fa5e58530f43fb916030bf772/prepare-commit-msg.sh
# chmod +x ./.git/hooks/prepare-commit-msg
#
parse_issue () {
echo "$(echo "$1" | sed -n "s/.*\/\(ch[[:digit:]]\+\)\/.*/\1/p")"
}
to_upper(){
echo "$1" | sed -e "s/\(^.\)/\u\1/g"
}
source=$2
commit_msg=$(to_upper "$(cat "$1")") #uppercase first character
branch="$(git symbolic-ref --short HEAD)"
commit_issue=$(parse_issue "$(echo "$commit_msg" | grep "^[^#;]" )" ) #ignore comment lines when parsing issue
branch_issue=$(parse_issue "$branch")
# when source is cli message
if [ "$source" = "message" ] && [ -n "$branch_issue" ] && [ "$branch_issue" != "$commit_issue" ]
then
new_commit_msg="$commit_msg [$branch_issue]"
echo "$new_commit_msg" > "$1"
#when source is empty and editor will be brought up
elif [ -z "$source" ] && [ "$GIT_EDITOR" != ":" ] && [ -n "$branch_issue" ] && [ "$branch_issue" != "$commit_issue" ]
then
echo "$commit_msg [$branch_issue]" > "$1"
else
echo "$commit_msg" > "$1"
fi
#!/bin/sh
# If commit message does not refer to any CH story number,
# this hook will parse the CH story from the current branch and
# append it to the commit message
#
# Example:
# Branch: hassansin/ch1643/custom-subdomains
# commit message: add support for EU base urls
# appended commit message: Add support for EU base urls [ch1643]
#
# Install
# curl -s -o ./.git/hooks/prepare-commit-msg https://gist.githubusercontent.com/hassansin/5a7fd3ff2d4f69f4656ad84c332303c8/raw/effbf687b261980fa5e58530f43fb916030bf772/prepare-commit-msg.sh
# chmod +x ./.git/hooks/prepare-commit-msg
#
parse_issue () {
echo "$(echo "$1" | sed -n -E "s/(PHL-[[:digit:]]+).*/\1/p")"
}
to_upper(){
echo "$1" | sed -e "s/\(^.\)/\u\1/g"
}
source=$2
commit_msg=$(to_upper "$(cat "$1")") #uppercase first character
branch="$(git symbolic-ref --short HEAD)"
commit_issue=$(parse_issue "$(echo "$commit_msg" | grep "^[^#;]" )" ) #ignore comment lines when parsing issue
branch_issue=$(parse_issue "$branch")
# when source is cli message
if [[ "$source" =~ message|commit ]] && [ -n "$branch_issue" ] && [ "$branch_issue" != "$commit_issue" ]
then
new_commit_msg="$branch_issue $commit_msg "
echo "$new_commit_msg" > "$1"
#when source is empty and editor will be brought up
elif [ -z "$source" ] && [ "$GIT_EDITOR" != ":" ] && [ -n "$branch_issue" ] && [ "$branch_issue" != "$commit_issue" ]
then
echo "$branch_issue $commit_msg" > "$1"
else
echo "$commit_msg" > "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment