Skip to content

Instantly share code, notes, and snippets.

@huguesbr
Created October 7, 2014 04:39
Show Gist options
  • Save huguesbr/927c75575cf8eb88f283 to your computer and use it in GitHub Desktop.
Save huguesbr/927c75575cf8eb88f283 to your computer and use it in GitHub Desktop.
Git Hook - prepare-commit-msg - auto suffix commit message with issue id
#!/usr/bin/ruby
#
# Called by "git commit" with the
# name of the file that has the
# commit message, followed by
# the description of the commit
# message's source. The hook's
# purpose is to edit the commit
# message file. If the hook fails
# with a non-zero status,
# the commit is aborted.
#
# install
# cp prepare-commit-msg ./.git/hooks/
# auto suffix every commit with issue id
# extract issue id from branch name
# git checkout "100-update-ui"
# ...
# git commit -am "updating ui"
# git log --pretty=format:"%s" -1
# > #100 - updating ui
message_file = ARGV[0]
message = File.read(message_file)
branch_name = %x( git symbolic-ref --short HEAD )
if match = /^([^\/]+\/)?(?<issue_id>[0-9]+)-/.match(branch_name)
File.open(message_file, 'w') { |f| f.write "##{match[:issue_id]} - #{message}" }
end
@Bhacaz
Copy link

Bhacaz commented Aug 10, 2017

Exactly what I was looking for. Nice work!

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