```python
#!/usr/bin/env python

# -*- coding: utf-8 -*-

  

import os

import sys

import subprocess

import re

from pathlib import Path

from typing import Tuple, List, Optional

  

# 版本号文件路径

VERSION_FILE = Path("src/version/version.py")

  

def get_current_version() -> Tuple[int, int, int]:

    """

    从版本文件中读取当前版本号

    返回:

        包含三个整数的元组 (主版本号, 次版本号, 修订号)

    """

    if not VERSION_FILE.exists():

        print(f"错误: 版本文件不存在: {VERSION_FILE}")

        sys.exit(1)

    version_content = VERSION_FILE.read_text(encoding="utf-8")

    version_match = re.search(r"VERSION\s*=\s*\((\d+),\s*(\d+),\s*(\d+)\)", version_content)

    if not version_match:

        print(f"错误: 无法从版本文件中解析版本号: {VERSION_FILE}")

        sys.exit(1)

    major = int(version_match.group(1))

    minor = int(version_match.group(2))

    patch = int(version_match.group(3))

    return (major, minor, patch)

  

def update_version_file(version: Tuple[int, int, int]) -> None:

    """

    更新版本文件中的版本号

    参数:

        version: 新的版本号元组 (主版本号, 次版本号, 修订号)

    """

    if not VERSION_FILE.exists():

        print(f"错误: 版本文件不存在: {VERSION_FILE}")

        sys.exit(1)

    version_content = VERSION_FILE.read_text(encoding="utf-8")

    version_str = f"({version[0]}, {version[1]}, {version[2]})"

    updated_content = re.sub(

        r"VERSION\s*=\s*\(\d+,\s*\d+,\s*\d+\)",

        f"VERSION = {version_str}",

        version_content

    )

    VERSION_FILE.write_text(updated_content, encoding="utf-8")

    print(f"已更新版本文件: {version_str}")

  

def create_git_tag(version: Tuple[int, int, int]) -> bool:

    """

    创建Git标签

    参数:

        version: 版本号元组 (主版本号, 次版本号, 修订号)

    返回:

        是否成功

    """

    version_str = f"{version[0]}.{version[1]}.{version[2]}"

    tag_name = f"v{version_str}"

    # 创建Git标签

    try:

        print(f"创建Git标签: {tag_name}")

        subprocess.run(["git", "tag", tag_name], check=True)

        return True

    except subprocess.CalledProcessError as e:

        print(f"创建Git标签失败: {e}")

        return False

  

def confirm_version_change(current_version: Tuple[int, int, int], new_version: Tuple[int, int, int]) -> bool:

    """确认版本更改"""

    current_str = f"{current_version[0]}.{current_version[1]}.{current_version[2]}"

    new_str = f"{new_version[0]}.{new_version[1]}.{new_version[2]}"

    confirm = input(f"确认将版本从 {current_str} 更新到 {new_str}? (y/n): ")

    return confirm.lower() in ["y", "yes"]

  

def main():

    """主函数"""

    print("SD Models Manager 版本更新工具")

    print("-" * 40)

    # 获取当前版本

    current_version = get_current_version()

    current_version_str = f"{current_version[0]}.{current_version[1]}.{current_version[2]}"

    print(f"当前版本: {current_version_str}")

    # 询问用户要更新的版本部分

    print("\n要更新哪个版本部分?")

    print("1) 主版本号 (Major)")

    print("2) 次版本号 (Minor)")

    print("3) 修订号 (Patch)")

    choice = input("请选择 (1-3): ").strip()

    # 计算新版本

    new_version = list(current_version)

    if choice == "1":

        new_version[0] += 1

        new_version[1] = 0

        new_version[2] = 0

    elif choice == "2":

        new_version[1] += 1

        new_version[2] = 0

    elif choice == "3":

        new_version[2] += 1

    else:

        print("无效的选择，退出程序。")

        sys.exit(1)

    new_version = tuple(new_version)

    # 确认版本更改

    if not confirm_version_change(current_version, new_version):

        print("操作已取消。")

        sys.exit(0)

    # 更新版本文件

    update_version_file(new_version)

    # 询问是否提交更改

    commit_changes = input("是否提交更改并创建标签? (y/n): ")

    if commit_changes.lower() in ["y", "yes"]:

        # 提交更改

        try:

            version_str = f"{new_version[0]}.{new_version[1]}.{new_version[2]}"

            subprocess.run(["git", "add", str(VERSION_FILE)], check=True)

            subprocess.run(["git", "commit", "-m", f"发布：v{version_str}"], check=True)

            print("已提交版本更改。")

        except subprocess.CalledProcessError as e:

            print(f"提交失败: {e}")

            sys.exit(1)

        # 创建Git标签

        if create_git_tag(new_version):

            print("\n✅ 版本更新完成！")

            print(f"新版本: v{new_version[0]}.{new_version[1]}.{new_version[2]}")

            print("已创建Git标签。")

            print("\n要推送到远程仓库，请手动执行以下命令：")

            print(f"git push origin")

            print(f"git push origin v{new_version[0]}.{new_version[1]}.{new_version[2]}")

        else:

            print("\n❌ Git标签创建失败。")

            sys.exit(1)

    else:

        print("\n✅ 版本文件已更新，但未提交更改。")

        print(f"新版本: v{new_version[0]}.{new_version[1]}.{new_version[2]}")

        print("请手动提交更改并创建标签：")

        print(f"git add {VERSION_FILE}")

        print(f"git commit -m \"发布：v{new_version[0]}.{new_version[1]}.{new_version[2]}\"")

        print(f"git tag v{new_version[0]}.{new_version[1]}.{new_version[2]}")

  

if __name__ == "__main__":

    main()
```