From dcc5d40a04433671541ab024a04e19b21d6266ce Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Thu, 5 Jan 2023 21:00:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E4=B9=8B=E5=90=8E?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E7=AE=A1=E7=90=86=E5=91=98=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/utils/updater.py | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/pkg/utils/updater.py b/pkg/utils/updater.py index f169f9ca..caf59eeb 100644 --- a/pkg/utils/updater.py +++ b/pkg/utils/updater.py @@ -1,5 +1,7 @@ import datetime +import pkg.utils.context + def update_all(): """使用dulwich更新源码""" @@ -8,9 +10,22 @@ def update_all(): except ModuleNotFoundError: raise Exception("dulwich模块未安装,请查看 https://github.com/RockChinQ/QChatGPT/issues/77") try: + before_commit_id = get_current_commit_id() from dulwich import porcelain repo = porcelain.open_repo('.') porcelain.pull(repo) + after_commit_id = get_current_commit_id() + + change_log = "" + + for entry in repo.get_walker(): + if str(entry.commit.id)[2:-1] == before_commit_id: + break + tz = datetime.timezone(datetime.timedelta(hours=entry.commit.commit_timezone // 3600)) + dt = datetime.datetime.fromtimestamp(entry.commit.commit_time, tz) + change_log += dt.strftime('%Y-%m-%d %H:%M:%S') + " [" + str(entry.commit.message, encoding="utf-8").strip()+"]\n" + + pkg.utils.context.get_qqbot_manager().notify_admin("更新完成,更新内容如下:\n"+change_log) except ModuleNotFoundError: raise Exception("dulwich模块未安装,请查看 https://github.com/RockChinQ/QChatGPT/issues/77") except dulwich.porcelain.DivergedBranches: @@ -59,6 +74,24 @@ def get_commit_id_and_time_and_msg() -> str: return str(entry.commit.id)[2:9] + " " + dt.strftime('%Y-%m-%d %H:%M:%S') + " [" + str(entry.commit.message, encoding="utf-8").strip()+"]" +def get_current_commit_id() -> str: + """检查是否有新版本""" + try: + import dulwich + except ModuleNotFoundError: + raise Exception("dulwich模块未安装,请查看 https://github.com/RockChinQ/QChatGPT/issues/77") + + from dulwich import porcelain + + repo = porcelain.open_repo('.') + current_commit_id = "" + for entry in repo.get_walker(): + current_commit_id = str(entry.commit.id)[2:-1] + break + + return current_commit_id + + def is_new_version_available() -> bool: """检查是否有新版本""" try: @@ -71,11 +104,8 @@ def is_new_version_available() -> bool: repo = porcelain.open_repo('.') fetch_res = porcelain.ls_remote(porcelain.get_remote_repo(repo, "origin")[1]) - current_commit_id = "" - for entry in repo.get_walker(): - current_commit_id = str(entry.commit.id)[2:9] - break + current_commit_id = get_current_commit_id() - latest_commit_id = str(fetch_res[b'HEAD'])[2:9] + latest_commit_id = str(fetch_res[b'HEAD'])[2:-1] return current_commit_id != latest_commit_id