@infly/libs 2.4.1 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ const { execFileSync } = require("node:child_process");
2
+
3
+ /** beforeBuild 与镜像发布共用的根仓更新;始终只快进,不切分支、不创建合并提交。 */
4
+ function updateRootRepository(rootDir, targetBranch = "master", {
5
+ run = execFileSync, requireRemoteHead = false, log = console.log,
6
+ } = {}) {
7
+ if (!/^[A-Za-z0-9][A-Za-z0-9._/-]*$/.test(targetBranch) || targetBranch.includes("..")) {
8
+ throw new Error("根仓目标分支无效");
9
+ }
10
+ const capture = (...args) => String(run("git", ["-C", rootDir, ...args], {
11
+ cwd: rootDir, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"],
12
+ })).trim();
13
+ if (capture("status", "--porcelain", "--untracked-files=all")) {
14
+ throw new Error("admin-monorepo 存在未提交改动,请先处理后重新构建");
15
+ }
16
+ const before = capture("rev-parse", "HEAD");
17
+ log(`构建前同步根仓 origin/${targetBranch}…`);
18
+ capture("fetch", "origin", `refs/heads/${targetBranch}:refs/remotes/origin/${targetBranch}`);
19
+ const expectedRef = `origin/${targetBranch}`;
20
+ const revision = capture("rev-parse", `${expectedRef}^{commit}`);
21
+ if (!/^[0-9a-f]{40}$/.test(revision)) throw new Error("无法解析根仓远端提交");
22
+ try { capture("merge", "--ff-only", revision); }
23
+ catch (error) { throw new Error("根仓分叉或无法快进更新,已停止;请手动处理冲突后重试", { cause: error }); }
24
+ const head = capture("rev-parse", "HEAD");
25
+ if (capture("status", "--porcelain", "--untracked-files=all") || (requireRemoteHead && head !== revision)) {
26
+ throw new Error("根仓未与远端 master 同步(可能本地领先或发生并发修改),已停止;不会自动 reset");
27
+ }
28
+ return { workspaceRoot: rootDir, before, revision: head, expectedRef, verified: head === revision };
29
+ }
30
+
31
+ module.exports = { updateRootRepository };