@ebay-dt/bonfire-node 0.1.0 → 0.2.0-alpha.3
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 +10 -4
- package/dist/agent-service.d.ts +4 -1
- package/dist/agent-service.d.ts.map +1 -1
- package/dist/agent-service.js +18 -2
- package/dist/agent-service.js.map +1 -1
- package/dist/claude.d.ts +12 -11
- package/dist/claude.d.ts.map +1 -1
- package/dist/claude.js +166 -11
- package/dist/claude.js.map +1 -1
- package/dist/git-repository.d.ts +1 -1
- package/dist/git-repository.d.ts.map +1 -1
- package/dist/git-repository.js +5 -2
- package/dist/git-repository.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/lifecycle/local-prototype-lifecycle.d.ts +9 -1
- package/dist/lifecycle/local-prototype-lifecycle.d.ts.map +1 -1
- package/dist/lifecycle/local-prototype-lifecycle.js +104 -25
- package/dist/lifecycle/local-prototype-lifecycle.js.map +1 -1
- package/dist/lifecycle/repository-summary.d.ts +17 -0
- package/dist/lifecycle/repository-summary.d.ts.map +1 -0
- package/dist/lifecycle/repository-summary.js +182 -0
- package/dist/lifecycle/repository-summary.js.map +1 -0
- package/dist/lifecycle/repository-synchronization.d.ts +35 -0
- package/dist/lifecycle/repository-synchronization.d.ts.map +1 -0
- package/dist/lifecycle/repository-synchronization.js +270 -0
- package/dist/lifecycle/repository-synchronization.js.map +1 -0
- package/dist/lifecycle/runtime-context.d.ts +3 -1
- package/dist/lifecycle/runtime-context.d.ts.map +1 -1
- package/dist/lifecycle/runtime-context.js +3 -0
- package/dist/lifecycle/runtime-context.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { relative } from "node:path";
|
|
2
|
+
import { getGitManagementCapabilities, parseRepositoryRelativePath, } from "@ebay-dt/bonfire-core";
|
|
3
|
+
import { hasUnresolvedRepositoryOperation } from "./repository-synchronization.js";
|
|
4
|
+
function normalizePath(value) {
|
|
5
|
+
return value.replaceAll("\\", "/").replace(/^\.\//u, "");
|
|
6
|
+
}
|
|
7
|
+
function pathPrefix(repositoryRoot, prototypeRoot) {
|
|
8
|
+
const value = normalizePath(relative(repositoryRoot, prototypeRoot));
|
|
9
|
+
return value === "." ? "" : value.replace(/\/+$/u, "");
|
|
10
|
+
}
|
|
11
|
+
function summaryKey(ref) {
|
|
12
|
+
return `${ref.login}/${ref.slug}`;
|
|
13
|
+
}
|
|
14
|
+
function contextForPath(value, prefix, summaries, identity) {
|
|
15
|
+
const path = normalizePath(value);
|
|
16
|
+
const rest = prefix === "" ? path : path.startsWith(`${prefix}/`) ? path.slice(prefix.length + 1) : null;
|
|
17
|
+
if (rest === null)
|
|
18
|
+
return { scope: "out-of-scope", prototype: undefined };
|
|
19
|
+
const segments = rest.split("/");
|
|
20
|
+
const login = segments[0];
|
|
21
|
+
const slug = segments[1];
|
|
22
|
+
if (login === undefined || slug === undefined) {
|
|
23
|
+
return { scope: "unknown", prototype: undefined };
|
|
24
|
+
}
|
|
25
|
+
const ref = { login, slug };
|
|
26
|
+
const summary = summaries.get(summaryKey(ref));
|
|
27
|
+
if (summary === undefined)
|
|
28
|
+
return { scope: "unknown", prototype: undefined };
|
|
29
|
+
return {
|
|
30
|
+
scope: login === identity.login ? "owned-prototype" : "other-prototype",
|
|
31
|
+
prototype: ref,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function safePath(value) {
|
|
35
|
+
try {
|
|
36
|
+
return parseRepositoryRelativePath(normalizePath(value));
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function addChange(changes, path, status, context) {
|
|
43
|
+
const safe = safePath(path);
|
|
44
|
+
if (safe === null)
|
|
45
|
+
return;
|
|
46
|
+
const key = `${status}:${safe}`;
|
|
47
|
+
changes.set(key, {
|
|
48
|
+
path: safe,
|
|
49
|
+
status,
|
|
50
|
+
scope: context.scope,
|
|
51
|
+
...(context.prototype === undefined ? {} : { prototype: context.prototype }),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function statusPaths(file) {
|
|
55
|
+
return [file.path, ...(file.from === undefined ? [] : [file.from])];
|
|
56
|
+
}
|
|
57
|
+
async function diffPaths(repository, range) {
|
|
58
|
+
const output = await repository.raw(["diff", "--name-only", "-z", range]);
|
|
59
|
+
return output.split("\0").filter((path) => path.length > 0);
|
|
60
|
+
}
|
|
61
|
+
async function refRelation(repository, left, right) {
|
|
62
|
+
const output = await repository.raw([
|
|
63
|
+
"rev-list",
|
|
64
|
+
"--left-right",
|
|
65
|
+
"--count",
|
|
66
|
+
`${left}...${right}`,
|
|
67
|
+
]);
|
|
68
|
+
const values = output.trim().split(/\s+/u).map(Number);
|
|
69
|
+
if (values.length !== 2 ||
|
|
70
|
+
values.some((value) => !Number.isSafeInteger(value) || value < 0)) {
|
|
71
|
+
throw new Error("Malformed Git ref relation");
|
|
72
|
+
}
|
|
73
|
+
return { ahead: values[0], behind: values[1] };
|
|
74
|
+
}
|
|
75
|
+
function addDiffChanges(changes, paths, status, prefix, summaries, identity) {
|
|
76
|
+
for (const path of paths) {
|
|
77
|
+
addChange(changes, path, status, contextForPath(path, prefix, summaries, identity));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export async function getRepositorySummary(options) {
|
|
81
|
+
const summaries = new Map((await options.catalog.scan()).map((summary) => [
|
|
82
|
+
summaryKey(summary.ref),
|
|
83
|
+
summary,
|
|
84
|
+
]));
|
|
85
|
+
const prefix = pathPrefix(options.repositoryRoot, options.prototypeRoot);
|
|
86
|
+
const changes = new Map();
|
|
87
|
+
const attention = new Set();
|
|
88
|
+
const status = await options.repository.status();
|
|
89
|
+
const currentBranch = await options.repository.currentBranch();
|
|
90
|
+
const unresolved = await hasUnresolvedRepositoryOperation(options.repository);
|
|
91
|
+
const detached = currentBranch === "HEAD" || currentBranch.length === 0;
|
|
92
|
+
const wrongBranch = !detached && currentBranch !== options.identity.login;
|
|
93
|
+
for (const file of status.files) {
|
|
94
|
+
const fileStatus = file.index.includes("U") || file.workingDirectory.includes("U")
|
|
95
|
+
? "unresolved"
|
|
96
|
+
: "uncommitted";
|
|
97
|
+
for (const path of statusPaths(file)) {
|
|
98
|
+
addChange(changes, path, fileStatus, contextForPath(path, prefix, summaries, options.identity));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
const dirtyWorktree = status.files.length > 0;
|
|
102
|
+
if (unresolved)
|
|
103
|
+
attention.add("unresolved-operation");
|
|
104
|
+
if (detached)
|
|
105
|
+
attention.add("detached-head");
|
|
106
|
+
if (wrongBranch)
|
|
107
|
+
attention.add("wrong-branch");
|
|
108
|
+
const remotes = await options.repository.remotes();
|
|
109
|
+
const hasRemote = remotes.some((remote) => remote.name === options.remote);
|
|
110
|
+
let fetchFailed = false;
|
|
111
|
+
if (options.refresh && options.mode === "managed" && hasRemote) {
|
|
112
|
+
try {
|
|
113
|
+
await options.repository.fetch(options.remote);
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
fetchFailed = true;
|
|
117
|
+
attention.add("fetch-failed");
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const branches = await options.repository.branches();
|
|
121
|
+
const localIdentity = branches.all.includes(options.identity.login);
|
|
122
|
+
const remoteIdentity = `remotes/${options.remote}/${options.identity.login}`;
|
|
123
|
+
const remotePrimary = `remotes/${options.remote}/${options.primaryBranch}`;
|
|
124
|
+
const hasRemoteIdentity = hasRemote && branches.all.includes(remoteIdentity);
|
|
125
|
+
const hasRemotePrimary = hasRemote && branches.all.includes(remotePrimary);
|
|
126
|
+
let updateCheck = options.mode === "manual" ? "not-checked" : "unavailable";
|
|
127
|
+
if (options.mode === "managed") {
|
|
128
|
+
if (fetchFailed) {
|
|
129
|
+
updateCheck = "attention";
|
|
130
|
+
}
|
|
131
|
+
else if (!hasRemote) {
|
|
132
|
+
updateCheck = "unavailable";
|
|
133
|
+
}
|
|
134
|
+
else if (!hasRemotePrimary || !localIdentity) {
|
|
135
|
+
updateCheck = "attention";
|
|
136
|
+
attention.add("missing-primary");
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
try {
|
|
140
|
+
const primaryRelation = await refRelation(options.repository, options.identity.login, remotePrimary);
|
|
141
|
+
updateCheck =
|
|
142
|
+
primaryRelation.behind > 0 ? "updates-available" : "up-to-date";
|
|
143
|
+
if (primaryRelation.behind > 0) {
|
|
144
|
+
if (dirtyWorktree)
|
|
145
|
+
attention.add("dirty-worktree");
|
|
146
|
+
addDiffChanges(changes, await diffPaths(options.repository, `${options.identity.login}..${remotePrimary}`), "updates-available", prefix, summaries, options.identity);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
updateCheck = "attention";
|
|
151
|
+
attention.add("unknown");
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (hasRemoteIdentity && localIdentity) {
|
|
155
|
+
try {
|
|
156
|
+
const identityRelation = await refRelation(options.repository, options.identity.login, remoteIdentity);
|
|
157
|
+
if (identityRelation.ahead > 0) {
|
|
158
|
+
addDiffChanges(changes, await diffPaths(options.repository, `${remoteIdentity}..${options.identity.login}`), "committed-unpushed", prefix, summaries, options.identity);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
attention.add("unknown");
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
let readiness = "ready";
|
|
167
|
+
if (detached || wrongBranch || unresolved) {
|
|
168
|
+
readiness = "blocked";
|
|
169
|
+
}
|
|
170
|
+
else if (!status.clean) {
|
|
171
|
+
readiness = "attention";
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
mode: options.mode,
|
|
175
|
+
readiness,
|
|
176
|
+
updateCheck,
|
|
177
|
+
changes: [...changes.values()].sort((left, right) => left.path < right.path ? -1 : left.path > right.path ? 1 : 0),
|
|
178
|
+
attention: [...attention].sort(),
|
|
179
|
+
capabilities: getGitManagementCapabilities(options.mode),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=repository-summary.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repository-summary.js","sourceRoot":"","sources":["../../src/lifecycle/repository-summary.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EACL,4BAA4B,EAC5B,2BAA2B,GAU5B,MAAM,uBAAuB,CAAC;AAM/B,OAAO,EAAE,gCAAgC,EAAE,MAAM,iCAAiC,CAAC;AAoBnF,SAAS,aAAa,CAAC,KAAa;IAClC,OAAO,KAAK,CAAC,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;AAC3D,CAAC;AAED,SAAS,UAAU,CAAC,cAAsB,EAAE,aAAqB;IAC/D,MAAM,KAAK,GAAG,aAAa,CAAC,QAAQ,CAAC,cAAc,EAAE,aAAa,CAAC,CAAC,CAAC;IACrE,OAAO,KAAK,KAAK,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;AACzD,CAAC;AAED,SAAS,UAAU,CAAC,GAAiB;IACnC,OAAO,GAAG,GAAG,CAAC,KAAK,IAAI,GAAG,CAAC,IAAI,EAAE,CAAC;AACpC,CAAC;AAED,SAAS,cAAc,CACrB,KAAa,EACb,MAAc,EACd,SAAgD,EAChD,QAAyB;IAEzB,MAAM,IAAI,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC;IAClC,MAAM,IAAI,GAAG,MAAM,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,MAAM,GAAG,CAAC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,EAClB,GAAG,cAAc,KAAK,OAAO,CAAC,QAAQ,CAAC,KAAK,EAAE,CAC/C,EACD,oBAAoB,EACpB,MAAM,EACN,SAAS,EACT,OAAO,CAAC,QAAQ,CACjB,CAAC;gBACJ,CAAC;YACH,CAAC;YAAC,MAAM,CAAC;gBACP,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;YAC3B,CAAC;QACH,CAAC;IACH,CAAC;IAED,IAAI,SAAS,GAAmC,OAAO,CAAC;IACxD,IAAI,QAAQ,IAAI,WAAW,IAAI,UAAU,EAAE,CAAC;QAC1C,SAAS,GAAG,SAAS,CAAC;IACxB,CAAC;SAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QACzB,SAAS,GAAG,WAAW,CAAC;IAC1B,CAAC;IACD,OAAO;QACL,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,SAAS;QACT,WAAW;QACX,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAClD,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAC7D;QACD,SAAS,EAAE,CAAC,GAAG,SAAS,CAAC,CAAC,IAAI,EAAE;QAChC,YAAY,EAAE,4BAA4B,CAAC,OAAO,CAAC,IAAI,CAAC;KACzD,CAAC;AACJ,CAAC","sourcesContent":["import { relative } from \"node:path\";\nimport {\n getGitManagementCapabilities,\n parseRepositoryRelativePath,\n type BonfireIdentity,\n type GitMode,\n type PrototypeRef,\n type PrototypeSummary,\n type RepositoryAttentionCode,\n type RepositoryChange,\n type RepositoryChangeScope,\n type RepositoryChangeStatus,\n type RepositorySummary,\n} from \"@ebay-dt/bonfire-core\";\nimport type { PrototypeCatalogLike } from \"../prototype-catalog.js\";\nimport {\n type GitRepository,\n type GitStatusFile,\n} from \"../git-repository.js\";\nimport { hasUnresolvedRepositoryOperation } from \"./repository-synchronization.js\";\n\nexport type RepositorySummaryOptions = {\n repository: GitRepository;\n catalog: PrototypeCatalogLike;\n repositoryRoot: string;\n prototypeRoot: string;\n identity: BonfireIdentity;\n mode: GitMode;\n remote: string;\n primaryBranch: string;\n /** Fetch remote-tracking refs before comparing managed update state. */\n refresh?: boolean;\n};\n\ntype SummaryPathContext = {\n scope: RepositoryChangeScope;\n prototype: PrototypeRef | undefined;\n};\n\nfunction normalizePath(value: string) {\n return value.replaceAll(\"\\\\\", \"/\").replace(/^\\.\\//u, \"\");\n}\n\nfunction pathPrefix(repositoryRoot: string, prototypeRoot: string) {\n const value = normalizePath(relative(repositoryRoot, prototypeRoot));\n return value === \".\" ? \"\" : value.replace(/\\/+$/u, \"\");\n}\n\nfunction summaryKey(ref: PrototypeRef) {\n return `${ref.login}/${ref.slug}`;\n}\n\nfunction contextForPath(\n value: string,\n prefix: string,\n summaries: ReadonlyMap<string, PrototypeSummary>,\n identity: BonfireIdentity,\n): SummaryPathContext {\n const path = normalizePath(value);\n const rest = prefix === \"\" ? path : path.startsWith(`${prefix}/`) ? path.slice(prefix.length + 1) : null;\n if (rest === null) return { scope: \"out-of-scope\", prototype: undefined };\n\n const segments = rest.split(\"/\");\n const login = segments[0];\n const slug = segments[1];\n if (login === undefined || slug === undefined) {\n return { scope: \"unknown\", prototype: undefined };\n }\n\n const ref = { login, slug } satisfies PrototypeRef;\n const summary = summaries.get(summaryKey(ref));\n if (summary === undefined) return { scope: \"unknown\", prototype: undefined };\n\n return {\n scope: login === identity.login ? \"owned-prototype\" : \"other-prototype\",\n prototype: ref,\n };\n}\n\nfunction safePath(value: string) {\n try {\n return parseRepositoryRelativePath(normalizePath(value));\n } catch {\n return null;\n }\n}\n\nfunction addChange(\n changes: Map<string, RepositoryChange>,\n path: string,\n status: RepositoryChangeStatus,\n context: SummaryPathContext,\n) {\n const safe = safePath(path);\n if (safe === null) return;\n const key = `${status}:${safe}`;\n changes.set(key, {\n path: safe,\n status,\n scope: context.scope,\n ...(context.prototype === undefined ? {} : { prototype: context.prototype }),\n });\n}\n\nfunction statusPaths(file: GitStatusFile) {\n return [file.path, ...(file.from === undefined ? [] : [file.from])];\n}\n\nasync function diffPaths(repository: GitRepository, range: string) {\n const output = await repository.raw([\"diff\", \"--name-only\", \"-z\", range]);\n return output.split(\"\\0\").filter((path) => path.length > 0);\n}\n\nasync function refRelation(repository: GitRepository, left: string, right: string) {\n const output = await repository.raw([\n \"rev-list\",\n \"--left-right\",\n \"--count\",\n `${left}...${right}`,\n ]);\n const values = output.trim().split(/\\s+/u).map(Number);\n if (\n values.length !== 2 ||\n values.some((value) => !Number.isSafeInteger(value) || value < 0)\n ) {\n throw new Error(\"Malformed Git ref relation\");\n }\n return { ahead: values[0]!, behind: values[1]! };\n}\n\nfunction addDiffChanges(\n changes: Map<string, RepositoryChange>,\n paths: readonly string[],\n status: RepositoryChangeStatus,\n prefix: string,\n summaries: ReadonlyMap<string, PrototypeSummary>,\n identity: BonfireIdentity,\n) {\n for (const path of paths) {\n addChange(\n changes,\n path,\n status,\n contextForPath(path, prefix, summaries, identity),\n );\n }\n}\n\nexport async function getRepositorySummary(\n options: RepositorySummaryOptions,\n): Promise<RepositorySummary> {\n const summaries = new Map(\n (await options.catalog.scan()).map((summary) => [\n summaryKey(summary.ref),\n summary,\n ]),\n );\n const prefix = pathPrefix(options.repositoryRoot, options.prototypeRoot);\n const changes = new Map<string, RepositoryChange>();\n const attention = new Set<RepositoryAttentionCode>();\n const status = await options.repository.status();\n const currentBranch = await options.repository.currentBranch();\n const unresolved = await hasUnresolvedRepositoryOperation(options.repository);\n const detached = currentBranch === \"HEAD\" || currentBranch.length === 0;\n const wrongBranch = !detached && currentBranch !== options.identity.login;\n\n for (const file of status.files) {\n const fileStatus =\n file.index.includes(\"U\") || file.workingDirectory.includes(\"U\")\n ? \"unresolved\"\n : \"uncommitted\";\n for (const path of statusPaths(file)) {\n addChange(\n changes,\n path,\n fileStatus,\n contextForPath(path, prefix, summaries, options.identity),\n );\n }\n }\n\n const dirtyWorktree = status.files.length > 0;\n if (unresolved) attention.add(\"unresolved-operation\");\n if (detached) attention.add(\"detached-head\");\n if (wrongBranch) attention.add(\"wrong-branch\");\n\n const remotes = await options.repository.remotes();\n const hasRemote = remotes.some((remote) => remote.name === options.remote);\n let fetchFailed = false;\n if (options.refresh && options.mode === \"managed\" && hasRemote) {\n try {\n await options.repository.fetch(options.remote);\n } catch {\n fetchFailed = true;\n attention.add(\"fetch-failed\");\n }\n }\n const branches = await options.repository.branches();\n const localIdentity = branches.all.includes(options.identity.login);\n const remoteIdentity = `remotes/${options.remote}/${options.identity.login}`;\n const remotePrimary = `remotes/${options.remote}/${options.primaryBranch}`;\n const hasRemoteIdentity = hasRemote && branches.all.includes(remoteIdentity);\n const hasRemotePrimary = hasRemote && branches.all.includes(remotePrimary);\n let updateCheck: RepositorySummary[\"updateCheck\"] =\n options.mode === \"manual\" ? \"not-checked\" : \"unavailable\";\n\n if (options.mode === \"managed\") {\n if (fetchFailed) {\n updateCheck = \"attention\";\n } else if (!hasRemote) {\n updateCheck = \"unavailable\";\n } else if (!hasRemotePrimary || !localIdentity) {\n updateCheck = \"attention\";\n attention.add(\"missing-primary\");\n } else {\n try {\n const primaryRelation = await refRelation(\n options.repository,\n options.identity.login,\n remotePrimary,\n );\n updateCheck =\n primaryRelation.behind > 0 ? \"updates-available\" : \"up-to-date\";\n if (primaryRelation.behind > 0) {\n if (dirtyWorktree) attention.add(\"dirty-worktree\");\n addDiffChanges(\n changes,\n await diffPaths(\n options.repository,\n `${options.identity.login}..${remotePrimary}`,\n ),\n \"updates-available\",\n prefix,\n summaries,\n options.identity,\n );\n }\n } catch {\n updateCheck = \"attention\";\n attention.add(\"unknown\");\n }\n }\n\n if (hasRemoteIdentity && localIdentity) {\n try {\n const identityRelation = await refRelation(\n options.repository,\n options.identity.login,\n remoteIdentity,\n );\n if (identityRelation.ahead > 0) {\n addDiffChanges(\n changes,\n await diffPaths(\n options.repository,\n `${remoteIdentity}..${options.identity.login}`,\n ),\n \"committed-unpushed\",\n prefix,\n summaries,\n options.identity,\n );\n }\n } catch {\n attention.add(\"unknown\");\n }\n }\n }\n\n let readiness: RepositorySummary[\"readiness\"] = \"ready\";\n if (detached || wrongBranch || unresolved) {\n readiness = \"blocked\";\n } else if (!status.clean) {\n readiness = \"attention\";\n }\n return {\n mode: options.mode,\n readiness,\n updateCheck,\n changes: [...changes.values()].sort((left, right) =>\n left.path < right.path ? -1 : left.path > right.path ? 1 : 0,\n ),\n attention: [...attention].sort(),\n capabilities: getGitManagementCapabilities(options.mode),\n };\n}\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type GitRepository } from "../git-repository.js";
|
|
2
|
+
export type SynchronizationDeferredReason = "dirty-worktree" | "fetch-failed" | "merge-conflict";
|
|
3
|
+
export type SynchronizationUnsafeReason = "detached-head" | "unresolved-operation" | "missing-primary" | "identity-divergence" | "failed-abort" | "merge-failed" | "push-failed";
|
|
4
|
+
type SynchronizationBase = {
|
|
5
|
+
branch: string | null;
|
|
6
|
+
changed: boolean;
|
|
7
|
+
pushed: boolean;
|
|
8
|
+
affectedPaths: readonly string[];
|
|
9
|
+
};
|
|
10
|
+
export type RepositorySynchronizationResult = (SynchronizationBase & {
|
|
11
|
+
status: "synchronized";
|
|
12
|
+
}) | (SynchronizationBase & {
|
|
13
|
+
status: "deferred";
|
|
14
|
+
reason: SynchronizationDeferredReason;
|
|
15
|
+
}) | (SynchronizationBase & {
|
|
16
|
+
status: "unsafe";
|
|
17
|
+
reason: SynchronizationUnsafeReason;
|
|
18
|
+
});
|
|
19
|
+
export type SynchronizeIdentityBranchOptions = {
|
|
20
|
+
identityBranch: string;
|
|
21
|
+
primaryBranch: string;
|
|
22
|
+
remote: string;
|
|
23
|
+
};
|
|
24
|
+
export declare function hasUnresolvedRepositoryOperation(repository: GitRepository): Promise<boolean>;
|
|
25
|
+
export declare function assertIdentityBranchReady(repository: GitRepository, identityBranch: string): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Synchronize an identity branch from primary without checking out local
|
|
28
|
+
* primary. Remote-tracking refs are the source of truth whenever a configured
|
|
29
|
+
* remote is available. The result distinguishes non-blocking attention from
|
|
30
|
+
* unsafe repository state so callers can keep authentication/read-only flows
|
|
31
|
+
* available.
|
|
32
|
+
*/
|
|
33
|
+
export declare function synchronizeIdentityBranch(repository: GitRepository, options: SynchronizeIdentityBranchOptions): Promise<RepositorySynchronizationResult>;
|
|
34
|
+
export {};
|
|
35
|
+
//# sourceMappingURL=repository-synchronization.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repository-synchronization.d.ts","sourceRoot":"","sources":["../../src/lifecycle/repository-synchronization.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,KAAK,aAAa,EACnB,MAAM,sBAAsB,CAAC;AAE9B,MAAM,MAAM,6BAA6B,GACrC,gBAAgB,GAChB,cAAc,GACd,gBAAgB,CAAC;AAErB,MAAM,MAAM,2BAA2B,GACnC,eAAe,GACf,sBAAsB,GACtB,iBAAiB,GACjB,qBAAqB,GACrB,cAAc,GACd,cAAc,GACd,aAAa,CAAC;AAElB,KAAK,mBAAmB,GAAG;IACzB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,OAAO,CAAC;IAChB,aAAa,EAAE,SAAS,MAAM,EAAE,CAAC;CAClC,CAAC;AAEF,MAAM,MAAM,+BAA+B,GACvC,CAAC,mBAAmB,GAAG;IACrB,MAAM,EAAE,cAAc,CAAC;CACxB,CAAC,GACF,CAAC,mBAAmB,GAAG;IACrB,MAAM,EAAE,UAAU,CAAC;IACnB,MAAM,EAAE,6BAA6B,CAAC;CACvC,CAAC,GACF,CAAC,mBAAmB,GAAG;IACrB,MAAM,EAAE,QAAQ,CAAC;IACjB,MAAM,EAAE,2BAA2B,CAAC;CACrC,CAAC,CAAC;AAEP,MAAM,MAAM,gCAAgC,GAAG;IAC7C,cAAc,EAAE,MAAM,CAAC;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAgDF,wBAAsB,gCAAgC,CACpD,UAAU,EAAE,aAAa,oBAO1B;AAkFD,wBAAsB,yBAAyB,CAC7C,UAAU,EAAE,aAAa,EACzB,cAAc,EAAE,MAAM,GACrB,OAAO,CAAC,IAAI,CAAC,CAgBf;AAED;;;;;;GAMG;AACH,wBAAsB,yBAAyB,CAC7C,UAAU,EAAE,aAAa,EACzB,OAAO,EAAE,gCAAgC,GACxC,OAAO,CAAC,+BAA+B,CAAC,CAgL1C","sourcesContent":["import { lstat } from \"node:fs/promises\";\nimport { isAbsolute, resolve } from \"node:path\";\nimport {\n GitRepositoryError,\n type GitRepository,\n} from \"../git-repository.js\";\n\nexport type SynchronizationDeferredReason =\n | \"dirty-worktree\"\n | \"fetch-failed\"\n | \"merge-conflict\";\n\nexport type SynchronizationUnsafeReason =\n | \"detached-head\"\n | \"unresolved-operation\"\n | \"missing-primary\"\n | \"identity-divergence\"\n | \"failed-abort\"\n | \"merge-failed\"\n | \"push-failed\";\n\ntype SynchronizationBase = {\n branch: string | null;\n changed: boolean;\n pushed: boolean;\n affectedPaths: readonly string[];\n};\n\nexport type RepositorySynchronizationResult =\n | (SynchronizationBase & {\n status: \"synchronized\";\n })\n | (SynchronizationBase & {\n status: \"deferred\";\n reason: SynchronizationDeferredReason;\n })\n | (SynchronizationBase & {\n status: \"unsafe\";\n reason: SynchronizationUnsafeReason;\n });\n\nexport type SynchronizeIdentityBranchOptions = {\n identityBranch: string;\n primaryBranch: string;\n remote: string;\n};\n\nconst OPERATION_MARKERS = [\n \"MERGE_HEAD\",\n \"CHERRY_PICK_HEAD\",\n \"REVERT_HEAD\",\n \"rebase-merge\",\n \"rebase-apply\",\n] as const;\n\nfunction assertBranch(value: string, option: string) {\n if (\n value.length === 0 ||\n value.startsWith(\"-\") ||\n value.includes(\"\\0\") ||\n /\\s/u.test(value) ||\n value.includes(\"@{\") ||\n value.split(/[\\\\/]/u).some((segment) => segment === \"..\")\n ) {\n throw new GitRepositoryError(\"invalid-configuration\", option);\n }\n}\n\nfunction remoteRef(remote: string, branch: string) {\n return `remotes/${remote}/${branch}`;\n}\n\nfunction branchRef(branch: string) {\n return `refs/heads/${branch}`;\n}\n\nasync function markerExists(repository: GitRepository, marker: string) {\n const markerPath = (await repository.raw([\"rev-parse\", \"--git-path\", marker])).trim();\n if (markerPath.length === 0) return false;\n const absolutePath = isAbsolute(markerPath)\n ? markerPath\n : resolve(repository.repositoryRoot, markerPath);\n try {\n await lstat(absolutePath);\n return true;\n } catch (error) {\n if (error && typeof error === \"object\" && \"code\" in error && error.code === \"ENOENT\") {\n return false;\n }\n throw error;\n }\n}\n\nexport async function hasUnresolvedRepositoryOperation(\n repository: GitRepository,\n) {\n for (const marker of OPERATION_MARKERS) {\n if (await markerExists(repository, marker)) return true;\n }\n const status = await repository.status();\n return hasUnmergedFiles(status.files);\n}\n\nfunction hasUnmergedFiles(files: readonly { index: string; workingDirectory: string }[]) {\n return files.some(\n (file) => file.index.includes(\"U\") || file.workingDirectory.includes(\"U\"),\n );\n}\n\nasync function refCommit(repository: GitRepository, ref: string) {\n const value = await repository.raw([\"rev-parse\", \"--verify\", `${ref}^{commit}`]);\n const commit = value.trim();\n if (!/^[0-9a-f]{40}$/iu.test(commit)) {\n throw new GitRepositoryError(\"malformed-output\", \"ref-commit\");\n }\n return commit;\n}\n\nasync function compareRefs(\n repository: GitRepository,\n left: string,\n right: string,\n) {\n const output = await repository.raw([\n \"rev-list\",\n \"--left-right\",\n \"--count\",\n `${left}...${right}`,\n ]);\n const values = output.trim().split(/\\s+/u).map(Number);\n if (\n values.length !== 2 ||\n values.some((value) => !Number.isSafeInteger(value) || value < 0)\n ) {\n throw new GitRepositoryError(\"malformed-output\", \"ref-relation\");\n }\n return { ahead: values[0]!, behind: values[1]! };\n}\n\nasync function safelyAdvanceLocalPrimary(\n repository: GitRepository,\n primaryBranch: string,\n primaryRemoteRef: string,\n localPrimaryCommit: string,\n) {\n await repository.raw([\n \"update-ref\",\n branchRef(primaryBranch),\n primaryRemoteRef,\n localPrimaryCommit,\n ]);\n}\n\nfunction deferred(\n branch: string | null,\n reason: SynchronizationDeferredReason,\n affectedPaths: readonly string[] = [],\n): RepositorySynchronizationResult {\n return {\n status: \"deferred\",\n branch,\n changed: false,\n pushed: false,\n affectedPaths,\n reason,\n };\n}\n\nfunction unsafe(\n branch: string | null,\n reason: SynchronizationUnsafeReason,\n options: Partial<SynchronizationBase> = {},\n): RepositorySynchronizationResult {\n return {\n status: \"unsafe\",\n branch,\n changed: options.changed ?? false,\n pushed: options.pushed ?? false,\n affectedPaths: options.affectedPaths ?? [],\n reason,\n };\n}\n\nexport async function assertIdentityBranchReady(\n repository: GitRepository,\n identityBranch: string,\n): Promise<void> {\n assertBranch(identityBranch, \"identity-branch\");\n const currentBranch = await repository.currentBranch();\n if (currentBranch === \"HEAD\" || currentBranch.length === 0) {\n throw new GitRepositoryError(\"conflict\", \"current-branch\");\n }\n if (currentBranch !== identityBranch) {\n throw new GitRepositoryError(\"conflict\", \"identity-branch\");\n }\n if (await hasUnresolvedRepositoryOperation(repository)) {\n throw new GitRepositoryError(\"conflict\", \"unresolved-operation\");\n }\n const status = await repository.status();\n if (hasUnmergedFiles(status.files)) {\n throw new GitRepositoryError(\"conflict\", \"unresolved-operation\");\n }\n}\n\n/**\n * Synchronize an identity branch from primary without checking out local\n * primary. Remote-tracking refs are the source of truth whenever a configured\n * remote is available. The result distinguishes non-blocking attention from\n * unsafe repository state so callers can keep authentication/read-only flows\n * available.\n */\nexport async function synchronizeIdentityBranch(\n repository: GitRepository,\n options: SynchronizeIdentityBranchOptions,\n): Promise<RepositorySynchronizationResult> {\n assertBranch(options.identityBranch, \"identity-branch\");\n assertBranch(options.primaryBranch, \"primary-branch\");\n assertBranch(options.remote, \"remote\");\n\n if (options.identityBranch === options.primaryBranch) {\n throw new GitRepositoryError(\"invalid-configuration\", \"identity-primary-branch\");\n }\n\n const currentBranch = await repository.currentBranch();\n if (currentBranch === \"HEAD\" || currentBranch.length === 0) {\n return unsafe(null, \"detached-head\");\n }\n\n if (await hasUnresolvedRepositoryOperation(repository)) {\n return unsafe(currentBranch, \"unresolved-operation\");\n }\n\n const status = await repository.status();\n if (hasUnmergedFiles(status.files)) {\n return unsafe(currentBranch, \"unresolved-operation\", {\n affectedPaths: status.files.map((file) => file.path),\n });\n }\n if (!status.clean) {\n return deferred(\n currentBranch,\n \"dirty-worktree\",\n status.files.map((file) => file.path),\n );\n }\n\n const configuredRemote = (await repository.remotes()).some(\n (remote) => remote.name === options.remote,\n );\n if (configuredRemote) {\n try {\n await repository.fetch(options.remote);\n } catch (error) {\n if (\n error instanceof GitRepositoryError &&\n error.kind === \"invalid-configuration\"\n ) {\n throw error;\n }\n return deferred(currentBranch, \"fetch-failed\");\n }\n }\n\n const branches = await repository.branches();\n const localPrimary = branches.all.includes(options.primaryBranch);\n const primaryRemote = remoteRef(options.remote, options.primaryBranch);\n const remotePrimary = configuredRemote && branches.all.includes(primaryRemote);\n if (configuredRemote ? !remotePrimary : !localPrimary) {\n return unsafe(currentBranch, \"missing-primary\");\n }\n\n const localIdentity = branches.all.includes(options.identityBranch);\n const identityRemote = remoteRef(options.remote, options.identityBranch);\n const remoteIdentity =\n configuredRemote && branches.all.includes(identityRemote);\n\n if (localIdentity && remoteIdentity) {\n const relation = await compareRefs(\n repository,\n options.identityBranch,\n identityRemote,\n );\n if (relation.ahead > 0 && relation.behind > 0) {\n return unsafe(currentBranch, \"identity-divergence\");\n }\n }\n\n let primaryAdvance:\n | { localCommit: string; remoteRef: string }\n | undefined;\n if (localPrimary && remotePrimary) {\n const relation = await compareRefs(\n repository,\n options.primaryBranch,\n primaryRemote,\n );\n if (relation.ahead === 0 && relation.behind > 0) {\n primaryAdvance = {\n localCommit: await refCommit(repository, options.primaryBranch),\n remoteRef: primaryRemote,\n };\n }\n }\n\n const sourceRef = remotePrimary ? primaryRemote : options.primaryBranch;\n if (!localIdentity) {\n if (remoteIdentity) {\n await repository.checkout([\n \"-b\",\n options.identityBranch,\n \"--track\",\n `${options.remote}/${options.identityBranch}`,\n ]);\n } else {\n await repository.checkout([\"-b\", options.identityBranch, sourceRef]);\n }\n } else if (currentBranch !== options.identityBranch) {\n await repository.checkout([options.identityBranch]);\n }\n\n if (primaryAdvance && currentBranch !== options.primaryBranch) {\n await safelyAdvanceLocalPrimary(\n repository,\n options.primaryBranch,\n primaryAdvance.remoteRef,\n primaryAdvance.localCommit,\n );\n }\n\n if (localIdentity && remoteIdentity) {\n const relation = await compareRefs(\n repository,\n options.identityBranch,\n identityRemote,\n );\n if (relation.ahead === 0 && relation.behind > 0) {\n await repository.merge([identityRemote, \"--ff-only\"]);\n }\n }\n\n const beforeMerge = await refCommit(repository, options.identityBranch);\n try {\n await repository.merge([sourceRef, \"--no-edit\"]);\n } catch (error) {\n if (!(error instanceof GitRepositoryError) || error.kind !== \"conflict\") {\n return unsafe(options.identityBranch, \"merge-failed\");\n }\n let affectedPaths: readonly string[] = [];\n try {\n affectedPaths = (await repository.status()).files.map((file) => file.path);\n } catch {\n // Preserve the conflict classification even if status cannot be read.\n }\n try {\n await repository.raw([\"merge\", \"--abort\"]);\n } catch {\n return unsafe(options.identityBranch, \"failed-abort\", {\n affectedPaths,\n });\n }\n return deferred(options.identityBranch, \"merge-conflict\", affectedPaths);\n }\n\n const afterMerge = await refCommit(repository, options.identityBranch);\n const changed = beforeMerge !== afterMerge;\n if (!changed || !configuredRemote) {\n return {\n status: \"synchronized\",\n branch: options.identityBranch,\n changed,\n pushed: false,\n affectedPaths: [],\n };\n }\n\n try {\n await repository.push(options.remote, options.identityBranch, [\n \"--set-upstream\",\n ]);\n } catch {\n return unsafe(options.identityBranch, \"push-failed\", { changed });\n }\n\n return {\n status: \"synchronized\",\n branch: options.identityBranch,\n changed: true,\n pushed: true,\n affectedPaths: [],\n };\n}\n"]}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { lstat } from "node:fs/promises";
|
|
2
|
+
import { isAbsolute, resolve } from "node:path";
|
|
3
|
+
import { GitRepositoryError, } from "../git-repository.js";
|
|
4
|
+
const OPERATION_MARKERS = [
|
|
5
|
+
"MERGE_HEAD",
|
|
6
|
+
"CHERRY_PICK_HEAD",
|
|
7
|
+
"REVERT_HEAD",
|
|
8
|
+
"rebase-merge",
|
|
9
|
+
"rebase-apply",
|
|
10
|
+
];
|
|
11
|
+
function assertBranch(value, option) {
|
|
12
|
+
if (value.length === 0 ||
|
|
13
|
+
value.startsWith("-") ||
|
|
14
|
+
value.includes("\0") ||
|
|
15
|
+
/\s/u.test(value) ||
|
|
16
|
+
value.includes("@{") ||
|
|
17
|
+
value.split(/[\\/]/u).some((segment) => segment === "..")) {
|
|
18
|
+
throw new GitRepositoryError("invalid-configuration", option);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function remoteRef(remote, branch) {
|
|
22
|
+
return `remotes/${remote}/${branch}`;
|
|
23
|
+
}
|
|
24
|
+
function branchRef(branch) {
|
|
25
|
+
return `refs/heads/${branch}`;
|
|
26
|
+
}
|
|
27
|
+
async function markerExists(repository, marker) {
|
|
28
|
+
const markerPath = (await repository.raw(["rev-parse", "--git-path", marker])).trim();
|
|
29
|
+
if (markerPath.length === 0)
|
|
30
|
+
return false;
|
|
31
|
+
const absolutePath = isAbsolute(markerPath)
|
|
32
|
+
? markerPath
|
|
33
|
+
: resolve(repository.repositoryRoot, markerPath);
|
|
34
|
+
try {
|
|
35
|
+
await lstat(absolutePath);
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export async function hasUnresolvedRepositoryOperation(repository) {
|
|
46
|
+
for (const marker of OPERATION_MARKERS) {
|
|
47
|
+
if (await markerExists(repository, marker))
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
const status = await repository.status();
|
|
51
|
+
return hasUnmergedFiles(status.files);
|
|
52
|
+
}
|
|
53
|
+
function hasUnmergedFiles(files) {
|
|
54
|
+
return files.some((file) => file.index.includes("U") || file.workingDirectory.includes("U"));
|
|
55
|
+
}
|
|
56
|
+
async function refCommit(repository, ref) {
|
|
57
|
+
const value = await repository.raw(["rev-parse", "--verify", `${ref}^{commit}`]);
|
|
58
|
+
const commit = value.trim();
|
|
59
|
+
if (!/^[0-9a-f]{40}$/iu.test(commit)) {
|
|
60
|
+
throw new GitRepositoryError("malformed-output", "ref-commit");
|
|
61
|
+
}
|
|
62
|
+
return commit;
|
|
63
|
+
}
|
|
64
|
+
async function compareRefs(repository, left, right) {
|
|
65
|
+
const output = await repository.raw([
|
|
66
|
+
"rev-list",
|
|
67
|
+
"--left-right",
|
|
68
|
+
"--count",
|
|
69
|
+
`${left}...${right}`,
|
|
70
|
+
]);
|
|
71
|
+
const values = output.trim().split(/\s+/u).map(Number);
|
|
72
|
+
if (values.length !== 2 ||
|
|
73
|
+
values.some((value) => !Number.isSafeInteger(value) || value < 0)) {
|
|
74
|
+
throw new GitRepositoryError("malformed-output", "ref-relation");
|
|
75
|
+
}
|
|
76
|
+
return { ahead: values[0], behind: values[1] };
|
|
77
|
+
}
|
|
78
|
+
async function safelyAdvanceLocalPrimary(repository, primaryBranch, primaryRemoteRef, localPrimaryCommit) {
|
|
79
|
+
await repository.raw([
|
|
80
|
+
"update-ref",
|
|
81
|
+
branchRef(primaryBranch),
|
|
82
|
+
primaryRemoteRef,
|
|
83
|
+
localPrimaryCommit,
|
|
84
|
+
]);
|
|
85
|
+
}
|
|
86
|
+
function deferred(branch, reason, affectedPaths = []) {
|
|
87
|
+
return {
|
|
88
|
+
status: "deferred",
|
|
89
|
+
branch,
|
|
90
|
+
changed: false,
|
|
91
|
+
pushed: false,
|
|
92
|
+
affectedPaths,
|
|
93
|
+
reason,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function unsafe(branch, reason, options = {}) {
|
|
97
|
+
return {
|
|
98
|
+
status: "unsafe",
|
|
99
|
+
branch,
|
|
100
|
+
changed: options.changed ?? false,
|
|
101
|
+
pushed: options.pushed ?? false,
|
|
102
|
+
affectedPaths: options.affectedPaths ?? [],
|
|
103
|
+
reason,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
export async function assertIdentityBranchReady(repository, identityBranch) {
|
|
107
|
+
assertBranch(identityBranch, "identity-branch");
|
|
108
|
+
const currentBranch = await repository.currentBranch();
|
|
109
|
+
if (currentBranch === "HEAD" || currentBranch.length === 0) {
|
|
110
|
+
throw new GitRepositoryError("conflict", "current-branch");
|
|
111
|
+
}
|
|
112
|
+
if (currentBranch !== identityBranch) {
|
|
113
|
+
throw new GitRepositoryError("conflict", "identity-branch");
|
|
114
|
+
}
|
|
115
|
+
if (await hasUnresolvedRepositoryOperation(repository)) {
|
|
116
|
+
throw new GitRepositoryError("conflict", "unresolved-operation");
|
|
117
|
+
}
|
|
118
|
+
const status = await repository.status();
|
|
119
|
+
if (hasUnmergedFiles(status.files)) {
|
|
120
|
+
throw new GitRepositoryError("conflict", "unresolved-operation");
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Synchronize an identity branch from primary without checking out local
|
|
125
|
+
* primary. Remote-tracking refs are the source of truth whenever a configured
|
|
126
|
+
* remote is available. The result distinguishes non-blocking attention from
|
|
127
|
+
* unsafe repository state so callers can keep authentication/read-only flows
|
|
128
|
+
* available.
|
|
129
|
+
*/
|
|
130
|
+
export async function synchronizeIdentityBranch(repository, options) {
|
|
131
|
+
assertBranch(options.identityBranch, "identity-branch");
|
|
132
|
+
assertBranch(options.primaryBranch, "primary-branch");
|
|
133
|
+
assertBranch(options.remote, "remote");
|
|
134
|
+
if (options.identityBranch === options.primaryBranch) {
|
|
135
|
+
throw new GitRepositoryError("invalid-configuration", "identity-primary-branch");
|
|
136
|
+
}
|
|
137
|
+
const currentBranch = await repository.currentBranch();
|
|
138
|
+
if (currentBranch === "HEAD" || currentBranch.length === 0) {
|
|
139
|
+
return unsafe(null, "detached-head");
|
|
140
|
+
}
|
|
141
|
+
if (await hasUnresolvedRepositoryOperation(repository)) {
|
|
142
|
+
return unsafe(currentBranch, "unresolved-operation");
|
|
143
|
+
}
|
|
144
|
+
const status = await repository.status();
|
|
145
|
+
if (hasUnmergedFiles(status.files)) {
|
|
146
|
+
return unsafe(currentBranch, "unresolved-operation", {
|
|
147
|
+
affectedPaths: status.files.map((file) => file.path),
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
if (!status.clean) {
|
|
151
|
+
return deferred(currentBranch, "dirty-worktree", status.files.map((file) => file.path));
|
|
152
|
+
}
|
|
153
|
+
const configuredRemote = (await repository.remotes()).some((remote) => remote.name === options.remote);
|
|
154
|
+
if (configuredRemote) {
|
|
155
|
+
try {
|
|
156
|
+
await repository.fetch(options.remote);
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
if (error instanceof GitRepositoryError &&
|
|
160
|
+
error.kind === "invalid-configuration") {
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
return deferred(currentBranch, "fetch-failed");
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const branches = await repository.branches();
|
|
167
|
+
const localPrimary = branches.all.includes(options.primaryBranch);
|
|
168
|
+
const primaryRemote = remoteRef(options.remote, options.primaryBranch);
|
|
169
|
+
const remotePrimary = configuredRemote && branches.all.includes(primaryRemote);
|
|
170
|
+
if (configuredRemote ? !remotePrimary : !localPrimary) {
|
|
171
|
+
return unsafe(currentBranch, "missing-primary");
|
|
172
|
+
}
|
|
173
|
+
const localIdentity = branches.all.includes(options.identityBranch);
|
|
174
|
+
const identityRemote = remoteRef(options.remote, options.identityBranch);
|
|
175
|
+
const remoteIdentity = configuredRemote && branches.all.includes(identityRemote);
|
|
176
|
+
if (localIdentity && remoteIdentity) {
|
|
177
|
+
const relation = await compareRefs(repository, options.identityBranch, identityRemote);
|
|
178
|
+
if (relation.ahead > 0 && relation.behind > 0) {
|
|
179
|
+
return unsafe(currentBranch, "identity-divergence");
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
let primaryAdvance;
|
|
183
|
+
if (localPrimary && remotePrimary) {
|
|
184
|
+
const relation = await compareRefs(repository, options.primaryBranch, primaryRemote);
|
|
185
|
+
if (relation.ahead === 0 && relation.behind > 0) {
|
|
186
|
+
primaryAdvance = {
|
|
187
|
+
localCommit: await refCommit(repository, options.primaryBranch),
|
|
188
|
+
remoteRef: primaryRemote,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
const sourceRef = remotePrimary ? primaryRemote : options.primaryBranch;
|
|
193
|
+
if (!localIdentity) {
|
|
194
|
+
if (remoteIdentity) {
|
|
195
|
+
await repository.checkout([
|
|
196
|
+
"-b",
|
|
197
|
+
options.identityBranch,
|
|
198
|
+
"--track",
|
|
199
|
+
`${options.remote}/${options.identityBranch}`,
|
|
200
|
+
]);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
await repository.checkout(["-b", options.identityBranch, sourceRef]);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else if (currentBranch !== options.identityBranch) {
|
|
207
|
+
await repository.checkout([options.identityBranch]);
|
|
208
|
+
}
|
|
209
|
+
if (primaryAdvance && currentBranch !== options.primaryBranch) {
|
|
210
|
+
await safelyAdvanceLocalPrimary(repository, options.primaryBranch, primaryAdvance.remoteRef, primaryAdvance.localCommit);
|
|
211
|
+
}
|
|
212
|
+
if (localIdentity && remoteIdentity) {
|
|
213
|
+
const relation = await compareRefs(repository, options.identityBranch, identityRemote);
|
|
214
|
+
if (relation.ahead === 0 && relation.behind > 0) {
|
|
215
|
+
await repository.merge([identityRemote, "--ff-only"]);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const beforeMerge = await refCommit(repository, options.identityBranch);
|
|
219
|
+
try {
|
|
220
|
+
await repository.merge([sourceRef, "--no-edit"]);
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
if (!(error instanceof GitRepositoryError) || error.kind !== "conflict") {
|
|
224
|
+
return unsafe(options.identityBranch, "merge-failed");
|
|
225
|
+
}
|
|
226
|
+
let affectedPaths = [];
|
|
227
|
+
try {
|
|
228
|
+
affectedPaths = (await repository.status()).files.map((file) => file.path);
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
// Preserve the conflict classification even if status cannot be read.
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
await repository.raw(["merge", "--abort"]);
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
return unsafe(options.identityBranch, "failed-abort", {
|
|
238
|
+
affectedPaths,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
return deferred(options.identityBranch, "merge-conflict", affectedPaths);
|
|
242
|
+
}
|
|
243
|
+
const afterMerge = await refCommit(repository, options.identityBranch);
|
|
244
|
+
const changed = beforeMerge !== afterMerge;
|
|
245
|
+
if (!changed || !configuredRemote) {
|
|
246
|
+
return {
|
|
247
|
+
status: "synchronized",
|
|
248
|
+
branch: options.identityBranch,
|
|
249
|
+
changed,
|
|
250
|
+
pushed: false,
|
|
251
|
+
affectedPaths: [],
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
try {
|
|
255
|
+
await repository.push(options.remote, options.identityBranch, [
|
|
256
|
+
"--set-upstream",
|
|
257
|
+
]);
|
|
258
|
+
}
|
|
259
|
+
catch {
|
|
260
|
+
return unsafe(options.identityBranch, "push-failed", { changed });
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
status: "synchronized",
|
|
264
|
+
branch: options.identityBranch,
|
|
265
|
+
changed: true,
|
|
266
|
+
pushed: true,
|
|
267
|
+
affectedPaths: [],
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
//# sourceMappingURL=repository-synchronization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repository-synchronization.js","sourceRoot":"","sources":["../../src/lifecycle/repository-synchronization.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EACL,kBAAkB,GAEnB,MAAM,sBAAsB,CAAC;AA0C9B,MAAM,iBAAiB,GAAG;IACxB,YAAY;IACZ,kBAAkB;IAClB,aAAa;IACb,cAAc;IACd,cAAc;CACN,CAAC;AAEX,SAAS,YAAY,CAAC,KAAa,EAAE,MAAc;IACjD,IACE,KAAK,CAAC,MAAM,KAAK,CAAC;QAClB,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC;QACrB,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC;QACpB,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;QACjB,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC;QACpB,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,KAAK,IAAI,CAAC,EACzD,CAAC;QACD,MAAM,IAAI,kBAAkB,CAAC,uBAAuB,EAAE,MAAM,CAAC,CAAC;IAChE,CAAC;AACH,CAAC;AAED,SAAS,SAAS,CAAC,MAAc,EAAE,MAAc;IAC/C,OAAO,WAAW,MAAM,IAAI,MAAM,EAAE,CAAC;AACvC,CAAC;AAED,SAAS,SAAS,CAAC,MAAc;IAC/B,OAAO,cAAc,MAAM,EAAE,CAAC;AAChC,CAAC;AAED,KAAK,UAAU,Y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AAC,CAAC;IACpE,CAAC;IAED,OAAO;QACL,MAAM,EAAE,cAAc;QACtB,MAAM,EAAE,OAAO,CAAC,cAAc;QAC9B,OAAO,EAAE,IAAI;QACb,MAAM,EAAE,IAAI;QACZ,aAAa,EAAE,EAAE;KAClB,CAAC;AACJ,CAAC","sourcesContent":["import { lstat } from \"node:fs/promises\";\nimport { isAbsolute, resolve } from \"node:path\";\nimport {\n GitRepositoryError,\n type GitRepository,\n} from \"../git-repository.js\";\n\nexport type SynchronizationDeferredReason =\n | \"dirty-worktree\"\n | \"fetch-failed\"\n | \"merge-conflict\";\n\nexport type SynchronizationUnsafeReason =\n | \"detached-head\"\n | \"unresolved-operation\"\n | \"missing-primary\"\n | \"identity-divergence\"\n | \"failed-abort\"\n | \"merge-failed\"\n | \"push-failed\";\n\ntype SynchronizationBase = {\n branch: string | null;\n changed: boolean;\n pushed: boolean;\n affectedPaths: readonly string[];\n};\n\nexport type RepositorySynchronizationResult =\n | (SynchronizationBase & {\n status: \"synchronized\";\n })\n | (SynchronizationBase & {\n status: \"deferred\";\n reason: SynchronizationDeferredReason;\n })\n | (SynchronizationBase & {\n status: \"unsafe\";\n reason: SynchronizationUnsafeReason;\n });\n\nexport type SynchronizeIdentityBranchOptions = {\n identityBranch: string;\n primaryBranch: string;\n remote: string;\n};\n\nconst OPERATION_MARKERS = [\n \"MERGE_HEAD\",\n \"CHERRY_PICK_HEAD\",\n \"REVERT_HEAD\",\n \"rebase-merge\",\n \"rebase-apply\",\n] as const;\n\nfunction assertBranch(value: string, option: string) {\n if (\n value.length === 0 ||\n value.startsWith(\"-\") ||\n value.includes(\"\\0\") ||\n /\\s/u.test(value) ||\n value.includes(\"@{\") ||\n value.split(/[\\\\/]/u).some((segment) => segment === \"..\")\n ) {\n throw new GitRepositoryError(\"invalid-configuration\", option);\n }\n}\n\nfunction remoteRef(remote: string, branch: string) {\n return `remotes/${remote}/${branch}`;\n}\n\nfunction branchRef(branch: string) {\n return `refs/heads/${branch}`;\n}\n\nasync function markerExists(repository: GitRepository, marker: string) {\n const markerPath = (await repository.raw([\"rev-parse\", \"--git-path\", marker])).trim();\n if (markerPath.length === 0) return false;\n const absolutePath = isAbsolute(markerPath)\n ? markerPath\n : resolve(repository.repositoryRoot, markerPath);\n try {\n await lstat(absolutePath);\n return true;\n } catch (error) {\n if (error && typeof error === \"object\" && \"code\" in error && error.code === \"ENOENT\") {\n return false;\n }\n throw error;\n }\n}\n\nexport async function hasUnresolvedRepositoryOperation(\n repository: GitRepository,\n) {\n for (const marker of OPERATION_MARKERS) {\n if (await markerExists(repository, marker)) return true;\n }\n const status = await repository.status();\n return hasUnmergedFiles(status.files);\n}\n\nfunction hasUnmergedFiles(files: readonly { index: string; workingDirectory: string }[]) {\n return files.some(\n (file) => file.index.includes(\"U\") || file.workingDirectory.includes(\"U\"),\n );\n}\n\nasync function refCommit(repository: GitRepository, ref: string) {\n const value = await repository.raw([\"rev-parse\", \"--verify\", `${ref}^{commit}`]);\n const commit = value.trim();\n if (!/^[0-9a-f]{40}$/iu.test(commit)) {\n throw new GitRepositoryError(\"malformed-output\", \"ref-commit\");\n }\n return commit;\n}\n\nasync function compareRefs(\n repository: GitRepository,\n left: string,\n right: string,\n) {\n const output = await repository.raw([\n \"rev-list\",\n \"--left-right\",\n \"--count\",\n `${left}...${right}`,\n ]);\n const values = output.trim().split(/\\s+/u).map(Number);\n if (\n values.length !== 2 ||\n values.some((value) => !Number.isSafeInteger(value) || value < 0)\n ) {\n throw new GitRepositoryError(\"malformed-output\", \"ref-relation\");\n }\n return { ahead: values[0]!, behind: values[1]! };\n}\n\nasync function safelyAdvanceLocalPrimary(\n repository: GitRepository,\n primaryBranch: string,\n primaryRemoteRef: string,\n localPrimaryCommit: string,\n) {\n await repository.raw([\n \"update-ref\",\n branchRef(primaryBranch),\n primaryRemoteRef,\n localPrimaryCommit,\n ]);\n}\n\nfunction deferred(\n branch: string | null,\n reason: SynchronizationDeferredReason,\n affectedPaths: readonly string[] = [],\n): RepositorySynchronizationResult {\n return {\n status: \"deferred\",\n branch,\n changed: false,\n pushed: false,\n affectedPaths,\n reason,\n };\n}\n\nfunction unsafe(\n branch: string | null,\n reason: SynchronizationUnsafeReason,\n options: Partial<SynchronizationBase> = {},\n): RepositorySynchronizationResult {\n return {\n status: \"unsafe\",\n branch,\n changed: options.changed ?? false,\n pushed: options.pushed ?? false,\n affectedPaths: options.affectedPaths ?? [],\n reason,\n };\n}\n\nexport async function assertIdentityBranchReady(\n repository: GitRepository,\n identityBranch: string,\n): Promise<void> {\n assertBranch(identityBranch, \"identity-branch\");\n const currentBranch = await repository.currentBranch();\n if (currentBranch === \"HEAD\" || currentBranch.length === 0) {\n throw new GitRepositoryError(\"conflict\", \"current-branch\");\n }\n if (currentBranch !== identityBranch) {\n throw new GitRepositoryError(\"conflict\", \"identity-branch\");\n }\n if (await hasUnresolvedRepositoryOperation(repository)) {\n throw new GitRepositoryError(\"conflict\", \"unresolved-operation\");\n }\n const status = await repository.status();\n if (hasUnmergedFiles(status.files)) {\n throw new GitRepositoryError(\"conflict\", \"unresolved-operation\");\n }\n}\n\n/**\n * Synchronize an identity branch from primary without checking out local\n * primary. Remote-tracking refs are the source of truth whenever a configured\n * remote is available. The result distinguishes non-blocking attention from\n * unsafe repository state so callers can keep authentication/read-only flows\n * available.\n */\nexport async function synchronizeIdentityBranch(\n repository: GitRepository,\n options: SynchronizeIdentityBranchOptions,\n): Promise<RepositorySynchronizationResult> {\n assertBranch(options.identityBranch, \"identity-branch\");\n assertBranch(options.primaryBranch, \"primary-branch\");\n assertBranch(options.remote, \"remote\");\n\n if (options.identityBranch === options.primaryBranch) {\n throw new GitRepositoryError(\"invalid-configuration\", \"identity-primary-branch\");\n }\n\n const currentBranch = await repository.currentBranch();\n if (currentBranch === \"HEAD\" || currentBranch.length === 0) {\n return unsafe(null, \"detached-head\");\n }\n\n if (await hasUnresolvedRepositoryOperation(repository)) {\n return unsafe(currentBranch, \"unresolved-operation\");\n }\n\n const status = await repository.status();\n if (hasUnmergedFiles(status.files)) {\n return unsafe(currentBranch, \"unresolved-operation\", {\n affectedPaths: status.files.map((file) => file.path),\n });\n }\n if (!status.clean) {\n return deferred(\n currentBranch,\n \"dirty-worktree\",\n status.files.map((file) => file.path),\n );\n }\n\n const configuredRemote = (await repository.remotes()).some(\n (remote) => remote.name === options.remote,\n );\n if (configuredRemote) {\n try {\n await repository.fetch(options.remote);\n } catch (error) {\n if (\n error instanceof GitRepositoryError &&\n error.kind === \"invalid-configuration\"\n ) {\n throw error;\n }\n return deferred(currentBranch, \"fetch-failed\");\n }\n }\n\n const branches = await repository.branches();\n const localPrimary = branches.all.includes(options.primaryBranch);\n const primaryRemote = remoteRef(options.remote, options.primaryBranch);\n const remotePrimary = configuredRemote && branches.all.includes(primaryRemote);\n if (configuredRemote ? !remotePrimary : !localPrimary) {\n return unsafe(currentBranch, \"missing-primary\");\n }\n\n const localIdentity = branches.all.includes(options.identityBranch);\n const identityRemote = remoteRef(options.remote, options.identityBranch);\n const remoteIdentity =\n configuredRemote && branches.all.includes(identityRemote);\n\n if (localIdentity && remoteIdentity) {\n const relation = await compareRefs(\n repository,\n options.identityBranch,\n identityRemote,\n );\n if (relation.ahead > 0 && relation.behind > 0) {\n return unsafe(currentBranch, \"identity-divergence\");\n }\n }\n\n let primaryAdvance:\n | { localCommit: string; remoteRef: string }\n | undefined;\n if (localPrimary && remotePrimary) {\n const relation = await compareRefs(\n repository,\n options.primaryBranch,\n primaryRemote,\n );\n if (relation.ahead === 0 && relation.behind > 0) {\n primaryAdvance = {\n localCommit: await refCommit(repository, options.primaryBranch),\n remoteRef: primaryRemote,\n };\n }\n }\n\n const sourceRef = remotePrimary ? primaryRemote : options.primaryBranch;\n if (!localIdentity) {\n if (remoteIdentity) {\n await repository.checkout([\n \"-b\",\n options.identityBranch,\n \"--track\",\n `${options.remote}/${options.identityBranch}`,\n ]);\n } else {\n await repository.checkout([\"-b\", options.identityBranch, sourceRef]);\n }\n } else if (currentBranch !== options.identityBranch) {\n await repository.checkout([options.identityBranch]);\n }\n\n if (primaryAdvance && currentBranch !== options.primaryBranch) {\n await safelyAdvanceLocalPrimary(\n repository,\n options.primaryBranch,\n primaryAdvance.remoteRef,\n primaryAdvance.localCommit,\n );\n }\n\n if (localIdentity && remoteIdentity) {\n const relation = await compareRefs(\n repository,\n options.identityBranch,\n identityRemote,\n );\n if (relation.ahead === 0 && relation.behind > 0) {\n await repository.merge([identityRemote, \"--ff-only\"]);\n }\n }\n\n const beforeMerge = await refCommit(repository, options.identityBranch);\n try {\n await repository.merge([sourceRef, \"--no-edit\"]);\n } catch (error) {\n if (!(error instanceof GitRepositoryError) || error.kind !== \"conflict\") {\n return unsafe(options.identityBranch, \"merge-failed\");\n }\n let affectedPaths: readonly string[] = [];\n try {\n affectedPaths = (await repository.status()).files.map((file) => file.path);\n } catch {\n // Preserve the conflict classification even if status cannot be read.\n }\n try {\n await repository.raw([\"merge\", \"--abort\"]);\n } catch {\n return unsafe(options.identityBranch, \"failed-abort\", {\n affectedPaths,\n });\n }\n return deferred(options.identityBranch, \"merge-conflict\", affectedPaths);\n }\n\n const afterMerge = await refCommit(repository, options.identityBranch);\n const changed = beforeMerge !== afterMerge;\n if (!changed || !configuredRemote) {\n return {\n status: \"synchronized\",\n branch: options.identityBranch,\n changed,\n pushed: false,\n affectedPaths: [],\n };\n }\n\n try {\n await repository.push(options.remote, options.identityBranch, [\n \"--set-upstream\",\n ]);\n } catch {\n return unsafe(options.identityBranch, \"push-failed\", { changed });\n }\n\n return {\n status: \"synchronized\",\n branch: options.identityBranch,\n changed: true,\n pushed: true,\n affectedPaths: [],\n };\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type AutomaticBranchSwitchMode, type GitMode } from "@ebay-dt/bonfire-core";
|
|
2
2
|
import { type PrototypeCatalogOptions, type ResolvedPrototypeCatalogPaths } from "../prototype-paths.js";
|
|
3
3
|
import { type GitCommitIdentity, type GitRepository } from "../git-repository.js";
|
|
4
4
|
export type PrototypeLifecycleRuntimeOptions = PrototypeCatalogOptions & {
|
|
@@ -6,6 +6,7 @@ export type PrototypeLifecycleRuntimeOptions = PrototypeCatalogOptions & {
|
|
|
6
6
|
primaryBranch?: string;
|
|
7
7
|
commitIdentity?: GitCommitIdentity;
|
|
8
8
|
automaticBranchSwitch?: AutomaticBranchSwitchMode;
|
|
9
|
+
gitMode?: GitMode;
|
|
9
10
|
gitRepository?: GitRepository;
|
|
10
11
|
};
|
|
11
12
|
export type PrototypeLifecycleRuntimeContext = ResolvedPrototypeCatalogPaths & {
|
|
@@ -13,6 +14,7 @@ export type PrototypeLifecycleRuntimeContext = ResolvedPrototypeCatalogPaths & {
|
|
|
13
14
|
remote: string;
|
|
14
15
|
primaryBranch: string;
|
|
15
16
|
automaticBranchSwitch: AutomaticBranchSwitchMode;
|
|
17
|
+
gitMode: GitMode;
|
|
16
18
|
commitIdentity: Readonly<GitCommitIdentity>;
|
|
17
19
|
gitRepository: GitRepository;
|
|
18
20
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime-context.d.ts","sourceRoot":"","sources":["../../src/lifecycle/runtime-context.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"runtime-context.d.ts","sourceRoot":"","sources":["../../src/lifecycle/runtime-context.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,yBAAyB,EAC9B,KAAK,OAAO,EACb,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAEL,KAAK,uBAAuB,EAC5B,KAAK,6BAA6B,EACnC,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAEL,KAAK,iBAAiB,EACtB,KAAK,aAAa,EACnB,MAAM,sBAAsB,CAAC;AAS9B,MAAM,MAAM,gCAAgC,GAAG,uBAAuB,GAAG;IACvE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,iBAAiB,CAAC;IACnC,qBAAqB,CAAC,EAAE,yBAAyB,CAAC;IAClD,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,aAAa,CAAC,EAAE,aAAa,CAAC;CAC/B,CAAC;AAEF,MAAM,MAAM,gCAAgC,GAAG,6BAA6B,GAAG;IAC7E,kBAAkB,EAAE,MAAM,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,aAAa,EAAE,MAAM,CAAC;IACtB,qBAAqB,EAAE,yBAAyB,CAAC;IACjD,OAAO,EAAE,OAAO,CAAC;IACjB,cAAc,EAAE,QAAQ,CAAC,iBAAiB,CAAC,CAAC;IAC5C,aAAa,EAAE,aAAa,CAAC;CAC9B,CAAC;AASF,wBAAgB,+BAA+B,CAC7C,OAAO,EAAE,gCAAgC,GACxC,gCAAgC,CA8BlC","sourcesContent":["import { realpathSync } from \"node:fs\";\nimport {\n parseGitModeSetting,\n type AutomaticBranchSwitchMode,\n type GitMode,\n} from \"@ebay-dt/bonfire-core\";\nimport {\n resolvePrototypeCatalogPaths,\n type PrototypeCatalogOptions,\n type ResolvedPrototypeCatalogPaths,\n} from \"../prototype-paths.js\";\nimport {\n createGitRepository,\n type GitCommitIdentity,\n type GitRepository,\n} from \"../git-repository.js\";\n\nconst DEFAULT_REMOTE = \"origin\";\nconst DEFAULT_PRIMARY_BRANCH = \"main\";\nconst DEFAULT_COMMIT_IDENTITY: GitCommitIdentity = {\n name: \"Bonfire\",\n email: \"bonfire@localhost\",\n};\n\nexport type PrototypeLifecycleRuntimeOptions = PrototypeCatalogOptions & {\n remote?: string;\n primaryBranch?: string;\n commitIdentity?: GitCommitIdentity;\n automaticBranchSwitch?: AutomaticBranchSwitchMode;\n gitMode?: GitMode;\n gitRepository?: GitRepository;\n};\n\nexport type PrototypeLifecycleRuntimeContext = ResolvedPrototypeCatalogPaths & {\n repositoryRealPath: string;\n remote: string;\n primaryBranch: string;\n automaticBranchSwitch: AutomaticBranchSwitchMode;\n gitMode: GitMode;\n commitIdentity: Readonly<GitCommitIdentity>;\n gitRepository: GitRepository;\n};\n\nfunction requireNonEmptyOption(value: string | undefined, option: string) {\n if (value !== undefined && value.length === 0) {\n throw new TypeError(`${option} must be a nonempty string`);\n }\n return value;\n}\n\nexport function createPrototypeLifecycleRuntime(\n options: PrototypeLifecycleRuntimeOptions,\n): PrototypeLifecycleRuntimeContext {\n const paths = resolvePrototypeCatalogPaths(options);\n const repositoryRealPath = realpathSync(paths.repositoryRoot);\n const remote =\n requireNonEmptyOption(options.remote, \"remote\") ?? DEFAULT_REMOTE;\n const primaryBranch =\n requireNonEmptyOption(options.primaryBranch, \"primaryBranch\") ??\n DEFAULT_PRIMARY_BRANCH;\n const automaticBranchSwitch = options.automaticBranchSwitch ?? \"enabled\";\n const gitMode = parseGitModeSetting(options.gitMode);\n const commitIdentity = Object.freeze({\n ...DEFAULT_COMMIT_IDENTITY,\n ...options.commitIdentity,\n });\n\n return {\n ...paths,\n repositoryRealPath,\n remote,\n primaryBranch,\n automaticBranchSwitch,\n gitMode,\n commitIdentity,\n gitRepository:\n options.gitRepository ??\n createGitRepository({\n repositoryRoot: paths.repositoryRoot,\n commitIdentity,\n }),\n };\n}\n"]}
|