一、githooks許可權
Git Hooks是在Git版本控制的基礎上,提供了一些鉤子(hooks),可以在一些關鍵的節點上執行特定的自定義腳本。這些腳本代碼會自動執行,不需要手動操作,非常方便。而Git Hooks的許可權管理主要分為兩種:
第一種是全局許可權,表示該Git Hooks是系統級別的,並會對所有的倉庫生效。可以在用戶主目錄下的.gitconfig文件中對其進行配置設置。
第二種是局部許可權,表示該Git Hooks只在特定的倉庫中生效。可以在該倉庫下的.git/hooks目錄中對其進行配置。這種方式的優先順序高於全局許可權,如果同一倉庫下全局和局部同時配置了Git Hooks,就會以局部的為準。
二、githooks husky
Husky是一款流行的Git Hooks工具,可以幫助我們更方便地管理項目中的Git Hooks腳本。Husky提供了一系列鉤子命令,比如pre-commit、pre-push、commit-msg等,在特定的事件節點上自動執行我們編寫的腳本。
使用Husky還需要先安裝husky包,執行npm install husky -D即可。安裝完成後在package.json文件中對husky進行配置(也可以在.git/hooks中手動創建腳本):
{
"husky": {
"hooks": {
"pre-commit": "npm run lint",
"prepare-commit-msg": "node build/scripts/prepare-commit-message.js"
}
}
}
三、githooks沒執行
有時候我們在執行Git Hooks的腳本時,會遇到腳本沒有執行的情況。這種情況可能是因為Git Hooks腳本沒有執行許可權。我們可以通過執行chmod +x .git/hooks/*命令,為所有的Git Hooks腳本添加可執行許可權。如果想要針對單個腳本添加許可權,可以執行chmod +x .git/hooks/pre-commit命令。
四、githooks發送郵件
Git Hooks還可以配合其他的工具來實現更加複雜的操作,比如發送郵件通知。我們可以通過使用Git Hooks中的post-receive鉤子,在代碼推送後觸發一個腳本,來實現發送郵件的功能。
發送郵件的腳本可以使用node執行,內容可以參考下面的代碼片段:
#!/usr/bin/env node
const child_process = require("child_process");
const nodemailer = require("nodemailer");
// 獲取git提交信息
const commitMsg = child_process.execSync("git log -1 --pretty=tformat:%s").toString().trim();
// 發送郵件
const transporter = nodemailer.createTransport({
host: "smtp.ethereal.email",
port: 587,
auth: {
user: "",
pass: "",
},
});
const mailOptions = {
from: "",
to: "",
subject: "New commit from Git repository",
text: commitMsg,
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
五、githooks檢驗提交信息
有時候我們希望在提交代碼時,對提交信息進行檢驗,比如限制提交信息的長度、格式等等。通過使用Git Hooks中的commit-msg鉤子,我們可以在提交代碼之前執行一個腳本來實現檢驗。commit-msg腳本會在Git中的message文件中讀取提交信息,可以對提交信息進行修改或者檢驗。如果提交信息不符合要求,可以通過exit 1命令來制止提交操作。
下面是一個檢驗提交信息格式的示例代碼:
#!/usr/bin/env node
const fs = require('fs');
const COMMIT_MSG_FILE = process.argv[2];
const commitMsg = fs.readFileSync(COMMIT_MSG_FILE, 'utf-8');
const pattern = /^([A-Z]+-[0-9]+): (.*[^\.])$/gm;
if (!pattern.test(commitMsg)) {
console.log('Invalid commit message! Format should be: : ');
process.exit(1);
}
六、githooks怎麼停止push
有時候我們在執行Git Push操作時,可能會遇到一些問題,比如代碼有衝突、不符合規範等等。在這種情況下,我們希望能夠停止Push的操作,以免引起更加嚴重的問題。這時,我們可以使用Git Hooks中的pre-push鉤子,在Push操作之前執行一些檢查,如果檢查失敗,就可以採取措施終止Push操作。
下面是一個終止Push操作的示例代碼:
#!/bin/bash
while read local_ref local_sha1 remote_ref remote_sha1
do
if [[ "$local_ref" = "refs/heads/master" ]]; then
echo "Push to master branch is disabled!"
exit 1
fi
done
七、githook設置commit-msg
在一些項目中,規範的提交信息是非常重要的。為了保證每個開發者提交的信息都符合規範,我們可以使用Git Hooks中的commit-msg鉤子,在提交代碼之前執行檢驗。一般情況下,我們會規定提交信息的格式,比如以項目編號+冒號+提交信息的方式提交。commit-msg鉤子腳本可以自動驗證提交信息的格式是否符合規範,如果不符合就停止提交。
下面是一個設置commit-msg格式的示例代碼:
#!/bin/bash
COMMIT_REGEX='^[A-Z]+-[0-9]+: ';
while read line
do
if ! [[ "$line" =~ $COMMIT_REGEX ]]; then
echo "Invalid commit message format. Should be ': '."
exit 1
fi
done < "${1:-/dev/stdin}"
八、Git Hooks代碼檢查
在代碼開發過程中,代碼風格的一致性和代碼質量的保證非常重要。我們可以使用Git Hooks中的pre-commit鉤子,在代碼提交之前執行一些代碼檢查腳本,來保證代碼的質量和一致性。常用的代碼檢查工具有ESLint、Prettier、Stylelint等。
下面是一個使用ESLint進行代碼檢查的示例代碼:
#!/bin/bash
FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.jsx\?$')
# ESLint check
if [[ $FILES = "" ]] ; then
exit 0
fi
pass=true
for FILE in ${FILES}; do
git show ":$FILE" | eslint "$FILE"
if [[ "$?" != 0 ]]; then
pass=false
fi
done
if ! $pass; then
echo "ESLint check failed, please fix errors before committing."
exit 1
fi
原創文章,作者:KTUK,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/141817.html