|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import shutil |
| 5 | +import subprocess |
| 6 | +import colorama |
| 7 | + |
| 8 | +colorama.init() |
| 9 | + |
| 10 | +RED = "\033[91m" |
| 11 | +YELLOW = "\033[93m" |
| 12 | +GREEN = "\033[92m" |
| 13 | +GRAY = "\033[90m" |
| 14 | +RESET = "\033[0m" |
| 15 | + |
| 16 | +def print_error(msg): |
| 17 | + print(f"{RED}{msg}{RESET}") |
| 18 | + |
| 19 | + |
| 20 | +def print_warn(msg): |
| 21 | + print(f"{YELLOW}{msg}{RESET}") |
| 22 | + |
| 23 | + |
| 24 | +def print_info(msg): |
| 25 | + print(f"{GREEN}{msg}{RESET}") |
| 26 | + |
| 27 | + |
| 28 | +def print_ok(msg): |
| 29 | + print(f"{GRAY}{msg}{RESET}") |
| 30 | + |
| 31 | + |
| 32 | +def run_cmd(cmd): |
| 33 | + return subprocess.check_output(cmd, stderr=subprocess.STDOUT, text=True).strip() |
| 34 | + |
| 35 | + |
| 36 | +def main(): |
| 37 | + # 1. 检查 git 是否存在 |
| 38 | + if shutil.which("git") is None: |
| 39 | + print_info("[INFO] 没有安装git, 跳过更新检查步骤") |
| 40 | + sys.exit(1) |
| 41 | + |
| 42 | + # 2. 检查 .git 目录(是否为 Git 仓库) |
| 43 | + if not os.path.isdir(".git"): |
| 44 | + print_info("[INFO] 当前路径不是git仓库, 跳过更新检查步骤") |
| 45 | + sys.exit(1) |
| 46 | + |
| 47 | + # 3. 获取当前分支并检查是否为 dev |
| 48 | + try: |
| 49 | + current_branch = run_cmd(["git", "rev-parse", "--abbrev-ref", "HEAD"]) |
| 50 | + except subprocess.CalledProcessError as e: |
| 51 | + print_error(f"[ERROR] 无法获取当前分支, 错误: {e.output}") |
| 52 | + sys.exit(1) |
| 53 | + |
| 54 | + if current_branch != "dev": |
| 55 | + print_warn(f"[WARN] 你当前的分支是 {current_branch}, 可能不是最新更新, 建议执行 'git checkout dev' 切换至发布分支") |
| 56 | + sys.exit(1) |
| 57 | + |
| 58 | + # 4. 拉取远程更新信息 |
| 59 | + try: |
| 60 | + subprocess.check_call(["git", "fetch", "origin"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) |
| 61 | + except subprocess.CalledProcessError as e: |
| 62 | + print_warn(f"[WARN] 获取远程仓库信息失败: {e}, 跳过更新检查") |
| 63 | + sys.exit(1) |
| 64 | + |
| 65 | + # 5. 获取本地和远程的提交哈希 |
| 66 | + try: |
| 67 | + local_hash = run_cmd(["git", "rev-parse", "HEAD"]) |
| 68 | + remote_hash = run_cmd(["git", "rev-parse", "origin/dev"]) |
| 69 | + except subprocess.CalledProcessError as e: |
| 70 | + print_error(f"[ERROR] 获取commit信息错误: {e.output}, 退出更新检查") |
| 71 | + sys.exit(1) |
| 72 | + |
| 73 | + # 6. 对比并提示 |
| 74 | + if local_hash != remote_hash: |
| 75 | + print_info("\n[INFO] 检查到更新, 请执行 'git pull origin dev' 获取最新版本") |
| 76 | + else: |
| 77 | + print_ok("\n[OK] 当前文件为最新版本") |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + main() |
0 commit comments