@catamorphic/git 0.1.0-alpha.39 → 0.1.0-alpha.42
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/NOTICE.txt +7 -0
- package/dist/git-sync.d.ts +2 -0
- package/dist/git-sync.d.ts.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1218 -88
- package/dist/index.js.map +13 -11
- package/dist/network.d.ts +43 -4
- package/dist/network.d.ts.map +1 -1
- package/dist/origin-draft.d.ts +10 -2
- package/dist/origin-draft.d.ts.map +1 -1
- package/dist/origin-objects.d.ts +5 -0
- package/dist/origin-objects.d.ts.map +1 -1
- package/dist/project-manager.d.ts +96 -8
- package/dist/project-manager.d.ts.map +1 -1
- package/dist/project-repo.d.ts.map +1 -1
- package/dist/session-checkpoint.d.ts +75 -0
- package/dist/session-checkpoint.d.ts.map +1 -0
- package/dist/workspace-mirror.d.ts +4 -1
- package/dist/workspace-mirror.d.ts.map +1 -1
- package/dist/workspace-save.d.ts +170 -0
- package/dist/workspace-save.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/git-sync.ts +7 -0
- package/src/index.ts +20 -0
- package/src/network.ts +210 -7
- package/src/origin-draft.ts +42 -13
- package/src/origin-objects.ts +38 -12
- package/src/project-manager.ts +400 -46
- package/src/project-repo.ts +18 -3
- package/src/session-checkpoint.ts +210 -0
- package/src/workspace-mirror.ts +31 -30
- package/src/workspace-save.ts +999 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import type { DraftChange } from "./origin-draft.js";
|
|
2
|
+
/**
|
|
3
|
+
* Saving a sandbox's workspace (ADRs 0343, 0344). The sandbox packs the
|
|
4
|
+
* files that changed since the last save, with a manifest naming each path,
|
|
5
|
+
* its Git mode and its blob, and the commits its branch gained, into one
|
|
6
|
+
* Git pack. Everything read here came from the sandbox and is treated as
|
|
7
|
+
* hostile: the pack is indexed in a repository of its own that is thrown
|
|
8
|
+
* away, every path and every tree entry is checked before anything is
|
|
9
|
+
* written, and no write ever follows a link.
|
|
10
|
+
*/
|
|
11
|
+
/** A save that was refused, or whose pack or manifest could not be read. */
|
|
12
|
+
export declare class WorkspaceSaveError extends Error {
|
|
13
|
+
constructor(message: string, options?: ErrorOptions);
|
|
14
|
+
}
|
|
15
|
+
/** One change of a save: a deletion, a file with its mode, or a link. */
|
|
16
|
+
export type SavedChange = {
|
|
17
|
+
path: string;
|
|
18
|
+
kind: "deleted";
|
|
19
|
+
} | {
|
|
20
|
+
path: string;
|
|
21
|
+
kind: "file";
|
|
22
|
+
mode: "100644" | "100755";
|
|
23
|
+
oid: string;
|
|
24
|
+
} | {
|
|
25
|
+
path: string;
|
|
26
|
+
kind: "link";
|
|
27
|
+
oid: string;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Refuse a path a save may never name: one that is absolute, leaves the
|
|
31
|
+
* checkout, or passes through a Git directory. Git writes none of these
|
|
32
|
+
* into a tree, so a manifest holding one was not made by Git.
|
|
33
|
+
*/
|
|
34
|
+
export declare function assertSavedPath(filePath: string): void;
|
|
35
|
+
/**
|
|
36
|
+
* Read a save's manifest: the NUL-separated records of
|
|
37
|
+
* `git diff-tree -r --no-renames --raw --no-abbrev -z` between the last
|
|
38
|
+
* saved tree and the snapshot. Throws {@link WorkspaceSaveError} for a
|
|
39
|
+
* record Git would not write or a path a save may not name. Nested
|
|
40
|
+
* repositories (gitlinks) have no blob and are left out.
|
|
41
|
+
*/
|
|
42
|
+
export declare function parseSavedChanges(manifest: Uint8Array): SavedChange[];
|
|
43
|
+
/**
|
|
44
|
+
* Refuse a tree a saved commit may not hold: an entry whose name is not one
|
|
45
|
+
* path segment a checkout may contain, or whose mode is not a folder, a
|
|
46
|
+
* file, a link or a nested repository. Git's own trees never hold one.
|
|
47
|
+
*/
|
|
48
|
+
export declare function assertSavedTree(input: {
|
|
49
|
+
/** The tree object's raw content: `<mode> <name>NUL<id>` per entry. */
|
|
50
|
+
tree: Uint8Array;
|
|
51
|
+
/** Bytes of each entry's id: 20 (SHA-1) or 32 (SHA-256). */
|
|
52
|
+
idBytes: 20 | 32;
|
|
53
|
+
}): void;
|
|
54
|
+
/**
|
|
55
|
+
* A pack a sandbox sent, indexed in a repository of its own under the
|
|
56
|
+
* host's temporary directory. Native Git verifies the pack's checksum and
|
|
57
|
+
* every object's id as it indexes, so bytes damaged or cut short on the
|
|
58
|
+
* way are refused, never saved. Dispose it when the save is done.
|
|
59
|
+
*/
|
|
60
|
+
export declare class ReceivedPack {
|
|
61
|
+
private readonly directory;
|
|
62
|
+
/** Bytes of an object id in the sandbox's repository. */
|
|
63
|
+
private readonly idBytes;
|
|
64
|
+
private reader;
|
|
65
|
+
private constructor();
|
|
66
|
+
/** Index the pack arriving as `chunks`, in order. */
|
|
67
|
+
static receive(input: {
|
|
68
|
+
/** The sandbox repository's object format, by its ids' length. */
|
|
69
|
+
objectFormat: "sha1" | "sha256";
|
|
70
|
+
chunks: AsyncIterable<Uint8Array>;
|
|
71
|
+
/**
|
|
72
|
+
* A repository on this machine whose objects the pack's commits may
|
|
73
|
+
* build on (the session's copy): read through, never written.
|
|
74
|
+
*/
|
|
75
|
+
borrow?: string;
|
|
76
|
+
}): Promise<ReceivedPack>;
|
|
77
|
+
/**
|
|
78
|
+
* Make sure every id names a file's content in the pack, and that
|
|
79
|
+
* together they hold at most `maxBytes`, before anything is written.
|
|
80
|
+
*/
|
|
81
|
+
assertBlobs(input: {
|
|
82
|
+
oids: readonly string[];
|
|
83
|
+
maxBytes: number;
|
|
84
|
+
}): Promise<void>;
|
|
85
|
+
/**
|
|
86
|
+
* Check the commits a save says its branch gained (ADR 0344), before any
|
|
87
|
+
* of them is kept: `head` and everything it reaches that `known` (the
|
|
88
|
+
* workspace's base and its last saved head) does not. Every commit must
|
|
89
|
+
* be here whole, with history that ends at `known`; every tree may name
|
|
90
|
+
* only what a checkout may hold; and together they are bounded. Returns
|
|
91
|
+
* the new commits, newest first.
|
|
92
|
+
*/
|
|
93
|
+
assertHistory(input: {
|
|
94
|
+
head: string;
|
|
95
|
+
known: readonly string[];
|
|
96
|
+
maxCommits: number;
|
|
97
|
+
maxBytes: number;
|
|
98
|
+
}): Promise<{
|
|
99
|
+
commits: string[];
|
|
100
|
+
}>;
|
|
101
|
+
/**
|
|
102
|
+
* Keep checked commits (ADR 0344): `head` and what it reaches beyond
|
|
103
|
+
* `known`, unpacked loose into the repository at `repoPath`, so no pack
|
|
104
|
+
* stays there. Git checks each object and its links again as it writes.
|
|
105
|
+
*/
|
|
106
|
+
unpackHistory(input: {
|
|
107
|
+
repoPath: string;
|
|
108
|
+
head: string;
|
|
109
|
+
known: readonly string[];
|
|
110
|
+
}): Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* Build the checkpoint tree from its previous tree and checked file
|
|
113
|
+
* changes. Git's private index keeps paths, modes and bytes without
|
|
114
|
+
* re-reading a host working tree or applying a second set of filters.
|
|
115
|
+
*/
|
|
116
|
+
snapshotTree(input: {
|
|
117
|
+
repoPath: string;
|
|
118
|
+
previous: string;
|
|
119
|
+
changes: readonly SavedChange[];
|
|
120
|
+
containLinks: boolean;
|
|
121
|
+
}): Promise<string>;
|
|
122
|
+
private unpack;
|
|
123
|
+
/** One blob's bytes, refused when larger than `maxBytes`. */
|
|
124
|
+
read(input: {
|
|
125
|
+
oid: string;
|
|
126
|
+
maxBytes: number;
|
|
127
|
+
}): Promise<Uint8Array>;
|
|
128
|
+
/** One object's bytes, refused when it is not of `type`. */
|
|
129
|
+
private readObject;
|
|
130
|
+
/** Stream one blob's bytes to `write`, in order. */
|
|
131
|
+
copy(input: {
|
|
132
|
+
oid: string;
|
|
133
|
+
write: (chunk: Uint8Array) => Promise<void>;
|
|
134
|
+
}): Promise<void>;
|
|
135
|
+
private stream;
|
|
136
|
+
/** The manifest blob, parsed and checked. */
|
|
137
|
+
changes(input: {
|
|
138
|
+
manifest: string;
|
|
139
|
+
}): Promise<SavedChange[]>;
|
|
140
|
+
dispose(): Promise<void>;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Apply a save to a checkout on this machine, exactly: each file's bytes
|
|
144
|
+
* and executable bit, each link as a link, each deletion. Deletions go
|
|
145
|
+
* first, so a file may take a folder's place and a folder a file's. Nothing
|
|
146
|
+
* is written outside `repoPath`: parents are real folders or the save is
|
|
147
|
+
* refused, and what stands at a path is removed, never opened, so a link
|
|
148
|
+
* saved earlier cannot redirect a later write.
|
|
149
|
+
*/
|
|
150
|
+
export declare function writeSavedChanges(input: {
|
|
151
|
+
repoPath: string;
|
|
152
|
+
changes: readonly SavedChange[];
|
|
153
|
+
pack: ReceivedPack;
|
|
154
|
+
/**
|
|
155
|
+
* The checkout's file APIs follow links (a person's own folder): a link
|
|
156
|
+
* that could lead outside it is refused, so nothing read or written
|
|
157
|
+
* there later goes where the sandbox pointed.
|
|
158
|
+
*/
|
|
159
|
+
containLinks?: boolean;
|
|
160
|
+
}): Promise<void>;
|
|
161
|
+
/**
|
|
162
|
+
* A save as changes to a member's draft in the origin (ADR 0191): the same
|
|
163
|
+
* bytes, modes and links, written as Git objects. Pass them to
|
|
164
|
+
* `OriginDraftRepo.write` with `replaceLinks`.
|
|
165
|
+
*/
|
|
166
|
+
export declare function savedDraftChanges(input: {
|
|
167
|
+
changes: readonly SavedChange[];
|
|
168
|
+
pack: ReceivedPack;
|
|
169
|
+
}): Promise<DraftChange[]>;
|
|
170
|
+
//# sourceMappingURL=workspace-save.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"workspace-save.d.ts","sourceRoot":"","sources":["../src/workspace-save.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAGrD;;;;;;;;GAQG;AAEH,4EAA4E;AAC5E,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,YAAY,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,EAGlD;CACF;AAED,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GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAIjE;IAEK,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAI7B;CACF;AA0QD;;;;;;;GAOG;AACH,wBAAsB,iBAAiB,CAAC,KAAK,EAAE;IAC7C,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,SAAS,WAAW,EAAE,CAAC;IAChC,IAAI,EAAE,YAAY,CAAC;IACnB;;;;OAIG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB,GAAG,OAAO,CAAC,IAAI,CAAC,CA6DhB;AAcD;;;;GAIG;AACH,wBAAsB,iBAAiB,CAAC,KAAK,EAAE;IAC7C,OAAO,EAAE,SAAS,WAAW,EAAE,CAAC;IAChC,IAAI,EAAE,YAAY,CAAC;CACpB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAyBzB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@catamorphic/git",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.42",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
]
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@catamorphic/workflow": "0.1.0-alpha.
|
|
26
|
+
"@catamorphic/workflow": "0.1.0-alpha.42",
|
|
27
27
|
"diff3": "0.0.4",
|
|
28
28
|
"ignore": "^7.0.14",
|
|
29
29
|
"isomorphic-git": "^1.44.12"
|
package/src/git-sync.ts
CHANGED
|
@@ -37,6 +37,8 @@ export interface PushOpts {
|
|
|
37
37
|
remoteBranch?: string;
|
|
38
38
|
/** SHA on the dev side to push. Defaults to the tip of the current branch. */
|
|
39
39
|
localSha?: string;
|
|
40
|
+
/** The published ref from which this update was prepared, when known. */
|
|
41
|
+
expected?: string;
|
|
40
42
|
/**
|
|
41
43
|
* Move the branch even when it is not a fast-forward. Only for branches
|
|
42
44
|
* Work owns outright, such as a session's `sessions/<id>` whose base moved.
|
|
@@ -76,6 +78,11 @@ export async function push(opts: PushOpts): Promise<{ sha: string }> {
|
|
|
76
78
|
return { sha: localSha };
|
|
77
79
|
}
|
|
78
80
|
|
|
81
|
+
if (opts.expected !== undefined && currentRemote !== opts.expected)
|
|
82
|
+
throw new Error(
|
|
83
|
+
"The workspace checkpoint changed before it could be published.",
|
|
84
|
+
);
|
|
85
|
+
|
|
79
86
|
if (currentRemote && !opts.force) {
|
|
80
87
|
const fastForward = await isAncestor({
|
|
81
88
|
dev: opts.dev,
|
package/src/index.ts
CHANGED
|
@@ -23,6 +23,8 @@ export {
|
|
|
23
23
|
cloneFromRemote,
|
|
24
24
|
fetchFromRemote,
|
|
25
25
|
isManagedBranch,
|
|
26
|
+
type PushedBranch,
|
|
27
|
+
pushOwnBranch,
|
|
26
28
|
pushToRemote,
|
|
27
29
|
type RemoteOwnership,
|
|
28
30
|
RemotePushRefusedError,
|
|
@@ -76,6 +78,14 @@ export {
|
|
|
76
78
|
InvalidRefNameError,
|
|
77
79
|
isValidRefName,
|
|
78
80
|
} from "./ref-names.js";
|
|
81
|
+
export {
|
|
82
|
+
currentSessionWork,
|
|
83
|
+
isSessionBranchName,
|
|
84
|
+
parseCheckpointMessage,
|
|
85
|
+
readSessionWork,
|
|
86
|
+
type SessionWork,
|
|
87
|
+
sessionBranchName,
|
|
88
|
+
} from "./session-checkpoint.js";
|
|
79
89
|
export type {
|
|
80
90
|
BranchInfo,
|
|
81
91
|
CloneSource,
|
|
@@ -108,3 +118,13 @@ export {
|
|
|
108
118
|
unpinMirrorRef,
|
|
109
119
|
type WorkspaceBase,
|
|
110
120
|
} from "./workspace-mirror.js";
|
|
121
|
+
export {
|
|
122
|
+
assertSavedPath,
|
|
123
|
+
assertSavedTree,
|
|
124
|
+
parseSavedChanges,
|
|
125
|
+
ReceivedPack,
|
|
126
|
+
type SavedChange,
|
|
127
|
+
savedDraftChanges,
|
|
128
|
+
WorkspaceSaveError,
|
|
129
|
+
writeSavedChanges,
|
|
130
|
+
} from "./workspace-save.js";
|
package/src/network.ts
CHANGED
|
@@ -2,7 +2,8 @@ import nodeFs from "node:fs";
|
|
|
2
2
|
import { MANAGED_BRANCH_PREFIX } from "@catamorphic/workflow/project-layout";
|
|
3
3
|
import git from "isomorphic-git";
|
|
4
4
|
import http from "isomorphic-git/http/node";
|
|
5
|
-
import { nativeGit } from "./native-git.js";
|
|
5
|
+
import { type NativeGitAuth, nativeGit } from "./native-git.js";
|
|
6
|
+
import { isValidRefName } from "./ref-names.js";
|
|
6
7
|
import type { GitCredentials } from "./types.js";
|
|
7
8
|
|
|
8
9
|
export interface CloneFromRemoteOptions {
|
|
@@ -144,15 +145,20 @@ export function isManagedBranch(branch: string): boolean {
|
|
|
144
145
|
}
|
|
145
146
|
|
|
146
147
|
/**
|
|
147
|
-
* The one push rule (
|
|
148
|
-
* fast-forward branches under `work/`; the default branch and
|
|
149
|
-
* branch belong to the people who share the repository
|
|
150
|
-
*
|
|
148
|
+
* The one push rule (ADRs 0170, 0345). On an attached remote Work may only
|
|
149
|
+
* create or fast-forward branches under `work/`; the default branch and
|
|
150
|
+
* every other branch belong to the people who share the repository.
|
|
151
|
+
* History there is rewritten in one case only: a chat force-updates its
|
|
152
|
+
* own `work/` branch with a lease on the commit it last pushed, so
|
|
153
|
+
* anything anyone else pushed there refuses the push instead of being
|
|
154
|
+
* overwritten. Owned remotes are unrestricted.
|
|
151
155
|
*/
|
|
152
156
|
export function assertPushAllowed(input: {
|
|
153
157
|
ownership: RemoteOwnership;
|
|
154
158
|
remoteBranch: string;
|
|
155
159
|
force?: boolean;
|
|
160
|
+
/** The push names the commit it expects to replace. */
|
|
161
|
+
lease?: boolean;
|
|
156
162
|
}): void {
|
|
157
163
|
if (input.ownership === "owned") return;
|
|
158
164
|
if (!isManagedBranch(input.remoteBranch)) {
|
|
@@ -161,7 +167,7 @@ export function assertPushAllowed(input: {
|
|
|
161
167
|
`this repository is attached, so Work only pushes branches it creates under '${MANAGED_BRANCH_PREFIX}'. Open a pull request instead.`,
|
|
162
168
|
);
|
|
163
169
|
}
|
|
164
|
-
if (input.force) {
|
|
170
|
+
if (input.force && !input.lease) {
|
|
165
171
|
throw new RemotePushRefusedError(
|
|
166
172
|
input.remoteBranch,
|
|
167
173
|
"Work never rewrites history in an attached repository.",
|
|
@@ -187,6 +193,11 @@ export async function pushToRemote(opts: {
|
|
|
187
193
|
/** Branch name on the remote (a plain name, not `refs/heads/...`). */
|
|
188
194
|
remoteBranch: string;
|
|
189
195
|
force?: boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Replace the branch only while it still holds `expected` (null: only
|
|
198
|
+
* while it does not exist). Native Git only.
|
|
199
|
+
*/
|
|
200
|
+
lease?: { expected: string | null };
|
|
190
201
|
}): Promise<void> {
|
|
191
202
|
const { ref, remoteBranch } = opts;
|
|
192
203
|
// Plain names only: a `+`, `:` or leading `-` would smuggle a forced or
|
|
@@ -196,7 +207,31 @@ export async function pushToRemote(opts: {
|
|
|
196
207
|
throw new RemotePushRefusedError(name, "not a plain branch or ref name.");
|
|
197
208
|
}
|
|
198
209
|
}
|
|
199
|
-
assertPushAllowed(
|
|
210
|
+
assertPushAllowed({
|
|
211
|
+
ownership: opts.ownership,
|
|
212
|
+
remoteBranch,
|
|
213
|
+
force: opts.force ?? opts.lease !== undefined,
|
|
214
|
+
lease: opts.lease !== undefined,
|
|
215
|
+
});
|
|
216
|
+
if (opts.lease) {
|
|
217
|
+
const expected = opts.lease.expected ?? "";
|
|
218
|
+
if (!opts.native || (expected && !/^[0-9a-f]{40,64}$/.test(expected)))
|
|
219
|
+
throw new RemotePushRefusedError(
|
|
220
|
+
remoteBranch,
|
|
221
|
+
"a push with a lease names the commit it replaces.",
|
|
222
|
+
);
|
|
223
|
+
await pushWithLease({
|
|
224
|
+
repoPath: opts.repoPath,
|
|
225
|
+
url: opts.url,
|
|
226
|
+
ref,
|
|
227
|
+
target: `refs/heads/${remoteBranch}`,
|
|
228
|
+
expected,
|
|
229
|
+
auth: opts.credentials
|
|
230
|
+
? { ...opts.credentials, url: opts.url }
|
|
231
|
+
: undefined,
|
|
232
|
+
});
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
200
235
|
if (opts.native) {
|
|
201
236
|
await nativeGit(
|
|
202
237
|
opts.repoPath,
|
|
@@ -222,6 +257,174 @@ export async function pushToRemote(opts: {
|
|
|
222
257
|
});
|
|
223
258
|
}
|
|
224
259
|
|
|
260
|
+
function pushWithLease(input: {
|
|
261
|
+
repoPath: string;
|
|
262
|
+
url: string;
|
|
263
|
+
ref: string;
|
|
264
|
+
target: string;
|
|
265
|
+
expected: string;
|
|
266
|
+
auth: NativeGitAuth | undefined;
|
|
267
|
+
}): Promise<string> {
|
|
268
|
+
return nativeGit(
|
|
269
|
+
input.repoPath,
|
|
270
|
+
[
|
|
271
|
+
"push",
|
|
272
|
+
"--porcelain",
|
|
273
|
+
"--no-follow-tags",
|
|
274
|
+
`--force-with-lease=${input.target}:${input.expected}`,
|
|
275
|
+
input.url,
|
|
276
|
+
`${input.ref}:${input.target}`,
|
|
277
|
+
],
|
|
278
|
+
input.auth,
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** The branch a chat last pushed to the project's repository (ADR 0345). */
|
|
283
|
+
export interface PushedBranch {
|
|
284
|
+
branch: string;
|
|
285
|
+
commit: string;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Push a chat's branch (ADR 0345): a `work/` branch it creates, moves
|
|
290
|
+
* forward, or, after a rebase, replaces. It replaces only a branch it
|
|
291
|
+
* pushed before, and only while that branch still holds the commit it last
|
|
292
|
+
* pushed there; anything a person or another chat pushed since refuses
|
|
293
|
+
* the push. Never a branch outside `work/`, and never a deletion.
|
|
294
|
+
*/
|
|
295
|
+
export async function pushOwnBranch(opts: {
|
|
296
|
+
repoPath: string;
|
|
297
|
+
url: string;
|
|
298
|
+
credentials?: GitCredentials;
|
|
299
|
+
ownership: RemoteOwnership;
|
|
300
|
+
/** The local commit to push. */
|
|
301
|
+
commit: string;
|
|
302
|
+
remoteBranch: string;
|
|
303
|
+
/** What this chat last pushed, when it pushed before. */
|
|
304
|
+
pushed?: PushedBranch;
|
|
305
|
+
}): Promise<{
|
|
306
|
+
outcome: "created" | "updated" | "replaced" | "unchanged";
|
|
307
|
+
/** This chat created the branch or updated the exact tip it owned. */
|
|
308
|
+
recordOwnership: boolean;
|
|
309
|
+
}> {
|
|
310
|
+
const { remoteBranch, commit } = opts;
|
|
311
|
+
if (!isManagedBranch(remoteBranch) || !isValidRefName(remoteBranch))
|
|
312
|
+
throw new RemotePushRefusedError(
|
|
313
|
+
remoteBranch,
|
|
314
|
+
`a chat pushes only its own branch under '${MANAGED_BRANCH_PREFIX}'.`,
|
|
315
|
+
);
|
|
316
|
+
if (!/^[0-9a-f]{40}$|^[0-9a-f]{64}$/.test(commit))
|
|
317
|
+
throw new RemotePushRefusedError(remoteBranch, "not a commit to push.");
|
|
318
|
+
assertPushAllowed({
|
|
319
|
+
ownership: opts.ownership,
|
|
320
|
+
remoteBranch,
|
|
321
|
+
force: true,
|
|
322
|
+
lease: true,
|
|
323
|
+
});
|
|
324
|
+
const auth = opts.credentials
|
|
325
|
+
? { ...opts.credentials, url: opts.url }
|
|
326
|
+
: undefined;
|
|
327
|
+
const target = `refs/heads/${remoteBranch}`;
|
|
328
|
+
const listed = await nativeGit(
|
|
329
|
+
opts.repoPath,
|
|
330
|
+
["ls-remote", "--symref", opts.url, "HEAD", target],
|
|
331
|
+
auth,
|
|
332
|
+
);
|
|
333
|
+
const refs = new Map<string, string>();
|
|
334
|
+
for (const line of listed.split("\n")) {
|
|
335
|
+
const [value, name] = line.split("\t");
|
|
336
|
+
if (name && value && /^[0-9a-f]{40}$|^[0-9a-f]{64}$/.test(value))
|
|
337
|
+
refs.set(name, value);
|
|
338
|
+
}
|
|
339
|
+
const defaultRef = listed.match(/^ref: (refs\/heads\/[^\t\n]+)\tHEAD$/m)?.[1];
|
|
340
|
+
if (!defaultRef || !refs.get("HEAD"))
|
|
341
|
+
throw new RemotePushRefusedError(
|
|
342
|
+
remoteBranch,
|
|
343
|
+
"the remote names no default branch, so Work cannot check this push.",
|
|
344
|
+
);
|
|
345
|
+
if (target === defaultRef)
|
|
346
|
+
throw new RemotePushRefusedError(
|
|
347
|
+
remoteBranch,
|
|
348
|
+
"Work cannot push a chat to the repository's default branch.",
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
const remote = refs.get(target);
|
|
352
|
+
const push = async (expected: string) => {
|
|
353
|
+
const output = await pushWithLease({
|
|
354
|
+
repoPath: opts.repoPath,
|
|
355
|
+
url: opts.url,
|
|
356
|
+
ref: commit,
|
|
357
|
+
target,
|
|
358
|
+
expected,
|
|
359
|
+
auth,
|
|
360
|
+
}).catch((error: unknown) => {
|
|
361
|
+
throw isStaleLease(error)
|
|
362
|
+
? new RemotePushRefusedError(
|
|
363
|
+
remoteBranch,
|
|
364
|
+
"the branch changed in the repository while it was being pushed. Try again.",
|
|
365
|
+
)
|
|
366
|
+
: error;
|
|
367
|
+
});
|
|
368
|
+
// If another caller pushed this exact commit in the meantime, Git can
|
|
369
|
+
// report success without updating the ref, even with a lease.
|
|
370
|
+
const status = output
|
|
371
|
+
.split("\n")
|
|
372
|
+
.find((line) => line.split("\t")[1]?.endsWith(`:${target}`))?.[0];
|
|
373
|
+
if (status === "=") return false;
|
|
374
|
+
if (status === "*" || status === "+" || status === " ") return true;
|
|
375
|
+
throw new Error("Git did not confirm what happened to the pushed branch.");
|
|
376
|
+
};
|
|
377
|
+
const unchanged: Awaited<ReturnType<typeof pushOwnBranch>> = {
|
|
378
|
+
outcome: "unchanged",
|
|
379
|
+
recordOwnership: false,
|
|
380
|
+
};
|
|
381
|
+
if (!remote)
|
|
382
|
+
return (await push(""))
|
|
383
|
+
? { outcome: "created", recordOwnership: true }
|
|
384
|
+
: unchanged;
|
|
385
|
+
if (remote === commit) return unchanged;
|
|
386
|
+
|
|
387
|
+
const ownsTip =
|
|
388
|
+
opts.pushed?.branch === remoteBranch && opts.pushed.commit === remote;
|
|
389
|
+
const forward = await nativeGit(opts.repoPath, [
|
|
390
|
+
"merge-base",
|
|
391
|
+
"--is-ancestor",
|
|
392
|
+
remote,
|
|
393
|
+
commit,
|
|
394
|
+
]).then(
|
|
395
|
+
() => true,
|
|
396
|
+
() => false,
|
|
397
|
+
);
|
|
398
|
+
if (forward)
|
|
399
|
+
return (await push(remote))
|
|
400
|
+
? { outcome: "updated", recordOwnership: ownsTip }
|
|
401
|
+
: unchanged;
|
|
402
|
+
|
|
403
|
+
if (opts.pushed?.branch !== remoteBranch)
|
|
404
|
+
throw new RemotePushRefusedError(
|
|
405
|
+
remoteBranch,
|
|
406
|
+
"a branch of that name is already in the repository with other commits, and this chat did not create it. Switch to another branch name and try again.",
|
|
407
|
+
);
|
|
408
|
+
if (!ownsTip)
|
|
409
|
+
throw new RemotePushRefusedError(
|
|
410
|
+
remoteBranch,
|
|
411
|
+
"someone else pushed to this branch since this chat last did, so it was not overwritten. Fetch the branch, rebase or merge onto it, and try again.",
|
|
412
|
+
);
|
|
413
|
+
return (await push(remote))
|
|
414
|
+
? { outcome: "replaced", recordOwnership: true }
|
|
415
|
+
: unchanged;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function isStaleLease(error: unknown): boolean {
|
|
419
|
+
const text =
|
|
420
|
+
error && typeof error === "object" && "stderr" in error
|
|
421
|
+
? String(error.stderr)
|
|
422
|
+
: error instanceof Error
|
|
423
|
+
? error.message
|
|
424
|
+
: "";
|
|
425
|
+
return /stale info|\[rejected\]|already exists/i.test(text);
|
|
426
|
+
}
|
|
427
|
+
|
|
225
428
|
/**
|
|
226
429
|
* Fetch a branch from a network remote into a tracking ref without touching
|
|
227
430
|
* the working tree. Returns the fetched sha (null when the remote branch does
|
package/src/origin-draft.ts
CHANGED
|
@@ -46,9 +46,17 @@ const MAX_ATTEMPTS = 8;
|
|
|
46
46
|
*/
|
|
47
47
|
export type ProjectDraft = ProjectRepo | OriginDraftRepo;
|
|
48
48
|
|
|
49
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* One change to a draft: new content, or `delete: true`. `mode` is the
|
|
51
|
+
* file's Git mode, kept from the file it replaces when left out: `100755`
|
|
52
|
+
* for an executable, `120000` for a link whose target is the content.
|
|
53
|
+
*/
|
|
50
54
|
export type DraftChange =
|
|
51
|
-
| {
|
|
55
|
+
| {
|
|
56
|
+
path: string;
|
|
57
|
+
content: string | Uint8Array;
|
|
58
|
+
mode?: "100644" | "100755" | "120000";
|
|
59
|
+
}
|
|
52
60
|
| { path: string; delete: true };
|
|
53
61
|
|
|
54
62
|
export type DraftPublishResult =
|
|
@@ -466,7 +474,9 @@ export class OriginDraftRepo {
|
|
|
466
474
|
* Paths the project's ignore rules keep out of history are refused, or
|
|
467
475
|
* dropped with `skipIgnored`; with `skipMissingDeletes`, deleting a file
|
|
468
476
|
* the draft lacks is not an error (a sandbox sync reporting a file it
|
|
469
|
-
* made and removed again).
|
|
477
|
+
* made and removed again). With `replaceLinks`, a change to a path that
|
|
478
|
+
* is a link replaces or deletes the link itself, as Git would (a saved
|
|
479
|
+
* workspace, ADR 0343); otherwise it is refused. No write follows one.
|
|
470
480
|
*/
|
|
471
481
|
async write(input: {
|
|
472
482
|
changes: readonly DraftChange[];
|
|
@@ -478,16 +488,24 @@ export class OriginDraftRepo {
|
|
|
478
488
|
expected?: Record<string, string>;
|
|
479
489
|
skipIgnored?: boolean;
|
|
480
490
|
skipMissingDeletes?: boolean;
|
|
491
|
+
replaceLinks?: boolean;
|
|
481
492
|
}): Promise<string | null> {
|
|
482
|
-
const pending = new Map<
|
|
493
|
+
const pending = new Map<
|
|
494
|
+
string,
|
|
495
|
+
{ content: Uint8Array; mode?: string } | null
|
|
496
|
+
>();
|
|
483
497
|
for (const change of input.changes)
|
|
484
498
|
pending.set(
|
|
485
499
|
normalizePath(change.path),
|
|
486
500
|
"delete" in change
|
|
487
501
|
? null
|
|
488
|
-
:
|
|
489
|
-
|
|
490
|
-
|
|
502
|
+
: {
|
|
503
|
+
content:
|
|
504
|
+
typeof change.content === "string"
|
|
505
|
+
? new TextEncoder().encode(change.content)
|
|
506
|
+
: change.content,
|
|
507
|
+
...(change.mode ? { mode: change.mode } : {}),
|
|
508
|
+
},
|
|
491
509
|
);
|
|
492
510
|
const draftRefName = this.draftRefOrThrow();
|
|
493
511
|
if (pending.size === 0) return null;
|
|
@@ -495,13 +513,18 @@ export class OriginDraftRepo {
|
|
|
495
513
|
input.message ??
|
|
496
514
|
`Draft: ${[...pending.keys()].slice(0, 3).join(", ")}${pending.size > 3 ? ", ..." : ""}`;
|
|
497
515
|
// Contents are immutable objects: write them once, before any race.
|
|
498
|
-
const written = [...pending].
|
|
499
|
-
|
|
516
|
+
const written = [...pending].flatMap(([file, change]) =>
|
|
517
|
+
change ? [{ file, ...change }] : [],
|
|
500
518
|
);
|
|
501
519
|
const oids = await this.withObjects((objects) =>
|
|
502
|
-
objects.writeBlobs(written.map((
|
|
520
|
+
objects.writeBlobs(written.map((change) => change.content)),
|
|
521
|
+
);
|
|
522
|
+
const blobs = new Map(
|
|
523
|
+
written.map((change, index) => [
|
|
524
|
+
change.file,
|
|
525
|
+
{ oid: oids[index], mode: change.mode },
|
|
526
|
+
]),
|
|
503
527
|
);
|
|
504
|
-
const blobs = new Map(written.map(([file], index) => [file, oids[index]]));
|
|
505
528
|
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
|
|
506
529
|
const outcome = await this.attempt(async (objects) => {
|
|
507
530
|
const { draft, main } = await this.tips(objects);
|
|
@@ -525,14 +548,20 @@ export class OriginDraftRepo {
|
|
|
525
548
|
ignored.push(file);
|
|
526
549
|
continue;
|
|
527
550
|
}
|
|
528
|
-
const
|
|
529
|
-
changes.set(
|
|
551
|
+
const blob = blobs.get(file);
|
|
552
|
+
changes.set(
|
|
553
|
+
file,
|
|
554
|
+
blob?.oid
|
|
555
|
+
? { oid: blob.oid, ...(blob.mode ? { mode: blob.mode } : {}) }
|
|
556
|
+
: null,
|
|
557
|
+
);
|
|
530
558
|
}
|
|
531
559
|
if (ignored.length > 0 && !input.skipIgnored)
|
|
532
560
|
throw new DraftIgnoredPathError(ignored);
|
|
533
561
|
if (changes.size === 0) return { sha: parent };
|
|
534
562
|
const tree = await objects.writeTree(baseTree, changes, {
|
|
535
563
|
skipMissingDeletes: input.skipMissingDeletes,
|
|
564
|
+
replaceLinks: input.replaceLinks,
|
|
536
565
|
});
|
|
537
566
|
if (tree === baseTree) return { sha: parent };
|
|
538
567
|
const sha = await objects.writeCommit({
|