@leitwerk-dev/coding 0.2.0 → 0.3.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.
- package/README.md +28 -10
- package/dist/actions.d.ts +12 -3
- package/dist/actions.d.ts.map +1 -1
- package/dist/auto-work-branch-internal.d.ts +4 -0
- package/dist/auto-work-branch-internal.d.ts.map +1 -0
- package/dist/auto-work-branch.d.ts +1 -12
- package/dist/auto-work-branch.d.ts.map +1 -1
- package/dist/auto-work-branch.js +18 -97
- package/dist/auto-work-branch.js.map +1 -1
- package/dist/{chunk-EP3ZC37F.js → chunk-34B3Q75M.js} +1 -1
- package/dist/chunk-34B3Q75M.js.map +1 -0
- package/dist/{chunk-OFDNIT5X.js → chunk-F6CEEAFE.js} +1 -1
- package/dist/chunk-F6CEEAFE.js.map +1 -0
- package/dist/chunk-GO44KCUS.js +154 -0
- package/dist/chunk-GO44KCUS.js.map +1 -0
- package/dist/{chunk-WQEPA35Z.js → chunk-R3UCUGY6.js} +1 -1
- package/dist/chunk-R3UCUGY6.js.map +1 -0
- package/dist/{chunk-67YKGZLM.js → chunk-W7VJT6AN.js} +1 -1
- package/dist/chunk-W7VJT6AN.js.map +1 -0
- package/dist/{chunk-NIJCT4VK.js → chunk-WKHVLLKE.js} +1 -1
- package/dist/chunk-WKHVLLKE.js.map +1 -0
- package/dist/{chunk-UBVLJV3V.js → chunk-YSC5GEQJ.js} +1 -1
- package/dist/chunk-YSC5GEQJ.js.map +1 -0
- package/dist/finalization-git-internal.d.ts +33 -0
- package/dist/finalization-git-internal.d.ts.map +1 -0
- package/dist/finalization-git.d.ts +2 -47
- package/dist/finalization-git.d.ts.map +1 -1
- package/dist/finalization-git.js +3 -9
- package/dist/index.d.ts +3 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +73 -112
- package/dist/index.js.map +1 -1
- package/dist/repository-change-launch-internal.d.ts +35 -0
- package/dist/repository-change-launch-internal.d.ts.map +1 -0
- package/dist/repository-change-launch.d.ts +2 -64
- package/dist/repository-change-launch.d.ts.map +1 -1
- package/dist/repository-change-launch.js +22 -11
- package/dist/repository-change-launch.js.map +1 -1
- package/dist/repository-change-process.d.ts +31 -13
- package/dist/repository-change-process.d.ts.map +1 -1
- package/dist/repository-change-publication.d.ts +229 -0
- package/dist/repository-change-publication.d.ts.map +1 -0
- package/dist/repository-change-publication.js +368 -0
- package/dist/repository-change-publication.js.map +1 -0
- package/dist/repository-change-state-internal.d.ts +21 -0
- package/dist/repository-change-state-internal.d.ts.map +1 -0
- package/dist/repository-change-state.d.ts +1 -17
- package/dist/repository-change-state.d.ts.map +1 -1
- package/dist/repository-change-state.js +0 -13
- package/dist/turns/generate-commit-message.d.ts +2 -0
- package/dist/turns/generate-commit-message.d.ts.map +1 -1
- package/dist/turns/generate-commit-message.js +1 -1
- package/dist/turns/generate-plan.d.ts +1 -0
- package/dist/turns/generate-plan.d.ts.map +1 -1
- package/dist/turns/generate-plan.js +1 -1
- package/dist/turns/implement.d.ts +1 -0
- package/dist/turns/implement.d.ts.map +1 -1
- package/dist/turns/implement.js +1 -1
- package/dist/turns/review-implementation.d.ts +1 -0
- package/dist/turns/review-implementation.d.ts.map +1 -1
- package/dist/turns/review-implementation.js +1 -1
- package/dist/turns/review-plan.d.ts +1 -0
- package/dist/turns/review-plan.d.ts.map +1 -1
- package/dist/turns/review-plan.js +1 -1
- package/dist/turns/simplify-implementation.d.ts +1 -0
- package/dist/turns/simplify-implementation.d.ts.map +1 -1
- package/dist/turns/simplify-implementation.js +1 -1
- package/package.json +24 -8
- package/src/actions.ts +12 -9
- package/src/auto-work-branch-internal.ts +41 -0
- package/src/auto-work-branch.ts +1 -140
- package/src/finalization-git-internal.ts +218 -0
- package/src/finalization-git.ts +2 -828
- package/src/index.ts +15 -4
- package/src/repository-change-launch-internal.ts +64 -0
- package/src/repository-change-launch.ts +10 -264
- package/src/repository-change-process.ts +33 -123
- package/src/repository-change-publication.ts +689 -0
- package/src/repository-change-state-internal.ts +60 -0
- package/src/repository-change-state.ts +1 -78
- package/src/turns/generate-commit-message.ts +2 -0
- package/src/turns/generate-plan.ts +1 -0
- package/src/turns/implement.ts +1 -0
- package/src/turns/review-implementation.ts +1 -0
- package/src/turns/review-plan.ts +1 -0
- package/src/turns/simplify-implementation.ts +1 -0
- package/dist/chunk-5LVPKVI2.js +0 -27
- package/dist/chunk-5LVPKVI2.js.map +0 -1
- package/dist/chunk-67YKGZLM.js.map +0 -1
- package/dist/chunk-73LKROYK.js +0 -616
- package/dist/chunk-73LKROYK.js.map +0 -1
- package/dist/chunk-EP3ZC37F.js.map +0 -1
- package/dist/chunk-NIJCT4VK.js.map +0 -1
- package/dist/chunk-OFDNIT5X.js.map +0 -1
- package/dist/chunk-UBVLJV3V.js.map +0 -1
- package/dist/chunk-WQEPA35Z.js.map +0 -1
- package/dist/chunk-Y2S6JFVO.js +0 -185
- package/dist/chunk-Y2S6JFVO.js.map +0 -1
- package/dist/chunk-YIP4MP64.js +0 -61
- package/dist/chunk-YIP4MP64.js.map +0 -1
- package/dist/turns/resolve-merge-conflict.d.ts +0 -3
- package/dist/turns/resolve-merge-conflict.d.ts.map +0 -1
- package/dist/turns/resolve-merge-conflict.js +0 -8
- package/dist/turns/resolve-merge-conflict.js.map +0 -1
- package/src/turns/resolve-merge-conflict.ts +0 -25
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leitwerk-dev/coding",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/leitwerk-dev/leitwerk.git",
|
|
@@ -40,21 +40,37 @@
|
|
|
40
40
|
"import": "./dist/repository-change-launch.js",
|
|
41
41
|
"types": "./dist/repository-change-launch.d.ts"
|
|
42
42
|
},
|
|
43
|
-
"./turns
|
|
44
|
-
"source": "./src/turns
|
|
45
|
-
"import": "./dist/turns
|
|
46
|
-
"types": "./dist/turns
|
|
43
|
+
"./turns/implement": {
|
|
44
|
+
"source": "./src/turns/implement.ts",
|
|
45
|
+
"import": "./dist/turns/implement.js",
|
|
46
|
+
"types": "./dist/turns/implement.d.ts"
|
|
47
|
+
},
|
|
48
|
+
"./turns/review-implementation": {
|
|
49
|
+
"source": "./src/turns/review-implementation.ts",
|
|
50
|
+
"import": "./dist/turns/review-implementation.js",
|
|
51
|
+
"types": "./dist/turns/review-implementation.d.ts"
|
|
52
|
+
},
|
|
53
|
+
"./turns/simplify-implementation": {
|
|
54
|
+
"source": "./src/turns/simplify-implementation.ts",
|
|
55
|
+
"import": "./dist/turns/simplify-implementation.js",
|
|
56
|
+
"types": "./dist/turns/simplify-implementation.d.ts"
|
|
57
|
+
},
|
|
58
|
+
"./repository-change-publication": {
|
|
59
|
+
"source": "./src/repository-change-publication.ts",
|
|
60
|
+
"import": "./dist/repository-change-publication.js",
|
|
61
|
+
"types": "./dist/repository-change-publication.d.ts"
|
|
47
62
|
}
|
|
48
63
|
},
|
|
49
64
|
"scripts": {
|
|
50
65
|
"build": "tsup --silent"
|
|
51
66
|
},
|
|
52
67
|
"dependencies": {
|
|
53
|
-
"@leitwerk-dev/domain": "0.
|
|
54
|
-
"@leitwerk-dev/process-sdk": "0.
|
|
68
|
+
"@leitwerk-dev/domain": "0.3.0",
|
|
69
|
+
"@leitwerk-dev/process-sdk": "0.3.0",
|
|
70
|
+
"@leitwerk-dev/repository-rebase": "0.3.0"
|
|
55
71
|
},
|
|
56
72
|
"devDependencies": {
|
|
57
|
-
"@leitwerk-dev/test-support": "0.
|
|
73
|
+
"@leitwerk-dev/test-support": "0.3.0",
|
|
58
74
|
"tsup": "^8.5.1"
|
|
59
75
|
},
|
|
60
76
|
"license": "Apache-2.0",
|
package/src/actions.ts
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
import type { FormDefinition } from "@leitwerk-dev/process-sdk";
|
|
2
2
|
|
|
3
|
+
/** @public */
|
|
3
4
|
export const codingActionIds = {
|
|
5
|
+
/** @public */
|
|
4
6
|
approvePlan: "approve_plan",
|
|
7
|
+
/** @internal */
|
|
5
8
|
requestRevision: "request_revision",
|
|
9
|
+
/** @internal */
|
|
6
10
|
runReview: "run_review",
|
|
11
|
+
/** @internal */
|
|
7
12
|
simplify: "simplify",
|
|
13
|
+
/** @internal */
|
|
8
14
|
acceptReview: "accept_review",
|
|
15
|
+
/** @internal */
|
|
9
16
|
requestReviewChanges: "request_review_changes",
|
|
17
|
+
/** @internal */
|
|
10
18
|
dismissReview: "dismiss_review",
|
|
19
|
+
/** @public */
|
|
11
20
|
finalizeChange: "finalize_change",
|
|
12
21
|
} as const;
|
|
13
22
|
|
|
@@ -22,18 +31,21 @@ function notesForm(id: string, title: string, label: string, description: string
|
|
|
22
31
|
};
|
|
23
32
|
}
|
|
24
33
|
|
|
34
|
+
/** @internal */
|
|
25
35
|
export const requestRevisionForm = notesForm(
|
|
26
36
|
codingActionIds.requestRevision,
|
|
27
37
|
"Request revision",
|
|
28
38
|
"Revision notes",
|
|
29
39
|
"Instructions for the next implementation pass.",
|
|
30
40
|
);
|
|
41
|
+
/** @internal */
|
|
31
42
|
export const requestReviewChangesForm = notesForm(
|
|
32
43
|
codingActionIds.requestReviewChanges,
|
|
33
44
|
"Request review changes",
|
|
34
45
|
"Review notes",
|
|
35
46
|
"Instructions for the next review pass.",
|
|
36
47
|
);
|
|
48
|
+
/** @internal */
|
|
37
49
|
export const acceptReviewForm: FormDefinition = {
|
|
38
50
|
id: codingActionIds.acceptReview,
|
|
39
51
|
title: "Accept review",
|
|
@@ -49,12 +61,3 @@ export const acceptReviewForm: FormDefinition = {
|
|
|
49
61
|
],
|
|
50
62
|
submitLabel: "Accept review",
|
|
51
63
|
};
|
|
52
|
-
|
|
53
|
-
export function createFinalizeChangeForm(copy: { title: string }): FormDefinition {
|
|
54
|
-
return {
|
|
55
|
-
id: codingActionIds.finalizeChange,
|
|
56
|
-
title: copy.title,
|
|
57
|
-
fields: [],
|
|
58
|
-
submitLabel: copy.title,
|
|
59
|
-
};
|
|
60
|
-
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
const BASE_DIGEST_SUFFIX_LENGTH = 12;
|
|
4
|
+
const MAX_BRANCH_SLUG_LENGTH = 48;
|
|
5
|
+
const PROMPT_WORD_COUNT = 5;
|
|
6
|
+
const RANDOM_HEX_LENGTH = 3;
|
|
7
|
+
|
|
8
|
+
function slugifyBranchSource(sourceText: string): string {
|
|
9
|
+
const words =
|
|
10
|
+
sourceText
|
|
11
|
+
.normalize("NFKD")
|
|
12
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
13
|
+
.toLowerCase()
|
|
14
|
+
.replace(/[’']/g, "")
|
|
15
|
+
.match(/[a-z0-9]+/g) ?? [];
|
|
16
|
+
const normalized = words.slice(0, PROMPT_WORD_COUNT).join("-");
|
|
17
|
+
const slug =
|
|
18
|
+
normalized.length <= MAX_BRANCH_SLUG_LENGTH
|
|
19
|
+
? normalized
|
|
20
|
+
: normalized.slice(0, MAX_BRANCH_SLUG_LENGTH).replace(/-+$/g, "");
|
|
21
|
+
return slug || "change";
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function randomHex(): string {
|
|
25
|
+
return randomBytes(2).toString("hex").slice(0, RANDOM_HEX_LENGTH);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Build a safe branch name without requiring authenticated remote access. */
|
|
29
|
+
/** @public */
|
|
30
|
+
export function buildAutoWorkBranchFromSeed(
|
|
31
|
+
sourceText: string,
|
|
32
|
+
seed: string,
|
|
33
|
+
suffix = randomHex(),
|
|
34
|
+
): string {
|
|
35
|
+
const normalizedSuffix = suffix.trim().toLowerCase();
|
|
36
|
+
if (!new RegExp(`^[0-9a-f]{${RANDOM_HEX_LENGTH}}$`).test(normalizedSuffix)) {
|
|
37
|
+
throw new Error(`Expected ${RANDOM_HEX_LENGTH} random hex characters, got '${suffix}'`);
|
|
38
|
+
}
|
|
39
|
+
const digest = createHash("sha256").update(seed).digest("hex");
|
|
40
|
+
return `${slugifyBranchSource(sourceText)}-${normalizedSuffix}-${digest.slice(0, BASE_DIGEST_SUFFIX_LENGTH)}`;
|
|
41
|
+
}
|
package/src/auto-work-branch.ts
CHANGED
|
@@ -1,140 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { createHash, randomBytes } from "node:crypto";
|
|
3
|
-
import { promisify } from "node:util";
|
|
4
|
-
import { trimToNull } from "@leitwerk-dev/domain";
|
|
5
|
-
import { sanitizeWorkerSubprocessEnv } from "@leitwerk-dev/process-sdk";
|
|
6
|
-
import { resolveGitBinary } from "@leitwerk-dev/process-sdk/git-binary";
|
|
7
|
-
|
|
8
|
-
const DEFAULT_BASE_SHA_SUFFIX_LENGTH = 12;
|
|
9
|
-
const DEFAULT_MAX_BRANCH_SLUG_LENGTH = 48;
|
|
10
|
-
const DEFAULT_PROMPT_WORD_COUNT = 5;
|
|
11
|
-
const AUTO_BRANCH_RANDOM_HEX_LENGTH = 3;
|
|
12
|
-
const DEFAULT_GIT_TIMEOUT_MS = 15_000;
|
|
13
|
-
const execFileAsync = promisify(execFile);
|
|
14
|
-
|
|
15
|
-
export interface ResolveBaseBranchShaInput {
|
|
16
|
-
repoLocator: string;
|
|
17
|
-
baseBranch: string;
|
|
18
|
-
timeoutMs?: number;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function truncateSlugAtSeparator(value: string, maxLength: number): string {
|
|
22
|
-
if (value.length <= maxLength) {
|
|
23
|
-
return value;
|
|
24
|
-
}
|
|
25
|
-
return value.slice(0, maxLength).replace(/-+$/g, "");
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function normalizedBranchWords(value: string): string[] {
|
|
29
|
-
return (
|
|
30
|
-
value
|
|
31
|
-
.normalize("NFKD")
|
|
32
|
-
.replace(/[\u0300-\u036f]/g, "")
|
|
33
|
-
.toLowerCase()
|
|
34
|
-
.replace(/[’']/g, "")
|
|
35
|
-
.match(/[a-z0-9]+/g) ?? []
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function slugifyBranchSourceForBranch(sourceText: string): string {
|
|
40
|
-
const words = normalizedBranchWords(sourceText).slice(0, DEFAULT_PROMPT_WORD_COUNT);
|
|
41
|
-
const normalized = words.join("-");
|
|
42
|
-
const slug = truncateSlugAtSeparator(normalized, DEFAULT_MAX_BRANCH_SLUG_LENGTH);
|
|
43
|
-
return slug || "change";
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function normalizeSha(value: string): string {
|
|
47
|
-
const sha = value.trim().toLowerCase();
|
|
48
|
-
if (!/^[0-9a-f]{7,64}$/.test(sha)) {
|
|
49
|
-
throw new Error(`Expected a git object sha, got '${value}'`);
|
|
50
|
-
}
|
|
51
|
-
return sha;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function generateAutoWorkBranchRandomHex(): string {
|
|
55
|
-
return randomBytes(2).toString("hex").slice(0, AUTO_BRANCH_RANDOM_HEX_LENGTH);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function normalizeRandomHex(value: string): string {
|
|
59
|
-
const normalized = value.trim().toLowerCase();
|
|
60
|
-
if (!new RegExp(`^[0-9a-f]{${AUTO_BRANCH_RANDOM_HEX_LENGTH}}$`).test(normalized)) {
|
|
61
|
-
throw new Error(
|
|
62
|
-
`Expected ${AUTO_BRANCH_RANDOM_HEX_LENGTH} random hex characters, got '${value}'`,
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
return normalized;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function buildAutoWorkBranch(
|
|
69
|
-
sourceText: string,
|
|
70
|
-
baseBranchSha: string,
|
|
71
|
-
randomHex = generateAutoWorkBranchRandomHex(),
|
|
72
|
-
): string {
|
|
73
|
-
const slug = slugifyBranchSourceForBranch(sourceText);
|
|
74
|
-
const suffix = normalizeRandomHex(randomHex);
|
|
75
|
-
const sha = normalizeSha(baseBranchSha);
|
|
76
|
-
return `${slug}-${suffix}-${sha.slice(0, DEFAULT_BASE_SHA_SUFFIX_LENGTH)}`;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** Builds the same safe branch shape when authenticated remote SHA lookup must wait for a worker. */
|
|
80
|
-
export function gitShaLikeDigestFromSeed(seed: string): string {
|
|
81
|
-
return createHash("sha256").update(seed).digest("hex");
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function buildAutoWorkBranchFromSeed(
|
|
85
|
-
sourceText: string,
|
|
86
|
-
seed: string,
|
|
87
|
-
randomHex = generateAutoWorkBranchRandomHex(),
|
|
88
|
-
): string {
|
|
89
|
-
return buildAutoWorkBranch(sourceText, gitShaLikeDigestFromSeed(seed), randomHex);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function baseBranchRef(baseBranch: string): string {
|
|
93
|
-
const trimmed = trimToNull(baseBranch);
|
|
94
|
-
if (!trimmed) {
|
|
95
|
-
throw new Error("baseBranch is required to resolve an automatic work branch");
|
|
96
|
-
}
|
|
97
|
-
return trimmed.startsWith("refs/heads/") ? trimmed : `refs/heads/${trimmed}`;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function outputToString(value: unknown): string {
|
|
101
|
-
return typeof value === "string" ? value : Buffer.isBuffer(value) ? value.toString("utf8") : "";
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export async function resolveBaseBranchSha(input: ResolveBaseBranchShaInput): Promise<string> {
|
|
105
|
-
const repoLocator = trimToNull(input.repoLocator);
|
|
106
|
-
if (!repoLocator) {
|
|
107
|
-
throw new Error("repoLocator is required to resolve an automatic work branch");
|
|
108
|
-
}
|
|
109
|
-
const ref = baseBranchRef(input.baseBranch);
|
|
110
|
-
let stdout = "";
|
|
111
|
-
try {
|
|
112
|
-
const result = await execFileAsync(resolveGitBinary(), ["ls-remote", repoLocator, ref], {
|
|
113
|
-
encoding: "utf8",
|
|
114
|
-
env: sanitizeWorkerSubprocessEnv(process.env, { GIT_TERMINAL_PROMPT: "0" }),
|
|
115
|
-
timeout: input.timeoutMs ?? DEFAULT_GIT_TIMEOUT_MS,
|
|
116
|
-
});
|
|
117
|
-
stdout = outputToString(result.stdout);
|
|
118
|
-
} catch (error) {
|
|
119
|
-
const errorOutput = error as { message?: string; stdout?: unknown; stderr?: unknown };
|
|
120
|
-
const message =
|
|
121
|
-
trimToNull(outputToString(errorOutput.stderr)) ??
|
|
122
|
-
trimToNull(outputToString(errorOutput.stdout)) ??
|
|
123
|
-
errorOutput.message ??
|
|
124
|
-
"unknown git error";
|
|
125
|
-
throw new Error(`Failed to resolve '${ref}' from '${repoLocator}': ${message}`);
|
|
126
|
-
}
|
|
127
|
-
const match = stdout
|
|
128
|
-
.split(/\r?\n/)
|
|
129
|
-
.map((line) => line.trim())
|
|
130
|
-
.filter(Boolean)
|
|
131
|
-
.map((line) => line.split(/\s+/))
|
|
132
|
-
.find((parts) => parts[1] === ref);
|
|
133
|
-
const sha = match?.[0];
|
|
134
|
-
if (!sha) {
|
|
135
|
-
throw new Error(
|
|
136
|
-
`Repository '${repoLocator}' does not expose base branch '${input.baseBranch}'`,
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
return normalizeSha(sha);
|
|
140
|
-
}
|
|
1
|
+
export { buildAutoWorkBranchFromSeed } from "./auto-work-branch-internal.js";
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { trimToNull, type WorkerErrorClass } from "@leitwerk-dev/domain";
|
|
5
|
+
import { repositoryGitArgs, repositoryGitSubprocessEnv } from "@leitwerk-dev/process-sdk";
|
|
6
|
+
import { resolveGitBinary } from "@leitwerk-dev/process-sdk/git-binary";
|
|
7
|
+
|
|
8
|
+
/** @public */
|
|
9
|
+
export interface GitIdentity {
|
|
10
|
+
/** @public */
|
|
11
|
+
name: string;
|
|
12
|
+
/** @public */
|
|
13
|
+
email: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** @internal */
|
|
17
|
+
export class DeterministicGitError extends Error {
|
|
18
|
+
/** @internal */
|
|
19
|
+
readonly errorClass: WorkerErrorClass = "git_error";
|
|
20
|
+
|
|
21
|
+
/** @internal */
|
|
22
|
+
constructor(message: string) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = "DeterministicGitError";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface GitExecResult {
|
|
29
|
+
ok: boolean;
|
|
30
|
+
stdout: string;
|
|
31
|
+
stderr: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function gitConfigIdentityEnv(): NodeJS.ProcessEnv {
|
|
35
|
+
const env = repositoryGitSubprocessEnv("repo", { GIT_TERMINAL_PROMPT: "0" });
|
|
36
|
+
for (const key of [
|
|
37
|
+
"GIT_AUTHOR_NAME",
|
|
38
|
+
"GIT_AUTHOR_EMAIL",
|
|
39
|
+
"GIT_AUTHOR_DATE",
|
|
40
|
+
"GIT_COMMITTER_NAME",
|
|
41
|
+
"GIT_COMMITTER_EMAIL",
|
|
42
|
+
"GIT_COMMITTER_DATE",
|
|
43
|
+
])
|
|
44
|
+
delete env[key];
|
|
45
|
+
return env;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function gitIdentityArgs(identity: GitIdentity): string[] {
|
|
49
|
+
const name = trimToNull(identity.name);
|
|
50
|
+
const email = trimToNull(identity.email);
|
|
51
|
+
if (!name || !email || /[\r\n\0]/.test(name) || /[\r\n\0]/.test(email)) {
|
|
52
|
+
throw new DeterministicGitError("Git identity requires a valid name and email");
|
|
53
|
+
}
|
|
54
|
+
return ["-c", `user.name=${name}`, "-c", `user.email=${email}`];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function runGit(repoPath: string, args: readonly string[], identity?: GitIdentity): GitExecResult {
|
|
58
|
+
const trustedArgs = identity ? [...gitIdentityArgs(identity), ...args] : args;
|
|
59
|
+
try {
|
|
60
|
+
const stdout = execFileSync(resolveGitBinary(), repositoryGitArgs(trustedArgs), {
|
|
61
|
+
cwd: repoPath,
|
|
62
|
+
encoding: "utf8",
|
|
63
|
+
env: gitConfigIdentityEnv(),
|
|
64
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
65
|
+
});
|
|
66
|
+
return { ok: true, stdout, stderr: "" };
|
|
67
|
+
} catch (error) {
|
|
68
|
+
const execError = error as {
|
|
69
|
+
stdout?: string | Buffer;
|
|
70
|
+
stderr?: string | Buffer;
|
|
71
|
+
message?: string;
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
ok: false,
|
|
75
|
+
stdout: execError.stdout?.toString() ?? "",
|
|
76
|
+
stderr: execError.stderr?.toString() ?? trimToNull(execError.message) ?? "",
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function checkedGit(
|
|
82
|
+
repoPath: string,
|
|
83
|
+
args: readonly string[],
|
|
84
|
+
failureMessage: string,
|
|
85
|
+
identity?: GitIdentity,
|
|
86
|
+
): string {
|
|
87
|
+
const result = runGit(repoPath, args, identity);
|
|
88
|
+
if (!result.ok) {
|
|
89
|
+
const detail = trimToNull(result.stderr) ?? trimToNull(result.stdout) ?? "unknown git error";
|
|
90
|
+
throw new DeterministicGitError(`${failureMessage}: ${detail}`);
|
|
91
|
+
}
|
|
92
|
+
return result.stdout.trim();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function git(repoPath: string, ...args: string[]): string {
|
|
96
|
+
return checkedGit(repoPath, args, `git ${args.join(" ")} failed in '${repoPath}'`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function gitOrNull(repoPath: string, ...args: string[]): string | null {
|
|
100
|
+
const result = runGit(repoPath, args);
|
|
101
|
+
return result.ok ? trimToNull(result.stdout) : null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function workingTreeStatus(repoPath: string): string[] {
|
|
105
|
+
return (gitOrNull(repoPath, "status", "--porcelain") ?? "")
|
|
106
|
+
.split(/\r?\n/)
|
|
107
|
+
.map((line) => line.replace(/\s+$/, ""))
|
|
108
|
+
.filter(Boolean)
|
|
109
|
+
.map((line) => line.slice(2).trim())
|
|
110
|
+
.filter(Boolean);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function conflictedFiles(repoPath: string): string[] {
|
|
114
|
+
return (gitOrNull(repoPath, "diff", "--name-only", "--diff-filter=U") ?? "")
|
|
115
|
+
.split(/\r?\n/)
|
|
116
|
+
.map((line) => line.trim())
|
|
117
|
+
.filter(Boolean);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function mergeInProgress(repoPath: string): boolean {
|
|
121
|
+
const gitDir = git(repoPath, "rev-parse", "--git-dir");
|
|
122
|
+
return existsSync(path.resolve(repoPath, gitDir, "MERGE_HEAD"));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function assertGitIdentity(repoPath: string, identity: GitIdentity): void {
|
|
126
|
+
if (
|
|
127
|
+
!runGit(repoPath, ["var", "GIT_AUTHOR_IDENT"], identity).ok ||
|
|
128
|
+
!runGit(repoPath, ["var", "GIT_COMMITTER_IDENT"], identity).ok
|
|
129
|
+
) {
|
|
130
|
+
throw new DeterministicGitError("Git author/committer identity is not configured");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function commitIfDirty(input: {
|
|
135
|
+
/** @public */
|
|
136
|
+
repoPath: string;
|
|
137
|
+
/** @public */
|
|
138
|
+
commitMessage: string;
|
|
139
|
+
/** @public */
|
|
140
|
+
gitIdentity: GitIdentity;
|
|
141
|
+
}): string {
|
|
142
|
+
if (workingTreeStatus(input.repoPath).length === 0)
|
|
143
|
+
return git(input.repoPath, "rev-parse", "HEAD");
|
|
144
|
+
assertGitIdentity(input.repoPath, input.gitIdentity);
|
|
145
|
+
checkedGit(input.repoPath, ["add", "--all"], "Failed to stage the completed change");
|
|
146
|
+
checkedGit(
|
|
147
|
+
input.repoPath,
|
|
148
|
+
["-c", "core.hooksPath=/dev/null", "commit", "--no-gpg-sign", "-m", input.commitMessage],
|
|
149
|
+
"Failed to commit the completed change",
|
|
150
|
+
input.gitIdentity,
|
|
151
|
+
);
|
|
152
|
+
const remaining = workingTreeStatus(input.repoPath);
|
|
153
|
+
if (remaining.length > 0) {
|
|
154
|
+
throw new DeterministicGitError(`Commit left uncommitted changes: ${remaining.join(", ")}`);
|
|
155
|
+
}
|
|
156
|
+
return git(input.repoPath, "rev-parse", "HEAD");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function pushAndVerify(repoPath: string, branch: string, expectedHeadSha: string): string {
|
|
160
|
+
const ref = `refs/heads/${branch}`;
|
|
161
|
+
const pushTarget = `origin/${branch}`;
|
|
162
|
+
checkedGit(repoPath, ["push", "origin", `HEAD:${ref}`], `Failed to push HEAD to '${pushTarget}'`);
|
|
163
|
+
const remoteHead = gitOrNull(repoPath, "ls-remote", "--heads", "origin", ref)
|
|
164
|
+
?.split(/\s+/)[0]
|
|
165
|
+
?.trim();
|
|
166
|
+
if (remoteHead !== expectedHeadSha) {
|
|
167
|
+
throw new DeterministicGitError(
|
|
168
|
+
`Published '${pushTarget}' resolved to '${remoteHead ?? "missing"}', expected '${expectedHeadSha}'`,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
return pushTarget;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Commit the current workspace change and non-force push only the feature branch. */
|
|
175
|
+
/** @public */
|
|
176
|
+
export function commitAndPushWorkBranch(input: {
|
|
177
|
+
/** @public */
|
|
178
|
+
repoPath: string;
|
|
179
|
+
/** @public */
|
|
180
|
+
workBranch: string;
|
|
181
|
+
/** @public */
|
|
182
|
+
commitMessage: string;
|
|
183
|
+
/** @public */
|
|
184
|
+
gitIdentity: GitIdentity;
|
|
185
|
+
}) {
|
|
186
|
+
const repoPath = path.resolve(input.repoPath);
|
|
187
|
+
if (!existsSync(repoPath) || !runGit(repoPath, ["rev-parse", "--git-dir"]).ok) {
|
|
188
|
+
throw new DeterministicGitError(
|
|
189
|
+
`Repository workspace '${repoPath}' does not exist or is not a git repository`,
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
const branch = git(repoPath, "branch", "--show-current");
|
|
193
|
+
if (branch !== input.workBranch) {
|
|
194
|
+
throw new DeterministicGitError(
|
|
195
|
+
`Expected '${repoPath}' to be checked out on '${input.workBranch}', but found '${branch || "detached HEAD"}'`,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
if (!trimToNull(input.commitMessage)) {
|
|
199
|
+
throw new DeterministicGitError("A generated commit message is required before publication");
|
|
200
|
+
}
|
|
201
|
+
const conflicts = conflictedFiles(repoPath);
|
|
202
|
+
if (mergeInProgress(repoPath) || conflicts.length > 0) {
|
|
203
|
+
throw new DeterministicGitError(
|
|
204
|
+
`Cannot publish a work branch with unresolved conflicts: ${conflicts.join(", ") || "merge in progress"}`,
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
const headSha = commitIfDirty({
|
|
208
|
+
repoPath,
|
|
209
|
+
commitMessage: input.commitMessage,
|
|
210
|
+
gitIdentity: input.gitIdentity,
|
|
211
|
+
});
|
|
212
|
+
return {
|
|
213
|
+
/** @public */
|
|
214
|
+
headSha,
|
|
215
|
+
/** @internal */
|
|
216
|
+
pushTarget: pushAndVerify(repoPath, input.workBranch, headSha),
|
|
217
|
+
};
|
|
218
|
+
}
|