Skip to content

Instantly share code, notes, and snippets.

@dabrady
Created February 21, 2020 21:50
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 dabrady/29b33ef867ca6f0c5d9a60ae5b36f7fc to your computer and use it in GitHub Desktop.
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
# 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
}
#!/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
@dabrady
Copy link
Author

dabrady commented Feb 21, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment