我正在嘗試設定一個自動版本控制系統,如果我git commit使用 message PATCH: {message},應用程式的補丁版本將自動更新(同樣適用于MINORand的前綴MAJOR)。我正在使用Husky作為我的pre-commit和pre-push鉤子,所以我試圖用一個.husky/commit-msg看起來像這樣的鉤子來讓它作業:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npm run auto-version $1 && git add --all
這可以根據需要使用我的auto-version.js腳本自動讀取提交訊息并進行./version.json相應更新。唯一的問題是提交是用舊version.json檔案創建的,我不確定為什么。我可以說它git add是有效的,因為我在提交后留下了version.json位于該部分的更新檔案Staged Changes。我的.husky/pre-commit鉤子看起來像這樣,并且在提交之前階段更新檔案就好了:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npm run lint && npm run format && git add --all
我認為這可能與commit-msg觸發鉤子和何時git接受新暫存檔案之間的時間有關,但赫斯基沒有提供關于commit-msg鉤子如何運作的優秀檔案。我也嘗試使用鉤子來設定它pre-commit,但是在這個階段新的提交訊息沒有被保存.git/COMMIT_EDITMSG(只有舊的提交訊息存在)。
對于一些額外的背景關系,我們目前只是觸發鉤子,然后在需要時手動更改次要版本和主要版本npm version patch --no-git-tag-version。pre-commit我想制作一個更強大和自動化的系統,這導致了這個阻止程式。
自動版本.js
const { readFileSync, writeFileSync } = require('fs');
const versionJsonPath = './version.json';
const commitMessagePath = '.git/COMMIT_EDITMSG';
const prefixOptions = ['PATCH', 'MINOR', 'MAJOR'];
const postfixMinLength = 12;
(() => {
// read commit message
const rawMessage = readFileSync(commitMessagePath, 'utf-8');
const message = rawMessage.split(/\r?\n/)[0];
console.log(`Reading commit message "${message}"...`);
// check for merge commit
if (message.startsWith('Merge branch')) {
process.exit();
}
// check for core composition
const messageParts = message.split(':');
if (messageParts.length != 2) {
throwError(`Commit message should take the form "{${prefixOptions.join('|')}}: {message}".`);
}
// check for valid prefix
const messagePrefix = messageParts[0];
if (!prefixOptions.includes(messagePrefix)) {
throwError(`Commit message prefix must be one of the following version types: [${prefixOptions.join(', ')}].`);
}
// check for valid postfix
const messagePostfix = messageParts[1];
if (messagePostfix.trim().length < postfixMinLength) {
throwError(`Commit message postfix must be at least ${postfixMinLength} characters.`);
}
// update app version
const versionJson = JSON.parse(readFileSync(versionJsonPath, 'utf-8'));
const oldVersion = versionJson.appVersion;
const versionParts = oldVersion.split('.').map(v => parseInt(v, 10));
if (messagePrefix == 'MAJOR') {
versionParts[0] ;
versionParts[1] = 0;
versionParts[2] = 0;
} else if (messagePrefix == 'MINOR') {
versionParts[1] ;
versionParts[2] = 0;
} else {
versionParts[2] ;
}
const newVersion = versionParts.join('.');
versionJson.appVersion = newVersion;
console.log(`Updating app version from ${oldVersion} to ${newVersion}...`);
writeFileSync(versionJsonPath, JSON.stringify(versionJson));
process.exit();
})();
function throwError(message) {
console.error(message);
process.exit(1);
}
版本.json
{
"appVersion": "0.12.15"
}
uj5u.com熱心網友回復:
查看另一篇文章(Git hook commit-msg git add file),似乎在鉤子階段git add期間/之后無法執行此操作。commit-msg根據那里的一個答案,我通過使用post-commit鉤子來修改提交以包含更新后的version.json檔案,從而解決了這個問題git commit --amend -C HEAD -n version.json。
.husky/提交后
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npm run auto-version-post-commit
該post-commit腳本確保有一個version.json檔案要添加以防止無限 git hook 遞回:
var exec = require('child_process').exec;
const versionJsonPath = 'version.json';
(() => {
exec('git diff --name-only', (error, stdout, stderr) => {
const modifiedFiles = stdout.trim().split(/\r?\n/);
// check if version.json has been modified by commit-msg hook
if (modifiedFiles.includes(versionJsonPath)) {
// amend the last commit to include the updated version.json
exec(`git commit --amend -C HEAD -n ${versionJsonPath}`);
}
});
})();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/514535.html
上一篇:依賴3rd方包
