Files
tra-app/devops/iso/新的/sync_remote_branch.py
T
2025-11-10 10:00:46 +08:00

121 lines
4.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
import os
import subprocess
import sys
# 颜色定义
RED = '\033[0;31m'
GREEN = '\033[0;32m'
YELLOW = '\033[1;33m'
NC = '\033[0m' # 无颜色
def run_command(command, check=True, capture_output=False):
"""执行系统命令并返回结果"""
try:
if capture_output:
result = subprocess.run(
command,
shell=True,
check=check,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
return result
else:
subprocess.run(command, shell=True, check=check)
return None
except subprocess.CalledProcessError as e:
print(f"{RED}错误:命令执行失败: {e}{NC}")
sys.exit(1)
def main():
# 检查参数
if len(sys.argv) < 2:
print(f"{RED}错误:未传入分支名称参数!{NC}")
print(f"使用方法:{YELLOW}{sys.argv[0]} <远程分支名>{NC}")
sys.exit(1)
print(f"▶ 先设置内网网络\n")
run_command("sudo route add -host 25.13.9.101 25.64.32.254")
# 配置参数
target_branch = sys.argv[1]
# 关键修改:跨平台路径拼接(build + target_branch
# 1. 获取脚本所在目录(作为相对路径基准,可选,根据实际需求调整)
script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
parent_dir = os.path.dirname(script_dir)
# 2. 拼接路径:build/<target_branch>(相对脚本目录)
work_dir = os.path.join(parent_dir, "build", target_branch)
# 3. 转换为绝对路径(消除相对路径符号,更可靠)
work_dir = os.path.abspath(work_dir)
print(f"▶ 本地工作目录:{YELLOW}{work_dir}{NC}\n")
remote_name = "origin"
local_branch = "develop"
print(f"\n{YELLOW}===== 开始执行分支同步脚本 ====={NC}")
print(f"▶ 远程目标分支:{YELLOW}{target_branch}{NC}")
# 1. 进入工作目录
print(f"{YELLOW}1. 检查并进入工作目录...{NC}")
if not os.path.isdir(work_dir):
print(f"{RED}错误:工作目录不存在!{NC}")
sys.exit(1)
try:
os.chdir(work_dir)
except OSError as e:
print(f"{RED}错误:无法进入工作目录!{e}{NC}")
sys.exit(1)
print(f"✅ 成功进入工作目录:{os.getcwd()}\n")
# 2. 检查Git仓库
print(f"{YELLOW}2. 检查Git仓库...{NC}")
if not os.path.isdir(".git"):
print(f"{RED}错误:当前目录不是Git仓库!{NC}")
sys.exit(1)
print(f"✅ 确认是Git仓库\n")
# 3. 关键修复:获取远程分支完整信息
print(f"{YELLOW}3. 完整获取远程所有分支完整信息...{NC}")
# 完整拉取所有分支信息
run_command(f"git fetch {remote_name} '+refs/heads/*:refs/remotes/{remote_name}/*'")
print(f"✅ 远程分支信息拉取完成\n")
# 4. 验证远程分支是否存在
print(f"{YELLOW}4. 验证远程 {target_branch} 分支...{NC}")
result = run_command(
f"git show-ref --verify --quiet 'refs/remotes/{remote_name}/{target_branch}'",
check=False,
capture_output=True
)
if result.returncode != 0:
print(f"{RED}错误:远程确实不存在 {target_branch} 分支!{NC}")
print("远程所有分支列表:")
run_command("git branch -r")
sys.exit(1)
print(f"✅ 确认远程存在 {YELLOW}{target_branch}{NC} 分支\n")
# 5. 同步到本地develop分支
print(f"{YELLOW}5. 同步远程 {target_branch} 到本地 {local_branch}...{NC}")
print(f"{YELLOW}⚠️ 警告:将覆盖本地develop分支所有内容!{NC}")
# 确保本地有develop分支
run_command(f"git checkout -B {local_branch} >/dev/null 2>&1", check=False)
# 关键命令:使用完整引用路径
run_command(f"git reset --hard 'refs/remotes/{remote_name}/{target_branch}'")
# 6. 完成提示
print(f"\n{GREEN}===== 操作完成 ====={NC}")
print(f"✅ 本地 {YELLOW}{local_branch}{NC} 已同步为远程 {YELLOW}{target_branch}{NC} 最新状态")
print("当前版本:")
run_command("git log -1 --pretty=format:\"%h - %an, %ar : %s\"")
if __name__ == "__main__":
main()