markdown auto-prepend-jira-id-to-git-commit-msg.md

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown auto-prepend-jira-id-to-git-commit-msg.md相关的知识,希望对你有一定的参考价值。

# Automatically Prepend a Jira Issue ID to Git Commit Messages
> Use a [git hook](https://git-scm.com/docs/githooks) to match a Jira issue ID from the current branch, and prepend it to every commit message

Assuming the current branch contains a Jira issue ID, you can use a git hook script to prepend it to every commit message.

1. Create an empty *commit-msg* git hook file, and make it executable. From your project's root directory:

        install -b -m 755 /dev/null .git/hooks/commit-msg
    
1. Save the following script to the newly-created *.git/hooks/commit-msg* file:

<!-- scriptstart -->
```bash
#!/bin/sh

COMMIT_FILE=$1
COMMIT_MSG=$(cat $1)
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
JIRA_ID=$(echo "$CURRENT_BRANCH" | grep -Eo "[A-Z0-9]{1,10}-?[A-Z0-9]+-\d+")

if [ ! -z "$JIRA_ID" ]; then
    echo "$JIRA_ID $COMMIT_MSG" > $COMMIT_FILE
    echo "JIRA ID '$JIRA_ID', matched in current branch name, prepended to commit message. (Use --no-verify to skip)"
fi
```
<!-- scriptend -->

## But I'm Lazy

Me too! Here's a one-liner to install the hook above:

```bash
if [ -d .git/hooks ]; then HOOKF=.git/hooks/commit-msg && if [ ! -f $HOOKF ]; then TMPF=`date +%s`-ghook && curl -s https://gist.githubusercontent.com/robatron/01b9a1061e1e8b35d270/raw > $TMPF && STARTL=`grep -n "scriptstart" $TMPF | head -1 | cut -d: -f1` && ENDL=`grep -n "scriptend" $TMPF | head -1 | cut -d: -f1` && sed -n `expr \`echo $STARTL\` + 2`,`expr \`echo $ENDL\` - 2`p $TMPF > $HOOKF && chmod 755 $HOOKF; rm $TMPF; else echo "Error: $HOOKF exists"; fi else echo "Error: Not a git repo"; fi
```

## Enable Hooks Globally

To enable git hooks for all new git projects, you can use your own git template directory to build out your hooks. See the following article:

https://coderwall.com/p/jp7d5q/create-a-global-git-commit-hook

## References
- https://git-scm.com/docs/githooks
- https://confluence.atlassian.com/display/STASHKB/Integrating+with+custom+JIRA+issue+key

以上是关于markdown auto-prepend-jira-id-to-git-commit-msg.md的主要内容,如果未能解决你的问题,请参考以下文章

转换rst到markdown总结

markdown [Markdown HowTo]作为Markdown语法的秘籍

python markdown干啥用的

markdown前端渲染

如何用markdown生成目录

markdown排版示例