@catamorphic/git 0.0.0-stage → 0.1.0-alpha.31
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.txt +202 -0
- package/NOTICE.txt +32 -0
- package/dist/checkout-remote-backend.d.ts +12 -0
- package/dist/checkout-remote-backend.d.ts.map +1 -0
- package/dist/file-reads.d.ts +15 -0
- package/dist/file-reads.d.ts.map +1 -0
- package/dist/fs-backend.d.ts +30 -0
- package/dist/fs-backend.d.ts.map +1 -0
- package/dist/fs-remote-backend.d.ts +47 -0
- package/dist/fs-remote-backend.d.ts.map +1 -0
- package/dist/git-object-codec.d.ts +31 -0
- package/dist/git-object-codec.d.ts.map +1 -0
- package/dist/git-sync.d.ts +61 -0
- package/dist/git-sync.d.ts.map +1 -0
- package/dist/in-memory-object-store.d.ts +23 -0
- package/dist/in-memory-object-store.d.ts.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4746 -0
- package/dist/index.js.map +32 -0
- package/dist/migrate-workflow.d.ts +12 -0
- package/dist/migrate-workflow.d.ts.map +1 -0
- package/dist/native-git.d.ts +25 -0
- package/dist/native-git.d.ts.map +1 -0
- package/dist/native-project-repo.d.ts +57 -0
- package/dist/native-project-repo.d.ts.map +1 -0
- package/dist/network-sync.d.ts +58 -0
- package/dist/network-sync.d.ts.map +1 -0
- package/dist/network.d.ts +81 -0
- package/dist/network.d.ts.map +1 -0
- package/dist/object-remote-backend.d.ts +95 -0
- package/dist/object-remote-backend.d.ts.map +1 -0
- package/dist/object-store.d.ts +42 -0
- package/dist/object-store.d.ts.map +1 -0
- package/dist/origin-draft.d.ts +264 -0
- package/dist/origin-draft.d.ts.map +1 -0
- package/dist/origin-objects.d.ts +201 -0
- package/dist/origin-objects.d.ts.map +1 -0
- package/dist/personal-files.d.ts +6 -0
- package/dist/personal-files.d.ts.map +1 -0
- package/dist/project-manager.d.ts +214 -0
- package/dist/project-manager.d.ts.map +1 -0
- package/dist/project-repo.d.ts +79 -0
- package/dist/project-repo.d.ts.map +1 -0
- package/dist/ref-moved-error.d.ts +13 -0
- package/dist/ref-moved-error.d.ts.map +1 -0
- package/dist/ref-names.d.ts +18 -0
- package/dist/ref-names.d.ts.map +1 -0
- package/dist/types.d.ts +276 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/workspace-mirror.d.ts +110 -0
- package/dist/workspace-mirror.d.ts.map +1 -0
- package/package.json +53 -4
- package/src/checkout-remote-backend.ts +154 -0
- package/src/diff3.d.ts +16 -0
- package/src/file-reads.ts +53 -0
- package/src/fs-backend.ts +206 -0
- package/src/fs-remote-backend.ts +252 -0
- package/src/git-object-codec.ts +87 -0
- package/src/git-sync.ts +539 -0
- package/src/in-memory-object-store.ts +60 -0
- package/src/index.ts +110 -0
- package/src/migrate-workflow.ts +30 -0
- package/src/native-git.ts +177 -0
- package/src/native-project-repo.ts +515 -0
- package/src/network-sync.ts +239 -0
- package/src/network.ts +269 -0
- package/src/object-remote-backend.ts +366 -0
- package/src/object-store.ts +41 -0
- package/src/origin-draft.ts +1033 -0
- package/src/origin-objects.ts +906 -0
- package/src/personal-files.ts +57 -0
- package/src/project-manager.ts +743 -0
- package/src/project-repo.ts +800 -0
- package/src/ref-moved-error.ts +18 -0
- package/src/ref-names.ts +47 -0
- package/src/types.ts +289 -0
- package/src/workspace-mirror.ts +400 -0
- package/README.md +0 -3
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import nodeFs from "node:fs";
|
|
2
|
+
import { MANAGED_BRANCH_PREFIX } from "@catamorphic/workflow/project-layout";
|
|
3
|
+
import git from "isomorphic-git";
|
|
4
|
+
import { nativeGit } from "./native-git.js";
|
|
5
|
+
import { NativeProjectRepo } from "./native-project-repo.js";
|
|
6
|
+
import {
|
|
7
|
+
fetchFromRemote,
|
|
8
|
+
pushToRemote,
|
|
9
|
+
type RemoteOwnership,
|
|
10
|
+
} from "./network.js";
|
|
11
|
+
import type { GitCredentials, ProjectRepo } from "./types.js";
|
|
12
|
+
|
|
13
|
+
export type NetworkSyncStatus =
|
|
14
|
+
/** Not on `main` (a deploy owns the tree right now) — nothing done. */
|
|
15
|
+
| "no-op"
|
|
16
|
+
| "up-to-date"
|
|
17
|
+
/** Owned remotes: local commits pushed (including creating a missing
|
|
18
|
+
* remote branch). */
|
|
19
|
+
| "pushed"
|
|
20
|
+
/** Remote commits fast-forwarded into the local tree. */
|
|
21
|
+
| "pulled"
|
|
22
|
+
/** Owned remotes: histories diverged; a clean 3-way merge landed and was
|
|
23
|
+
* pushed. */
|
|
24
|
+
| "merged"
|
|
25
|
+
/** The tree has uncommitted edits and integrating the remote would touch
|
|
26
|
+
* it — deferred untouched; the next sync retries. */
|
|
27
|
+
| "deferred"
|
|
28
|
+
/** Attached remotes: the local branch has commits the remote does not
|
|
29
|
+
* (or the remote branch does not exist). Work never pushes them; they
|
|
30
|
+
* reach the repository as a pull request. */
|
|
31
|
+
| "ahead"
|
|
32
|
+
/** Histories diverged. Owned remotes: the merge conflicts, and local
|
|
33
|
+
* `main` was pushed to `rescueBranch` so no work is stranded. Attached
|
|
34
|
+
* remotes: nothing was merged or pushed; the local commits reach the
|
|
35
|
+
* repository as a pull request. The user's tree is never touched. */
|
|
36
|
+
| "diverged";
|
|
37
|
+
|
|
38
|
+
export interface NetworkSyncResult {
|
|
39
|
+
status: NetworkSyncStatus;
|
|
40
|
+
localSha: string | null;
|
|
41
|
+
remoteSha: string | null;
|
|
42
|
+
rescueBranch?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Converge the dev repo's current branch with a branch on a network git
|
|
47
|
+
* remote. Knows nothing about any particular code host — a URL plus optional
|
|
48
|
+
* credentials is the entire contract (ADR 0044).
|
|
49
|
+
*
|
|
50
|
+
* Both ownerships fetch, report equality, and fast-forward over a clean tree
|
|
51
|
+
* when strictly behind. Only an owned remote is ever written to (ADR 0170):
|
|
52
|
+
* missing remote branch or strictly ahead → push; diverged → 3-way merge
|
|
53
|
+
* that aborts on conflict (a background sync must NEVER leave conflict
|
|
54
|
+
* markers), falling back to pushing a rescue branch. An attached remote
|
|
55
|
+
* reports `ahead` or `diverged` instead and pushes nothing.
|
|
56
|
+
*/
|
|
57
|
+
export async function syncWithNetworkRemote(opts: {
|
|
58
|
+
dev: ProjectRepo;
|
|
59
|
+
url: string;
|
|
60
|
+
credentials?: GitCredentials;
|
|
61
|
+
remoteBranch: string;
|
|
62
|
+
/** Who created the remote; only owned remotes are pushed to. */
|
|
63
|
+
ownership: RemoteOwnership;
|
|
64
|
+
author: { name: string; email: string };
|
|
65
|
+
/** Injectable clock for deterministic rescue-branch names in tests. */
|
|
66
|
+
now?: Date;
|
|
67
|
+
}): Promise<NetworkSyncResult> {
|
|
68
|
+
const { dev } = opts;
|
|
69
|
+
const dir = dev.repoPath;
|
|
70
|
+
const owned = opts.ownership === "owned";
|
|
71
|
+
|
|
72
|
+
const currentBranch = await dev.currentBranch();
|
|
73
|
+
if (dev instanceof NativeProjectRepo) {
|
|
74
|
+
if (currentBranch === "HEAD")
|
|
75
|
+
return { status: "no-op", localSha: null, remoteSha: null };
|
|
76
|
+
const localSha = await dev.resolveRef();
|
|
77
|
+
const auth = opts.credentials
|
|
78
|
+
? { ...opts.credentials, url: opts.url }
|
|
79
|
+
: undefined;
|
|
80
|
+
const target =
|
|
81
|
+
(
|
|
82
|
+
await nativeGit(dir, [
|
|
83
|
+
"config",
|
|
84
|
+
"--get",
|
|
85
|
+
`branch.${currentBranch}.merge`,
|
|
86
|
+
]).catch(() => "")
|
|
87
|
+
)
|
|
88
|
+
.trim()
|
|
89
|
+
.replace(/^refs\/heads\//, "") || currentBranch;
|
|
90
|
+
const remote = (
|
|
91
|
+
await nativeGit(
|
|
92
|
+
dir,
|
|
93
|
+
["ls-remote", opts.url, `refs/heads/${target}`],
|
|
94
|
+
auth,
|
|
95
|
+
)
|
|
96
|
+
).trim();
|
|
97
|
+
const remoteSha = remote.split(/\s+/)[0] || null;
|
|
98
|
+
const push = async (): Promise<NetworkSyncResult> => {
|
|
99
|
+
if (!owned) return { status: "ahead", localSha, remoteSha };
|
|
100
|
+
await pushToRemote({
|
|
101
|
+
repoPath: dir,
|
|
102
|
+
native: true,
|
|
103
|
+
url: opts.url,
|
|
104
|
+
credentials: opts.credentials,
|
|
105
|
+
ownership: opts.ownership,
|
|
106
|
+
ref: currentBranch,
|
|
107
|
+
remoteBranch: target,
|
|
108
|
+
});
|
|
109
|
+
return { status: "pushed", localSha, remoteSha: localSha };
|
|
110
|
+
};
|
|
111
|
+
if (!remoteSha) return push();
|
|
112
|
+
if (remoteSha === localSha)
|
|
113
|
+
return { status: "up-to-date", localSha, remoteSha };
|
|
114
|
+
await nativeGit(dir, ["fetch", opts.url, `refs/heads/${target}`], auth);
|
|
115
|
+
if (
|
|
116
|
+
await nativeGit(dir, [
|
|
117
|
+
"merge-base",
|
|
118
|
+
"--is-ancestor",
|
|
119
|
+
remoteSha,
|
|
120
|
+
localSha,
|
|
121
|
+
]).then(
|
|
122
|
+
() => true,
|
|
123
|
+
() => false,
|
|
124
|
+
)
|
|
125
|
+
) {
|
|
126
|
+
return push();
|
|
127
|
+
}
|
|
128
|
+
if (
|
|
129
|
+
!(await nativeGit(dir, [
|
|
130
|
+
"merge-base",
|
|
131
|
+
"--is-ancestor",
|
|
132
|
+
localSha,
|
|
133
|
+
remoteSha,
|
|
134
|
+
]).then(
|
|
135
|
+
() => true,
|
|
136
|
+
() => false,
|
|
137
|
+
))
|
|
138
|
+
) {
|
|
139
|
+
return { status: "diverged", localSha, remoteSha };
|
|
140
|
+
}
|
|
141
|
+
if ((await dev.status()).dirty)
|
|
142
|
+
return { status: "deferred", localSha, remoteSha };
|
|
143
|
+
await nativeGit(dir, ["merge", "--ff-only", remoteSha]);
|
|
144
|
+
return { status: "pulled", localSha: remoteSha, remoteSha };
|
|
145
|
+
}
|
|
146
|
+
if (currentBranch !== "main") {
|
|
147
|
+
return { status: "no-op", localSha: null, remoteSha: null };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const localSha = await dev.resolveRef("refs/heads/main");
|
|
151
|
+
const { sha: remoteSha } = await fetchFromRemote({
|
|
152
|
+
repoPath: dir,
|
|
153
|
+
url: opts.url,
|
|
154
|
+
credentials: opts.credentials,
|
|
155
|
+
branch: opts.remoteBranch,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const push = (remoteBranch: string) =>
|
|
159
|
+
pushToRemote({
|
|
160
|
+
repoPath: dir,
|
|
161
|
+
url: opts.url,
|
|
162
|
+
credentials: opts.credentials,
|
|
163
|
+
ownership: opts.ownership,
|
|
164
|
+
ref: "main",
|
|
165
|
+
remoteBranch,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
if (!remoteSha) {
|
|
169
|
+
if (!owned) return { status: "ahead", localSha, remoteSha };
|
|
170
|
+
await push(opts.remoteBranch);
|
|
171
|
+
return { status: "pushed", localSha, remoteSha: localSha };
|
|
172
|
+
}
|
|
173
|
+
if (remoteSha === localSha) {
|
|
174
|
+
return { status: "up-to-date", localSha, remoteSha };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const isDescendent = (oid: string, ancestor: string) =>
|
|
178
|
+
git
|
|
179
|
+
.isDescendent({ fs: nodeFs, dir, oid, ancestor, depth: -1 })
|
|
180
|
+
.catch(() => false);
|
|
181
|
+
|
|
182
|
+
if (await isDescendent(localSha, remoteSha)) {
|
|
183
|
+
if (!owned) return { status: "ahead", localSha, remoteSha };
|
|
184
|
+
await push(opts.remoteBranch);
|
|
185
|
+
return { status: "pushed", localSha, remoteSha: localSha };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const behind = await isDescendent(remoteSha, localSha);
|
|
189
|
+
if (!behind && !owned) return { status: "diverged", localSha, remoteSha };
|
|
190
|
+
|
|
191
|
+
const { dirty } = await dev.status();
|
|
192
|
+
if (dirty) return { status: "deferred", localSha, remoteSha };
|
|
193
|
+
|
|
194
|
+
if (behind) {
|
|
195
|
+
await git.writeRef({
|
|
196
|
+
fs: nodeFs,
|
|
197
|
+
dir,
|
|
198
|
+
ref: "refs/heads/main",
|
|
199
|
+
value: remoteSha,
|
|
200
|
+
force: true,
|
|
201
|
+
});
|
|
202
|
+
await git.checkout({ fs: nodeFs, dir, ref: "main", force: true });
|
|
203
|
+
return { status: "pulled", localSha: remoteSha, remoteSha };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Diverged histories on an owned remote.
|
|
207
|
+
try {
|
|
208
|
+
const merge = await git.merge({
|
|
209
|
+
fs: nodeFs,
|
|
210
|
+
dir,
|
|
211
|
+
ours: "main",
|
|
212
|
+
theirs: remoteSha,
|
|
213
|
+
author: opts.author,
|
|
214
|
+
committer: opts.author,
|
|
215
|
+
fastForward: true,
|
|
216
|
+
abortOnConflict: true,
|
|
217
|
+
message: `Merge remote ${opts.remoteBranch} into main`,
|
|
218
|
+
});
|
|
219
|
+
await git.checkout({ fs: nodeFs, dir, ref: "main", force: true });
|
|
220
|
+
const mergedSha = merge.oid ?? (await dev.resolveRef("refs/heads/main"));
|
|
221
|
+
await push(opts.remoteBranch);
|
|
222
|
+
return { status: "merged", localSha: mergedSha, remoteSha: mergedSha };
|
|
223
|
+
} catch (err) {
|
|
224
|
+
if ((err as { code?: string })?.code !== "MergeConflictError") throw err;
|
|
225
|
+
const rescueBranch = rescueBranchName(opts.now ?? new Date());
|
|
226
|
+
await push(rescueBranch);
|
|
227
|
+
return { status: "diverged", localSha, remoteSha, rescueBranch };
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** `work/diverged-YYYY-MM-DD_HH-mm` — groups rescue pushes on the host. */
|
|
232
|
+
function rescueBranchName(now: Date): string {
|
|
233
|
+
const pad = (n: number) => String(n).padStart(2, "0");
|
|
234
|
+
return `${MANAGED_BRANCH_PREFIX}diverged-${now.getUTCFullYear()}-${pad(
|
|
235
|
+
now.getUTCMonth() + 1,
|
|
236
|
+
)}-${pad(now.getUTCDate())}_${pad(now.getUTCHours())}-${pad(
|
|
237
|
+
now.getUTCMinutes(),
|
|
238
|
+
)}`;
|
|
239
|
+
}
|
package/src/network.ts
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import nodeFs from "node:fs";
|
|
2
|
+
import { MANAGED_BRANCH_PREFIX } from "@catamorphic/workflow/project-layout";
|
|
3
|
+
import git from "isomorphic-git";
|
|
4
|
+
import http from "isomorphic-git/http/node";
|
|
5
|
+
import { nativeGit } from "./native-git.js";
|
|
6
|
+
import type { GitCredentials } from "./types.js";
|
|
7
|
+
|
|
8
|
+
export interface CloneFromRemoteOptions {
|
|
9
|
+
native?: boolean;
|
|
10
|
+
/** Directory that already holds an initialized (empty) git repo. */
|
|
11
|
+
repoPath: string;
|
|
12
|
+
url: string;
|
|
13
|
+
credentials?: GitCredentials;
|
|
14
|
+
/** Branch to clone; defaults to the remote's default branch. */
|
|
15
|
+
branch?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function onAuthFor(credentials?: GitCredentials) {
|
|
19
|
+
return credentials
|
|
20
|
+
? () => ({
|
|
21
|
+
username: credentials.username,
|
|
22
|
+
password: credentials.password,
|
|
23
|
+
})
|
|
24
|
+
: undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Populate a freshly-initialized repo from a network git remote (e.g. a
|
|
29
|
+
* GitHub repository). The remote's history lands on the local `main` branch
|
|
30
|
+
* regardless of the remote's branch name — catamorphic's internal sync
|
|
31
|
+
* (`git-sync`, drafts, checkout seeding) assumes `main` throughout, so the remote
|
|
32
|
+
* branch name is only remembered by the caller for push-back.
|
|
33
|
+
*
|
|
34
|
+
* Returns the checked-out sha and the remote's branch name.
|
|
35
|
+
*/
|
|
36
|
+
export async function cloneFromRemote(
|
|
37
|
+
opts: CloneFromRemoteOptions,
|
|
38
|
+
): Promise<{ sha: string; remoteBranch: string }> {
|
|
39
|
+
if (opts.native) {
|
|
40
|
+
const auth = opts.credentials
|
|
41
|
+
? { ...opts.credentials, url: opts.url }
|
|
42
|
+
: undefined;
|
|
43
|
+
await nativeGit(opts.repoPath, ["remote", "add", "origin", opts.url]);
|
|
44
|
+
await nativeGit(opts.repoPath, ["fetch", "--tags", "origin"], auth);
|
|
45
|
+
const remoteBranch =
|
|
46
|
+
opts.branch ??
|
|
47
|
+
(
|
|
48
|
+
await nativeGit(
|
|
49
|
+
opts.repoPath,
|
|
50
|
+
["ls-remote", "--symref", "origin", "HEAD"],
|
|
51
|
+
auth,
|
|
52
|
+
)
|
|
53
|
+
).match(/ref: refs\/heads\/(.+)\tHEAD/)?.[1];
|
|
54
|
+
if (!remoteBranch)
|
|
55
|
+
throw new Error("The repository has no default branch to check out");
|
|
56
|
+
await nativeGit(opts.repoPath, [
|
|
57
|
+
"checkout",
|
|
58
|
+
"-B",
|
|
59
|
+
remoteBranch,
|
|
60
|
+
"--track",
|
|
61
|
+
`origin/${remoteBranch}`,
|
|
62
|
+
]);
|
|
63
|
+
await nativeGit(opts.repoPath, [
|
|
64
|
+
"symbolic-ref",
|
|
65
|
+
"refs/remotes/origin/HEAD",
|
|
66
|
+
`refs/remotes/origin/${remoteBranch}`,
|
|
67
|
+
]);
|
|
68
|
+
return {
|
|
69
|
+
sha: (await nativeGit(opts.repoPath, ["rev-parse", "HEAD"])).trim(),
|
|
70
|
+
remoteBranch,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
await git.addRemote({
|
|
74
|
+
fs: nodeFs,
|
|
75
|
+
dir: opts.repoPath,
|
|
76
|
+
remote: "origin",
|
|
77
|
+
url: opts.url,
|
|
78
|
+
force: true,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const result = await git.fetch({
|
|
82
|
+
fs: nodeFs,
|
|
83
|
+
http,
|
|
84
|
+
dir: opts.repoPath,
|
|
85
|
+
remote: "origin",
|
|
86
|
+
...(opts.branch ? { ref: opts.branch } : {}),
|
|
87
|
+
singleBranch: true,
|
|
88
|
+
tags: false,
|
|
89
|
+
onAuth: onAuthFor(opts.credentials),
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
const sha = result.fetchHead;
|
|
93
|
+
if (!sha) {
|
|
94
|
+
throw new Error(`Remote '${opts.url}' has no commits to clone`);
|
|
95
|
+
}
|
|
96
|
+
const remoteBranch =
|
|
97
|
+
opts.branch ??
|
|
98
|
+
(result.defaultBranch
|
|
99
|
+
? result.defaultBranch.replace(/^refs\/heads\//, "")
|
|
100
|
+
: "main");
|
|
101
|
+
|
|
102
|
+
await git.writeRef({
|
|
103
|
+
fs: nodeFs,
|
|
104
|
+
dir: opts.repoPath,
|
|
105
|
+
ref: "refs/heads/main",
|
|
106
|
+
value: sha,
|
|
107
|
+
force: true,
|
|
108
|
+
});
|
|
109
|
+
await git.checkout({
|
|
110
|
+
fs: nodeFs,
|
|
111
|
+
dir: opts.repoPath,
|
|
112
|
+
ref: "main",
|
|
113
|
+
force: true,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
return { sha, remoteBranch };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Who created a linked network remote (ADR 0170). `owned`: Work created the
|
|
121
|
+
* repository, so ADR 0044 sync may update its tracked branch. `attached`: the
|
|
122
|
+
* repository existed before Work (an opened folder with an origin, a clone,
|
|
123
|
+
* an imported company repository). Work never updates its default branch or
|
|
124
|
+
* any branch it did not create there.
|
|
125
|
+
*/
|
|
126
|
+
export type RemoteOwnership = "owned" | "attached";
|
|
127
|
+
|
|
128
|
+
export class RemotePushRefusedError extends Error {
|
|
129
|
+
constructor(
|
|
130
|
+
readonly remoteBranch: string,
|
|
131
|
+
reason: string,
|
|
132
|
+
) {
|
|
133
|
+
super(`Work will not push '${remoteBranch}': ${reason}`);
|
|
134
|
+
this.name = "RemotePushRefusedError";
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Whether `branch` is one Work creates: `work/<something>`. */
|
|
139
|
+
export function isManagedBranch(branch: string): boolean {
|
|
140
|
+
return (
|
|
141
|
+
branch.startsWith(MANAGED_BRANCH_PREFIX) &&
|
|
142
|
+
branch.length > MANAGED_BRANCH_PREFIX.length
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The one push rule (ADR 0170). On an attached remote Work may only create or
|
|
148
|
+
* fast-forward branches under `work/`; the default branch and every other
|
|
149
|
+
* branch belong to the people who share the repository, and history there is
|
|
150
|
+
* never rewritten. Owned remotes are unrestricted.
|
|
151
|
+
*/
|
|
152
|
+
export function assertPushAllowed(input: {
|
|
153
|
+
ownership: RemoteOwnership;
|
|
154
|
+
remoteBranch: string;
|
|
155
|
+
force?: boolean;
|
|
156
|
+
}): void {
|
|
157
|
+
if (input.ownership === "owned") return;
|
|
158
|
+
if (!isManagedBranch(input.remoteBranch)) {
|
|
159
|
+
throw new RemotePushRefusedError(
|
|
160
|
+
input.remoteBranch,
|
|
161
|
+
`this repository is attached, so Work only pushes branches it creates under '${MANAGED_BRANCH_PREFIX}'. Open a pull request instead.`,
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
if (input.force) {
|
|
165
|
+
throw new RemotePushRefusedError(
|
|
166
|
+
input.remoteBranch,
|
|
167
|
+
"Work never rewrites history in an attached repository.",
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Push a local ref to a network git remote, bypassing the repo's configured
|
|
174
|
+
* `origin` (which catamorphic points at its internal remote backend). Every
|
|
175
|
+
* network push goes through here, so {@link assertPushAllowed} decides what
|
|
176
|
+
* may reach a remote before anything is sent.
|
|
177
|
+
*/
|
|
178
|
+
export async function pushToRemote(opts: {
|
|
179
|
+
repoPath: string;
|
|
180
|
+
native?: boolean;
|
|
181
|
+
url: string;
|
|
182
|
+
credentials?: GitCredentials;
|
|
183
|
+
/** Who created the remote: attached remotes only accept Work's branches. */
|
|
184
|
+
ownership: RemoteOwnership;
|
|
185
|
+
/** Local ref to push. */
|
|
186
|
+
ref: string;
|
|
187
|
+
/** Branch name on the remote (a plain name, not `refs/heads/...`). */
|
|
188
|
+
remoteBranch: string;
|
|
189
|
+
force?: boolean;
|
|
190
|
+
}): Promise<void> {
|
|
191
|
+
const { ref, remoteBranch } = opts;
|
|
192
|
+
// Plain names only: a `+`, `:` or leading `-` would smuggle a forced or
|
|
193
|
+
// retargeted refspec past the rule below.
|
|
194
|
+
for (const name of [ref, remoteBranch]) {
|
|
195
|
+
if (!name || /[:\s+]|^-|^refs\/heads\//.test(name)) {
|
|
196
|
+
throw new RemotePushRefusedError(name, "not a plain branch or ref name.");
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
assertPushAllowed(opts);
|
|
200
|
+
if (opts.native) {
|
|
201
|
+
await nativeGit(
|
|
202
|
+
opts.repoPath,
|
|
203
|
+
[
|
|
204
|
+
"push",
|
|
205
|
+
...(opts.force ? ["--force-with-lease"] : []),
|
|
206
|
+
opts.url,
|
|
207
|
+
`${ref}:refs/heads/${remoteBranch}`,
|
|
208
|
+
],
|
|
209
|
+
opts.credentials ? { ...opts.credentials, url: opts.url } : undefined,
|
|
210
|
+
);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
await git.push({
|
|
214
|
+
fs: nodeFs,
|
|
215
|
+
http,
|
|
216
|
+
dir: opts.repoPath,
|
|
217
|
+
url: opts.url,
|
|
218
|
+
ref,
|
|
219
|
+
remoteRef: `refs/heads/${remoteBranch}`,
|
|
220
|
+
force: opts.force ?? false,
|
|
221
|
+
onAuth: onAuthFor(opts.credentials),
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Fetch a branch from a network remote into a tracking ref without touching
|
|
227
|
+
* the working tree. Returns the fetched sha (null when the remote branch does
|
|
228
|
+
* not exist).
|
|
229
|
+
*/
|
|
230
|
+
export async function fetchFromRemote(opts: {
|
|
231
|
+
repoPath: string;
|
|
232
|
+
url: string;
|
|
233
|
+
credentials?: GitCredentials;
|
|
234
|
+
branch: string;
|
|
235
|
+
}): Promise<{ sha: string | null }> {
|
|
236
|
+
// Disposable origin snapshots have no network remote/refspec. Keep this
|
|
237
|
+
// tracking namespace separate from the host's internal origin.
|
|
238
|
+
const remote = "catamorphic-network";
|
|
239
|
+
await git.addRemote({
|
|
240
|
+
fs: nodeFs,
|
|
241
|
+
dir: opts.repoPath,
|
|
242
|
+
remote,
|
|
243
|
+
url: opts.url,
|
|
244
|
+
force: true,
|
|
245
|
+
});
|
|
246
|
+
try {
|
|
247
|
+
const result = await git.fetch({
|
|
248
|
+
fs: nodeFs,
|
|
249
|
+
http,
|
|
250
|
+
dir: opts.repoPath,
|
|
251
|
+
url: opts.url,
|
|
252
|
+
remote,
|
|
253
|
+
ref: opts.branch,
|
|
254
|
+
remoteRef: opts.branch,
|
|
255
|
+
singleBranch: true,
|
|
256
|
+
tags: false,
|
|
257
|
+
onAuth: onAuthFor(opts.credentials),
|
|
258
|
+
});
|
|
259
|
+
return { sha: result.fetchHead };
|
|
260
|
+
} catch (err) {
|
|
261
|
+
if (
|
|
262
|
+
err instanceof git.Errors.NotFoundError &&
|
|
263
|
+
err.data.what === opts.branch
|
|
264
|
+
) {
|
|
265
|
+
return { sha: null };
|
|
266
|
+
}
|
|
267
|
+
throw err;
|
|
268
|
+
}
|
|
269
|
+
}
|