@yaks/git 0.0.0-stage → 0.2.36

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.
Files changed (183) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +754 -2
  3. package/_dist/address.d.ts +6 -0
  4. package/_dist/address.d.ts.map +1 -0
  5. package/_dist/checkout_vocab.d.ts +3 -0
  6. package/_dist/checkout_vocab.d.ts.map +1 -0
  7. package/_dist/cites.d.ts +48 -0
  8. package/_dist/cites.d.ts.map +1 -0
  9. package/_dist/commit.d.ts +36 -0
  10. package/_dist/commit.d.ts.map +1 -0
  11. package/_dist/comp.d.ts +29 -0
  12. package/_dist/comp.d.ts.map +1 -0
  13. package/_dist/door.d.ts +29 -0
  14. package/_dist/door.d.ts.map +1 -0
  15. package/_dist/format.d.ts +2 -0
  16. package/_dist/format.d.ts.map +1 -0
  17. package/_dist/hold.d.ts +6 -0
  18. package/_dist/hold.d.ts.map +1 -0
  19. package/_dist/host.d.ts +126 -0
  20. package/_dist/host.d.ts.map +1 -0
  21. package/_dist/http.d.ts +33 -0
  22. package/_dist/http.d.ts.map +1 -0
  23. package/_dist/import.d.ts +13 -0
  24. package/_dist/import.d.ts.map +1 -0
  25. package/_dist/index.d.ts +80 -0
  26. package/_dist/index.d.ts.map +1 -0
  27. package/_dist/land.d.ts +77 -0
  28. package/_dist/land.d.ts.map +1 -0
  29. package/_dist/mod.d.ts +73 -0
  30. package/_dist/mod.d.ts.map +1 -0
  31. package/_dist/objects.d.ts +40 -0
  32. package/_dist/objects.d.ts.map +1 -0
  33. package/_dist/oid.d.ts +24 -0
  34. package/_dist/oid.d.ts.map +1 -0
  35. package/_dist/pack-store.d.ts +9 -0
  36. package/_dist/pack-store.d.ts.map +1 -0
  37. package/_dist/pack.d.ts +26 -0
  38. package/_dist/pack.d.ts.map +1 -0
  39. package/_dist/pkt.d.ts +14 -0
  40. package/_dist/pkt.d.ts.map +1 -0
  41. package/_dist/placement.d.ts +4 -0
  42. package/_dist/placement.d.ts.map +1 -0
  43. package/_dist/plugin.d.ts +43 -0
  44. package/_dist/plugin.d.ts.map +1 -0
  45. package/_dist/project.d.ts +20 -0
  46. package/_dist/project.d.ts.map +1 -0
  47. package/_dist/push.d.ts +42 -0
  48. package/_dist/push.d.ts.map +1 -0
  49. package/_dist/refs.d.ts +81 -0
  50. package/_dist/refs.d.ts.map +1 -0
  51. package/_dist/rewrite.d.ts +35 -0
  52. package/_dist/rewrite.d.ts.map +1 -0
  53. package/_dist/service.d.ts +35 -0
  54. package/_dist/service.d.ts.map +1 -0
  55. package/_dist/sha1.d.ts +11 -0
  56. package/_dist/sha1.d.ts.map +1 -0
  57. package/_dist/source.d.ts +12 -0
  58. package/_dist/source.d.ts.map +1 -0
  59. package/_dist/tools.d.ts +11 -0
  60. package/_dist/tools.d.ts.map +1 -0
  61. package/_dist/tree.d.ts +28 -0
  62. package/_dist/tree.d.ts.map +1 -0
  63. package/_dist/unpack.d.ts +5 -0
  64. package/_dist/unpack.d.ts.map +1 -0
  65. package/_dist/vocab.d.ts +8 -0
  66. package/_dist/vocab.d.ts.map +1 -0
  67. package/_dist/workspace.d.ts +17 -0
  68. package/_dist/workspace.d.ts.map +1 -0
  69. package/address.js +9 -0
  70. package/address.js.map +1 -0
  71. package/address.ts +15 -0
  72. package/cache.js +9 -0
  73. package/cache.js.map +1 -0
  74. package/cache.ts +6 -0
  75. package/checkout.js +18 -0
  76. package/checkout.js.map +1 -0
  77. package/checkout.ts +24 -0
  78. package/checkout_vocab.js +14 -0
  79. package/checkout_vocab.js.map +1 -0
  80. package/checkout_vocab.ts +11 -0
  81. package/cites.js +189 -0
  82. package/cites.js.map +1 -0
  83. package/cites.ts +206 -0
  84. package/commit.js +128 -0
  85. package/commit.js.map +1 -0
  86. package/commit.ts +183 -0
  87. package/comp.js +94 -0
  88. package/comp.js.map +1 -0
  89. package/comp.ts +111 -0
  90. package/deno.json +1 -0
  91. package/door.js +88 -0
  92. package/door.js.map +1 -0
  93. package/door.ts +142 -0
  94. package/format.js +43 -0
  95. package/format.js.map +1 -0
  96. package/format.ts +49 -0
  97. package/hold.js +17 -0
  98. package/hold.js.map +1 -0
  99. package/hold.ts +21 -0
  100. package/host.js +1024 -0
  101. package/host.js.map +1 -0
  102. package/host.ts +1140 -0
  103. package/http.js +268 -0
  104. package/http.js.map +1 -0
  105. package/http.ts +329 -0
  106. package/import.js +141 -0
  107. package/import.js.map +1 -0
  108. package/import.ts +128 -0
  109. package/index.js +496 -0
  110. package/index.js.map +1 -0
  111. package/index.ts +634 -0
  112. package/land.js +788 -0
  113. package/land.js.map +1 -0
  114. package/land.ts +871 -0
  115. package/mod.js +73 -0
  116. package/mod.js.map +1 -0
  117. package/mod.ts +74 -0
  118. package/objects.js +331 -0
  119. package/objects.js.map +1 -0
  120. package/objects.ts +390 -0
  121. package/oid.js +50 -0
  122. package/oid.js.map +1 -0
  123. package/oid.ts +83 -0
  124. package/pack-index.js +103 -0
  125. package/pack-index.js.map +1 -0
  126. package/pack-index.ts +104 -0
  127. package/pack-store.js +216 -0
  128. package/pack-store.js.map +1 -0
  129. package/pack-store.ts +292 -0
  130. package/pack.js +121 -0
  131. package/pack.js.map +1 -0
  132. package/pack.ts +146 -0
  133. package/package.json +78 -4
  134. package/pkt.js +73 -0
  135. package/pkt.js.map +1 -0
  136. package/pkt.ts +93 -0
  137. package/placement.js +44 -0
  138. package/placement.js.map +1 -0
  139. package/placement.ts +38 -0
  140. package/plugin.js +66 -0
  141. package/plugin.js.map +1 -0
  142. package/plugin.ts +88 -0
  143. package/process-cwd.py +57 -0
  144. package/project.js +142 -0
  145. package/project.js.map +1 -0
  146. package/project.ts +151 -0
  147. package/push.js +401 -0
  148. package/push.js.map +1 -0
  149. package/push.ts +451 -0
  150. package/refs.js +176 -0
  151. package/refs.js.map +1 -0
  152. package/refs.ts +285 -0
  153. package/rewrite.js +176 -0
  154. package/rewrite.js.map +1 -0
  155. package/rewrite.ts +173 -0
  156. package/service.js +104 -0
  157. package/service.js.map +1 -0
  158. package/service.ts +194 -0
  159. package/sha1.js +115 -0
  160. package/sha1.js.map +1 -0
  161. package/sha1.ts +146 -0
  162. package/source.js +100 -0
  163. package/source.js.map +1 -0
  164. package/source.ts +116 -0
  165. package/spare.js +108 -0
  166. package/spare.js.map +1 -0
  167. package/spare.ts +94 -0
  168. package/tools.js +278 -0
  169. package/tools.js.map +1 -0
  170. package/tools.ts +310 -0
  171. package/tree.js +105 -0
  172. package/tree.js.map +1 -0
  173. package/tree.ts +132 -0
  174. package/unpack.js +207 -0
  175. package/unpack.js.map +1 -0
  176. package/unpack.ts +210 -0
  177. package/vocab.js +20 -0
  178. package/vocab.js.map +1 -0
  179. package/vocab.json +386 -0
  180. package/vocab.ts +22 -0
  181. package/workspace.js +449 -0
  182. package/workspace.js.map +1 -0
  183. package/workspace.ts +445 -0
package/refs.ts ADDED
@@ -0,0 +1,285 @@
1
+ // Where a branch points, and how a manifest lands on it: the one step between
2
+ // "here are some files" and "this repository has a new commit".
3
+ //
4
+ // A ref is A row, one per repository and branch name ({@link refEid}), so
5
+ // moving a branch updates the row that is already there rather than adding a
6
+ // second row someone has to notice is stale. It is the one part of a
7
+ // repository that belongs to exactly one repository — objects are named by the
8
+ // digest of their own bytes and are therefore global — which is why the ref
9
+ // graph and the object graph are two separate arguments here ({@link Repo})
10
+ // and may be two separate stores.
11
+ //
12
+ // A landing holds what A commit needs and nothing else: the manifest, the two
13
+ // signatures, the message. The parent is not among them — it is read from the
14
+ // ref, so a commit written by whatever watches for releases and a commit
15
+ // written later by a repair pass are built the same way and land on the same
16
+ // chain.
17
+ //
18
+ // Writing A commit is repeatable, which is what makes a repair pass cheap:
19
+ // every id is a digest of the bytes, and the bytes are the manifest and the
20
+ // timestamps the caller hands over, so landing the same release twice produces
21
+ // the same objects. What it must not do twice is append — hence `beside`,
22
+ // where the caller writes the rows it has already checked are not there.
23
+
24
+ import { type Blobs, openedOf } from '@yaks/blob'
25
+ import type { Bundle, Comp, Eid } from 'jsr:@yaks/graph@0.2.36'
26
+ import { Stale } from '@yaks/graph/errors'
27
+ import { token } from '@yaks/graph/sha256'
28
+ import { line } from '@yaks/query'
29
+ import { treeOf, type Who } from './commit.js'
30
+ import { BLOB, REF, refEid } from './comp.js'
31
+ import { MAIN } from './http.js'
32
+ import { type Base, index, names, type Writes } from './index.js'
33
+ import type { Oids } from './oid.ts'
34
+ import type { Files } from './tree.ts'
35
+
36
+ // Reads a reference property in either of the two forms a store returns: the
37
+ // bare eid that a read inside a transaction gives, and the `{eid, name}`
38
+ // object that a read over a store's HTTP API gives, which names what the
39
+ // reference points at as it goes. One reader, so a branch reads the same
40
+ // however the graph was reached.
41
+ let id = (v: unknown): string =>
42
+ typeof v == 'string' ? v : String((v as { eid?: unknown } | null)?.eid ?? '')
43
+
44
+ /**
45
+ * The two graphs and the byte store a repository is kept in.
46
+ *
47
+ * `refs` and `objects` may be the same graph — nothing here needs them
48
+ * separate — but they are two arguments because a server that decides read
49
+ * access per repository keeps its branches where that decision is made, and
50
+ * its objects, which are global, in a store of their own.
51
+ */
52
+ export type Repo = {
53
+ /** the graph the `ref` row lives in */
54
+ refs: Writes
55
+ /** the graph the objects live in */
56
+ objects: Writes
57
+ /** the bytes: a tree's and a commit's, and the file a blob names */
58
+ bytes: Blobs
59
+ }
60
+
61
+ /** One release, as landing it on a branch needs it. */
62
+ export type Landing = {
63
+ /** the entity the repository belongs to */
64
+ app: Eid
65
+ /** the branch; defaults to `refs/heads/main` */
66
+ branch?: string
67
+ /** the manifest: path → the SHA-256 the byte store holds that file under */
68
+ files: Files
69
+ /** who made the change */
70
+ author: Who
71
+ /** who recorded it — the platform writing the commit */
72
+ committer: Who
73
+ /** the commit message */
74
+ message: string
75
+ headers?: Record<string, string>
76
+ /**
77
+ * The manifest a commit on this branch was made from, where the caller
78
+ * knows it. The new manifest is folded against the branch's last commit
79
+ * (./index.ts `Base`), so a release reads and writes only what it changed;
80
+ * without it, every file is named again.
81
+ */
82
+ was?: (commit: string) => Files | null | Promise<Files | null>
83
+ /**
84
+ * Rows the caller wants recorded about this commit, written in the same
85
+ * transaction as the moved ref. This package writes no row joining a commit
86
+ * to whatever it was built from: those rows belong to whoever builds it.
87
+ */
88
+ beside?: (oids: Oids) => Bundle[]
89
+ }
90
+
91
+ /**
92
+ * Where a branch points: the commit its ref names, or `null` before the first
93
+ * commit. This is all of `ls-refs` on the server's side, so whatever mounts
94
+ * the HTTP handlers reads a branch through this rather than by querying the
95
+ * row itself.
96
+ */
97
+ export let refAt = async (
98
+ refs: Pick<Writes, 'read'>,
99
+ app: Eid,
100
+ name = MAIN,
101
+ ): Promise<string | null> => {
102
+ let [row] = await refs.read(line`.entity.eid=${refEid(app, name)}`)
103
+ return id((row?.[REF] as Record<string, unknown> | undefined)?.commit) ||
104
+ null
105
+ }
106
+
107
+ /** An object already written, with both its ids: its SHA-1 id is the row's own
108
+ * eid, and its SHA-256 id is in the @yaks/key beside it. */
109
+ let named = async (g: Writes, oid: string): Promise<Oids | null> => {
110
+ let name = (await names(g, [oid])).get(oid)
111
+ return name ? { oid, oid256: name } : null
112
+ }
113
+
114
+ /** What a commit is folded against: its tree, read off its body, and the
115
+ * manifest the caller says it was made from. */
116
+ let based = async (
117
+ repo: Pick<Repo, 'objects' | 'bytes'>,
118
+ commit: string,
119
+ was: NonNullable<Landing['was']>,
120
+ held?: Oids,
121
+ ): Promise<Base | undefined> => {
122
+ let files = await was(commit)
123
+ if (!files) return
124
+ let read = async () => {
125
+ let [row] = await repo.objects.read(
126
+ line`.gitobj.type=commit&?${BLOB}&.entity.eid=${commit}`,
127
+ )
128
+ let body = row && await repo.bytes.get(String((row[BLOB] as Comp)?.sha))
129
+ let tree = body && treeOf(body)
130
+ return (tree && await named(repo.objects, tree)) || undefined
131
+ }
132
+ return { tree: held ?? read(), files }
133
+ }
134
+
135
+ /** The row that points a branch at a commit. */
136
+ export let moved = (app: Eid, name: string, commit: string): Bundle => ({
137
+ entity: { eid: refEid(app, name) },
138
+ [REF]: { app, name, commit },
139
+ })
140
+
141
+ /** Advance a ref only from the tip its writer read. */
142
+ export let advancing = (
143
+ repo: Eid,
144
+ name: string,
145
+ before: string | null,
146
+ after: string,
147
+ ): Bundle => ({
148
+ ...moved(repo, name, after),
149
+ $was: { ref: { commit: token(before) } },
150
+ })
151
+
152
+ /** A ref held at the tip its writer read, moving nothing: the write it rides
153
+ * in is refused once another writer has moved the ref. */
154
+ export let unmoved = (
155
+ repo: Eid,
156
+ name: string,
157
+ at: string | null,
158
+ ): Bundle => ({
159
+ entity: { eid: refEid(repo, name) },
160
+ $was: { ref: { commit: token(at) } },
161
+ })
162
+
163
+ /** Whether a guarded ref write lost to another writer. */
164
+ export let refMoved = (error: unknown, ref: Eid): boolean =>
165
+ error instanceof Stale && error.eid == ref && error.comp == REF &&
166
+ error.prop == 'commit'
167
+
168
+ /**
169
+ * One manifest as a commit on a branch: every object it needs written, the
170
+ * branch pointed at it, and whatever the caller passes in `beside` written in
171
+ * that same transaction. Returns the commit's two ids.
172
+ *
173
+ * The parent is wherever the branch points now, read here rather than passed
174
+ * in by the caller, so the chain follows the order the releases actually
175
+ * happened in.
176
+ */
177
+ export let commitOnto = async (
178
+ repo: Repo,
179
+ l: Landing,
180
+ ): Promise<Oids> => {
181
+ let name = l.branch ?? MAIN
182
+ let at = await refAt(repo.refs, l.app, name)
183
+ let { commit } = await prepareCommit(repo, l, at)
184
+ await repo.refs.apply([
185
+ ...l.beside?.(commit) ?? [],
186
+ moved(l.app, name, commit.oid),
187
+ ])
188
+ return commit
189
+ }
190
+
191
+ /** Bytes a batch's caller already has ({@link preparing}): `held` by their
192
+ * SHA-256, and `kept` once the caller's own puts of them have landed. */
193
+ export type InHand = {
194
+ held?: ReadonlyMap<string, Uint8Array>
195
+ kept?: Promise<unknown>
196
+ }
197
+
198
+ /** Build several objects with batched graph writes. Their bodies are kept
199
+ * before a batch can publish pointers to them. A blob named over `held` bytes
200
+ * is hashed from them, never read back from the store, and nothing is
201
+ * published before `kept` has landed, so the caller's puts and the batch's
202
+ * work go on at once. */
203
+ export let preparing = async <T>(
204
+ repo: Pick<Repo, 'objects' | 'bytes'>,
205
+ build: (git: ReturnType<typeof index>) => Promise<T>,
206
+ { held, kept }: InHand = {},
207
+ ): Promise<T> => {
208
+ let { objects, bytes } = repo
209
+ let git = index(
210
+ kept
211
+ ? {
212
+ read: objects.read,
213
+ apply: async (rows) => (await kept, objects.apply(rows)),
214
+ }
215
+ : objects,
216
+ held?.size ? inHand(bytes, held) : bytes,
217
+ true,
218
+ )
219
+ let result = await build(git)
220
+ await git.flush()
221
+ return result
222
+ }
223
+
224
+ // A byte store that answers `held` from memory.
225
+ let inHand = (
226
+ bytes: Blobs,
227
+ held: ReadonlyMap<string, Uint8Array>,
228
+ ): Blobs => ({
229
+ ...bytes,
230
+ has: (sha) => held.has(sha) || bytes.has(sha),
231
+ get: (sha) => held.get(sha) ?? bytes.get(sha),
232
+ ...bytes.open && {
233
+ open: (sha) => {
234
+ let body = held.get(sha)
235
+ return body ? Promise.resolve(openedOf(body)) : bytes.open!(sha)
236
+ },
237
+ },
238
+ })
239
+
240
+ /** A published commit and its root tree, both addressed by content. */
241
+ export type CommitTree = { commit: Oids; tree: Oids }
242
+
243
+ /** Build a commit with the object writer the caller is already using. A
244
+ * caller holding the published parent can supply its commit and tree IDs,
245
+ * and one still reading who made the change hands its author as a promise:
246
+ * the tree is made meanwhile, and only the commit waits for it. */
247
+ export let buildCommit = (
248
+ repo: Pick<Repo, 'objects' | 'bytes'>,
249
+ l:
250
+ & Pick<Landing, 'files' | 'committer' | 'message' | 'headers' | 'was'>
251
+ & { author: Landing['author'] | Promise<Landing['author']> },
252
+ at: string | CommitTree | null,
253
+ ): (git: ReturnType<typeof index>) => Promise<CommitTree> =>
254
+ async (git) => {
255
+ let held = typeof at == 'string' ? undefined : at
256
+ let oid = typeof at == 'string' ? at : held?.commit.oid
257
+ // The parent's other id is asked for while its tree is read and followed.
258
+ let naming = typeof at == 'string' ? named(repo.objects, at) : held?.commit
259
+ let base = oid && l.was
260
+ ? await based(repo, oid, l.was, held?.tree)
261
+ : undefined
262
+ let [parent, tree, author] = await Promise.all([
263
+ naming,
264
+ git.files(l.files, base),
265
+ l.author,
266
+ ])
267
+ if (at && !parent) throw new Error(`git: missing parent ${at}`)
268
+ let commit = await git.commit({
269
+ tree,
270
+ parents: parent ? [parent] : [],
271
+ author,
272
+ committer: l.committer,
273
+ message: l.message,
274
+ headers: l.headers,
275
+ })
276
+ return { commit, tree }
277
+ }
278
+
279
+ /** Write immutable objects before the caller's transaction moves its refs. */
280
+ export let prepareCommit = (
281
+ repo: Parameters<typeof buildCommit>[0],
282
+ l: Parameters<typeof buildCommit>[1],
283
+ at: Parameters<typeof buildCommit>[2],
284
+ had?: InHand,
285
+ ): Promise<CommitTree> => preparing(repo, buildCommit(repo, l, at), had)
package/rewrite.js ADDED
@@ -0,0 +1,176 @@
1
+ import { commitParts, rewrittenCommit } from "./commit.js";
2
+ import { names } from "./index.js";
3
+ import { objects } from "./objects.js";
4
+ import { preparing } from "./refs.js";
5
+ import { DIR, treeEntries } from "./tree.js";
6
+ // A topological walk over every parent, including merged histories. Use a
7
+ // stack so the length of a branch does not limit what can be rewritten.
8
+ let ordered = function*(tip, history) {
9
+ let stack = [
10
+ tip
11
+ ];
12
+ let done = new Set();
13
+ while(stack.length){
14
+ let oid = stack.at(-1);
15
+ let rev = history.get(oid);
16
+ if (!rev) throw new Error(`git: missing revision ${oid}`);
17
+ let next = rev.parents.find((parent)=>!done.has(parent));
18
+ if (next) stack.push(next);
19
+ else {
20
+ stack.pop();
21
+ done.add(oid);
22
+ yield [
23
+ oid,
24
+ rev
25
+ ];
26
+ }
27
+ }
28
+ };
29
+ export let rewrites = ()=>({
30
+ history: new Map(),
31
+ trees: new Map(),
32
+ named: new Map(),
33
+ rooted: new Map(),
34
+ made: new Map()
35
+ });
36
+ /** Remove selected entries from one root directory throughout a branch.
37
+ * The selector sees the original snapshots in parent order. Commits whose
38
+ * tree and parents stand keep their exact object IDs. */ export let rewriteDirectory = async (repo, tip, folder, remove, memo = rewrites())=>{
39
+ let from = objects(repo.objects, repo.bytes);
40
+ let { history, trees } = memo;
41
+ for (let [oid, bytes] of (await from.commits(tip, history))){
42
+ history.set(oid, {
43
+ ...commitParts(bytes),
44
+ bytes
45
+ });
46
+ }
47
+ let read = async (ids)=>{
48
+ let at = 0;
49
+ for await (let obj of from.read(ids)){
50
+ trees.set(ids[at++], treeEntries(obj.bytes));
51
+ }
52
+ };
53
+ let roots = [
54
+ ...new Set([
55
+ ...history.values()
56
+ ].map((rev)=>rev.tree))
57
+ ];
58
+ let atFolder = (entries)=>entries.find((entry)=>entry.name == folder && entry.mode == DIR);
59
+ await read(roots.filter((oid)=>!trees.has(oid)));
60
+ let folders = [
61
+ ...new Set(roots.flatMap((oid)=>{
62
+ let child = atFolder(trees.get(oid));
63
+ return child ? [
64
+ child.oid
65
+ ] : [];
66
+ }))
67
+ ];
68
+ await read(folders.filter((oid)=>!trees.has(oid)));
69
+ let named = new Map(memo.named);
70
+ let known = async (ids)=>{
71
+ for (let [oid, oid256] of (await names(repo.objects, ids.filter((oid)=>!named.has(oid)))))named.set(oid, {
72
+ oid,
73
+ oid256
74
+ });
75
+ return ids.map((oid)=>{
76
+ let found = named.get(oid);
77
+ if (!found) throw new Error(`git: missing compatibility ID for ${oid}`);
78
+ return found;
79
+ });
80
+ };
81
+ let rewritten = [];
82
+ let moved = new Map();
83
+ let rooted = new Map(memo.rooted);
84
+ let made = new Map(memo.made);
85
+ await preparing(repo, async (git)=>{
86
+ let treeOf = async (entries)=>{
87
+ let ids = await known(entries.map((entry)=>entry.oid));
88
+ let tree = await git.tree(entries.map((entry, i)=>({
89
+ ...entry,
90
+ ...ids[i]
91
+ })));
92
+ named.set(tree.oid, tree);
93
+ trees.set(tree.oid, entries);
94
+ return tree.oid;
95
+ };
96
+ let filtered = async (tree, discard)=>{
97
+ let key = JSON.stringify([
98
+ tree,
99
+ [
100
+ ...discard
101
+ ].sort()
102
+ ]);
103
+ let saved = rooted.get(key);
104
+ if (saved) return saved;
105
+ let root = trees.get(tree);
106
+ let child = atFolder(root);
107
+ let entries = trees.get(child.oid).filter((e)=>!discard.has(e.name));
108
+ let kept = root.filter((entry)=>entry != child);
109
+ if (entries.length) kept.push({
110
+ ...child,
111
+ oid: await treeOf(entries)
112
+ });
113
+ let oid = await treeOf(kept);
114
+ rooted.set(key, oid);
115
+ return oid;
116
+ };
117
+ for (let [oid, rev] of ordered(tip, history)){
118
+ let child = atFolder(trees.get(rev.tree));
119
+ let discard = remove({
120
+ oid,
121
+ parents: rev.parents,
122
+ entries: child ? trees.get(child.oid) : []
123
+ });
124
+ let tree = discard.size ? await filtered(rev.tree, discard) : rev.tree;
125
+ let parents = rev.parents.map((parent)=>moved.get(parent) ?? parent);
126
+ if (tree == rev.tree && parents.every((p, i)=>p == rev.parents[i])) {
127
+ continue;
128
+ }
129
+ let key = [
130
+ oid,
131
+ tree,
132
+ ...parents
133
+ ].join(' ');
134
+ let commit = made.get(key);
135
+ if (!commit) {
136
+ let ids = await known([
137
+ tree,
138
+ ...parents
139
+ ]);
140
+ let bytes = rewrittenCommit(rev.bytes, tree, parents);
141
+ let receipt = await git.receive({
142
+ type: 'commit',
143
+ bytes
144
+ }, new Map(ids.map((id)=>[
145
+ id.oid,
146
+ id
147
+ ])));
148
+ named.set(receipt.oid, receipt);
149
+ made.set(key, commit = receipt.oid);
150
+ // A later tip that builds on this one walks it as read.
151
+ history.set(commit, {
152
+ ...commitParts(bytes),
153
+ bytes
154
+ });
155
+ }
156
+ moved.set(oid, commit);
157
+ rewritten.push({
158
+ before: oid,
159
+ oid: commit,
160
+ tree,
161
+ parents,
162
+ message: rev.message
163
+ });
164
+ }
165
+ });
166
+ Object.assign(memo, {
167
+ named,
168
+ rooted,
169
+ made
170
+ });
171
+ return {
172
+ tip: moved.get(tip) ?? tip,
173
+ rewritten
174
+ };
175
+ };
176
+ //# sourceMappingURL=rewrite.js.map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AAK,EAAE;MAC7C;MACA,IAAI,OAAO,QAAQ,IAAI,GAAG,MAAM,SAAS,IAAI,IAAI,EAAE,WAAW,IAAI,IAAI;MACtE,IAAI,UAAU,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC,SAAW,MAAM,GAAG,CAAC,WAAW;MAC/D,IAAI,QAAQ,IAAI,IAAI,IAAI,QAAQ,KAAK,CAAC,CAAC,GAAG,IAAM,KAAK,IAAI,OAAO,CAAC,EAAE,GAAG;QACpE;MACF;MACA,IAAI,MAAM;QAAC;QAAK;WAAS;OAAQ,CAAC,IAAI,CAAC;MACvC,IAAI,SAAS,KAAK,GAAG,CAAC;MACtB,IAAI,CAAC,QAAQ;QACX,IAAI,MAAM,MAAM,MAAM;UAAC;aAAS;SAAQ;QACxC,IAAI,QAAQ,gBAAgB,IAAI,KAAK,EAAE,MAAM;QAC7C,IAAI,UAAU,MAAM,IAAI,OAAO,CAC7B;UAAE,MAAM;UAAU;QAAM,GACxB,IAAI,IAAI,IAAI,GAAG,CAAC,CAAC,KAAO;YAAC,GAAG,GAAG;YAAE;WAAG;QAEtC,MAAM,GAAG,CAAC,QAAQ,GAAG,EAAE;QACvB,KAAK,GAAG,CAAC,KAAK,SAAS,QAAQ,GAAG;QAClC,wDAAwD;QACxD,QAAQ,GAAG,CAAC,QAAQ;UAAE,GAAG,YAAY,MAAM;UAAE;QAAM;MACrD;MACA,MAAM,GAAG,CAAC,KAAK;MACf,UAAU,IAAI,CAAC;QACb,QAAQ;QACR,KAAK;QACL;QACA;QACA,SAAS,IAAI,OAAO;MACtB;IACF;EACF;EACA,OAAO,MAAM,CAAC,MAAM;IAAE;IAAO;IAAQ;EAAK;EAC1C,OAAO;IAAE,KAAK,MAAM,GAAG,CAAC,QAAQ;IAAK;EAAU;AACjD,EAAC"}
package/rewrite.ts ADDED
@@ -0,0 +1,173 @@
1
+ import { commitParts, rewrittenCommit } from './commit.js'
2
+ import { names } from './index.js'
3
+ import { objects } from './objects.js'
4
+ import type { Oids } from './oid.ts'
5
+ import { preparing, type Repo } from './refs.js'
6
+ import { DIR, type Entry, treeEntries } from './tree.js'
7
+
8
+ export type Snapshot = {
9
+ oid: string
10
+ parents: string[]
11
+ entries: Entry[]
12
+ }
13
+
14
+ export type Revision = ReturnType<typeof commitParts> & { bytes: Uint8Array }
15
+ type Rewritten = ReturnType<typeof commitParts> & {
16
+ before: string
17
+ oid: string
18
+ }
19
+
20
+ // A topological walk over every parent, including merged histories. Use a
21
+ // stack so the length of a branch does not limit what can be rewritten.
22
+ let ordered = function* (tip: string, history: Map<string, Revision>) {
23
+ let stack = [tip]
24
+ let done = new Set<string>()
25
+ while (stack.length) {
26
+ let oid = stack.at(-1)!
27
+ let rev = history.get(oid)
28
+ if (!rev) throw new Error(`git: missing revision ${oid}`)
29
+ let next = rev.parents.find((parent) => !done.has(parent))
30
+ if (next) stack.push(next)
31
+ else {
32
+ stack.pop()
33
+ done.add(oid)
34
+ yield [oid, rev] as const
35
+ }
36
+ }
37
+ }
38
+
39
+ /** What rewrites of one branch have read and written. Passed to a rewrite of
40
+ * a later tip, it reads and writes only the commits that landed since, so a
41
+ * rewrite that lost its ref to a push can be made again onto the new tip at
42
+ * the cost of what the push brought. What a rewrite wrote is read back from
43
+ * here only by a tip that reaches it, which only a stored object can be. */
44
+ export type Rewrites = {
45
+ history: Map<string, Revision>
46
+ trees: Map<string, Entry[]>
47
+ named: Map<string, Oids>
48
+ rooted: Map<string, string>
49
+ made: Map<string, string>
50
+ }
51
+
52
+ export let rewrites = (): Rewrites => ({
53
+ history: new Map(),
54
+ trees: new Map(),
55
+ named: new Map(),
56
+ rooted: new Map(),
57
+ made: new Map(),
58
+ })
59
+
60
+ /** Remove selected entries from one root directory throughout a branch.
61
+ * The selector sees the original snapshots in parent order. Commits whose
62
+ * tree and parents stand keep their exact object IDs. */
63
+ export let rewriteDirectory = async (
64
+ repo: Pick<Repo, 'objects' | 'bytes'>,
65
+ tip: string,
66
+ folder: string,
67
+ remove: (snapshot: Snapshot) => Set<string>,
68
+ memo: Rewrites = rewrites(),
69
+ ): Promise<{ tip: string; rewritten: Rewritten[] }> => {
70
+ let from = objects(repo.objects, repo.bytes)
71
+ let { history, trees } = memo
72
+ for (let [oid, bytes] of await from.commits(tip, history)) {
73
+ history.set(oid, { ...commitParts(bytes), bytes })
74
+ }
75
+ let read = async (ids: string[]) => {
76
+ let at = 0
77
+ for await (let obj of from.read(ids)) {
78
+ trees.set(ids[at++], treeEntries(obj.bytes))
79
+ }
80
+ }
81
+ let roots = [...new Set([...history.values()].map((rev) => rev.tree))]
82
+ let atFolder = (entries: Entry[]) =>
83
+ entries.find((entry) => entry.name == folder && entry.mode == DIR)
84
+ await read(roots.filter((oid) => !trees.has(oid)))
85
+ let folders = [
86
+ ...new Set(roots.flatMap((oid) => {
87
+ let child = atFolder(trees.get(oid)!)
88
+ return child ? [child.oid] : []
89
+ })),
90
+ ]
91
+ await read(folders.filter((oid) => !trees.has(oid)))
92
+
93
+ let named = new Map(memo.named)
94
+ let known = async (ids: string[]) => {
95
+ for (
96
+ let [oid, oid256] of await names(
97
+ repo.objects,
98
+ ids.filter((oid) => !named.has(oid)),
99
+ )
100
+ ) named.set(oid, { oid, oid256 })
101
+ return ids.map((oid) => {
102
+ let found = named.get(oid)
103
+ if (!found) throw new Error(`git: missing compatibility ID for ${oid}`)
104
+ return found
105
+ })
106
+ }
107
+ let rewritten: Rewritten[] = []
108
+ let moved = new Map<string, string>()
109
+ let rooted = new Map(memo.rooted)
110
+ let made = new Map(memo.made)
111
+ await preparing(repo, async (git) => {
112
+ let treeOf = async (entries: Entry[]) => {
113
+ let ids = await known(entries.map((entry) => entry.oid))
114
+ let tree = await git.tree(entries.map((entry, i) => ({
115
+ ...entry,
116
+ ...ids[i],
117
+ })))
118
+ named.set(tree.oid, tree)
119
+ trees.set(tree.oid, entries)
120
+ return tree.oid
121
+ }
122
+ let filtered = async (tree: string, discard: Set<string>) => {
123
+ let key = JSON.stringify([tree, [...discard].sort()])
124
+ let saved = rooted.get(key)
125
+ if (saved) return saved
126
+ let root = trees.get(tree)!
127
+ let child = atFolder(root)!
128
+ let entries = trees.get(child.oid)!.filter((e) => !discard.has(e.name))
129
+ let kept = root.filter((entry) => entry != child)
130
+ if (entries.length) kept.push({ ...child, oid: await treeOf(entries) })
131
+ let oid = await treeOf(kept)
132
+ rooted.set(key, oid)
133
+ return oid
134
+ }
135
+ for (let [oid, rev] of ordered(tip, history)) {
136
+ let child = atFolder(trees.get(rev.tree)!)
137
+ let discard = remove({
138
+ oid,
139
+ parents: rev.parents,
140
+ entries: child ? trees.get(child.oid)! : [],
141
+ })
142
+ let tree = discard.size ? await filtered(rev.tree, discard) : rev.tree
143
+ let parents = rev.parents.map((parent) => moved.get(parent) ?? parent)
144
+ if (tree == rev.tree && parents.every((p, i) => p == rev.parents[i])) {
145
+ continue
146
+ }
147
+ let key = [oid, tree, ...parents].join(' ')
148
+ let commit = made.get(key)
149
+ if (!commit) {
150
+ let ids = await known([tree, ...parents])
151
+ let bytes = rewrittenCommit(rev.bytes, tree, parents)
152
+ let receipt = await git.receive(
153
+ { type: 'commit', bytes },
154
+ new Map(ids.map((id) => [id.oid, id])),
155
+ )
156
+ named.set(receipt.oid, receipt)
157
+ made.set(key, commit = receipt.oid)
158
+ // A later tip that builds on this one walks it as read.
159
+ history.set(commit, { ...commitParts(bytes), bytes })
160
+ }
161
+ moved.set(oid, commit)
162
+ rewritten.push({
163
+ before: oid,
164
+ oid: commit,
165
+ tree,
166
+ parents,
167
+ message: rev.message,
168
+ })
169
+ }
170
+ })
171
+ Object.assign(memo, { named, rooted, made })
172
+ return { tip: moved.get(tip) ?? tip, rewritten }
173
+ }