diff --git a/src/tools/ignore.py b/src/tools/ignore.py index 4d80044..70cd8ee 100644 --- a/src/tools/ignore.py +++ b/src/tools/ignore.py @@ -9,6 +9,8 @@ from function.maintain.config import 读取配置 from function.github.token import read_token from catfood.functions.files import open_file +from catfood.functions.terminal import runCommand +from function.constant.general import RETRY_INTERVAL def 追加忽略字段(检测程序路径: str, 忽略字段: str, 理由: str) -> bool | str: with open(检测程序路径, 'r', encoding="utf-8") as file: @@ -193,7 +195,8 @@ def main(args: list[str]) -> int: print(f"{消息头.信息} 开始同步...") subprocess.run(["git", "checkout", "main"], check=True) print(f"{消息头.信息} 已签出到 main 分支") - subprocess.run(["git", "pull"], check=True) + if e := runCommand(["git", "pull"], retry=RETRY_INTERVAL): + return 1 print(f"{消息头.信息} 已拉取远程修改") except subprocess.CalledProcessError as e: print(f"{消息头.错误} 同步失败:\n{Fore.RED}{e}{Fore.RESET}") @@ -232,7 +235,8 @@ def main(args: list[str]) -> int: print(f"{消息头.信息} 开始提交和推送...") subprocess.run(["git", "add", 检测程序], check=True) subprocess.run(["git", "commit", "-m", f"[Auto] 自动忽略追加 - {忽略字段}"], check=True) - subprocess.run(["git", "push", "--set-upstream", "origin", 新分支名], check=True) + if e := runCommand(["git", "push", "--set-upstream", "origin", 新分支名], retry=RETRY_INTERVAL): + return 1 print(f"{消息头.成功} 成功推送到远程") if not 创建拉取请求(新分支名, owner, 忽略字段): 失败 = True @@ -248,7 +252,8 @@ def main(args: list[str]) -> int: print(f"{消息头.信息} 开始提交和推送...") subprocess.run(["git", "add", 检测程序], check=True) subprocess.run(["git", "commit", "-m", f"[Auto] 自动忽略移除 - {格式化忽略字段}"], check=True) - subprocess.run(["git", "push", "--set-upstream", "origin", 新分支名], check=True) + if e := runCommand(["git", "push", "--set-upstream", "origin", 新分支名], retry=RETRY_INTERVAL): + return 1 print(f"{消息头.成功} 成功推送到远程") if not 创建拉取请求(新分支名, owner, 忽略字段, 理由): 失败 = True @@ -261,7 +266,8 @@ def main(args: list[str]) -> int: print(f"{消息头.信息} 开始提交和推送...") subprocess.run(["git", "add", 检测程序], check=True) subprocess.run(["git", "commit", "-m", "chore(checker): 更新忽略字段"], check=True) - subprocess.run(["git", "push", "--set-upstream", "origin", 新分支名], check=True) + if e := runCommand(["git", "push", "--set-upstream", "origin", 新分支名], retry=RETRY_INTERVAL): + return 1 print(f"{消息头.成功} 成功推送到远程") if not 创建拉取请求(新分支名, owner, 理由="edit"): 失败 = True diff --git a/src/tools/modify.py b/src/tools/modify.py index db2e057..1ec416f 100644 --- a/src/tools/modify.py +++ b/src/tools/modify.py @@ -5,6 +5,7 @@ import random import requests import subprocess +import tools.sync from colorama import Fore from datetime import datetime from catfood.constant import YES @@ -13,9 +14,10 @@ from function.maintain.config import 读取配置 from catfood.functions.files import open_file from function.files.manifest import 获取清单目录 +from catfood.functions.terminal import runCommand from function.files.manifest import FormatManifest -from function.constant.general import PR_TOOL_NOTE from function.github.token import read_token, 这是谁的Token +from function.constant.general import PR_TOOL_NOTE, RETRY_INTERVAL def main(args: list[str]): global 包标识符, 包版本, 日志文件路径 @@ -243,9 +245,8 @@ def 修改版本(版本文件夹: str): 新分支 = branchName(f"Modify-S-{包标识符}-{版本文件夹}-{int(time.time())}") print(f" 创建并切换到新分支: {新分支}") 写入日志(f" Create and checkout to a new branch: {新分支}") - subprocess.run(["git", "checkout", "master"], check=True) # 确保从 master 分支开始 - subprocess.run(["git", "fetch", "upstream"], check=True) # 拉取上游修改 - subprocess.run(["git", "rebase", "upstream/master"], check=True) # 变基上游修改 + if e := tools.sync.main(): + return e subprocess.run(["git", "checkout", "-b", 新分支], check=True) # 创建并切换到新的分支 # 遍历该版本文件夹中的所有文件 @@ -321,8 +322,11 @@ def 修改版本(版本文件夹: str): # 推送更改到远程仓库 print(" 推送更改到远程仓库") 写入日志(" Pushing changes to remote (origin) repository") - subprocess.run(["git", "push", "origin", 新分支], check=True) - print(f" {Fore.GREEN}推送到远程成功: {新分支}") + if e := runCommand(["git", "push", "origin", 新分支], retry=RETRY_INTERVAL): + print(f"{消息头.错误} 推送到远程失败: Git 返回退出代码 {e}") + return e + else: + print(f" {Fore.GREEN}推送到远程成功: {新分支}") 写入日志(f" Successfully pushed to remote (origin): {新分支}") # 创建拉取请求 diff --git a/src/tools/prune.py b/src/tools/prune.py index c553d10..de64d69 100644 --- a/src/tools/prune.py +++ b/src/tools/prune.py @@ -1,32 +1,27 @@ import os -import subprocess from colorama import Fore from catfood.functions.print import 消息头 from function.maintain.config import 读取配置 -from catfood.exceptions.operation import OperationFailed +from catfood.functions.terminal import runCommand +from function.constant.general import RETRY_INTERVAL def main() -> int: try: for 仓库 in ["winget-pkgs", "winget-tools"]: 仓库路径 = 读取配置(f"paths.{仓库}") if isinstance(仓库路径, str): - 清理远程(仓库, 仓库路径) + os.chdir(仓库路径) + print(f"清理 {Fore.BLUE}{仓库}{Fore.RESET} 仓库的远程已删除分支...") + if e := runCommand(["git", "remote", "prune", "origin"], retry=RETRY_INTERVAL): + print(f"{消息头.错误} 清理 {Fore.BLUE}{仓库}{Fore.RESET} 的远程已删除分支时出错: Git 返回退出代码 {e}") + return e + else: + print(f"{Fore.GREEN}✓{Fore.RESET} 清理完毕") else: - raise OperationFailed + raise ValueError except KeyboardInterrupt: print(f"{消息头.错误} 已取消操作") return 1 - except OperationFailed: + except ValueError: return 1 return 0 - - -def 清理远程(仓库: str, 仓库路径: str): - try: - print(f"清理 {Fore.BLUE}{仓库}{Fore.RESET} 仓库的远程已删除分支...") - os.chdir(仓库路径) - subprocess.run(["git", "remote", "prune", "origin"], check=True) - print(f"{Fore.GREEN}✓{Fore.RESET} 清理完毕") - except subprocess.CalledProcessError as e: - print(f"{消息头.错误} 清理 {Fore.BLUE}{仓库}{Fore.RESET} 的远程已删除分支时出错,git 返回 {Fore.BLUE}{e.returncode}{Fore.RESET}") - raise OperationFailed diff --git a/src/tools/remove.py b/src/tools/remove.py index 9b7e54e..5483fc1 100644 --- a/src/tools/remove.py +++ b/src/tools/remove.py @@ -15,10 +15,11 @@ from function.git.format import branchName from function.maintain.config import 读取配置 from translate import Translator # type: ignore -from function.constant.general import PR_TOOL_NOTE +from catfood.functions.terminal import runCommand from catfood.exceptions.operation import OperationFailed from function.github.token import read_token, 这是谁的Token from function.files.manifest import 获取清单目录, 获取现有包版本 +from function.constant.general import PR_TOOL_NOTE, RETRY_INTERVAL # 创建拉取请求 def 创建拉取请求(包标识符: str, 分支名: str, 版本文件夹: str, 理由: str): @@ -204,8 +205,11 @@ def main(args: list[str]) -> int: subprocess.run(["git", "commit", "-m", f"Remove version: {包标识符} version {包版本} (Auto)"], check=True) print(f"{Fore.BLUE} 已提交修改") - subprocess.run(["git", "push"], check=True) - print(f"{Fore.BLUE} 已推送修改") + if e := runCommand(["git", "push"], retry=RETRY_INTERVAL): + print(f"{消息头.错误} 推送到远程失败: Git 返回退出代码 {e}") + return e + else: + print(f"{Fore.BLUE} 已推送修改") while (not 理由): 理由 = input("移除此包版本的理由: ") diff --git a/src/tools/sync.py b/src/tools/sync.py index 166dec9..13a8b89 100644 --- a/src/tools/sync.py +++ b/src/tools/sync.py @@ -4,6 +4,8 @@ from catfood.constant import YES from catfood.functions.print import 消息头 from function.maintain.config import 读取配置 +from catfood.functions.terminal import runCommand +from function.constant.general import RETRY_INTERVAL def main() -> int: try: @@ -11,62 +13,57 @@ def main() -> int: if not isinstance(winget_pkgs目录, str): return 1 - # 入口 os.chdir(winget_pkgs目录) + + # 签出 master try: - subprocess.run(["git", "checkout", "master"], check=True) # 确保从 master 分支开始 - print(f"{Fore.BLUE} 已签出到 master 分支") + subprocess.run(["git", "checkout", "master"], check=True) + print(f"{消息头.信息} 已签出到 master 分支") except subprocess.CalledProcessError as e: print(f"{消息头.错误} 签出到 master 分支失败:\n{Fore.RED}{e}{Fore.RESET}") return 1 - while True: - try: - subprocess.run(["git", "fetch", "upstream"], check=True) # 拉取上游修改 - print(f"{Fore.BLUE} 已获取上游修改") - break - except subprocess.CalledProcessError as e: - print(f"{消息头.错误} 获取上游修改失败:\n{Fore.RED}{e}{Fore.RESET}") - if input(f"{消息头.问题} 是否重试?(默认为{Fore.GREEN}是{Fore.RESET}): ").lower() not in (*YES, ""): - print(f"{消息头.消息} 已取消操作") - return 1 + # 获取上游 + if e := runCommand(["git", "fetch", "upstream"], retry=RETRY_INTERVAL): + print(f"{消息头.错误} 获取上游修改失败: Git 返回退出代码 {e}") + return e + else: + print(f"{消息头.信息} 已获取上游修改") - try: - subprocess.run(["git", "fetch", "origin"], check=True) # 拉取远程修改 - print(f"{Fore.BLUE} 已获取远程修改") - except subprocess.CalledProcessError as e: - print(f"{消息头.警告} 拉取远程修改失败:\n{Fore.YELLOW}{e}{Fore.RESET}") - # 不影响...不影响 + # 获取远程 + if e := runCommand(["git", "fetch", "origin"], retry=RETRY_INTERVAL): + print(f"{消息头.错误} 获取远程修改失败: Git 返回退出代码 {e}") + return e + else: + print(f"{消息头.信息} 已获取远程修改") try: subprocess.run(["git", "rebase", "upstream/master"], check=True) # 变基合并上游修改 - print(f"{Fore.BLUE} 已变基上游修改") + print(f"{消息头.信息} 已变基上游修改") except subprocess.CalledProcessError as e: print(f"{消息头.错误} 变基上游修改失败:\n{Fore.RED}{e}{Fore.RESET}") - if input(f"{消息头.问题} 是否尝试替换 master 分支?(默认为{Fore.YELLOW}否{Fore.RESET}): ").lower() in YES: + if input(f"{消息头.问题} 是否尝试替换 master 分支?(默认为{Fore.YELLOW}否{Fore.RESET}): ").lower() in ["y", "yes", "要", "是", "true"]: try: subprocess.run(["git", "checkout", "upstream/master"], check=True) # 签出到上游 master 分支 - print(f"{Fore.BLUE} 已签出到上游 master 分支") + print(f"{消息头.信息} 已签出到上游 master 分支") subprocess.run(["git", "branch", "-D", "master"], check=True) # 移除旧的 master 分支 - print(f"{Fore.BLUE} 已移除旧 master 分支") + print(f"{消息头.信息} 已移除旧 master 分支") subprocess.run(["git", "switch", "-c", "master"], check=True) # 创建并签出到 master 分支 - print(f"{Fore.BLUE} 已创建并签出到 master 分支") + print(f"{消息头.信息} 已创建并签出到 master 分支") except subprocess.CalledProcessError as e: print(f"{消息头.错误} 替换 master 分支失败:\n{Fore.RED}{e}{Fore.RESET}") return 1 else: - print(f"{消息头.消息} 已取消操作") - return 1 + raise KeyboardInterrupt - try: - # 推送 master - subprocess.run(["git", "push", "origin", "master"], check=True) - print(f"{Fore.BLUE} 已推送 master 分支") - except subprocess.CalledProcessError as e: - print(f"{消息头.错误} 推送 master 分支失败:\n{Fore.RED}{e}{Fore.RESET}") - return 1 + # 推送到远程 + if e := runCommand(["git", "push", "origin", "master"], retry=RETRY_INTERVAL): + print(f"{消息头.错误} 推送到远程失败: Git 返回退出代码 {e}") + return e + else: + print(f"{消息头.信息} 已推送到远程") - print(f"{Fore.GREEN}✓{Fore.RESET} 同步完成") + print(f"{消息头.成功} 同步完成") except KeyboardInterrupt: print(f"{消息头.错误} 用户已取消操作") return 1