diff --git a/scripts/ci/auto_fix_formatting.py b/scripts/ci/auto_fix_formatting.py index 0ec56ad7c..9f1778c85 100755 --- a/scripts/ci/auto_fix_formatting.py +++ b/scripts/ci/auto_fix_formatting.py @@ -242,11 +242,43 @@ def main(): # 推送(head_branch已从ensure_git_repo获取) print(f"\nPR来源分支: {head_branch}") - run(f'git push origin "HEAD:{head_branch}"') + # 推送带rebase+重试,避免并发提交导致冲突 + max_push_retries = 3 + push_success = False + for attempt in range(1, max_push_retries + 1): + print(f"推送尝试 {attempt}/{max_push_retries}...") + try: + # 先pull --rebase拉取最新提交 + run(f"git pull --rebase origin {head_branch}", check=False) + # 如果rebase有冲突,中止rebase并报错 + result = run("git status --porcelain", capture=True) + if "rebase" in result.stdout or "both modified" in result.stdout: + run("git rebase --abort", check=False) + print("rebase冲突,跳过本次推送", file=sys.stderr) + break + # 推送 + push_result = run(f'git push origin "HEAD:{head_branch}"', check=False) + if push_result.returncode == 0: + push_success = True + break + else: + print(f"推送失败: {push_result.stderr[-200:]}", file=sys.stderr) + if attempt < max_push_retries: + import time + time.sleep(2 ** attempt) + except Exception as e: + print(f"推送异常: {e}", file=sys.stderr) + if attempt < max_push_retries: + import time + time.sleep(2 ** attempt) - print() - print("✅ 格式已自动修复并推送回分支") - print("新的commit会重新触发CI检查") + if not push_success: + print("⚠️ 自动格式化推送失败,跳过(请手动检查)", file=sys.stderr) + sys.exit(0) # 不影响Validate的失败标记 + else: + print() + print("✅ 格式已自动修复并推送回分支") + print("新的commit会重新触发CI检查") if __name__ == "__main__":