2023-01-05 15:41:13 +08:00
|
|
|
import datetime
|
2023-01-05 15:34:14 +08:00
|
|
|
|
2023-01-05 21:00:17 +08:00
|
|
|
import pkg.utils.context
|
|
|
|
|
2023-01-02 12:54:38 +08:00
|
|
|
|
2023-01-17 12:30:45 +08:00
|
|
|
def check_dulwich_closure():
|
2023-01-16 21:34:35 +08:00
|
|
|
try:
|
|
|
|
import pkg.utils.pkgmgr
|
|
|
|
pkg.utils.pkgmgr.ensure_dulwich()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2023-01-02 13:37:28 +08:00
|
|
|
try:
|
|
|
|
import dulwich
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
raise Exception("dulwich模块未安装,请查看 https://github.com/RockChinQ/QChatGPT/issues/77")
|
2023-01-17 12:30:45 +08:00
|
|
|
|
|
|
|
|
2023-01-17 12:35:40 +08:00
|
|
|
def pull_latest(repo_path: str) -> bool:
|
|
|
|
"""拉取最新代码"""
|
|
|
|
check_dulwich_closure()
|
|
|
|
|
|
|
|
from dulwich import porcelain
|
|
|
|
|
|
|
|
repo = porcelain.open_repo(repo_path)
|
|
|
|
porcelain.pull(repo)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2023-01-17 12:30:45 +08:00
|
|
|
def update_all() -> bool:
|
|
|
|
"""使用dulwich更新源码"""
|
|
|
|
check_dulwich_closure()
|
|
|
|
import dulwich
|
2023-01-01 22:52:27 +08:00
|
|
|
try:
|
2023-01-05 21:00:17 +08:00
|
|
|
before_commit_id = get_current_commit_id()
|
2023-01-01 22:52:27 +08:00
|
|
|
from dulwich import porcelain
|
|
|
|
repo = porcelain.open_repo('.')
|
|
|
|
porcelain.pull(repo)
|
2023-01-05 21:00:17 +08:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2023-01-05 21:01:37 +08:00
|
|
|
if change_log != "":
|
|
|
|
pkg.utils.context.get_qqbot_manager().notify_admin("代码拉取完成,更新内容如下:\n"+change_log)
|
2023-01-05 21:04:52 +08:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2023-01-01 22:52:27 +08:00
|
|
|
except ModuleNotFoundError:
|
2023-01-02 12:54:38 +08:00
|
|
|
raise Exception("dulwich模块未安装,请查看 https://github.com/RockChinQ/QChatGPT/issues/77")
|
|
|
|
except dulwich.porcelain.DivergedBranches:
|
2023-01-02 12:56:52 +08:00
|
|
|
raise Exception("分支不一致,自动更新仅支持master分支,请手动更新(https://github.com/RockChinQ/QChatGPT/issues/76)")
|
2023-01-05 15:34:14 +08:00
|
|
|
|
|
|
|
|
2023-01-17 12:30:45 +08:00
|
|
|
def is_repo(path: str) -> bool:
|
|
|
|
"""检查是否是git仓库"""
|
|
|
|
check_dulwich_closure()
|
|
|
|
|
|
|
|
from dulwich import porcelain
|
2023-01-16 21:34:35 +08:00
|
|
|
try:
|
2023-01-17 12:30:45 +08:00
|
|
|
porcelain.open_repo(path)
|
|
|
|
return True
|
2023-01-16 21:34:35 +08:00
|
|
|
except:
|
2023-01-17 12:30:45 +08:00
|
|
|
return False
|
2023-01-16 21:34:35 +08:00
|
|
|
|
2023-01-17 12:30:45 +08:00
|
|
|
|
|
|
|
def get_remote_url(repo_path: str) -> str:
|
|
|
|
"""获取远程仓库地址"""
|
|
|
|
check_dulwich_closure()
|
|
|
|
|
|
|
|
from dulwich import porcelain
|
|
|
|
repo = porcelain.open_repo(repo_path)
|
|
|
|
return str(porcelain.get_remote_repo(repo, "origin")[1])
|
|
|
|
|
|
|
|
|
|
|
|
def get_current_version_info() -> str:
|
|
|
|
"""获取当前版本信息"""
|
|
|
|
check_dulwich_closure()
|
2023-01-05 15:34:14 +08:00
|
|
|
|
|
|
|
from dulwich import porcelain
|
|
|
|
|
|
|
|
repo = porcelain.open_repo('.')
|
|
|
|
|
|
|
|
version_str = ""
|
|
|
|
|
|
|
|
for entry in repo.get_walker():
|
|
|
|
version_str += "提交编号: "+str(entry.commit.id)[2:9] + "\n"
|
2023-01-05 15:41:13 +08:00
|
|
|
tz = datetime.timezone(datetime.timedelta(hours=entry.commit.commit_timezone // 3600))
|
|
|
|
dt = datetime.datetime.fromtimestamp(entry.commit.commit_time, tz)
|
2023-01-08 16:32:27 +08:00
|
|
|
version_str += "时间: "+dt.strftime('%m-%d %H:%M:%S') + "\n"
|
2023-01-05 15:34:14 +08:00
|
|
|
version_str += "说明: "+str(entry.commit.message, encoding="utf-8").strip() + "\n"
|
|
|
|
version_str += "提交作者: '" + str(entry.commit.author)[2:-1] + "'"
|
|
|
|
break
|
|
|
|
|
|
|
|
return version_str
|
2023-01-05 15:50:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_commit_id_and_time_and_msg() -> str:
|
|
|
|
"""获取当前提交id和时间和提交信息"""
|
2023-01-17 12:30:45 +08:00
|
|
|
check_dulwich_closure()
|
2023-01-05 15:50:45 +08:00
|
|
|
|
|
|
|
from dulwich import porcelain
|
|
|
|
|
|
|
|
repo = porcelain.open_repo('.')
|
|
|
|
|
|
|
|
for entry in repo.get_walker():
|
|
|
|
tz = datetime.timezone(datetime.timedelta(hours=entry.commit.commit_timezone // 3600))
|
|
|
|
dt = datetime.datetime.fromtimestamp(entry.commit.commit_time, tz)
|
|
|
|
return str(entry.commit.id)[2:9] + " " + dt.strftime('%Y-%m-%d %H:%M:%S') + " [" + str(entry.commit.message, encoding="utf-8").strip()+"]"
|
|
|
|
|
2023-01-05 20:52:59 +08:00
|
|
|
|
2023-01-05 21:00:17 +08:00
|
|
|
def get_current_commit_id() -> str:
|
2023-01-05 20:52:59 +08:00
|
|
|
"""检查是否有新版本"""
|
2023-01-17 12:30:45 +08:00
|
|
|
check_dulwich_closure()
|
2023-01-05 20:52:59 +08:00
|
|
|
|
|
|
|
from dulwich import porcelain
|
|
|
|
|
|
|
|
repo = porcelain.open_repo('.')
|
|
|
|
current_commit_id = ""
|
|
|
|
for entry in repo.get_walker():
|
2023-01-05 21:00:17 +08:00
|
|
|
current_commit_id = str(entry.commit.id)[2:-1]
|
2023-01-05 20:52:59 +08:00
|
|
|
break
|
|
|
|
|
2023-01-05 21:00:17 +08:00
|
|
|
return current_commit_id
|
|
|
|
|
|
|
|
|
|
|
|
def is_new_version_available() -> bool:
|
|
|
|
"""检查是否有新版本"""
|
2023-01-17 12:30:45 +08:00
|
|
|
check_dulwich_closure()
|
2023-01-05 21:00:17 +08:00
|
|
|
|
|
|
|
from dulwich import porcelain
|
|
|
|
|
|
|
|
repo = porcelain.open_repo('.')
|
|
|
|
fetch_res = porcelain.ls_remote(porcelain.get_remote_repo(repo, "origin")[1])
|
|
|
|
|
|
|
|
current_commit_id = get_current_commit_id()
|
|
|
|
|
|
|
|
latest_commit_id = str(fetch_res[b'HEAD'])[2:-1]
|
2023-01-05 20:52:59 +08:00
|
|
|
|
|
|
|
return current_commit_id != latest_commit_id
|