Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a scripted commit template for git?

We are working on tickets, and when we use the ticket number in the git commit message on the first line, then the ticket is updated with the commit message.

To make things simple we always work on a branch with the commit number.

Now I'd like to be presented with a commit message where the ticket number would already be filled in.

It must be possible, because the branch is already in the commit template, but in comments which are removed by git. I have scoured the docs and the 'net a couple of times, but I must be looking for the wrong words, because I cannot find it.

Can anyone help?

like image 775
Peter Tillemans Avatar asked Aug 19 '10 19:08

Peter Tillemans


People also ask

What is a git commit template?

commit. template allows you to “specify the pathname of a file to use as the template for new commit messages”, which basically means that you can save your commit message template as a file in a project and then configure Git to use it every time you git commit your changes.


2 Answers

You probably want to set up a prepare-commit-msg hook on your local repository. It might look like this (say the branches are named 'work-on-ticket-XXXX':

#!/bin/sh ORIG_MSG_FILE="$1" TEMP=`mktemp /tmp/git-XXXXX`  TICKETNO=`git branch | grep '^\*' | cut -b3-`  (echo "Work on ticket #$TICKETNO"; cat "$ORIG_MSG_FILE") > "$TEMP" cat "$TEMP" > "$ORIG_MSG_FILE" 

Put something like that (marked executable) in .git/hooks/prepare-commit-msg. You may have to adjust and elaborate on it of course.

like image 80
Walter Mundt Avatar answered Sep 28 '22 13:09

Walter Mundt


Looks like you should be able to do this by using the .git/hooks/pre-commit-msg

An simple example of this would be:

#!/bin/sh # $1 contains the file with the commit msg we're about to edit. # We'll just completely clobber it for this example. echo "Hello" > "$1" 

This would make your commit begin with "Hello". Obviously, since it's a script you can work some magic here to fill out your ticket number and any other information. There should be a pre-commit-msg.sample in the .git/hooks/ directory telling you which args the script receives if you need anything else.

like image 37
signine Avatar answered Sep 28 '22 13:09

signine