@tama-create/sf-tools 0.0.0-stage → 1.4.0-dev.1
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.
- package/LICENSE +24 -0
- package/README.md +212 -2
- package/bin/sft.js +22 -0
- package/dist/cli.js +35 -0
- package/dist/commands/admin.js +46 -0
- package/dist/commands/check.js +20 -0
- package/dist/commands/ci.js +123 -0
- package/dist/commands/deploy.js +31 -0
- package/dist/commands/doctor.js +11 -0
- package/dist/commands/dryrun.js +17 -0
- package/dist/commands/hook.js +46 -0
- package/dist/commands/index.js +36 -0
- package/dist/commands/job.js +31 -0
- package/dist/commands/launcher.js +40 -0
- package/dist/commands/log.js +58 -0
- package/dist/commands/metasync.js +21 -0
- package/dist/commands/pr.js +32 -0
- package/dist/commands/project.js +93 -0
- package/dist/commands/push.js +16 -0
- package/dist/commands/release.js +19 -0
- package/dist/commands/start.js +19 -0
- package/dist/commands/switch-org.js +12 -0
- package/dist/commands/update-cli.js +11 -0
- package/dist/commands/version.js +18 -0
- package/dist/core/admin-clone.js +79 -0
- package/dist/core/ai-answers.js +40 -0
- package/dist/core/branches.js +88 -0
- package/dist/core/check-targets.js +170 -0
- package/dist/core/check.js +34 -0
- package/dist/core/ci-check-branch.js +150 -0
- package/dist/core/ci-login-steps.js +73 -0
- package/dist/core/ci-release-steps.js +193 -0
- package/dist/core/ci-sequence-steps.js +188 -0
- package/dist/core/ci-validate-steps.js +140 -0
- package/dist/core/command.js +25 -0
- package/dist/core/create-connect.js +519 -0
- package/dist/core/create-setup.js +314 -0
- package/dist/core/create-state.js +82 -0
- package/dist/core/delegate.js +45 -0
- package/dist/core/deploy.js +66 -0
- package/dist/core/diag.js +200 -0
- package/dist/core/doctor.js +211 -0
- package/dist/core/eca.js +259 -0
- package/dist/core/exit-codes.js +12 -0
- package/dist/core/gh-identity.js +46 -0
- package/dist/core/hook-install.js +130 -0
- package/dist/core/hook.js +116 -0
- package/dist/core/identity.js +48 -0
- package/dist/core/job.js +243 -0
- package/dist/core/launcher-status.js +184 -0
- package/dist/core/lines.js +25 -0
- package/dist/core/log-auto.js +56 -0
- package/dist/core/log-box.js +85 -0
- package/dist/core/log-collector.js +370 -0
- package/dist/core/log-format.js +44 -0
- package/dist/core/log-pull.js +565 -0
- package/dist/core/log-select.js +126 -0
- package/dist/core/log-session.js +362 -0
- package/dist/core/log-start.js +421 -0
- package/dist/core/log-status.js +223 -0
- package/dist/core/log-summarize.js +127 -0
- package/dist/core/logger.js +71 -0
- package/dist/core/login-url.js +50 -0
- package/dist/core/manifest-build.js +118 -0
- package/dist/core/mask.js +70 -0
- package/dist/core/metadata-types.js +79 -0
- package/dist/core/metasync.js +684 -0
- package/dist/core/org.js +115 -0
- package/dist/core/origin.js +62 -0
- package/dist/core/package-xml.js +157 -0
- package/dist/core/pr.js +199 -0
- package/dist/core/project-create.js +733 -0
- package/dist/core/project-reconnect.js +126 -0
- package/dist/core/project-status.js +235 -0
- package/dist/core/project-tier.js +374 -0
- package/dist/core/project-verify.js +517 -0
- package/dist/core/promote-check.js +368 -0
- package/dist/core/promote.js +141 -0
- package/dist/core/push.js +152 -0
- package/dist/core/release.js +256 -0
- package/dist/core/scoped-box.js +29 -0
- package/dist/core/start.js +155 -0
- package/dist/core/switch-org.js +49 -0
- package/dist/core/test-types.js +212 -0
- package/dist/core/toolbox.js +4 -0
- package/dist/core/update-cli.js +156 -0
- package/dist/core/update-secret.js +301 -0
- package/dist/core/update-workflows.js +132 -0
- package/dist/core/verify-gh.js +286 -0
- package/dist/core/verify-samples.js +152 -0
- package/dist/core/windows-shim.js +21 -0
- package/dist/io/capability-file.js +11 -0
- package/dist/io/create-deps.js +68 -0
- package/dist/io/doctor-deps.js +32 -0
- package/dist/io/hook-install-deps.js +11 -0
- package/dist/io/job-deps.js +31 -0
- package/dist/io/key-file.js +17 -0
- package/dist/io/keys.js +161 -0
- package/dist/io/log-deps.js +106 -0
- package/dist/io/metasync-deps.js +21 -0
- package/dist/io/open-browser.js +21 -0
- package/dist/io/package-version.js +10 -0
- package/dist/io/pinned-project.js +111 -0
- package/dist/io/promote-deps.js +27 -0
- package/dist/io/push-refs.js +20 -0
- package/dist/io/resolve.js +74 -0
- package/dist/io/run.js +201 -0
- package/dist/io/switch-org-deps.js +10 -0
- package/dist/io/temp-dir.js +10 -0
- package/dist/io/tool-versions.js +14 -0
- package/dist/io/toolbox.js +68 -0
- package/dist/io/update-cli-deps.js +75 -0
- package/dist/io/update-secret-deps.js +27 -0
- package/dist/launch.js +152 -0
- package/dist/ui/confirm.js +18 -0
- package/dist/ui/launcher.js +132 -0
- package/dist/ui/prompt.js +66 -0
- package/package.json +37 -4
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
// sft ci promote-check: 一方通行のリリース(develop → staging → main)の確認。シェル版の bin/sf-promote-check.sh の移し先
|
|
2
|
+
// (doc/rebuild-design.md 11.2 の段階 2。規則の正本は doc/one-way-promotion-design.md 3.2)。読み取りのみ(組織・GitHub を変えない)。
|
|
3
|
+
// 判定は promote.ts。ここは、gh・git から事実を集めて、判定に渡す(外との出入りは、道具の箱だけを通す: 7.2)。
|
|
4
|
+
// 用法: --pr <番号> [--only sequence|stack] [--tooling] [--json] / --plan [--json]
|
|
5
|
+
// --tooling: 対象が空でも、版・ワークフローを変える PR として、前の段階の印(R1)を求める(R2 は、対象が空のため見ない)。
|
|
6
|
+
// マージ後の「印だけ付ける」ジョブ用: マージ後は PR の先端が出し先の祖先になり、差分で変更を読めないため、呼ぶ側が API で確かめた結果として受け取る。確認は増える側にだけ働く
|
|
7
|
+
// 終了コード: 0 = すべて満たす(またはスキップ)/ 1 = 破った/ 2 = 判断できない(R0)。--json の形は、シェル版と同じ(凍結: 4.7)
|
|
8
|
+
import { tierList } from "./branches.js";
|
|
9
|
+
import { originRepo } from "./origin.js";
|
|
10
|
+
import { checkMarkRun, envBranches, isToolingPath, missingParts, nextTier, PROMOTE_EXIT, prevTier, promoteJson, selectMarkStatus, } from "./promote.js";
|
|
11
|
+
const BRANCH_LIST_FILE = "sf-tools/config/branches.txt";
|
|
12
|
+
/** 起動でき、終了コードが 0 だったか(起動できなかったときは null) */
|
|
13
|
+
function succeeded(r) {
|
|
14
|
+
return r !== null && r.code === 0;
|
|
15
|
+
}
|
|
16
|
+
/** 判断できない(R0)。投げて、いちばん外で受ける */
|
|
17
|
+
class CannotJudge extends Error {
|
|
18
|
+
}
|
|
19
|
+
/** deploy-target.txt・remove-target.txt の、実質的な行の数。ファイルがなければ null */
|
|
20
|
+
function countTargets(box, path) {
|
|
21
|
+
if (!box.files.exists(path))
|
|
22
|
+
return null;
|
|
23
|
+
let n = 0;
|
|
24
|
+
for (const raw of box.files.readText(path).split("\n")) {
|
|
25
|
+
const line = raw.replace(/\r/g, "").trim();
|
|
26
|
+
if (line === "" || line.startsWith("#") || line === "[files]" || line === "[members]")
|
|
27
|
+
continue;
|
|
28
|
+
n++;
|
|
29
|
+
}
|
|
30
|
+
return n;
|
|
31
|
+
}
|
|
32
|
+
export async function runPromoteCheck(args, box, deps) {
|
|
33
|
+
const log = (level, text) => box.output.err(`[${level}] ${text}`);
|
|
34
|
+
const fail2 = (text) => {
|
|
35
|
+
log("ERROR", text);
|
|
36
|
+
return PROMOTE_EXIT.cannotJudge;
|
|
37
|
+
};
|
|
38
|
+
// --- 引数 ---
|
|
39
|
+
let mode = "";
|
|
40
|
+
let prNumber = "";
|
|
41
|
+
let only = "";
|
|
42
|
+
let onlySet = false;
|
|
43
|
+
let json = false;
|
|
44
|
+
let tooling = false;
|
|
45
|
+
for (let i = 0; i < args.length; i++) {
|
|
46
|
+
const a = args[i];
|
|
47
|
+
if (a === "--pr") {
|
|
48
|
+
if (mode !== "")
|
|
49
|
+
return fail2("--pr と --plan は、どちらか 1 つです。");
|
|
50
|
+
const v = args[i + 1] ?? "";
|
|
51
|
+
if (!/^[1-9][0-9]*$/.test(v))
|
|
52
|
+
return fail2("--pr には、PR の番号(1 以上の整数)を指定してください。");
|
|
53
|
+
mode = "pr";
|
|
54
|
+
prNumber = v;
|
|
55
|
+
i++;
|
|
56
|
+
}
|
|
57
|
+
else if (a === "--plan") {
|
|
58
|
+
if (mode !== "")
|
|
59
|
+
return fail2("--pr と --plan は、どちらか 1 つです。");
|
|
60
|
+
mode = "plan";
|
|
61
|
+
}
|
|
62
|
+
else if (a === "--only") {
|
|
63
|
+
if (onlySet)
|
|
64
|
+
return fail2("--only は 1 回だけ指定できます。");
|
|
65
|
+
const v = args[i + 1] ?? "";
|
|
66
|
+
if (v !== "sequence" && v !== "stack")
|
|
67
|
+
return fail2(`--only の値は sequence か stack です: ${v === "" ? "(空)" : v}`);
|
|
68
|
+
only = v;
|
|
69
|
+
onlySet = true;
|
|
70
|
+
i++;
|
|
71
|
+
}
|
|
72
|
+
else if (a === "--tooling") {
|
|
73
|
+
// マージ後(印だけ付けるジョブ)は、先端がマージ先の祖先になり、差分で版・ワークフローの変更を読めない。呼ぶ側が API で確かめた結果として受け取る
|
|
74
|
+
tooling = true;
|
|
75
|
+
}
|
|
76
|
+
else if (a === "--json") {
|
|
77
|
+
json = true;
|
|
78
|
+
}
|
|
79
|
+
else if (a === "--verbose" || a === "-v") {
|
|
80
|
+
// 画面の詳細は、道具の箱が決める
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
return fail2(`不明なオプションです: ${a}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (mode === "")
|
|
87
|
+
return fail2("--pr <番号> か --plan を指定してください。");
|
|
88
|
+
if (mode !== "pr" && onlySet)
|
|
89
|
+
return fail2("--only は --pr と一緒に使います。");
|
|
90
|
+
if (mode !== "pr" && tooling)
|
|
91
|
+
return fail2("--tooling は --pr と一緒に使います。");
|
|
92
|
+
const checkR1 = only !== "stack";
|
|
93
|
+
const checkR2 = only !== "sequence";
|
|
94
|
+
const checkR3 = only !== "stack";
|
|
95
|
+
const result = {
|
|
96
|
+
mode,
|
|
97
|
+
ok: false,
|
|
98
|
+
skipped: false,
|
|
99
|
+
branch: "",
|
|
100
|
+
head: "",
|
|
101
|
+
target: "",
|
|
102
|
+
nextTier: "",
|
|
103
|
+
prOpen: "",
|
|
104
|
+
tiers: [],
|
|
105
|
+
violations: [],
|
|
106
|
+
error: "",
|
|
107
|
+
};
|
|
108
|
+
const emit = (ok) => {
|
|
109
|
+
if (!json)
|
|
110
|
+
return;
|
|
111
|
+
box.output.out(promoteJson({ ...result, ok }));
|
|
112
|
+
};
|
|
113
|
+
const violation = (rule, detail) => {
|
|
114
|
+
result.violations.push({ rule, detail });
|
|
115
|
+
log("ERROR", `[${rule}] ${detail}`);
|
|
116
|
+
};
|
|
117
|
+
const temp = deps.makeTempDir();
|
|
118
|
+
const worktrees = [];
|
|
119
|
+
try {
|
|
120
|
+
log("HEADER", "一方通行のリリースを確かめます (sf-promote-check)");
|
|
121
|
+
const git = async (...a) => box.run("git", a);
|
|
122
|
+
const gh = async (...a) => box.run("gh", a);
|
|
123
|
+
const must = async (r, why) => {
|
|
124
|
+
let got;
|
|
125
|
+
try {
|
|
126
|
+
got = await r;
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
throw new CannotJudge(why);
|
|
130
|
+
}
|
|
131
|
+
if (got.code !== 0)
|
|
132
|
+
throw new CannotJudge(why);
|
|
133
|
+
return got.stdout;
|
|
134
|
+
};
|
|
135
|
+
// --- R0 の前提: リポジトリ・段階 ---
|
|
136
|
+
await must(git("rev-parse", "--is-inside-work-tree"), "Git リポジトリの中ではありません。");
|
|
137
|
+
const origin = await originRepo(box);
|
|
138
|
+
if (origin.kind === "none")
|
|
139
|
+
throw new CannotJudge("origin の URL を取得できません。");
|
|
140
|
+
if (origin.kind === "notGithub")
|
|
141
|
+
throw new CannotJudge("origin が GitHub のリポジトリではありません。");
|
|
142
|
+
const repo = `${origin.owner}/${origin.repo}`;
|
|
143
|
+
const repoLc = repo.toLowerCase();
|
|
144
|
+
await must(git("fetch", "--quiet", "origin", "main"), "origin の main を取得できません。");
|
|
145
|
+
const branchesTxt = await must(git("show", `origin/main:${BRANCH_LIST_FILE}`), `origin/main の ${BRANCH_LIST_FILE} を読めません。`);
|
|
146
|
+
const tiers = tierList(branchesTxt, { strict: true, fileName: BRANCH_LIST_FILE });
|
|
147
|
+
if (!tiers.ok) {
|
|
148
|
+
log("ERROR", tiers.error);
|
|
149
|
+
throw new CannotJudge(`段階の構成を判断できません(origin/main の ${BRANCH_LIST_FILE})。`);
|
|
150
|
+
}
|
|
151
|
+
const tiersUp = [...tiers.tiers].reverse(); // develop → staging → main(下の段階から)
|
|
152
|
+
result.tiers = tiersUp;
|
|
153
|
+
const topTier = "main";
|
|
154
|
+
const envList = envBranches(branchesTxt);
|
|
155
|
+
const isEnvBranch = (b) => envList.includes(b);
|
|
156
|
+
// --- リリースの印: 0 = ある / 1 = ない / 2 = 判断できない ---
|
|
157
|
+
const hasMark = async (sha, tier) => {
|
|
158
|
+
const r = await gh("api", "--paginate", `repos/${repo}/commits/${sha}/statuses?per_page=100`, "--jq", ".[] | {context, createdAt: .created_at, state, creator: .creator.login, targetUrl: .target_url}").catch(() => null);
|
|
159
|
+
if (!succeeded(r))
|
|
160
|
+
return 2;
|
|
161
|
+
const statuses = [];
|
|
162
|
+
for (const line of r.stdout.split("\n")) {
|
|
163
|
+
if (line.trim() === "")
|
|
164
|
+
continue;
|
|
165
|
+
try {
|
|
166
|
+
statuses.push(JSON.parse(line));
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
return 2;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const sel = selectMarkStatus(statuses, repo, sha, tier);
|
|
173
|
+
if (sel.kind === "none") {
|
|
174
|
+
if (sel.warning)
|
|
175
|
+
log("WARNING", sel.warning);
|
|
176
|
+
return 1;
|
|
177
|
+
}
|
|
178
|
+
if (sel.kind === "unknown")
|
|
179
|
+
return 2;
|
|
180
|
+
const run = await gh("api", `repos/${repo}/actions/runs/${sel.runId}`, "--jq", "{repository: .repository.full_name, path, event, conclusion, headSha: .head_sha}").catch(() => null);
|
|
181
|
+
if (!succeeded(run))
|
|
182
|
+
return 2;
|
|
183
|
+
let info;
|
|
184
|
+
try {
|
|
185
|
+
info = JSON.parse(run.stdout);
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
return 2;
|
|
189
|
+
}
|
|
190
|
+
const checked = checkMarkRun(info, repo, sha, tier);
|
|
191
|
+
if (checked.trusted)
|
|
192
|
+
return 0;
|
|
193
|
+
log("WARNING", checked.warning);
|
|
194
|
+
return 1;
|
|
195
|
+
};
|
|
196
|
+
const markOrStop = async (sha, tier) => {
|
|
197
|
+
const m = await hasMark(sha, tier);
|
|
198
|
+
if (m === 2)
|
|
199
|
+
throw new CannotJudge(`${sha.slice(0, 7)} の ${tier} の印を確かめられません。`);
|
|
200
|
+
return m === 0;
|
|
201
|
+
};
|
|
202
|
+
const targetsEmpty = (branch) => {
|
|
203
|
+
const dir = `sf-tools/release/${branch}`;
|
|
204
|
+
const a = countTargets(box, `${dir}/deploy-target.txt`);
|
|
205
|
+
const b = a === null ? null : countTargets(box, `${dir}/remove-target.txt`);
|
|
206
|
+
if (a === null || b === null) {
|
|
207
|
+
throw new CannotJudge(`${dir}/ の deploy-target.txt・remove-target.txt がありません。`);
|
|
208
|
+
}
|
|
209
|
+
return a + b === 0;
|
|
210
|
+
};
|
|
211
|
+
// --- R2: 積み重ね ---
|
|
212
|
+
const checkR2Rule = async (branch, head, tier) => {
|
|
213
|
+
log("INFO", `R2(積み重ね): ${branch} から ${tier} へ前に出したリリースの部品が、今回に含まれるかを確かめます...`);
|
|
214
|
+
const listed = await must(gh("pr", "list", "--repo", repo, "--state", "merged", "--head", branch, "--base", tier, "--limit", "1000", "--json", "number,headRefOid,isCrossRepository", "--jq", ".[] | {number, oid: .headRefOid, cross: .isCrossRepository}"), `${branch} → ${tier} のマージ済みの PR を取得できません。`);
|
|
215
|
+
const prs = listed
|
|
216
|
+
.split("\n")
|
|
217
|
+
.filter((l) => l.trim() !== "")
|
|
218
|
+
.map((l) => {
|
|
219
|
+
try {
|
|
220
|
+
return JSON.parse(l);
|
|
221
|
+
}
|
|
222
|
+
catch {
|
|
223
|
+
throw new CannotJudge(`${branch} → ${tier} のマージ済みの PR の一覧を読めません。`);
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
if (prs.length >= 1000)
|
|
227
|
+
throw new CannotJudge(`${branch} → ${tier} のマージ済みの PR が 1000 件以上あり、すべてを確かめられません。`);
|
|
228
|
+
const current = await deps.buildPairs(".", branch);
|
|
229
|
+
if (!current || current.length === 0)
|
|
230
|
+
throw new CannotJudge(`今回(${head.slice(0, 7)})のマニフェストを作れません(または空)。`);
|
|
231
|
+
for (const pr of prs) {
|
|
232
|
+
if (pr.cross)
|
|
233
|
+
continue; // フォークからの PR は対象にしない
|
|
234
|
+
if (pr.oid === head)
|
|
235
|
+
continue; // 同じコミット
|
|
236
|
+
if (!(await markOrStop(pr.oid, tier)))
|
|
237
|
+
continue; // tier に出ていない(リリースに失敗した PR など)
|
|
238
|
+
if (tier !== topTier && (await markOrStop(pr.oid, topTier)))
|
|
239
|
+
continue; // 1 周が終わった(main まで届いた)
|
|
240
|
+
const have = await git("cat-file", "-e", `${pr.oid}^{commit}`).catch(() => ({ code: 1 }));
|
|
241
|
+
if (have.code !== 0) {
|
|
242
|
+
const fetched = await git("fetch", "--quiet", "origin", pr.oid).catch(() => ({ code: 1 }));
|
|
243
|
+
if (fetched.code !== 0)
|
|
244
|
+
throw new CannotJudge(`前のリリース(PR #${pr.number}・${pr.oid.slice(0, 7)})のコミットを取得できません。`);
|
|
245
|
+
}
|
|
246
|
+
const wt = `${temp}/wt-${pr.number}`;
|
|
247
|
+
const added = await git("worktree", "add", "--quiet", "--detach", wt, pr.oid).catch(() => ({ code: 1 }));
|
|
248
|
+
if (added.code !== 0)
|
|
249
|
+
throw new CannotJudge(`前のリリース(PR #${pr.number})の作業ツリーを作れません。`);
|
|
250
|
+
worktrees.push(wt);
|
|
251
|
+
const previous = await deps.buildPairs(wt, branch);
|
|
252
|
+
if (!previous || previous.length === 0) {
|
|
253
|
+
throw new CannotJudge(`前のリリース(PR #${pr.number}・${pr.oid.slice(0, 7)})のマニフェストを作れません(または空)。`);
|
|
254
|
+
}
|
|
255
|
+
const missing = missingParts(previous, current);
|
|
256
|
+
if (missing.length > 0) {
|
|
257
|
+
violation("R2", `前に ${tier} へ出したリリース(PR #${pr.number}・${pr.oid.slice(0, 7)})の部品が、今回の deploy-target.txt・remove-target.txt にありません: ${missing.join(", ")}`);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
// --- メイン ---
|
|
262
|
+
const headNow = (await must(git("rev-parse", "HEAD"), "HEAD を読めません。")).trim();
|
|
263
|
+
if (mode === "pr") {
|
|
264
|
+
const view = await must(gh("pr", "view", prNumber, "--repo", repo, "--json", "headRefOid,headRefName,baseRefName,isCrossRepository,url", "--jq", "{head: .headRefOid, branch: .headRefName, target: .baseRefName, cross: .isCrossRepository, url}"), `PR #${prNumber} を取得できません(${repo})。`);
|
|
265
|
+
const pr = JSON.parse(view);
|
|
266
|
+
result.head = pr.head;
|
|
267
|
+
result.branch = pr.branch;
|
|
268
|
+
result.target = pr.target;
|
|
269
|
+
if (!pr.url.toLowerCase().endsWith(`github.com/${repoLc}/pull/${prNumber}`))
|
|
270
|
+
throw new CannotJudge(`PR のリポジトリが、origin(${repo})と違います: ${pr.url}`);
|
|
271
|
+
if (pr.cross)
|
|
272
|
+
throw new CannotJudge("フォークからの PR です(force-* では受け付けません)。");
|
|
273
|
+
if (headNow !== pr.head)
|
|
274
|
+
throw new CannotJudge(`作業ツリーの HEAD(${headNow.slice(0, 7)})が、PR の先端のコミット(${pr.head.slice(0, 7)})と違います。`);
|
|
275
|
+
if (!tiersUp.includes(pr.target)) {
|
|
276
|
+
log("INFO", `${pr.target} は段階ではありません(origin/main の ${BRANCH_LIST_FILE})。確かめるものはありません。`);
|
|
277
|
+
result.skipped = true;
|
|
278
|
+
emit(true);
|
|
279
|
+
return PROMOTE_EXIT.ok;
|
|
280
|
+
}
|
|
281
|
+
log("INFO", `PR #${prNumber}: ${pr.branch}(${pr.head.slice(0, 7)})→ ${pr.target}`);
|
|
282
|
+
if (checkR3 && isEnvBranch(pr.branch)) {
|
|
283
|
+
violation("R3", `環境ブランチ ${pr.branch} から、直接 ${pr.target} へは出せません(開発のブランチから出してください)。`);
|
|
284
|
+
}
|
|
285
|
+
if (!isEnvBranch(pr.branch)) {
|
|
286
|
+
// 対象が空でも、sf-tools の版・ワークフローを変える PR は、前の段階の印を求める(R1 だけ。部品がないため R2 は要らない)
|
|
287
|
+
const empty = targetsEmpty(pr.branch);
|
|
288
|
+
// 移動(rename)は、削除と追加の 2 行にして、移動前のパスも見る(ci-check-branch の API の判定と同じ)
|
|
289
|
+
if (empty && !tooling) {
|
|
290
|
+
await must(git("fetch", "--quiet", "origin", pr.target), `origin の ${pr.target} を取得できません。`);
|
|
291
|
+
const changed = await must(git("diff", "--name-only", "--no-renames", `origin/${pr.target}...HEAD`), `${pr.target} との差分の一覧を取得できません。`);
|
|
292
|
+
const toolingFiles = changed.split("\n").filter((f) => isToolingPath(f.trim()));
|
|
293
|
+
if (toolingFiles.length === 0) {
|
|
294
|
+
log("INFO", "対象が空です(deploy-target.txt・remove-target.txt)。リリースしないため、確かめません。");
|
|
295
|
+
result.skipped = true;
|
|
296
|
+
emit(true);
|
|
297
|
+
return PROMOTE_EXIT.ok;
|
|
298
|
+
}
|
|
299
|
+
log("INFO", `対象は空ですが、sf-tools の版・ワークフローを変えています(${toolingFiles.slice(0, 3).join(", ")}${toolingFiles.length > 3 ? " ほか" : ""})。前の段階の印を確かめます。`);
|
|
300
|
+
}
|
|
301
|
+
if (checkR1) {
|
|
302
|
+
const prev = prevTier(tiersUp, pr.target);
|
|
303
|
+
if (prev !== null && !(await markOrStop(pr.head, prev))) {
|
|
304
|
+
violation("R1", `${pr.head.slice(0, 7)} は、まだ ${prev} に出ていません(${prev} へのリリースが成功した、同じコミットだけを ${pr.target} へ出せます)。${prev} に出したあとに、このブランチへ変更を足していないか確かめてください。`);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
if (checkR2 && !empty)
|
|
308
|
+
await checkR2Rule(pr.branch, pr.head, pr.target);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
// --plan: 手元のブランチ
|
|
313
|
+
const sym = await git("symbolic-ref", "--quiet", "--short", "HEAD").catch(() => ({ code: 1, stdout: "" }));
|
|
314
|
+
if (sym.code !== 0)
|
|
315
|
+
throw new CannotJudge("ブランチの上にいません(detached HEAD)。");
|
|
316
|
+
result.branch = sym.stdout.trim();
|
|
317
|
+
result.head = headNow;
|
|
318
|
+
if (isEnvBranch(result.branch)) {
|
|
319
|
+
violation("R3", `環境ブランチ ${result.branch} の上にいます(開発のブランチで実行してください)。`);
|
|
320
|
+
emit(false);
|
|
321
|
+
return PROMOTE_EXIT.violated;
|
|
322
|
+
}
|
|
323
|
+
await must(git("fetch", "--quiet", "origin", result.branch), `origin の ${result.branch} を取得できません(push していない可能性があります)。`);
|
|
324
|
+
const remote = (await must(git("rev-parse", `origin/${result.branch}`), `origin/${result.branch} を読めません。`)).trim();
|
|
325
|
+
if (remote !== headNow) {
|
|
326
|
+
throw new CannotJudge(`手元(${headNow.slice(0, 7)})と origin/${result.branch}(${remote.slice(0, 7)})が違います(push していない変更があるか、取り込んでいない変更があります)。`);
|
|
327
|
+
}
|
|
328
|
+
result.skipped = targetsEmpty(result.branch);
|
|
329
|
+
const marks = [];
|
|
330
|
+
for (const t of tiersUp)
|
|
331
|
+
marks.push(await markOrStop(headNow, t));
|
|
332
|
+
const next = nextTier(tiersUp, marks, headNow.slice(0, 7));
|
|
333
|
+
if (!next.ok)
|
|
334
|
+
throw new CannotJudge(next.error);
|
|
335
|
+
result.nextTier = next.next;
|
|
336
|
+
result.target = next.next;
|
|
337
|
+
if (next.next !== "none") {
|
|
338
|
+
const open = await must(gh("pr", "list", "--repo", repo, "--state", "open", "--head", result.branch, "--base", next.next, "--json", "number", "--jq", ".[0].number // empty"), "PR の一覧を取得できません。");
|
|
339
|
+
result.prOpen = open.trim();
|
|
340
|
+
if (!result.skipped)
|
|
341
|
+
await checkR2Rule(result.branch, headNow, next.next);
|
|
342
|
+
}
|
|
343
|
+
log("INFO", `次に出してよい段階: ${next.next}${result.prOpen ? `(開いている PR: #${result.prOpen})` : ""}`);
|
|
344
|
+
}
|
|
345
|
+
if (result.violations.length > 0) {
|
|
346
|
+
log("ERROR", `一方通行のリリースの決まりを破っています(${result.violations.length} 件。doc/one-way-promotion-design.md)。`);
|
|
347
|
+
emit(false);
|
|
348
|
+
return PROMOTE_EXIT.violated;
|
|
349
|
+
}
|
|
350
|
+
log("SUCCESS", "一方通行のリリースの決まりを満たしています。");
|
|
351
|
+
emit(true);
|
|
352
|
+
return PROMOTE_EXIT.ok;
|
|
353
|
+
}
|
|
354
|
+
catch (e) {
|
|
355
|
+
if (e instanceof CannotJudge) {
|
|
356
|
+
result.error = e.message;
|
|
357
|
+
log("ERROR", `[R0] 判断できません: ${e.message}`);
|
|
358
|
+
emit(false);
|
|
359
|
+
return PROMOTE_EXIT.cannotJudge;
|
|
360
|
+
}
|
|
361
|
+
throw e;
|
|
362
|
+
}
|
|
363
|
+
finally {
|
|
364
|
+
for (const w of worktrees)
|
|
365
|
+
await box.run("git", ["worktree", "remove", "--force", w]).catch(() => undefined);
|
|
366
|
+
deps.removeDir(temp);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// 一方通行のリリース(develop → staging → main)の確認の、判定の部分。シェル版の bin/sf-promote-check.sh の移し先
|
|
2
|
+
// (doc/rebuild-design.md 11.2 の段階 2。規則の正本は doc/one-way-promotion-design.md 3.2)。
|
|
3
|
+
// この部品は、GitHub・git から集めた事実を受け取って判定するだけ(読み取りのみ・外と話さない)。
|
|
4
|
+
// 集める側(gh・git worktree・manifest_build)は、`sft ci promote-check` の側で、道具の箱を通して行う。
|
|
5
|
+
// 規則: R0 前提(満たさなければ 2 = 判断できない)/ R1 順序 / R2 積み重ね / R3 環境ブランチ
|
|
6
|
+
import { compareBytes } from "./package-xml.js";
|
|
7
|
+
/** wf-release が、組織へのリリースに成功したときに、コミットに付ける commit status の context の前半 */
|
|
8
|
+
export const MARK_PREFIX = "sf-tools/released-";
|
|
9
|
+
/** 印を信じる実行の、ワークフローのパス */
|
|
10
|
+
export const RELEASE_WORKFLOW_PATH = ".github/workflows/wf-release.yml";
|
|
11
|
+
/** 印を付けてよい者 */
|
|
12
|
+
const MARK_CREATOR = "github-actions[bot]";
|
|
13
|
+
/** 終了コード(この確認だけの意味。0 = すべて満たす/スキップ・1 = 破った・2 = 判断できない) */
|
|
14
|
+
export const PROMOTE_EXIT = { ok: 0, violated: 1, cannotJudge: 2 };
|
|
15
|
+
/**
|
|
16
|
+
* あるコミットの statuses から、段階 tier の印を選ぶ。一番新しい状態(作成日時。ISO 8601 の並べ替え)だけを見る
|
|
17
|
+
* (API の返す順番には頼らない)。success で、付けたのが github-actions[bot] で、target_url の実行が、このリポジトリのものなら、
|
|
18
|
+
* その実行の確認(checkMarkRun)へ進む。人や、ほかのワークフローが付けた印・ほかのリポジトリを指す印は、無視する(none + 警告)
|
|
19
|
+
*/
|
|
20
|
+
export function selectMarkStatus(statuses, repo, sha, tier) {
|
|
21
|
+
const context = `${MARK_PREFIX}${tier}`;
|
|
22
|
+
const rows = statuses
|
|
23
|
+
.filter((s) => s.context === context)
|
|
24
|
+
.map((s) => ({ ...s, key: `${s.createdAt}\t${s.state}\t${s.creator}\t${s.targetUrl}` }))
|
|
25
|
+
.sort((a, b) => compareBytes(a.key, b.key));
|
|
26
|
+
const latest = rows[rows.length - 1];
|
|
27
|
+
if (!latest)
|
|
28
|
+
return { kind: "none" };
|
|
29
|
+
if (latest.state !== "success")
|
|
30
|
+
return { kind: "none" };
|
|
31
|
+
const short = sha.slice(0, 7);
|
|
32
|
+
if (latest.creator !== MARK_CREATOR) {
|
|
33
|
+
return { kind: "none", warning: `${short} の ${context} は、${MARK_CREATOR} 以外(${latest.creator})が付けたため、無視します。` };
|
|
34
|
+
}
|
|
35
|
+
const found = /\/actions\/runs\/([0-9]+)/.exec(latest.targetUrl);
|
|
36
|
+
if (!found)
|
|
37
|
+
return { kind: "unknown", reason: `${short} の ${context} の実行の ID を取り出せません(壊れています)。` };
|
|
38
|
+
const runId = found[1];
|
|
39
|
+
if (!latest.targetUrl.toLowerCase().includes(`github.com/${repo.toLowerCase()}/actions/runs/${runId}`)) {
|
|
40
|
+
return {
|
|
41
|
+
kind: "none",
|
|
42
|
+
warning: `${short} の ${context} は、ほかのリポジトリの実行を指しているため、無視します(${latest.targetUrl})。`,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return { kind: "check", runId };
|
|
46
|
+
}
|
|
47
|
+
/** 印の実行が、このリポジトリの wf-release の pull_request の成功した実行で、head_sha がそのコミットのときだけ、信じる */
|
|
48
|
+
export function checkMarkRun(info, repo, sha, tier) {
|
|
49
|
+
const ok = info.repository.toLowerCase() === repo.toLowerCase() &&
|
|
50
|
+
info.path === RELEASE_WORKFLOW_PATH &&
|
|
51
|
+
info.event === "pull_request" &&
|
|
52
|
+
info.conclusion === "success" &&
|
|
53
|
+
info.headSha === sha;
|
|
54
|
+
if (ok)
|
|
55
|
+
return { trusted: true };
|
|
56
|
+
const shown = [info.repository, info.path, info.event, info.conclusion, info.headSha].join(" / ");
|
|
57
|
+
return {
|
|
58
|
+
trusted: false,
|
|
59
|
+
warning: `${sha.slice(0, 7)} の ${MARK_PREFIX}${tier} は、このリポジトリの wf-release の成功した実行(このコミット)のものでないため、無視します(${shown})。`,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* マニフェストの「種類:メンバー」(* を含む。重複なし・バイト順)。
|
|
64
|
+
* 行の分け方に頼らない(1 行に詰まった形も、複数行の形も読む): 全体を 1 つの文字列にして、<types>〜</types> ごとに、
|
|
65
|
+
* 最初の <name> と、すべての <members> を取り出す(package-xml.ts の行ごとの読み方とは、わざと違う。シェル版と同じ)
|
|
66
|
+
*/
|
|
67
|
+
export function pairsAll(xml) {
|
|
68
|
+
const out = new Set();
|
|
69
|
+
const text = xml.split("\n").join(" ");
|
|
70
|
+
for (const block of text.matchAll(/<types>([\s\S]*?)<\/types>/g)) {
|
|
71
|
+
const body = block[1];
|
|
72
|
+
const name = (/<name>([^<]*)<\/name>/.exec(body)?.[1] ?? "").trim();
|
|
73
|
+
if (name === "")
|
|
74
|
+
continue;
|
|
75
|
+
for (const m of body.matchAll(/<members>([^<]*)<\/members>/g)) {
|
|
76
|
+
const member = m[1].trim();
|
|
77
|
+
if (member !== "")
|
|
78
|
+
out.add(`${name}:${member}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return [...out].sort(compareBytes);
|
|
82
|
+
}
|
|
83
|
+
/** 前のリリースの部品のうち、今回の部品にないもの。今回に同じ種類の * があれば、含まれているとみなす */
|
|
84
|
+
export function missingParts(previous, current) {
|
|
85
|
+
const have = new Set(current);
|
|
86
|
+
return previous.filter((p) => !have.has(p) && !have.has(`${p.slice(0, p.indexOf(":"))}:*`));
|
|
87
|
+
}
|
|
88
|
+
/** 段階の 1 つ前(下の段階)。tiersUp は下の段階から(develop → staging → main)。なければ null */
|
|
89
|
+
export function prevTier(tiersUp, tier) {
|
|
90
|
+
const i = tiersUp.indexOf(tier);
|
|
91
|
+
return i > 0 ? tiersUp[i - 1] : null;
|
|
92
|
+
}
|
|
93
|
+
/** branches.txt の全行(環境ブランチ)。前後の空白と CR を除き、コメント・空行を除く */
|
|
94
|
+
export function envBranches(branchesTxt) {
|
|
95
|
+
return branchesTxt
|
|
96
|
+
.split("\n")
|
|
97
|
+
.map((l) => l.replace(/\r/g, "").trim())
|
|
98
|
+
.filter((l) => l !== "" && !l.startsWith("#"));
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* --plan: 次に出してよい段階。下の段階から順に見て、印のない最初の段階。上の段階に印があるのに下の段階にない(原則に反する)
|
|
102
|
+
* ときは、判断できない。全部にあれば none。marks は、tiersUp と同じ並びで、その段階の印があるか
|
|
103
|
+
*/
|
|
104
|
+
export function nextTier(tiersUp, marks, shortSha) {
|
|
105
|
+
let missing = "";
|
|
106
|
+
for (let i = 0; i < tiersUp.length; i++) {
|
|
107
|
+
const tier = tiersUp[i];
|
|
108
|
+
if (marks[i]) {
|
|
109
|
+
if (missing !== "")
|
|
110
|
+
return { ok: false, error: `${shortSha} は ${tier} に出ているのに、下の段階 ${missing} に出ていません(原則に反する状態)。` };
|
|
111
|
+
}
|
|
112
|
+
else if (missing === "") {
|
|
113
|
+
missing = tier;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return { ok: true, next: missing === "" ? "none" : missing };
|
|
117
|
+
}
|
|
118
|
+
/** --json の 1 行(キーの名前と並びは、シェル版と同じ。凍結: doc/rebuild-design.md 4.7 の「yml から呼ぶ引数と終了コード」) */
|
|
119
|
+
export function promoteJson(r) {
|
|
120
|
+
return JSON.stringify({
|
|
121
|
+
mode: r.mode,
|
|
122
|
+
ok: r.ok,
|
|
123
|
+
skipped: r.skipped,
|
|
124
|
+
branch: r.branch,
|
|
125
|
+
head: r.head,
|
|
126
|
+
target: r.target,
|
|
127
|
+
next_tier: r.nextTier,
|
|
128
|
+
pr_open: r.prOpen,
|
|
129
|
+
tiers: r.tiers,
|
|
130
|
+
violations: r.violations.map((v) => ({ rule: v.rule, detail: v.detail })),
|
|
131
|
+
error: r.error,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* sf-tools の版(sf-tools/package.json・package-lock.json)と、ワークフロー(.github/workflows/)のパスか。
|
|
136
|
+
* これらを変える PR は、リリースの対象が空でも、前の段階を通った印を求める(版の更新の PR が、develop を飛ばして main に入らないように。
|
|
137
|
+
* doc/rebuild-design.md 4.3)。sf-tools/config/ は含めない(branches.txt は main のものが唯一のスイッチで、段階を足す・外す PR が成り立たなくなるため)
|
|
138
|
+
*/
|
|
139
|
+
export function isToolingPath(path) {
|
|
140
|
+
return path === "sf-tools/package.json" || path === "sf-tools/package-lock.json" || path.startsWith(".github/workflows/");
|
|
141
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// sft push: カレント配下をコミットしてプッシュする(シェル版の sf-push.sh の移し先)。
|
|
2
|
+
// origin/main を取り込み(main 以外のとき)・カレント配下だけをステージ・ターゲットファイルを検証・コミットメッセージを VS Code で入力(-m で省く)→ commit → push。
|
|
3
|
+
// カレント配下に変更がなく、未プッシュのコミットがあれば、push だけ行う。AI 操作(SF_TOOLS_AI=1)で -m がなければ、確認待ち(終了コード 20)で、取り込みの前に止める。
|
|
4
|
+
// 終了コード: 0 = 正常(何もせず終了した場合を含む)/ 1 = エラー/ 20 = 確認待ち
|
|
5
|
+
import { branchList, isProtectedBranch } from "./branches.js";
|
|
6
|
+
import { runCheck } from "./check.js";
|
|
7
|
+
import { EXIT } from "./exit-codes.js";
|
|
8
|
+
/**
|
|
9
|
+
* コミットメッセージの整形: 行末の CR を除き、# で始まる行(前の空白は可)を除き、先頭と末尾の空行(空白だけの行を含む)を除く。
|
|
10
|
+
* 途中の空行(件名と本文の間など)は残す(空白だけの行は、空行にする)。残りが空なら ""
|
|
11
|
+
*/
|
|
12
|
+
export function cleanCommitMessage(text) {
|
|
13
|
+
const out = [];
|
|
14
|
+
let blank = 0;
|
|
15
|
+
for (const raw of text.split("\n")) {
|
|
16
|
+
const line = raw.replace(/\r$/, "");
|
|
17
|
+
if (/^\s*#/.test(line))
|
|
18
|
+
continue;
|
|
19
|
+
if (/^\s*$/.test(line)) {
|
|
20
|
+
if (out.length > 0)
|
|
21
|
+
blank++;
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
for (let i = 0; i < blank; i++)
|
|
25
|
+
out.push("");
|
|
26
|
+
blank = 0;
|
|
27
|
+
out.push(line);
|
|
28
|
+
}
|
|
29
|
+
return out.join("\n");
|
|
30
|
+
}
|
|
31
|
+
const TEMPLATE = [
|
|
32
|
+
"# 1行目に要約を書いてください。",
|
|
33
|
+
"# 例: deploy-target.txt を更新",
|
|
34
|
+
"#",
|
|
35
|
+
"# 必要なら詳細を下に書いてください。",
|
|
36
|
+
"",
|
|
37
|
+
].join("\n");
|
|
38
|
+
export async function runPush(args, box, deps) {
|
|
39
|
+
const log = (level, text) => box.output.err(`[${level}] ${text}`);
|
|
40
|
+
const die = (text) => {
|
|
41
|
+
log("ERROR", text);
|
|
42
|
+
return EXIT.failure;
|
|
43
|
+
};
|
|
44
|
+
let messageFile = "";
|
|
45
|
+
for (let i = 0; i < args.length; i++) {
|
|
46
|
+
const a = args[i];
|
|
47
|
+
if (a === "-m" || a === "--message") {
|
|
48
|
+
const v = args[i + 1] ?? "";
|
|
49
|
+
if (v === "" || v.startsWith("-"))
|
|
50
|
+
return die("-m(--message)に、メッセージのファイルがありません。");
|
|
51
|
+
messageFile = v;
|
|
52
|
+
i++;
|
|
53
|
+
}
|
|
54
|
+
else if (a === "-v" || a === "--verbose")
|
|
55
|
+
continue;
|
|
56
|
+
else
|
|
57
|
+
return die(`不明なオプションです: ${a}`);
|
|
58
|
+
}
|
|
59
|
+
if (messageFile !== "") {
|
|
60
|
+
if (!box.files.exists(messageFile) || box.files.isDir(messageFile))
|
|
61
|
+
return die(`-m のファイルを読めません: ${messageFile}`);
|
|
62
|
+
}
|
|
63
|
+
else if (box.env("SF_TOOLS_AI") === "1") {
|
|
64
|
+
// AI 操作: VS Code での入力はできない。取り込み・ステージの前に、確認待ちで止める(doc/ai-operation-design.md 4.5)
|
|
65
|
+
log("ERROR", "[確認待ち] push.message: コミットのメッセージがありません(AI 操作では、-m <ファイル> で渡します)");
|
|
66
|
+
return EXIT.needAnswer;
|
|
67
|
+
}
|
|
68
|
+
const gitIn = async (root, ...a) => box.run("git", ["-C", root, ...a]);
|
|
69
|
+
const top = await box.run("git", ["rev-parse", "--show-toplevel"]).catch(() => null);
|
|
70
|
+
if (top === null || top.code !== 0)
|
|
71
|
+
return die("現在のディレクトリから Git リポジトリを見つけられません。");
|
|
72
|
+
const root = top.stdout.trim();
|
|
73
|
+
const sym = await gitIn(root, "symbolic-ref", "--quiet", "--short", "HEAD").catch(() => null);
|
|
74
|
+
if (sym === null || sym.code !== 0) {
|
|
75
|
+
const short = await gitIn(root, "rev-parse", "--short", "HEAD").catch(() => null);
|
|
76
|
+
if (short === null || short.code !== 0)
|
|
77
|
+
return die("Git のブランチ名を取得できません。");
|
|
78
|
+
return die(`HEAD がブランチを指していません(detached HEAD: ${short.stdout.trim()})。`);
|
|
79
|
+
}
|
|
80
|
+
const branch = sym.stdout.trim();
|
|
81
|
+
// サブディレクトリから動かしても読めるよう、リポジトリのルートから読む(保護ブランチの判定を、cwd で緩めない)
|
|
82
|
+
const listFile = `${root}/sf-tools/config/branches.txt`;
|
|
83
|
+
const list = branchList(box.files.exists(listFile) ? box.files.readText(listFile) : null);
|
|
84
|
+
if (isProtectedBranch(branch, list.branches))
|
|
85
|
+
return die(`${branch} は保護ブランチです(ここでは、コミットもプッシュも行いません)。`);
|
|
86
|
+
const prefix = ((await box.run("git", ["rev-parse", "--show-prefix"]).catch(() => null))?.stdout ?? "").trim();
|
|
87
|
+
const pathspec = prefix === "" ? "." : prefix.replace(/\/$/, "");
|
|
88
|
+
log("HEADER", "カレント配下のコミット・プッシュを開始します (sf-push)");
|
|
89
|
+
log("INFO", `対象: ${branch}`);
|
|
90
|
+
if (branch !== "main") {
|
|
91
|
+
log("INFO", "origin/main を取り込みます。");
|
|
92
|
+
if ((await gitIn(root, "fetch", "origin", "main")).code !== 0)
|
|
93
|
+
return die("git fetch に失敗しました。");
|
|
94
|
+
const merged = await gitIn(root, "merge", "origin/main", "--no-edit");
|
|
95
|
+
if (merged.code !== 0) {
|
|
96
|
+
if (merged.output !== "")
|
|
97
|
+
box.output.err(merged.output.trimEnd());
|
|
98
|
+
await gitIn(root, "merge", "--abort").catch(() => null);
|
|
99
|
+
return die("origin/main とのマージに失敗しました。");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if ((await gitIn(root, "add", "--all", "--", pathspec)).code !== 0)
|
|
103
|
+
return die("git add に失敗しました。");
|
|
104
|
+
if ((await gitIn(root, "diff", "--cached", "--quiet", "--", pathspec)).code === 0) {
|
|
105
|
+
const counted = await gitIn(root, "rev-list", "--count", "@{u}..HEAD").catch(() => null);
|
|
106
|
+
const unpushed = counted !== null && counted.code === 0 ? Number(counted.stdout.trim()) : 0;
|
|
107
|
+
if (Number.isInteger(unpushed) && unpushed > 0) {
|
|
108
|
+
log("INFO", `未プッシュのコミットが ${unpushed} 件あります。コミットはせず、プッシュだけ行います。`);
|
|
109
|
+
if ((await gitIn(root, "push")).code !== 0)
|
|
110
|
+
return die("git push に失敗しました。");
|
|
111
|
+
log("SUCCESS", "未プッシュのコミットのプッシュが完了しました。");
|
|
112
|
+
return EXIT.ok;
|
|
113
|
+
}
|
|
114
|
+
log("WARNING", "現在のディレクトリ配下に、コミット対象の変更がありません。");
|
|
115
|
+
return EXIT.ok;
|
|
116
|
+
}
|
|
117
|
+
log("INFO", "ターゲットファイルを検証します(sft check)。");
|
|
118
|
+
if (runCheck([], box, deps.capability()) !== 0)
|
|
119
|
+
return die("ターゲットファイルの検証に失敗しました。コミットを中止します。");
|
|
120
|
+
const tmp = deps.makeTempDir();
|
|
121
|
+
try {
|
|
122
|
+
const file = `${tmp}/commit-msg.txt`;
|
|
123
|
+
box.files.writeText(file, TEMPLATE);
|
|
124
|
+
if (messageFile !== "") {
|
|
125
|
+
log("INFO", `コミットメッセージを、-m のファイルから読みます: ${messageFile}`);
|
|
126
|
+
box.files.writeText(file, box.files.readText(messageFile));
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
log("INFO", "VS Code でコミットメッセージ入力画面を開きます。");
|
|
130
|
+
const code = await box.run("code", ["--new-window", "--wait", file], { allowCmdShell: true }).catch(() => null);
|
|
131
|
+
if (code === null || code.code !== 0)
|
|
132
|
+
return die("VS Code の起動に失敗しました。");
|
|
133
|
+
}
|
|
134
|
+
const message = cleanCommitMessage(box.files.readText(file));
|
|
135
|
+
if (message === "") {
|
|
136
|
+
log("INFO", "コミットメッセージが未入力のため、何もせず終了します。");
|
|
137
|
+
return EXIT.ok;
|
|
138
|
+
}
|
|
139
|
+
box.files.writeText(file, `${message}\n`);
|
|
140
|
+
log("INFO", "git commit を実行します。");
|
|
141
|
+
if ((await gitIn(root, "commit", "-F", file)).code !== 0)
|
|
142
|
+
return die("git commit に失敗しました。");
|
|
143
|
+
log("INFO", "git push を実行します。");
|
|
144
|
+
if ((await gitIn(root, "push")).code !== 0)
|
|
145
|
+
return die("git push に失敗しました。");
|
|
146
|
+
log("SUCCESS", "カレント配下のコミット・プッシュが完了しました。");
|
|
147
|
+
return EXIT.ok;
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
deps.removeDir(tmp);
|
|
151
|
+
}
|
|
152
|
+
}
|