Created
February 21, 2020 21:50
-
-
Save dabrady/29b33ef867ca6f0c5d9a60ae5b36f7fc to your computer and use it in GitHub Desktop.
A pre-commit Git hook for adding info about the currently tracked JIRA ticket
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
# An example approach to managing the JIRA ticket you are currently working on. | |
# This function simply manipulates an environment variable that can be read by other processes. | |
cj () { | |
local jira_config=$HOME/.jira | |
[[ -z "$jira_config" ]] || touch $jira_config # ensure it exists | |
. $jira_config # refresh the JIRA variable in the current session | |
local jira="$1" | |
# If no ticket specified, print the one currently being tracked. | |
[[ -z "$jira" ]] && echo "Now tracking $JIRA_CURRENT." && return 0 | |
# If the special character '-' was provided, switch to the previously tracked ticket. | |
if [[ $jira == '-' ]] | |
then | |
jira=$JIRA_PREVIOUS | |
fi | |
# If a new ticket is specified, start tracking it and save the old one for easy task switching. | |
if [[ $JIRA_CURRENT != $jira ]] | |
then | |
gsed -i "/JIRA_PREVIOUS/s/=.*/=$JIRA_CURRENT/" $jira_config | |
gsed -i "/JIRA_CURRENT/s/=.*/=$jira/" $jira_config | |
. $jira_config | |
echo "Now tracking $jira." | |
fi | |
} |
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
#!/usr/bin/env sh | |
# (From the docs for this hook) | |
# $1 - name of the file that contains the commit log message | |
# $2 - source of the commit message if not from editor (one of: 'message', 'template', 'merge', 'squash', 'commit') | |
# $3 - if $2 is 'commit', this will be the commit SHA-1 | |
# Uncoment the conditional block to only apply logic to messages that will be passed to an editor. | |
#if [[ -z ${2+x} ]]; then | |
# Appends the text "[Contributes to <ticket number>]" to your commit message, where '<ticket number>' | |
# is a value in an environment variable managed by the `cj` function defined in `jira_functions.sh` | |
gsed -i "1s/^/\n\n[Contributes to $JIRA_CURRENT]\n/" "$1" | |
#fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
prepare-commit-msg
file goes in your.git/hooks
directory within the repo you wish it to apply to. See https://githooks.com/ for more details.Alternatively/additionally, you can put it in the
hooks
folder of your Git template directory, and it will be added automatically to the.git
folder of every repo you create/clone after that point. See https://git-scm.com/docs/git-init#_template_directory for more details.