如何将Git的分支名称添加到提交消息中?

时间:2011-05-05 08:47:32

标签: git bash git-branch

我需要一些Bash脚本的帮助,它会自动将git的分支名称添加为提交消息中的哈希值。

10 个答案:

答案 0 :(得分:157)

这是我的commit-msg脚本作为示例:

#!/bin/sh
#
# Automatically adds branch name and branch description to every commit message.
#
NAME=$(git branch | grep '*' | sed 's/* //') 
DESCRIPTION=$(git config branch."$NAME".description)

echo "$NAME"': '$(cat "$1") > "$1"
if [ -n "$DESCRIPTION" ] 
then
   echo "" >> "$1"
   echo $DESCRIPTION >> "$1"
fi 

创建以下提交消息:

[branch_name]: [original_message]

[branch_description]

我使用的问题编号为branch_name,使用branch_description命令将问题说明放在git branch --edit-description [branch_name]上。

有关此Q&A可以找到的分支说明的更多信息。

代码示例存储在以下Gist

答案 1 :(得分:47)

使用prepare-commit-msgcommit-msg githook

您的PROJECT/.git/hooks/目录中已有示例。

作为安全措施,您必须在要使用它的每个存储库上手动启用此类挂钩。但是,您可以提交脚本并将其复制到所有克隆中的.git/hooks/目录。

答案 2 :(得分:27)

一个更简单的脚本,在您编辑之前将分支名称添加到提交消息。因此,如果您想要更改或删除它,您可以。

创建此文件 .git / hooks / prepare-commit-msg

#!/bin/bash

branchPath=$(git symbolic-ref -q HEAD) #Somthing like refs/heads/myBranchName
branchName=${branchPath##*/}      #Get text behind the last / of the branch path

firstLine=$(head -n1 $1)

if [ -z "$firstLine"  ] ;then #Check that this is not an amend by checking that the first line is empty
    sed -i "1s/^/$branchName: \n/" $1 #Insert branch name at the start of the commit message file
fi

答案 3 :(得分:25)

您可以使用prepare-commit-msg和预提交挂钩的组合来完成它。

<强>的.git /钩/准备提交-MSG

#!/bin/sh

BRANCH=`git branch | grep '^\*' | cut -b3-`
FILE=`cat "$1"`
echo "$BRANCH $FILE" > "$1"

<强>的.git /钩/预提交

#!/bin/bash

find vendor -name ".git*" -type d | while read i
do
        if [ -d "$i" ]; then
                DIR=`dirname $i`
                rm -fR $i
                git rm -r --cached $DIR > /dev/null 2>&1
                git add $DIR > /dev/null 2>&1
        fi
done

设置权限

sudo chmod 755 .git/hooks/prepare-commit-msg
sudo chmod 755 .git/hooks/pre-commit

答案 4 :(得分:9)

在prepare-commit-msg文件中添加以下代码。

#!/bin/sh
#
# Automatically add branch name and branch description to every commit message except merge commit.
#

COMMIT_EDITMSG=$1

addBranchName() {
  NAME=$(git branch | grep '*' | sed 's/* //') 
  DESCRIPTION=$(git config branch."$NAME".description)
  echo "[$NAME]: $(cat $COMMIT_EDITMSG)" > $COMMIT_EDITMSG
  if [ -n "$DESCRIPTION" ] 
  then
     echo "" >> $COMMIT_EDITMSG
     echo $DESCRIPTION >> $COMMIT_EDITMSG
  fi 
}

MERGE=$(cat $COMMIT_EDITMSG|grep -i 'merge'|wc -l)

if [ $MERGE -eq 0 ] ; then
  addBranchName
fi

它将添加分支名称以提交除merge-commit之外的消息。 合并提交默认情况下具有分支信息,因此不需要额外的分支名称,并使消息变得丑陋。

答案 5 :(得分:3)

受Tim的答案启发,该答案建立在最佳答案的基础上,结果是prepare-commit-msg钩子作为argument what kind of commit is occurring。如默认的prepare-commit-msg中所示,如果$ 2是'merge',那么它是一个合并提交。因此可以改变case开关以包括Tim的addBranchName()函数。

我已经包含了我自己的偏好,即如何添加分支名称以及默认prepare-commit-msg.sample挂钩的所有未注释部分。

  

准备提交-MSG

#!/bin/sh

addMyBranchName() {
  # Get name of current branch
  NAME=$(git branch | grep '*' | sed 's/* //')

  # First blank line is title, second is break for body, third is start of body
  BODY=`cut -d \| -f 6 $1 | grep -v -E .\+ -n | cut -d ':' -f1 | sed '3q;d'`

  # Put in string "(branch_name/): " at start of commit message body.
  # For templates with commit bodies
  if test ! -z $BODY; then
    awk 'NR=='$BODY'{$0="\('$NAME'/\): "}1;' $1 > tmp_msg && mv tmp_msg "$1"
  else
    echo "title\n\n($NAME/):\n`cat $1`\n" > "$1"
  fi
}

# You might need to consider squashes
case "$2,$3" in
  # Commits that already have a message
  commit,?*)
  ;;

  # Messages are one line messages you decide how to handle
  message,)
  ;;

  # Merge commits
  merge,)
    # Comments out the "Conflicts:" part of a merge commit.
    perl -i.bak -ne 's/^/# /, s/^# #/#/ if /^Conflicts/ .. /#/; print' "$1"
  ;;

  # Non-merges with no prior messages
  *)
    addMyBranchName $1
  ;;
esac

答案 6 :(得分:2)

如果要使其全局(对于所有项目):

使用shytikov's answer的内容创建mkdir -p ~/.git_hooks # make it executable chmod a+x ~/.git_hooks/commit-msg 文件,并将其放在某个文件夹中:

git config --global init.templatedir '~/.git_hooks'

现在启用挂钩:

git init
在每个要使用它的项目中再次

java -jar jenkins-cli.jar -s http://localhost:8080 -i ~/.ssh/id_rsa build RTT/RTT-CI-Tools/RTT-CI-Tools-Distribute -s -p SLAVE_REGEX=testserver

答案 7 :(得分:2)

如果要将JIRA票证添加到提交消息中,请使用下面的脚本。

提交类似PROJECT-2313: Add awesome feature的消息 这需要您的分支机构名称以jira Ticket开头。

这是以下解决方案的组合:

使用sed -i '.bak'为OS X进行了修改,并且在SourceTree中也可以正常工作。

https://gist.github.com/georgescumihai/c368e199a9455807b9fbd66f44160095

#!/bin/sh
#
# A hook script to prepare the commit log message.
# If the branch name it's a jira Ticket.
# It adds the branch name to the commit message, if it is not already part of it.

branchPath=$(git symbolic-ref -q HEAD) #Somthing like refs/heads/myBranchName
branchName=${branchPath##*/}      #Get text behind the last / of the branch path

regex="(PROJECTNAME-[0-9]*)"

if [[ $branchName =~ $regex ]]
then
    # Get the captured portion of the branch name.
    jiraTicketName="${BASH_REMATCH[1]}"

    originalMessage=`cat $1`

    # If the message already begins with PROJECTNAME-#, do not edit the commit message.
    if [[ $originalMessage == $jiraTicketName* ]]
        then
        exit
    fi

    sed -i '.bak' "1s/^/$jiraTicketName: /" $1 #Insert branch name at the start of the commit message file
fi

答案 8 :(得分:1)

由于它使用BSD sed而不是GNU sed,因此我遇到了使这些解决方案适用于MacOS的问题。我设法创建了一个简单的脚本来完成这项工作。仍在使用.git/hooks/pre-commit

#!/bin/sh
BRANCH=$(cat .git/HEAD  | cut -d '_' -f2)
if [ ! -z "$BRANCH" ]
then
    echo "$BRANCH" > "/Users/username/.gitmessage" 
else
    echo "[JIRA NUMBER]" > "/Users/username/.gitmessage"
fi 

这假定分支命名标准类似于functional-desc_JIRA-NUMBER。如果您的分行名称只是您的Jira票号,您可以简单地删除从管道到f2的所有内容。它还要求您的主目录中有一个名为.gitmessage的文件。

答案 9 :(得分:-3)

没有理由grep然后管道sed。

由于我们的分支是我们的jira问题,我们总是必须将我们的jira添加到提交消息(与分支名称相同)。

   "Principal": {
      "Service":  [
            "batch.amazonaws.com"
      ]
    },