🔧 chore(hooks): 提交前同步远程并静默无效URL测试日志

This commit is contained in:
Abner
2026-04-18 00:36:30 +08:00
parent ca76df59a2
commit a1fa26dc40
4 changed files with 61 additions and 20 deletions

View File

@@ -71,6 +71,39 @@ function getTouchedProtectedFiles() {
return output.split('\0').filter(Boolean);
}
function getUpstreamRef() {
try {
return git([
'rev-parse',
'--abbrev-ref',
'--symbolic-full-name',
'@{u}'
]).trim();
} catch (_) {
return '';
}
}
function syncWithRemote() {
const upstreamRef = getUpstreamRef();
if (!upstreamRef) {
console.log(' 当前分支未配置 upstream跳过远程同步');
return;
}
console.log(`🔄 提交前同步远程更新(${upstreamRef}...`);
try {
git(['pull', '--rebase', '--autostash', '--quiet'], {
stdio: 'inherit'
});
console.log('✅ 远程更新同步完成');
} catch (_) {
console.error('\n❌ pre-commit 远程同步失败:');
console.error(' - 请先手动执行 `git pull --rebase` 解决冲突后再提交。');
process.exit(1);
}
}
function shouldFormat(relPath) {
const ext = path.extname(relPath).toLowerCase();
if (TEXT_EXTENSIONS.has(ext)) {
@@ -191,6 +224,8 @@ function main() {
process.exit(1);
}
syncWithRemote();
const stagedFiles = getStagedFiles();
if (stagedFiles.length === 0) {
process.exit(0);