2016-08-03 89 views
1

我想用prepare-commit-msg鉤子。我使用的功能和bug修正分支(功能/ ISSUEKEY-23123-一些特徵),我想前面加上ISSUEKEY-23123到commit消息:git - 用部分分支名稱擴展commit hook

#!/bin/bash 
BRANCH_NAME=$(git branch | grep '*' | sed 's/* //') 
STR=`echo $BRANCH_NAME | grep -E 'ISSUEKEY-[0-9]*' -o` 
if [[ $BRANCH_NAME == *"feature"* ]] || [[ $BRANCH_NAME == *"bugfix"* ]] 
then 
    echo $STR > $1 
fi 

這工作,但它摒棄了標準的消息v正顯示出我要提交,如:

# Please enter the commit message for your changes. Lines starting 
# with '#' will be ignored, and an empty message aborts the commit. 
# Explicit paths specified without -i or -o; assuming --only paths... 
# On branch feature/ISSUEKEY-1716-model-implement 
# Your branch is based on 'origin/feature/ISSUEKEY-1716-model-implement', but the upstream is gone. 
# (use "git branch --unset-upstream" to fixup) 
# 
# Changes to be committed: 
#  new file: asd 
# 
# Untracked files: 
#  FSTD-1716 
#  TMP 
# 

有沒有辦法要麼預先考慮STR到輸出,或撥打一個git命令什麼打印我所提到的標準提交信息?

打開的提交信息應該是:

ISSUEKEY-1716 
# Please enter the commit message for your changes. Lines starting 
# with '#' will be ignored, and an empty message aborts the commit. 
# Explicit paths specified without -i or -o; assuming --only paths... 
# On branch feature/ISSUEKEY-1716-model-implement 
# Your branch is based on 'origin/feature/ISSUEKEY-1716-model-implement', but the upstream is gone. 
# (use "git branch --unset-upstream" to fixup) 
# 
# Changes to be committed: 
#  new file: asd 
# 
# Untracked files: 
#  FSTD-1716 
#  TMP 
# 

回答

0

我固定它:

#!/bin/bash 

BRANCH_NAME=$(git branch | grep '*' | sed 's/* //') 
COMMIT_MSG=`echo $BRANCH_NAME | grep -E 'ISSUEKEY-[0-9]*' -o` 
if [[ $BRANCH_NAME == *"feature/"* ]] || [[ $BRANCH_NAME == *"bugfix/"* ]] 
then 
    PRE_COMMIT_MSG=$(cat $1) 
    if [ -z "$COMMIT_MSG" ] ; then 
     exit 0 
    else 
     COMMIT_MSG="$COMMIT_MSG - $PRE_COMMIT_MSG" 
     echo "$COMMIT_MSG" > $1 
    fi 
fi 
2

the githooks documentation引用:

該鉤子被GIT中調用後立即提交準備默認日誌消息,並在編輯器啓動之前。 ...掛鉤的目的是編輯到位消息文件,並...

(粗體礦)。這意味着你必須按照它所說的去做:編輯文件。不要只覆蓋它!因此,而不是:

echo $STR > $1 

你可以這樣做:

ed - "$1" << end 
0a 
$STR 
. 
w 
q 
end 

運行在由指令線0後添加的$STR的擴張「這裏文件」腳本ed編輯器,然後寫並退出編輯器。 (您可以使用任何編輯; sed是流行在這裏。)

順便說一句,不這樣做:

BRANCH_NAME=$(git branch | grep '*' | sed 's/* //') 

,因爲它過於依賴各種「瓷都」命令輸出的樣式。相反,使用git symbolic-ref(通常是腳本的首選方法)或git rev-parse。這兩者之間的區別是有點哲理:

  • git symbolic-ref <options> HEAD讓你分支你目前的名稱,或者如果你是在特殊的匿名「分離的頭」的情況下失敗。 (您想在這裏<options>只是--short省略refs/heads/前綴)。

  • git rev-parse <options> HEAD主要是保證產生某種成功的名字,因爲HEAD總是有效的名稱,因此將對應東西。這意味着將HEAD轉換爲修訂哈希ID,但使用--symbolic選項時,它將留下一個「儘可能符號」的名稱,並且在--abbrev-ref的情況下它也會將refs/heads/刪除。

的主要區別是,雖然symbolic-ref方法執行rev-parse方法不當HEAD是「分離」失敗。失敗讓你區分這種情況,這有時很重要。出於您的目的,這不是那麼重要,並且在使用symbolic-ref時,您需要爲「當前分支的無名字」分離的HEAD案例進行回退。

因此,你想要麼:

BRANCH_NAME=$(git rev-parse --symbolic --abbrev-ref HEAD) 

或:

BRANCH_NAME=$(git symbolic-ref --short HEAD || echo HEAD) 

(奇怪的是,這些都是一模一樣的一些命令字節)。


這裏有一個角落的情況。雖然HEAD總是有效的 - 如果它無效,你沒有一個Git倉庫(Git會給你一個「致命的:」消息來達到這個效果) - 如果你在一個「未出生的分支」上,HEAD是一個對不存在的分支名稱的符號引用。在這種特殊情況下,git symbolic-ref成功git rev-parse HEAD失敗,這是從更常見的情況下分離的HEAD倒退。