Can I use a scripted commit template for git?

前端 未结 2 1721
生来不讨喜
生来不讨喜 2021-01-31 08:15

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 thi

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-31 08:53

    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.

提交回复
热议问题