Update clashctl

This commit is contained in:
Arvin
2026-03-22 16:41:31 +08:00
parent 38a4093a15
commit de18f3182e
+39 -32
View File
@@ -306,8 +306,8 @@ safe_regenerate_config() {
}
cmd_update() {
local resume_mode="${1:-}"
local branch remote_name dirty pull_ok=1
local regen_result=0
if ! has_git_repo; then
err "当前目录不是 Git 仓库: $PROJECT_DIR"
@@ -322,52 +322,60 @@ cmd_update() {
remote_name="origin"
# 保护本地 .env
if [ -f "$PROJECT_DIR/.env" ]; then
cp -f "$PROJECT_DIR/.env" "$PROJECT_DIR/.env.bak" 2>/dev/null || true
fi
# 第二阶段:跳过 git 更新,只执行更新后的收尾逻辑
if [ "$resume_mode" != "--resume-after-self-update" ]; then
if [ -f "$PROJECT_DIR/.env" ]; then
cp -f "$PROJECT_DIR/.env" "$PROJECT_DIR/.env.bak" 2>/dev/null || true
fi
dirty="$(git -C "$PROJECT_DIR" status --porcelain --untracked-files=no)"
if [ -n "${dirty:-}" ]; then
echo "[WARN] 检测到本地已修改但未提交的文件,自动切换到安全强制更新模式"
pull_ok=0
fi
if [ "$pull_ok" -eq 1 ]; then
echo "[INFO] 正在从 ${remote_name}/${branch} 拉取最新代码 ..."
if ! git -C "$PROJECT_DIR" pull --ff-only "$remote_name" "$branch"; then
echo "[WARN] git pull 失败,自动切换到强制更新模式"
dirty="$(git -C "$PROJECT_DIR" status --porcelain --untracked-files=no)"
if [ -n "${dirty:-}" ]; then
echo "[WARN] 检测到本地已修改但未提交的文件,自动切换到安全强制更新模式"
pull_ok=0
fi
fi
if [ "$pull_ok" -eq 0 ]; then
echo "[INFO] 正在获取 ${remote_name}/${branch} 最新代码 ..."
if ! git -C "$PROJECT_DIR" fetch "$remote_name" "$branch"; then
err "git fetch 失败"
exit 1
if [ "$pull_ok" -eq 1 ]; then
echo "[INFO] 正在从 ${remote_name}/${branch} 拉取最新代码 ..."
if ! git -C "$PROJECT_DIR" pull --ff-only "$remote_name" "$branch"; then
echo "[WARN] git pull 失败,自动切换到强制更新模式"
pull_ok=0
fi
fi
echo "[WARN] 正在重置本地代码到 ${remote_name}/${branch}"
if ! git -C "$PROJECT_DIR" reset --hard "${remote_name}/${branch}"; then
err "git reset --hard 失败"
exit 1
if [ "$pull_ok" -eq 0 ]; then
echo "[INFO] 正在获取 ${remote_name}/${branch} 最新代码 ..."
if ! git -C "$PROJECT_DIR" fetch "$remote_name" "$branch"; then
err "git fetch 失败"
exit 1
fi
echo "[WARN] 正在重置本地代码到 ${remote_name}/${branch}"
if ! git -C "$PROJECT_DIR" reset --hard "${remote_name}/${branch}"; then
err "git reset --hard 失败"
exit 1
fi
fi
if [ -f "$PROJECT_DIR/.env.bak" ]; then
mv -f "$PROJECT_DIR/.env.bak" "$PROJECT_DIR/.env" 2>/dev/null || true
fi
hash -r
echo "[INFO] 已更新到最新代码,正在切换到新版本继续执行..."
exec "$PROJECT_DIR/clashctl" update --resume-after-self-update
fi
# 恢复 .env
if [ -f "$PROJECT_DIR/.env.bak" ]; then
mv -f "$PROJECT_DIR/.env.bak" "$PROJECT_DIR/.env" 2>/dev/null || true
fi
# ===== 从这里开始,一定是新版本脚本在执行 =====
echo "[INFO] 修复执行权限中..."
chmod +x "$PROJECT_DIR"/clashctl 2>/dev/null || true
chmod +x "$PROJECT_DIR/clashctl" 2>/dev/null || true
chmod +x "$PROJECT_DIR"/scripts/*.sh 2>/dev/null || true
chmod +x "$PROJECT_DIR"/bin/* 2>/dev/null || true
sed -i 's/\r$//' "$PROJECT_DIR"/scripts/*.sh 2>/dev/null || true
safe_regenerate_config
regen_result=$?
local regen_result=$?
if [ "$regen_result" -eq 1 ]; then
err "配置生成失败,且不存在可用旧配置"
@@ -406,7 +414,6 @@ cmd_update() {
else
ok "更新完成"
fi
cmd_status
fi
}