diff --git a/scripts/ci/auto_fix_formatting.py b/scripts/ci/auto_fix_formatting.py index 0ec56ad7c..76b77fdab 100755 --- a/scripts/ci/auto_fix_formatting.py +++ b/scripts/ci/auto_fix_formatting.py @@ -9,6 +9,7 @@ import json import os import subprocess import sys +import time import urllib.request @@ -241,8 +242,47 @@ def main(): # 推送(head_branch已从ensure_git_repo获取) print(f"\nPR来源分支: {head_branch}") + print("推送格式修复到远端...") - run(f'git push origin "HEAD:{head_branch}"') + # 推送前先 rebase 拉取远端最新,避免快进冲突 + # 最多重试 3 次:rebase → push,失败则重新拉取再试 + max_retries = 3 + push_success = False + last_error = "" + + for attempt in range(1, max_retries + 1): + print(f" 尝试 {attempt}/{max_retries}: 拉取最新代码并推送...") + + # 先拉取远端最新 commit 并 rebase + fetch_result = run(f"git fetch origin {head_branch}", check=False) + if fetch_result.returncode != 0: + last_error = f"git fetch 失败: {fetch_result.stderr.strip()}" + print(f" {last_error}") + time.sleep(2) + continue + + rebase_result = run(f"git rebase origin/{head_branch}", check=False) + if rebase_result.returncode != 0: + last_error = f"git rebase 失败,中止并重置: {rebase_result.stderr.strip()[:200]}" + print(f" {last_error}") + run("git rebase --abort", check=False) + # rebase 失败通常是冲突,重试没用,直接跳出 + break + + # 推送 + push_result = run(f'git push origin "HEAD:{head_branch}"', check=False) + if push_result.returncode == 0: + push_success = True + break + + last_error = push_result.stderr.strip() or push_result.stdout.strip() + print(f" push 失败: {last_error[:200]}") + time.sleep(3) + + if not push_success: + print(f"\n❌ 推送失败(已重试 {max_retries} 次)", file=sys.stderr) + print(f"最后错误: {last_error}", file=sys.stderr) + sys.exit(1) print() print("✅ 格式已自动修复并推送回分支")