@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/plugin.js ADDED
@@ -0,0 +1,66 @@
1
+ // The package as a graph plugin: the `ref` component, and the post-commit step
2
+ // that writes a Git commit when a release lands.
3
+ //
4
+ // It needs nothing from the application, because everything it would otherwise
5
+ // have to know about a release — what a release is called, where its manifest
6
+ // is, who authored it, what clock it happened on, which graphs and byte store
7
+ // hold its objects and branches — is supplied by the one callback passed to
8
+ // the factory ({@link Releases}). What is left is Git's: the objects, the
9
+ // branch, and the order they are written in.
10
+ //
11
+ // It hooks `effect` and only `effect`. A Git commit is written about data that
12
+ // is already durable — the release stands whether or not its history was
13
+ // written — so a failure here costs that release its Git commit and nothing
14
+ // else, which is exactly what the phase promises. It also means an application
15
+ // with its own post-commit hook registry (@yaks/effects) can register the same
16
+ // step there directly: {@link minting} is the step, and the plugin is that step
17
+ // plus the phase it is hooked on.
18
+ import { refDoc } from "./comp.js";
19
+ import { commitOnto } from "./refs.js";
20
+ /**
21
+ * The step itself: land every release in a committed transaction. An
22
+ * application with its own post-commit hook registry calls this directly;
23
+ * {@link commits} is the same step wrapped as a graph plugin.
24
+ */ export let minting = (r)=>async (bundles, tx)=>{
25
+ for (let b of bundles){
26
+ if (!b[r.comp]) continue;
27
+ let landing = await r.of(b, tx);
28
+ if (landing) await commitOnto(landing.repo, landing);
29
+ }
30
+ };
31
+ /**
32
+ * The Git plugin: the `ref` component, and an `effect` hook that writes a Git
33
+ * commit for every release the transaction committed.
34
+ *
35
+ * ```ts
36
+ * import { loadVocab } from '@yaks/vocab'
37
+ * import { graph } from '@yaks/graph'
38
+ * import { ram } from '@yaks/ram'
39
+ * import { commits, refDoc } from '@yaks/git'
40
+ *
41
+ * let deploy = { $defs: { deploy: { component: true, type: 'object' } } }
42
+ * let vocab = loadVocab([refDoc, deploy])
43
+ * // `of` says where each deploy lands, or null for one already committed.
44
+ * let plugin = commits({ comp: 'deploy', of: async () => null })
45
+ * let g = graph({ storage: ram(vocab), vocab, plugins: [plugin] })
46
+ * ```
47
+ *
48
+ * The Git object components are not part of this plugin's vocabulary: an
49
+ * application that keeps them in a store of their own loads {@link gitDoc}
50
+ * there, and this plugin's graph is wherever the branches are.
51
+ */ export let commits = (r)=>{
52
+ let mint = minting(r);
53
+ return {
54
+ name: '@yaks/git',
55
+ vocab: [
56
+ refDoc
57
+ ],
58
+ hooks: {
59
+ effect: async (bundles, tx)=>{
60
+ await mint(bundles, tx);
61
+ return bundles;
62
+ }
63
+ }
64
+ };
65
+ };
66
+ //# sourceMappingURL=plugin.js.map
package/plugin.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.js","sources":["./plugin.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,iDAAiD;AACjD,EAAE;AACF,+EAA+E;AAC/E,8EAA8E;AAC9E,8EAA8E;AAC9E,4EAA4E;AAC5E,0EAA0E;AAC1E,6CAA6C;AAC7C,EAAE;AACF,+EAA+E;AAC/E,yEAAyE;AACzE,4EAA4E;AAC5E,+EAA+E;AAC/E,+EAA+E;AAC/E,gFAAgF;AAChF,kCAAkC;AAGlC,SAAS,MAAM,oBAAmB;AAClC,SAAS,UAAU,oBAA4C;AAoB/D;;;;CAIC,GACD,OAAO,IAAI,UACT,CAAC,IAAgB,OAAO,SAAmB;IACzC,KAAK,IAAI,KAAK,QAAS;MACrB,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE;MAChB,IAAI,UAAU,MAAM,EAAE,EAAE,CAAC,GAAG;MAC5B,IAAI,SAAS,MAAM,WAAW,QAAQ,IAAI,EAAE;IAC9C;EACF,EAAC;AAEH;;;;;;;;;;;;;;;;;;;;CAoBC,GACD,OAAO,IAAI,UAAU,CAAC;EACpB,IAAI,OAAO,QAAQ;EACnB,OAAO;IACL,MAAM;IACN,OAAO;MAAC;KAAO;IACf,OAAO;MACL,QAAQ,OAAO,SAAS;QACtB,MAAM,KAAK,SAAS;QACpB,OAAO;MACT;IACF;EACF;AACF,EAAC"}
package/plugin.ts ADDED
@@ -0,0 +1,88 @@
1
+ // The package as a graph plugin: the `ref` component, and the post-commit step
2
+ // that writes a Git commit when a release lands.
3
+ //
4
+ // It needs nothing from the application, because everything it would otherwise
5
+ // have to know about a release — what a release is called, where its manifest
6
+ // is, who authored it, what clock it happened on, which graphs and byte store
7
+ // hold its objects and branches — is supplied by the one callback passed to
8
+ // the factory ({@link Releases}). What is left is Git's: the objects, the
9
+ // branch, and the order they are written in.
10
+ //
11
+ // It hooks `effect` and only `effect`. A Git commit is written about data that
12
+ // is already durable — the release stands whether or not its history was
13
+ // written — so a failure here costs that release its Git commit and nothing
14
+ // else, which is exactly what the phase promises. It also means an application
15
+ // with its own post-commit hook registry (@yaks/effects) can register the same
16
+ // step there directly: {@link minting} is the step, and the plugin is that step
17
+ // plus the phase it is hooked on.
18
+
19
+ import type { Bundle, Plugin, ReadTx } from 'jsr:@yaks/graph@0.2.36'
20
+ import { refDoc } from './comp.js'
21
+ import { commitOnto, type Landing, type Repo } from './refs.js'
22
+
23
+ /** A release, read in full: what to commit, and the repository to commit it
24
+ * in. */
25
+ export type Released = Landing & { repo: Repo }
26
+
27
+ /** How an application's releases become Git commits. */
28
+ export type Releases = {
29
+ /** the component that marks a bundle as a release — `deploy`, say */
30
+ comp: string
31
+ /**
32
+ * Turns one committed bundle into a release to land, or returns `null` when
33
+ * there is nothing to commit: a bundle that is not a release, one whose
34
+ * manifest will not parse, or one this graph already holds a commit for.
35
+ * Everything the application knows and this package cannot — the manifest,
36
+ * the author, the clock, the stores — is read here.
37
+ */
38
+ of: (b: Bundle, tx: ReadTx) => Promise<Released | null> | Released | null
39
+ }
40
+
41
+ /**
42
+ * The step itself: land every release in a committed transaction. An
43
+ * application with its own post-commit hook registry calls this directly;
44
+ * {@link commits} is the same step wrapped as a graph plugin.
45
+ */
46
+ export let minting =
47
+ (r: Releases) => async (bundles: Bundle[], tx: ReadTx): Promise<void> => {
48
+ for (let b of bundles) {
49
+ if (!b[r.comp]) continue
50
+ let landing = await r.of(b, tx)
51
+ if (landing) await commitOnto(landing.repo, landing)
52
+ }
53
+ }
54
+
55
+ /**
56
+ * The Git plugin: the `ref` component, and an `effect` hook that writes a Git
57
+ * commit for every release the transaction committed.
58
+ *
59
+ * ```ts
60
+ * import { loadVocab } from '@yaks/vocab'
61
+ * import { graph } from '@yaks/graph'
62
+ * import { ram } from '@yaks/ram'
63
+ * import { commits, refDoc } from '@yaks/git'
64
+ *
65
+ * let deploy = { $defs: { deploy: { component: true, type: 'object' } } }
66
+ * let vocab = loadVocab([refDoc, deploy])
67
+ * // `of` says where each deploy lands, or null for one already committed.
68
+ * let plugin = commits({ comp: 'deploy', of: async () => null })
69
+ * let g = graph({ storage: ram(vocab), vocab, plugins: [plugin] })
70
+ * ```
71
+ *
72
+ * The Git object components are not part of this plugin's vocabulary: an
73
+ * application that keeps them in a store of their own loads {@link gitDoc}
74
+ * there, and this plugin's graph is wherever the branches are.
75
+ */
76
+ export let commits = (r: Releases): Plugin => {
77
+ let mint = minting(r)
78
+ return {
79
+ name: '@yaks/git',
80
+ vocab: [refDoc],
81
+ hooks: {
82
+ effect: async (bundles, tx) => {
83
+ await mint(bundles, tx)
84
+ return bundles
85
+ },
86
+ },
87
+ }
88
+ }
package/process-cwd.py ADDED
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/python3 -I
2
+ # The privileged read for worktree collection. A held process directory ties
3
+ # the owner check, cwd and descriptor reads to one process even if its PID is reused.
4
+ import json
5
+ import os
6
+ import re
7
+ import sys
8
+
9
+ def paths(pid, uid):
10
+ fd = os.open("/proc/" + pid, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
11
+ try:
12
+ if os.fstat(fd).st_uid != uid:
13
+ raise ValueError("process belongs to another user")
14
+ cwd = os.readlink("cwd", dir_fd=fd)
15
+ if not cwd.startswith("/"):
16
+ raise ValueError("process directory is not absolute")
17
+ # Like realpath/readlink -e, a deleted cwd refuses collection.
18
+ os.stat(cwd)
19
+ paths = [cwd]
20
+ descriptors = os.open("fd", os.O_RDONLY | os.O_DIRECTORY, dir_fd=fd)
21
+ try:
22
+ for name in os.listdir(descriptors):
23
+ try:
24
+ path = os.readlink(name, dir_fd=descriptors)
25
+ if path.startswith("/"):
26
+ paths.append(path.removesuffix(" (deleted)"))
27
+ except FileNotFoundError:
28
+ pass
29
+ finally:
30
+ os.close(descriptors)
31
+ return paths
32
+ finally:
33
+ os.close(fd)
34
+
35
+
36
+ try:
37
+ pids = sys.argv[1:]
38
+ if not pids or not all(re.fullmatch(r"[1-9][0-9]*", pid) for pid in pids):
39
+ raise ValueError("expected positive PIDs")
40
+ uid = os.environ.get("SUDO_UID", "")
41
+ if not re.fullmatch(r"[0-9]+", uid):
42
+ raise ValueError("expected the sudo caller's UID")
43
+ uid = int(uid)
44
+ # Keep the single-PID door for already running workers.
45
+ if len(pids) == 1:
46
+ print(json.dumps(paths(pids[0], uid)))
47
+ else:
48
+ found = {}
49
+ for pid in pids:
50
+ try:
51
+ found[pid] = paths(pid, uid)
52
+ except (OSError, ValueError):
53
+ pass
54
+ print(json.dumps(found))
55
+ except (OSError, ValueError) as error:
56
+ print("yak-process-cwd: " + str(error), file=sys.stderr)
57
+ sys.exit(1)
package/project.js ADDED
@@ -0,0 +1,142 @@
1
+ import { commitParts, projectedCommit } from "./commit.js";
2
+ import { names } from "./index.js";
3
+ import { preparing } from "./refs.js";
4
+ import { BadPush } from "./unpack.js";
5
+ import { DIR, treeEntries } from "./tree.js";
6
+ /** Follow the branch through the first parent that reaches its former tip. */ export let pushedCommits = async (before, tip, read)=>{
7
+ let next = [
8
+ {
9
+ oid: tip
10
+ }
11
+ ];
12
+ let commits = new Map();
13
+ while(next.length){
14
+ let { oid, child } = next.pop();
15
+ if (oid == before) {
16
+ let selected = [];
17
+ for(let commit = child; commit; commit = commits.get(commit.oid)?.child){
18
+ selected.push(commit);
19
+ }
20
+ return selected;
21
+ }
22
+ if (commits.has(oid)) continue;
23
+ let object = await read(oid);
24
+ if (object?.type != 'commit') throw new BadPush(`missing commit ${oid}`);
25
+ let commit = {
26
+ oid,
27
+ bytes: object.bytes,
28
+ ...commitParts(object.bytes)
29
+ };
30
+ commits.set(oid, {
31
+ commit,
32
+ child
33
+ });
34
+ if (!before && !commit.parents.length) {
35
+ let selected = [
36
+ commit
37
+ ];
38
+ for(let at = child; at; at = commits.get(at.oid)?.child){
39
+ selected.push(at);
40
+ }
41
+ return selected;
42
+ }
43
+ for (let parent of before ? [
44
+ ...commit.parents
45
+ ].reverse() : commit.parents.slice(0, 1)){
46
+ next.push({
47
+ oid: parent,
48
+ child: commit
49
+ });
50
+ }
51
+ }
52
+ throw new BadPush('non-fast-forward; pull before pushing again');
53
+ };
54
+ let subtrees = async (id, read, folder)=>{
55
+ let children = async (oid)=>{
56
+ let object = await read(oid);
57
+ if (object?.type != 'tree') throw new BadPush(`missing tree ${oid}`);
58
+ return treeEntries(object.bytes);
59
+ };
60
+ if (!id) return new Map();
61
+ let entry = (await children(id)).find((e)=>e.name == folder);
62
+ if (!entry) return new Map();
63
+ if (entry.mode != DIR) throw new BadPush(`${folder} must be a directory`);
64
+ let dirs = new Map();
65
+ for (let child of (await children(entry.oid))){
66
+ if (child.mode != DIR) {
67
+ throw new BadPush(`${folder}/${child.name} must be a directory`);
68
+ }
69
+ dirs.set(child.name, child.oid);
70
+ }
71
+ return dirs;
72
+ };
73
+ /** The names a commit's snapshot holds in one root directory. */ export let namesAt = async (commit, read, folder)=>{
74
+ let object = await read(commit);
75
+ if (object?.type != 'commit') throw new BadPush(`missing commit ${commit}`);
76
+ return [
77
+ ...(await subtrees(commitParts(object.bytes).tree, read, folder)).keys()
78
+ ];
79
+ };
80
+ /** Subtrees added, changed or removed from one directory between snapshots. */ let changedDirs = async (before, after, read, folder)=>{
81
+ if (before == after) return [];
82
+ let [old, next] = await Promise.all([
83
+ subtrees(before, read, folder),
84
+ subtrees(after, read, folder)
85
+ ]);
86
+ return [
87
+ ...new Set([
88
+ ...old.keys(),
89
+ ...next.keys()
90
+ ])
91
+ ].filter((slug)=>old.get(slug) != next.get(slug)).map((slug)=>({
92
+ slug,
93
+ tree: next.get(slug) ?? null
94
+ }));
95
+ };
96
+ /** Give a space commit an app root and app parents for the split history. */ export let projectCommit = async (repo, bytes, tree, parent, repository)=>{
97
+ let ids = [
98
+ tree,
99
+ ...parent ? [
100
+ parent
101
+ ] : []
102
+ ];
103
+ let keys = await names(repo.objects, ids);
104
+ return await preparing(repo, (git)=>git.receive({
105
+ type: 'commit',
106
+ bytes: projectedCommit(bytes, tree, parent, repository)
107
+ }, new Map(ids.map((oid)=>{
108
+ let oid256 = keys.get(oid);
109
+ if (!oid256) throw new Error(`git: missing compatibility ID for ${oid}`);
110
+ return [
111
+ oid,
112
+ {
113
+ oid,
114
+ oid256
115
+ }
116
+ ];
117
+ }))));
118
+ };
119
+ /** Group changed subtrees by directory in branch order. */ export let pushChanges = async (before, tip, read, folder)=>{
120
+ let [commits, first] = await Promise.all([
121
+ pushedCommits(before, tip, read),
122
+ before ? read(before) : null
123
+ ]);
124
+ let tree = first ? commitParts(first.bytes).tree : null;
125
+ let changes = new Map();
126
+ for (let commit of commits){
127
+ for (let change of (await changedDirs(tree, commit.tree, read, folder))){
128
+ let versions = changes.get(change.slug) ?? [];
129
+ versions.push({
130
+ tree: change.tree,
131
+ commit
132
+ });
133
+ changes.set(change.slug, versions);
134
+ }
135
+ tree = commit.tree;
136
+ }
137
+ return {
138
+ commits,
139
+ changes
140
+ };
141
+ };
142
+ //# sourceMappingURL=project.js.map
package/project.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"project.js","sources":["./project.ts"],"names":[],"mappings":"AAAA,SAAS,WAAW,EAAE,eAAe,sBAAqB;AAC1D,SAAS,KAAK,qBAAoB;AAElC,SAAS,SAAS,oBAA8B;AAChD,SAAS,OAAO,sBAAsC;AACtD,SAAS,GAAG,EAAE,WAAW,oBAAmB;AAO5C,4EAA4E,GAC5E,OAAO,IAAI,gBAAgB,OACzB,QACA,KACA;EAEA,IAAI,OAA4C;IAAC;MAAE,KAAK;IAAI;GAAE;EAC9D,IAAI,UAAU,IAAI;EAClB,MAAO,KAAK,MAAM,CAAE;IAClB,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,KAAK,GAAG;IAC7B,IAAI,OAAO,QAAQ;MACjB,IAAI,WAAuB,EAAE;MAC7B,IACE,IAAI,SAAS,OACb,QACA,SAAS,QAAQ,GAAG,CAAC,OAAO,GAAG,GAAG,MAClC;QACA,SAAS,IAAI,CAAC;MAChB;MACA,OAAO;IACT;IACA,IAAI,QAAQ,GAAG,CAAC,MAAM;IACtB,IAAI,SAAS,MAAM,KAAK;IACxB,IAAI,QAAQ,QAAQ,UAAU,MAAM,IAAI,QAAQ,CAAC,eAAe,EAAE,KAAK;IACvE,IAAI,SAAS;MAAE;MAAK,OAAO,OAAO,KAAK;MAAE,GAAG,YAAY,OAAO,KAAK,CAAC;IAAC;IACtE,QAAQ,GAAG,CAAC,KAAK;MAAE;MAAQ;IAAM;IACjC,IAAI,CAAC,UAAU,CAAC,OAAO,OAAO,CAAC,MAAM,EAAE;MACrC,IAAI,WAAW;QAAC;OAAO;MACvB,IAAK,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,SAAS,KAAK,UAAU;GACzB;EACD,IAAI,OAAO,QAAQ,YAAY,MAAM,KAAK,EAAE,IAAI,GAAG;EACnD,IAAI,UAAU,IAAI;EAClB,KAAK,IAAI,UAAU,QAAS;IAC1B,KAAK,IAAI,UAAU,CAAA,MAAM,YAAY,MAAM,OAAO,IAAI,EAAE,MAAM,OAAM,EAAG;MACrE,IAAI,WAAW,QAAQ,GAAG,CAAC,OAAO,IAAI,KAAK,EAAE;MAC7C,SAAS,IAAI,CAAC;QAAE,MAAM,OAAO,IAAI;QAAE;MAAO;MAC1C,QAAQ,GAAG,CAAC,OAAO,IAAI,EAAE;IAC3B;IACA,OAAO,OAAO,IAAI;EACpB;EACA,OAAO;IAAE;IAAS;EAAQ;AAC5B,EAAC"}
package/project.ts ADDED
@@ -0,0 +1,151 @@
1
+ import { commitParts, projectedCommit } from './commit.js'
2
+ import { names } from './index.js'
3
+ import type { Oids } from './oid.ts'
4
+ import { preparing, type Repo } from './refs.js'
5
+ import { BadPush, type ReadObject } from './unpack.js'
6
+ import { DIR, treeEntries } from './tree.js'
7
+
8
+ type Revision =
9
+ & { oid: string; bytes: Uint8Array }
10
+ & ReturnType<typeof commitParts>
11
+ type Change = { tree: string | null; commit: Revision }
12
+
13
+ /** Follow the branch through the first parent that reaches its former tip. */
14
+ export let pushedCommits = async (
15
+ before: string | null,
16
+ tip: string,
17
+ read: ReadObject,
18
+ ): Promise<Revision[]> => {
19
+ let next: { oid: string; child?: Revision }[] = [{ oid: tip }]
20
+ let commits = new Map<string, { commit: Revision; child?: Revision }>()
21
+ while (next.length) {
22
+ let { oid, child } = next.pop()!
23
+ if (oid == before) {
24
+ let selected: Revision[] = []
25
+ for (
26
+ let commit = child;
27
+ commit;
28
+ commit = commits.get(commit.oid)?.child
29
+ ) {
30
+ selected.push(commit)
31
+ }
32
+ return selected
33
+ }
34
+ if (commits.has(oid)) continue
35
+ let object = await read(oid)
36
+ if (object?.type != 'commit') throw new BadPush(`missing commit ${oid}`)
37
+ let commit = { oid, bytes: object.bytes, ...commitParts(object.bytes) }
38
+ commits.set(oid, { commit, child })
39
+ if (!before && !commit.parents.length) {
40
+ let selected = [commit]
41
+ for (let at = child; at; at = commits.get(at.oid)?.child) {
42
+ selected.push(at)
43
+ }
44
+ return selected
45
+ }
46
+ for (
47
+ let parent
48
+ of (before ? [...commit.parents].reverse() : commit.parents.slice(0, 1))
49
+ ) {
50
+ next.push({ oid: parent, child: commit })
51
+ }
52
+ }
53
+ throw new BadPush('non-fast-forward; pull before pushing again')
54
+ }
55
+
56
+ let subtrees = async (id: string | null, read: ReadObject, folder: string) => {
57
+ let children = async (oid: string) => {
58
+ let object = await read(oid)
59
+ if (object?.type != 'tree') throw new BadPush(`missing tree ${oid}`)
60
+ return treeEntries(object.bytes)
61
+ }
62
+ if (!id) return new Map<string, string>()
63
+ let entry = (await children(id)).find((e) => e.name == folder)
64
+ if (!entry) return new Map<string, string>()
65
+ if (entry.mode != DIR) throw new BadPush(`${folder} must be a directory`)
66
+ let dirs = new Map<string, string>()
67
+ for (let child of await children(entry.oid)) {
68
+ if (child.mode != DIR) {
69
+ throw new BadPush(`${folder}/${child.name} must be a directory`)
70
+ }
71
+ dirs.set(child.name, child.oid)
72
+ }
73
+ return dirs
74
+ }
75
+
76
+ /** The names a commit's snapshot holds in one root directory. */
77
+ export let namesAt = async (
78
+ commit: string,
79
+ read: ReadObject,
80
+ folder: string,
81
+ ): Promise<string[]> => {
82
+ let object = await read(commit)
83
+ if (object?.type != 'commit') throw new BadPush(`missing commit ${commit}`)
84
+ return [...(await subtrees(commitParts(object.bytes).tree, read, folder))
85
+ .keys()]
86
+ }
87
+
88
+ /** Subtrees added, changed or removed from one directory between snapshots. */
89
+ let changedDirs = async (
90
+ before: string | null,
91
+ after: string,
92
+ read: ReadObject,
93
+ folder: string,
94
+ ) => {
95
+ if (before == after) return []
96
+ let [old, next] = await Promise.all([
97
+ subtrees(before, read, folder),
98
+ subtrees(after, read, folder),
99
+ ])
100
+ return [...new Set([...old.keys(), ...next.keys()])]
101
+ .filter((slug) => old.get(slug) != next.get(slug))
102
+ .map((slug) => ({ slug, tree: next.get(slug) ?? null }))
103
+ }
104
+
105
+ /** Give a space commit an app root and app parents for the split history. */
106
+ export let projectCommit = async (
107
+ repo: Pick<Repo, 'objects' | 'bytes'>,
108
+ bytes: Uint8Array,
109
+ tree: string,
110
+ parent: string | null,
111
+ repository: string,
112
+ ): Promise<Oids> => {
113
+ let ids = [tree, ...parent ? [parent] : []]
114
+ let keys = await names(repo.objects, ids)
115
+ return await preparing(repo, (git) =>
116
+ git.receive(
117
+ {
118
+ type: 'commit',
119
+ bytes: projectedCommit(bytes, tree, parent, repository),
120
+ },
121
+ new Map(ids.map((oid) => {
122
+ let oid256 = keys.get(oid)
123
+ if (!oid256) throw new Error(`git: missing compatibility ID for ${oid}`)
124
+ return [oid, { oid, oid256 }]
125
+ })),
126
+ ))
127
+ }
128
+
129
+ /** Group changed subtrees by directory in branch order. */
130
+ export let pushChanges = async (
131
+ before: string | null,
132
+ tip: string,
133
+ read: ReadObject,
134
+ folder: string,
135
+ ): Promise<{ commits: Revision[]; changes: Map<string, Change[]> }> => {
136
+ let [commits, first] = await Promise.all([
137
+ pushedCommits(before, tip, read),
138
+ before ? read(before) : null,
139
+ ])
140
+ let tree = first ? commitParts(first.bytes).tree : null
141
+ let changes = new Map<string, Change[]>()
142
+ for (let commit of commits) {
143
+ for (let change of await changedDirs(tree, commit.tree, read, folder)) {
144
+ let versions = changes.get(change.slug) ?? []
145
+ versions.push({ tree: change.tree, commit })
146
+ changes.set(change.slug, versions)
147
+ }
148
+ tree = commit.tree
149
+ }
150
+ return { commits, changes }
151
+ }