From 319655aa1ee96493fce8b6570f381cbdb66a9b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B8=AD=E9=B8=AD=E3=80=8C=E3=82=AB=E3=83=A2=E3=80=8D?= <89643991+DuckDuckStudio@users.noreply.github.com> Date: Wed, 18 Feb 2026 04:09:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AE=A9=E9=83=A8=E5=88=86=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E4=BD=BF=E7=94=A8=20catfood=20=E7=9A=84=20functions.t?= =?UTF-8?q?erminal=20=E4=B8=AD=201.5.0=20=E6=96=B0=E5=A2=9E=E7=9A=84=20run?= =?UTF-8?q?Command=20=E8=BF=90=E8=A1=8C=EF=BC=8C=E4=BB=A5=E5=85=81?= =?UTF-8?q?=E8=AE=B8=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 此修改依赖于配置文件 1.4 --- src/tools/ignore.py | 14 ++++++++--- src/tools/modify.py | 16 +++++++----- src/tools/prune.py | 27 +++++++++------------ src/tools/remove.py | 10 +++++--- src/tools/sync.py | 59 ++++++++++++++++++++------------------------- 5 files changed, 64 insertions(+), 62 deletions(-) diff --git a/src/tools/ignore.py b/src/tools/ignore.py index ad4e9ee..8f6ee29 100644 --- a/src/tools/ignore.py +++ b/src/tools/ignore.py @@ -6,8 +6,10 @@ import requests from catfood.functions.files import open_file from catfood.functions.print import 消息头 +from catfood.functions.terminal import runCommand from colorama import Fore +from function.constant.general import RETRY_INTERVAL from function.git.format import branchName from function.github.token import read_token from function.maintain.config import 读取配置 @@ -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 967e0f3..b042d70 100644 --- a/src/tools/modify.py +++ b/src/tools/modify.py @@ -12,9 +12,11 @@ from catfood.functions.files import open_file from catfood.functions.github.api import 这是谁的Token from catfood.functions.print import 消息头 +from catfood.functions.terminal import runCommand from colorama import Fore -from function.constant.general import PR_TOOL_NOTE +import tools.sync +from function.constant.general import PR_TOOL_NOTE, RETRY_INTERVAL from function.files.manifest import FormatManifest, 获取清单目录 from function.git.format import branchName from function.github.token import read_token @@ -247,9 +249,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) # 创建并切换到新的分支 # 遍历该版本文件夹中的所有文件 @@ -325,8 +326,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 9239984..6e1b6e3 100644 --- a/src/tools/prune.py +++ b/src/tools/prune.py @@ -1,10 +1,10 @@ import os -import subprocess -from catfood.exceptions.operation import OperationFailed from catfood.functions.print import 消息头 +from catfood.functions.terminal import runCommand from colorama import Fore +from function.constant.general import RETRY_INTERVAL from function.maintain.config import 读取配置 @@ -13,23 +13,18 @@ def main() -> int: 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 0e8710e..e19ef51 100644 --- a/src/tools/remove.py +++ b/src/tools/remove.py @@ -12,12 +12,13 @@ from catfood.exceptions.operation import OperationFailed from catfood.functions.github.api import 这是谁的Token from catfood.functions.print import 消息头 +from catfood.functions.terminal import runCommand from colorama import Fore from translate import Translator # type: ignore import tools.cat as cat import tools.sync as sync -from function.constant.general import PR_TOOL_NOTE +from function.constant.general import PR_TOOL_NOTE, RETRY_INTERVAL from function.files.manifest import 获取清单目录, 获取现有包版本 from function.git.format import branchName from function.github.token import read_token @@ -208,8 +209,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 c9d1891..b3c6f26 100644 --- a/src/tools/sync.py +++ b/src/tools/sync.py @@ -3,8 +3,10 @@ from catfood.constant import YES from catfood.functions.print import 消息头 +from catfood.functions.terminal import runCommand from colorama import Fore +from function.constant.general import RETRY_INTERVAL from function.maintain.config import 读取配置 @@ -14,62 +16,53 @@ def main() -> int: if not isinstance(winget_pkgs目录, str): return 1 - # 入口 os.chdir(winget_pkgs目录) + 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: 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