Append ticket number using git commit hooks?

前端 未结 6 1616
[愿得一人]
[愿得一人] 2020-12-24 02:21

So my branch is named after bugtracker ticket number, something like \"issue-1234\", and we have a convention to always write down ticket number in commit message. I\'m wond

6条回答
  •  [愿得一人]
    2020-12-24 03:02

    You can as well use prepare-commit-msg hook, which accepts more parameters than commit-msg. Then you can check if the message is coming from a file, a template, etc to avoid appending the issue numbers when you don't want it.

    With the following script in .git/hooks/prepare-commit-msg when you are working in a feature branch named foo-123, then [#123] will be added to the third line of every commit you make.

    More information in this post I wrote

    #!/bin/sh
    
    if [ x = x${2} ]; then
      BRANCH_NAME=$(git symbolic-ref --short HEAD)
      STORY_NUMBER=$(echo $BRANCH_NAME | sed -n 's/.*-\([0-9]\)/\1/p')
      if [ x != x${STORY_NUMBER} ]; then
        sed -i.back "1s/^/\n\n[#$STORY_NUMBER]/" "$1"
      fi
    fi
    

提交回复
热议问题