pref(script): 优化生产分支合并脚本以支持暂存未提交更改

master
钟良源 2 months ago
parent 6c27dc8fa4
commit e8a3b8ddfa

@ -30,21 +30,20 @@ function run() {
log(' 合并 master 到 production 分支', 'cyan'); log(' 合并 master 到 production 分支', 'cyan');
log('========================================\n', 'cyan'); log('========================================\n', 'cyan');
// 1. 检查是否有未提交的更改 // 获取当前分支名
log('>>> 检查工作区状态...', 'yellow');
const status = exec('git status --porcelain', { silent: true });
if (status.trim()) {
log('\n错误: 工作区有未提交的更改,请先提交或暂存', 'red');
log('运行 git status 查看详情\n', 'red');
process.exit(1);
}
log('工作区干净\n', 'green');
// 2. 获取当前分支名
const currentBranch = exec('git branch --show-current', { silent: true }).trim(); const currentBranch = exec('git branch --show-current', { silent: true }).trim();
log(`当前分支: ${currentBranch}\n`, 'cyan'); log(`当前分支: ${currentBranch}\n`, 'cyan');
// 3. 拉取最新代码 // 暂存未提交的更改
const status = exec('git status --porcelain', { silent: true });
const hasChanges = status.trim().length > 0;
if (hasChanges) {
log('>>> 暂存未提交的更改...', 'yellow');
exec('git stash push -m "暂存未提交的更改"');
log('已暂存\n', 'green');
}
// 拉取最新代码
log('>>> 拉取远程最新代码...', 'yellow'); log('>>> 拉取远程最新代码...', 'yellow');
try { try {
exec('git fetch origin'); exec('git fetch origin');
@ -54,7 +53,7 @@ function run() {
} }
log('拉取完成\n', 'green'); log('拉取完成\n', 'green');
// 4. 切换到 master 并更新 // 切换到 master 并更新
log('>>> 切换到 master 分支并更新...', 'yellow'); log('>>> 切换到 master 分支并更新...', 'yellow');
try { try {
exec('git checkout master'); exec('git checkout master');
@ -65,7 +64,7 @@ function run() {
} }
log('master 分支已更新\n', 'green'); log('master 分支已更新\n', 'green');
// 5. 切换到 production 并更新 // 切换到 production 并更新
log('>>> 切换到 production 分支并更新...', 'yellow'); log('>>> 切换到 production 分支并更新...', 'yellow');
try { try {
exec('git checkout production'); exec('git checkout production');
@ -78,7 +77,7 @@ function run() {
} }
log('production 分支已更新\n', 'green'); log('production 分支已更新\n', 'green');
// 6. 合并 master 到 production // 合并 master 到 production
log('>>> 合并 master 到 production...', 'yellow'); log('>>> 合并 master 到 production...', 'yellow');
try { try {
exec('git merge master --no-edit'); exec('git merge master --no-edit');
@ -97,7 +96,7 @@ function run() {
} }
log('合并成功\n', 'green'); log('合并成功\n', 'green');
// 7. 推送到远程 // 推送到远程
log('>>> 推送到远程 production 分支...', 'yellow'); log('>>> 推送到远程 production 分支...', 'yellow');
try { try {
exec('git push origin production'); exec('git push origin production');
@ -107,10 +106,17 @@ function run() {
} }
log('推送成功\n', 'green'); log('推送成功\n', 'green');
// 8. 切换回原分支 // 切换回原分支
log(`>>> 切换回原分支 ${currentBranch}...`, 'yellow'); log(`>>> 切换回原分支 ${currentBranch}...`, 'yellow');
exec(`git checkout ${currentBranch}`, { ignoreError: true }); exec(`git checkout ${currentBranch}`, { ignoreError: true });
// 恢复暂存的更改
if (hasChanges) {
log('>>> 恢复暂存的更改...', 'yellow');
exec('git stash pop', { ignoreError: true });
log('已恢复\n', 'green');
}
log('\n========================================', 'green'); log('\n========================================', 'green');
log(' master 已成功合并到 production!', 'green'); log(' master 已成功合并到 production!', 'green');
log('========================================\n', 'green'); log('========================================\n', 'green');

Loading…
Cancel
Save