fix: 版本按提交号标当前,并拦住跨站调用与旧 pin 补丁

同版本号的旧标签不再显示当前。本机接口拒绝跨站请求,更新只接受已知 ref。#41 去掉 dsh-purge:committed 短路。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
叫我小杨同学andCursor committed 2026-09-22 00:15:14 +08:00
1 parent 566fc3683c
commit f3afd9a5cb
7 files changed
+172 -43

No files matched your search

+56 -19
View File
@@ -469,17 +469,34 @@ function rememberVersion(item, seen) {
return item;
}
function markCurrent(versions, state, localVer, localSha) {
return (versions || []).map((item) => ({
...item,
shaShort: item.shaShort || (item.sha ? String(item.sha).slice(0, 7) : ""),
current: Boolean(
(localSha && item.sha && sameRev(localSha, item.sha))
|| (state.pin && item.ref === state.pin)
|| (!state.pin && item.latest && item.channel === state.channel && item.version && item.version === localVer)
|| (!state.pin && item.version && item.version === localVer && item.channel === (isBetaName(localVer) ? "beta" : "stable")),
),
}));
const UPDATE_REF_RE = /^(?:master|beta|v\d+\.\d+[\w.-]{0,48}|[0-9a-f]{7,40})$/i;
export function safeUpdateRef(ref) {
const target = String(ref || "").trim();
if (!UPDATE_REF_RE.test(target)) return "";
return target;
}
export function markCurrent(versions, state, localVer, localSha) {
const pin = String(state?.pin || "");
return (versions || []).map((item) => {
const shaHit = Boolean(localSha && item.sha && sameRev(localSha, item.sha));
const pinHit = Boolean(pin && item.ref === pin);
// 没有本地提交号时,只把本通道「最新」标成当前。同版本号的旧标签不能再标。
const headHit = Boolean(
!pin
&& !localSha
&& item.latest
&& item.channel === state?.channel
&& item.version
&& item.version === localVer
);
return {
...item,
shaShort: item.shaShort || (item.sha ? String(item.sha).slice(0, 7) : ""),
current: shaHit || pinHit || headHit,
};
});
}
async function listGithubTags() {
@@ -589,8 +606,7 @@ function describeLane(id, listed, state, localVer, localRev) {
const version = latest?.version || "";
const onLane = state.channel === id;
const matchesHead = Boolean(localRev && sha && sameRev(localRev, sha));
const current = versions.some((item) => item.current) || matchesHead
|| Boolean(onLane && version && localVer && version === localVer && !state.pin);
const current = matchesHead || versions.some((item) => item.current && (item.latest || (state.pin && item.ref === state.pin)));
let hasUpdate = false;
if (onLane && state.pin) {
hasUpdate = Boolean(sha && localRev && !sameRev(localRev, sha));
@@ -708,7 +724,8 @@ function gitResetTo(dir, ref, { force = false } = {}) {
if (!force && gitTrackedDirty(dir)) {
throw new Error("本地插件文件有未提交改动,未覆盖");
}
const target = String(ref || STABLE_REF);
const target = safeUpdateRef(ref || STABLE_REF);
if (!target) throw new Error("未知版本");
try {
runGit(dir, ["fetch", "origin", "--tags"], 60000);
} catch {
@@ -790,7 +807,8 @@ export async function applyUpdate(jobs = planUpdates(), options = {}) {
jobs = planUpdates();
}
const state = readChannelState();
const ref = String(options.ref || currentChannelRef(state) || STABLE_REF);
const ref = safeUpdateRef(options.ref || currentChannelRef(state) || STABLE_REF);
if (!ref) throw new Error("未知版本");
const force = options.force !== false;
const zipDirs = [];
const vias = new Set();
@@ -884,12 +902,25 @@ export async function switchChannel(channel, { apply = true } = {}) {
return applyUpdate(planUpdates(), { ref: CHANNELS[next].ref, channel: next, pin: false, force: true });
}
function listedUpdateRef(target, listed) {
const safe = safeUpdateRef(target);
if (!safe) return "";
const hit = (listed?.versions || []).find((item) => (
item.ref === safe
|| item.id === safe
|| item.sha === safe
|| (item.sha && safe.length >= 7 && (item.sha.startsWith(safe) || safe.startsWith(item.sha)))
));
return safeUpdateRef(hit?.ref || "");
}
export async function switchVersion(ref) {
const target = String(ref || "").trim();
const target = safeUpdateRef(ref);
if (!target) throw new Error("请选择要回退的版本");
const listed = await listRemoteVersions();
const hit = listed.versions.find((item) => item.ref === target || item.id === target || item.version === target || item.sha === target);
const resolved = hit?.ref || target;
const resolved = listedUpdateRef(target, listed);
if (!resolved) throw new Error("未知版本");
const hit = listed.versions.find((item) => item.ref === resolved);
const channel = hit?.channel || readChannelState().channel;
const pin = hit?.latest ? "" : resolved;
return applyUpdate(planUpdates(), { ref: resolved, channel, pin, force: true });
@@ -902,7 +933,13 @@ export async function handleUpdateOp(body = {}) {
if (op === "switch" || op === "rollback") return switchVersion(body.ref || body.version || body.id);
if (op === "auto") return autoUpdateIfNeeded();
const state = readChannelState();
const ref = body.ref || currentChannelRef(state);
const requested = body.ref || currentChannelRef(state);
const ref = safeUpdateRef(requested);
if (!ref) throw new Error("未知版本");
if (ref !== STABLE_REF && ref !== BETA_REF) {
const listed = await listRemoteVersions();
if (!listedUpdateRef(ref, listed)) throw new Error("未知版本");
}
const channel = body.channel || (ref === BETA_REF ? "beta" : ref === STABLE_REF ? "stable" : state.channel);
return applyUpdate(planUpdates(), { ref, channel, pin: false, force: true });
}