2013-10-09 82 views
2

我需要做一個commit-msg掛鉤來檢查提交消息是否包含「app.asana」的任何部分。我搜索了一些引用和文檔,我知道我需要使用commit-msg。我必須使用Perl或Bash來做到這一點。Git commit hook - 如何使用commit-msg掛鉤檢查消息中的字符串?

有沒有人有關於這個或某處的線索我可以看看更多的例子來學習如何做到這一點?

謝謝。

+0

[This](https://www.kernel.org/pub/software/scm/git/docs/githooks.html)可能會有所幫助。 – devnull

+0

我已經到過這個頁面,但是謝謝。經過一些更多的研究之後,我做到了......實際上很簡單。 – lol

回答

8

我找到了我的問題的答案。如果它可以幫助別人......

我剛剛創建了一個commit-msg文件中包含.git/hooks

#!/bin/sh 

test -n "$(grep 'app.asana.com/' ${1})" || { 
     echo >&2 "ERROR: Commit message is missing Asana's task link.\n\nPlease append the Asana's task link relative to this commit into the commit message." 
     exit 1 
} 

每個用戶都需要有內部.git/hooks一個commit-msg文件。然後,作爲一個解決方案,我添加commit-msg到項目文件夾(這樣我就可以拉這個)與另一個文件名爲commit-msg-hook.sh包含:

#!/bin/sh 

cp commit-msg .git/hooks/commit-msg 
chmod +x .git/hooks/commit-msg 
echo "\nThe commit-msg hook was added to git hooks" 

我歡迎任何意見,以改善我做了什麼。謝謝。

+1

只是我還是第一個代碼段中的第二行不需要/不工作。 'chmod a + x .git/hooks/commit-msg'就是爲了讓它和git一起運行而提交commit-msg可執行文件。但是,如果文件(還有那個命令)沒有'+ x',那麼它將永遠無法執行,因此將這個命令包含在實際的文件中是沒有意義的。這篇文章是從2013年開始的,所以任何人都請糾正我,如果我錯了。當然有人注意到了這一點? – redfox05

+0

@Russell你是對的。我修正了它,它正在同行審查atm。 – greduan