fix(update): auto-detect remote/branch when AUTO_UPDATE_REMOTE is unset
This commit is contained in:
61
SubMind.py
61
SubMind.py
@@ -48,7 +48,7 @@ DB_FILE = 'submind.db'
|
||||
|
||||
# 自动更新配置
|
||||
UPDATE_OWNER_ID = os.getenv('UPDATE_OWNER_ID') # 仅允许此用户执行 /update
|
||||
AUTO_UPDATE_REMOTE = os.getenv('AUTO_UPDATE_REMOTE', 'gitllc')
|
||||
AUTO_UPDATE_REMOTE = os.getenv('AUTO_UPDATE_REMOTE', '').strip()
|
||||
AUTO_UPDATE_BRANCH = os.getenv('AUTO_UPDATE_BRANCH', 'main')
|
||||
|
||||
# --- 对话处理器状态 ---
|
||||
@@ -1625,6 +1625,49 @@ def _can_run_update(user_id: int) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def _resolve_update_target(repo_dir: str):
|
||||
"""
|
||||
解析更新目标 remote/branch。
|
||||
优先级:
|
||||
1) 环境变量 AUTO_UPDATE_REMOTE + AUTO_UPDATE_BRANCH
|
||||
2) 当前分支上游 @{u}
|
||||
3) 远程优先 gitllc,其次 origin,分支用 AUTO_UPDATE_BRANCH 或 main
|
||||
"""
|
||||
branch = (AUTO_UPDATE_BRANCH or 'main').strip() or 'main'
|
||||
|
||||
# 1) 明确指定 remote
|
||||
if AUTO_UPDATE_REMOTE:
|
||||
return AUTO_UPDATE_REMOTE, branch
|
||||
|
||||
# 2) 尝试读取上游分支(如 gitllc/main)
|
||||
upstream_proc = subprocess.run(
|
||||
["git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"],
|
||||
cwd=repo_dir, capture_output=True, text=True
|
||||
)
|
||||
if upstream_proc.returncode == 0:
|
||||
upstream = upstream_proc.stdout.strip()
|
||||
if '/' in upstream:
|
||||
remote, up_branch = upstream.split('/', 1)
|
||||
if remote and up_branch:
|
||||
return remote, up_branch
|
||||
|
||||
# 3) 回退:从远程列表推断
|
||||
remotes_proc = subprocess.run(["git", "remote"], cwd=repo_dir, capture_output=True, text=True)
|
||||
if remotes_proc.returncode != 0:
|
||||
return None, None
|
||||
|
||||
remotes = [r.strip() for r in remotes_proc.stdout.splitlines() if r.strip()]
|
||||
if not remotes:
|
||||
return None, None
|
||||
|
||||
if 'gitllc' in remotes:
|
||||
return 'gitllc', branch
|
||||
if 'origin' in remotes:
|
||||
return 'origin', branch
|
||||
|
||||
return remotes[0], branch
|
||||
|
||||
|
||||
async def update_bot(update: Update, context: CallbackContext):
|
||||
user_id = update.effective_user.id
|
||||
if not _can_run_update(user_id):
|
||||
@@ -1636,7 +1679,12 @@ async def update_bot(update: Update, context: CallbackContext):
|
||||
repo_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
try:
|
||||
fetch_cmd = ["git", "fetch", AUTO_UPDATE_REMOTE, AUTO_UPDATE_BRANCH]
|
||||
remote_name, branch_name = _resolve_update_target(repo_dir)
|
||||
if not remote_name or not branch_name:
|
||||
await update.message.reply_text("更新失败:无法解析 git 远程仓库,请检查仓库 remote 配置。")
|
||||
return
|
||||
|
||||
fetch_cmd = ["git", "fetch", remote_name, branch_name]
|
||||
fetch_proc = subprocess.run(fetch_cmd, cwd=repo_dir, capture_output=True, text=True)
|
||||
if fetch_proc.returncode != 0:
|
||||
err = (fetch_proc.stderr or fetch_proc.stdout or "未知错误").strip()
|
||||
@@ -1647,7 +1695,7 @@ async def update_bot(update: Update, context: CallbackContext):
|
||||
["git", "rev-parse", "HEAD"], cwd=repo_dir, capture_output=True, text=True
|
||||
)
|
||||
remote_rev = subprocess.run(
|
||||
["git", "rev-parse", f"{AUTO_UPDATE_REMOTE}/{AUTO_UPDATE_BRANCH}"],
|
||||
["git", "rev-parse", f"{remote_name}/{branch_name}"],
|
||||
cwd=repo_dir, capture_output=True, text=True
|
||||
)
|
||||
|
||||
@@ -1663,7 +1711,7 @@ async def update_bot(update: Update, context: CallbackContext):
|
||||
return
|
||||
|
||||
reset_proc = subprocess.run(
|
||||
["git", "reset", "--hard", f"{AUTO_UPDATE_REMOTE}/{AUTO_UPDATE_BRANCH}"],
|
||||
["git", "reset", "--hard", f"{remote_name}/{branch_name}"],
|
||||
cwd=repo_dir, capture_output=True, text=True
|
||||
)
|
||||
if reset_proc.returncode != 0:
|
||||
@@ -1680,7 +1728,10 @@ async def update_bot(update: Update, context: CallbackContext):
|
||||
await update.message.reply_text(f"依赖安装失败:\n<code>{escape_html(err[-1800:])}</code>", parse_mode='HTML')
|
||||
return
|
||||
|
||||
await update.message.reply_text("更新完成,正在重启机器人…")
|
||||
await update.message.reply_text(
|
||||
f"更新完成({escape_html(remote_name)}/{escape_html(branch_name)}),正在重启机器人…",
|
||||
parse_mode='HTML'
|
||||
)
|
||||
os.execv(sys.executable, [sys.executable] + sys.argv)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user