From e320bb5ab8c99c018aa303f91592a29dbf675e54 Mon Sep 17 00:00:00 2001 From: Soulter <37870767+Soulter@users.noreply.github.com> Date: Sun, 2 Apr 2023 20:23:31 +0800 Subject: [PATCH] refactor: command: update --- model/command/command.py | 29 ++++++++++++++++++++++++ model/command/command_openai_official.py | 2 ++ model/command/command_rev_chatgpt.py | 2 ++ model/command/command_rev_edgegpt.py | 2 ++ requirements.txt | 1 + 5 files changed, 36 insertions(+) diff --git a/model/command/command.py b/model/command/command.py index d6aa234d0..589d2bd04 100644 --- a/model/command/command.py +++ b/model/command/command.py @@ -1,5 +1,9 @@ import abc import json +from git.repo import Repo +import os +import sys + import requests from model.provider.provider import Provider @@ -13,6 +17,31 @@ class Command: if message.startswith("help") or message.startswith("帮助"): return True, self.help() return False, None + + def update(self, message: str): + l = message.split(" ") + if len(l) == 1: + # 得到当前commit hash + repo = Repo() + commit = repo.head.commit + # 得到最新的5条commit列表 + origin = repo.remotes.origin + origin.fetch() + commits = list(origin.refs.master.log())[0:5] + remote_commit_hash = origin.refs.master.commit.hexsha + + return True, f"当前版本: {commit.hexsha}\n最新版本: {remote_commit_hash}\n\n最新5条commit:\n\n使用update latest更新至最新版本\n" + else: + if l[1] == "latest": + try: + repo = Repo() + repo.remotes.origin.pull() + py = sys.executable + os.execl(py, py, *sys.argv) + return True, "更新成功" + + except BaseException as e: + return False, "更新失败: "+str(e) def reset(self): return False diff --git a/model/command/command_openai_official.py b/model/command/command_openai_official.py index b9b52b160..987ec92c2 100644 --- a/model/command/command_openai_official.py +++ b/model/command/command_openai_official.py @@ -29,6 +29,8 @@ class CommandOpenAIOfficial(Command): return True, self.set(message, session_id) elif message.startswith("画"): return True, self.draw(message) + elif message.startswith("update"): + return True, self.update() return False, None diff --git a/model/command/command_rev_chatgpt.py b/model/command/command_rev_chatgpt.py index 6378c7607..08edcf85c 100644 --- a/model/command/command_rev_chatgpt.py +++ b/model/command/command_rev_chatgpt.py @@ -13,6 +13,8 @@ class CommandRevChatGPT(Command): # return True, self.reset() if message.startswith("help") or message.startswith("帮助"): return True, self.help() + elif message.startswith("update"): + return True, self.update() return False, None def help(self): diff --git a/model/command/command_rev_edgegpt.py b/model/command/command_rev_edgegpt.py index 1b3ebf6f4..9ae8702ab 100644 --- a/model/command/command_rev_edgegpt.py +++ b/model/command/command_rev_edgegpt.py @@ -10,6 +10,8 @@ class CommandRevEdgeGPT(Command): return True, self.reset(loop) elif message.startswith("help") or message.startswith("帮助"): return True, self.help() + elif message.startswith("update"): + return True, self.update() return False, None def reset(self, loop): diff --git a/requirements.txt b/requirements.txt index 424b705c6..c6f9278a0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ baidu-aip EdgeGPT~=0.1.2 chardet Pillow +GitPython