From 136546354b7a94b8074192a4522e0156fd0b2730 Mon Sep 17 00:00:00 2001 From: CI Bot Date: Mon, 20 Jul 2026 12:55:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(ci):=20auto-fix=20push=E5=A2=9E=E5=8A=A0reb?= =?UTF-8?q?ase+=E9=87=8D=E8=AF=95=EF=BC=8C=E4=BF=AE=E5=A4=8D=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=BF=AE=E5=A4=8D=E6=8E=A8=E9=80=81=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit auto_fix_formatting.py 之前直接 git push 没有同步远端最新代码, 当分支有新 commit 时 push 会因 non-fast-forward 失败, 导致格式修复无法自动推送回 PR 分支,Validate 持续挂红。 修复方案: - push 前先 git fetch + git rebase 同步远端最新代码 - 最多重试 3 次,每次间隔 2-3 秒 - rebase 冲突时自动 abort 并退出,避免死锁 - 失败时输出明确的错误信息 从源头消除 Validate 因 auto-fix push 失败导致的反复挂红。 --- scripts/ci/auto_fix_formatting.py | 42 ++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) 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("✅ 格式已自动修复并推送回分支") -- 2.54.0