@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/service.js ADDED
@@ -0,0 +1,104 @@
1
+ import * as io from "@yaks/file/io";
2
+ import { line } from "@yaks/query";
3
+ import { retained } from "./workspace.js";
4
+ import { sleep } from "@yaks/effects";
5
+ import { discover, idleFor, inUse, linked, localFilesystem, processPaths, reclaim, reconcile, repositoryEid, shelve, worktreeEid } from "./host.js";
6
+ let HOUR = 3_600_000;
7
+ /** How long Git must have left a worktree alone before it is taken back. */ export let IDLE = 15 * 60_000;
8
+ /** How long a worktree that still holds work must be left alone before it is
9
+ * shelved (./host.ts `shelve`): its work committed onto its branch and the
10
+ * checkout removed. Three days, past any agent's run. */ export let STALE = 72 * HOUR;
11
+ /** How long the service waits between passes. */ export let EVERY = IDLE;
12
+ /** Where agents cut checkouts of their own: Claude Code's, inside the primary
13
+ * checkout. */ export let ROOTS = [
14
+ '.claude/worktrees'
15
+ ];
16
+ /// within('/r/.claude/worktrees/a', ['/r/.claude/worktrees']) -> true
17
+ /// within('/r/.claude/worktrees', ['/r/.claude/worktrees']) -> false
18
+ /// within('/r/elsewhere', ['/r/.claude/worktrees']) -> false
19
+ /** Whether a checkout lies under one of these directories. */ export let within = (path, roots)=>roots.some((root)=>path.startsWith(root.replace(/\/+$/, '') + '/'));
20
+ // The roots, as real paths: a relative one resolved in the primary checkout
21
+ // beside `common`.
22
+ let rooted = (common, roots)=>Promise.all(roots.map((root)=>{
23
+ let path = root.startsWith('/') ? root : common.replace(/\/\.git\/?$/, '') + '/' + root;
24
+ return io.realPath(path).catch(()=>path);
25
+ }));
26
+ let there = (path)=>io.stat(path).then(()=>true, ()=>false);
27
+ /** Take back every idle worktree of the repository at `common` that holds
28
+ * nothing, managed or under one of `roots`, shelve each one `stale` that
29
+ * still holds work, and return the idle ones kept, with why. A `signal` that
30
+ * aborts ends the pass before the next worktree. */ export let collect = async (g, common, idle = IDLE, now = Date.now(), processes = processPaths, signal, roots = ROOTS, stale = STALE)=>{
31
+ let filesystem = await localFilesystem(g);
32
+ let repository = repositoryEid(common, filesystem.entity.eid);
33
+ await reconcile(g, common, repository);
34
+ let own = await rooted(common, roots);
35
+ let kept = {};
36
+ for (let path of (await linked(common))){
37
+ if (signal?.aborted) break;
38
+ let quiet = await idleFor(path, now);
39
+ if (quiet < idle) continue;
40
+ let real = await io.realPath(path).catch(()=>path);
41
+ let [row] = await g.get([
42
+ worktreeEid(repository, real)
43
+ ]);
44
+ if (!row?.worktree?.managed && !within(real, own)) {
45
+ continue;
46
+ }
47
+ // The checkout's own row: its repository's refs are the same for every
48
+ // checkout here, and a shelving below records the one it moves.
49
+ let seen = await discover(g, path, {
50
+ refs: false
51
+ }).catch(()=>row);
52
+ if (!seen || await retained(g, seen.entity.eid)) continue;
53
+ let free = async ()=>!signal?.aborted && !await retained(g, seen.entity.eid) && !inUse(real, await processes());
54
+ let failed = ()=>'failed';
55
+ // A checkout still at the commit it was cut from holds no work of its own.
56
+ let base = seen.checkout?.base;
57
+ let held = await reclaim(path, 'refs/heads/main', typeof base == 'string' ? base : undefined, free).catch(failed);
58
+ // Work left this long is no agent's in progress: it moves onto its
59
+ // branch, where it costs no disk, and the checkout goes.
60
+ if ((held == 'dirty' || held == 'unlanded') && quiet >= stale) {
61
+ held = await shelve(path, ()=>discover(g, path), free).catch(failed);
62
+ }
63
+ if (held && held != 'taken') kept[path] = held;
64
+ }
65
+ return kept;
66
+ };
67
+ /** Collect every repository the graph knows that is on this machine, for as
68
+ * long as `signal` lets it: one pass when it has already aborted, the way a
69
+ * one-shot command runs a service. A removal Git refused is reported; a
70
+ * worktree kept for what it holds is not, since that is an agent's work in
71
+ * progress. */ export let service = async (host, options = {}, signal = AbortSignal.abort(), processes = processPaths)=>{
72
+ let report = (error, root)=>host.report ? host.report(error, {
73
+ tags: {
74
+ package: '@yaks/git',
75
+ step: 'worktree collection',
76
+ root
77
+ }
78
+ }) : console.error('@yaks/git worktree collection —', root, error);
79
+ // A pass the service is told to stop (its lease taken by another process)
80
+ // ends between worktrees; a one-shot pass runs whole.
81
+ let stopping = signal.aborted ? undefined : signal;
82
+ for(;;){
83
+ let filesystem = await localFilesystem(host.graph);
84
+ for (let r of (await host.graph.read(line`.repository.workspace=${filesystem.entity.eid}`))){
85
+ if (stopping?.aborted) return;
86
+ let common = r.repository.common;
87
+ if (typeof common != 'string') continue;
88
+ // One repository failing is reported and the pass goes on to the next;
89
+ // what it left is the next pass's to find.
90
+ let kept = await (await there(common) ? collect(host.graph, common, options.idle, Date.now(), processes, stopping, options.roots, options.stale) : reconcile(host.graph, common, r.entity.eid).then(()=>({}))).catch(async (error)=>{
91
+ await report(error, common);
92
+ return {};
93
+ });
94
+ let failed = Object.keys(kept).filter((p)=>kept[p] == 'failed');
95
+ if (failed.length) {
96
+ await report(new Error(`Could not take back ${failed.join(', ')}`), common);
97
+ }
98
+ }
99
+ if (signal.aborted) return;
100
+ await sleep(options.every ?? EVERY, signal);
101
+ if (signal.aborted) return;
102
+ }
103
+ };
104
+ //# sourceMappingURL=service.js.map
package/service.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.js","sources":["./service.ts"],"names":[],"mappings":"AAAA,YAAY,6BAAkC;AAC9C,SA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QAAQ,KAAK,IAAI,OAAO;IACpC,IAAI,OAAO,OAAO,EAAE;EACtB;AACF,EAAC"}
package/service.ts ADDED
@@ -0,0 +1,194 @@
1
+ import * as io from '@yaks/file/io'
2
+ import { line } from '@yaks/query'
3
+ // Host-checkout maintenance: idle, landed worktrees can be reclaimed only
4
+ // when no graph session's explicitly attached machine or local process uses
5
+ // them. Machine-provisioned sandboxes are their provider's business, not this
6
+ // service's. Managed checkout observations remain restore history.
7
+
8
+ import type { Comp, Graph } from 'jsr:@yaks/graph@0.2.36'
9
+ import { retained } from './workspace.js'
10
+ import { sleep } from '@yaks/effects'
11
+ import {
12
+ discover,
13
+ type Held,
14
+ idleFor,
15
+ inUse,
16
+ linked,
17
+ localFilesystem,
18
+ processPaths,
19
+ reclaim,
20
+ reconcile,
21
+ repositoryEid,
22
+ shelve,
23
+ worktreeEid,
24
+ } from './host.js'
25
+
26
+ let HOUR = 3_600_000
27
+
28
+ /** How long Git must have left a worktree alone before it is taken back. */
29
+ export let IDLE = 15 * 60_000
30
+ /** How long a worktree that still holds work must be left alone before it is
31
+ * shelved (./host.ts `shelve`): its work committed onto its branch and the
32
+ * checkout removed. Three days, past any agent's run. */
33
+ export let STALE = 72 * HOUR
34
+ /** How long the service waits between passes. */
35
+ export let EVERY = IDLE
36
+ /** Where agents cut checkouts of their own: Claude Code's, inside the primary
37
+ * checkout. */
38
+ export let ROOTS = ['.claude/worktrees']
39
+
40
+ export type Options = {
41
+ /** milliseconds without Git touching a worktree (default {@link IDLE}) */
42
+ idle?: number
43
+ /** milliseconds without Git touching a worktree that still holds work
44
+ * before it is shelved (default {@link STALE}) */
45
+ stale?: number
46
+ /** milliseconds between passes (default {@link EVERY}) */
47
+ every?: number
48
+ /** directories whose checkouts are agents' own, taken back like managed
49
+ * ones; a relative one is inside the repository's primary checkout
50
+ * (default {@link ROOTS}) */
51
+ roots?: string[]
52
+ }
53
+
54
+ /// within('/r/.claude/worktrees/a', ['/r/.claude/worktrees']) -> true
55
+ /// within('/r/.claude/worktrees', ['/r/.claude/worktrees']) -> false
56
+ /// within('/r/elsewhere', ['/r/.claude/worktrees']) -> false
57
+ /** Whether a checkout lies under one of these directories. */
58
+ export let within = (path: string, roots: string[]): boolean =>
59
+ roots.some((root) => path.startsWith(root.replace(/\/+$/, '') + '/'))
60
+
61
+ // The roots, as real paths: a relative one resolved in the primary checkout
62
+ // beside `common`.
63
+ let rooted = (common: string, roots: string[]): Promise<string[]> =>
64
+ Promise.all(roots.map((root) => {
65
+ let path = root.startsWith('/')
66
+ ? root
67
+ : common.replace(/\/\.git\/?$/, '') + '/' + root
68
+ return io.realPath(path).catch(() => path)
69
+ }))
70
+
71
+ let there = (path: string) => io.stat(path).then(() => true, () => false)
72
+
73
+ /** Take back every idle worktree of the repository at `common` that holds
74
+ * nothing, managed or under one of `roots`, shelve each one `stale` that
75
+ * still holds work, and return the idle ones kept, with why. A `signal` that
76
+ * aborts ends the pass before the next worktree. */
77
+ export let collect = async (
78
+ g: Graph,
79
+ common: string,
80
+ idle: number = IDLE,
81
+ now: number = Date.now(),
82
+ processes: () => Promise<Set<string>> = processPaths,
83
+ signal?: AbortSignal,
84
+ roots: string[] = ROOTS,
85
+ stale: number = STALE,
86
+ ): Promise<Record<string, Held>> => {
87
+ let filesystem = await localFilesystem(g)
88
+ let repository = repositoryEid(common, filesystem.entity.eid)
89
+ await reconcile(g, common, repository)
90
+ let own = await rooted(common, roots)
91
+ let kept: Record<string, Held> = {}
92
+ for (let path of await linked(common)) {
93
+ if (signal?.aborted) break
94
+ let quiet = await idleFor(path, now)
95
+ if (quiet < idle) continue
96
+ let real = await io.realPath(path).catch(() => path)
97
+ let [row] = await g.get([worktreeEid(repository, real)])
98
+ if (!(row?.worktree as Comp | undefined)?.managed && !within(real, own)) {
99
+ continue
100
+ }
101
+ // The checkout's own row: its repository's refs are the same for every
102
+ // checkout here, and a shelving below records the one it moves.
103
+ let seen = await discover(g, path, { refs: false }).catch(() => row)
104
+ if (!seen || await retained(g, seen.entity.eid)) continue
105
+ let free = async () =>
106
+ !signal?.aborted && !await retained(g, seen.entity.eid) &&
107
+ !inUse(real, await processes())
108
+ let failed = () => 'failed' as Held
109
+ // A checkout still at the commit it was cut from holds no work of its own.
110
+ let base = (seen.checkout as Comp | undefined)?.base
111
+ let held = await reclaim(
112
+ path,
113
+ 'refs/heads/main',
114
+ typeof base == 'string' ? base : undefined,
115
+ free,
116
+ ).catch(failed)
117
+ // Work left this long is no agent's in progress: it moves onto its
118
+ // branch, where it costs no disk, and the checkout goes.
119
+ if ((held == 'dirty' || held == 'unlanded') && quiet >= stale) {
120
+ held = await shelve(path, () => discover(g, path), free).catch(failed)
121
+ }
122
+ if (held && held != 'taken') kept[path] = held
123
+ }
124
+ return kept
125
+ }
126
+
127
+ /** Collect every repository the graph knows that is on this machine, for as
128
+ * long as `signal` lets it: one pass when it has already aborted, the way a
129
+ * one-shot command runs a service. A removal Git refused is reported; a
130
+ * worktree kept for what it holds is not, since that is an agent's work in
131
+ * progress. */
132
+ export let service = async (
133
+ host: {
134
+ graph: Graph
135
+ report?: (
136
+ error: unknown,
137
+ context: { tags: Record<string, string> },
138
+ ) => void | Promise<void>
139
+ },
140
+ options: Options = {},
141
+ signal: AbortSignal = AbortSignal.abort(),
142
+ processes: () => Promise<Set<string>> = processPaths,
143
+ ): Promise<void> => {
144
+ let report = (error: unknown, root: string) =>
145
+ host.report
146
+ ? host.report(error, {
147
+ tags: { package: '@yaks/git', step: 'worktree collection', root },
148
+ })
149
+ : console.error('@yaks/git worktree collection —', root, error)
150
+ // A pass the service is told to stop (its lease taken by another process)
151
+ // ends between worktrees; a one-shot pass runs whole.
152
+ let stopping = signal.aborted ? undefined : signal
153
+ for (;;) {
154
+ let filesystem = await localFilesystem(host.graph)
155
+ for (
156
+ let r of await host.graph.read(
157
+ line`.repository.workspace=${filesystem.entity.eid}`,
158
+ )
159
+ ) {
160
+ if (stopping?.aborted) return
161
+ let common = (r.repository as Comp).common
162
+ if (typeof common != 'string') continue
163
+ // One repository failing is reported and the pass goes on to the next;
164
+ // what it left is the next pass's to find.
165
+ let kept: Record<string, Held> = await (await there(common)
166
+ ? collect(
167
+ host.graph,
168
+ common,
169
+ options.idle,
170
+ Date.now(),
171
+ processes,
172
+ stopping,
173
+ options.roots,
174
+ options.stale,
175
+ )
176
+ : reconcile(host.graph, common, r.entity.eid).then(() => ({}))).catch(
177
+ async (error) => {
178
+ await report(error, common)
179
+ return {}
180
+ },
181
+ )
182
+ let failed = Object.keys(kept).filter((p) => kept[p] == 'failed')
183
+ if (failed.length) {
184
+ await report(
185
+ new Error(`Could not take back ${failed.join(', ')}`),
186
+ common,
187
+ )
188
+ }
189
+ }
190
+ if (signal.aborted) return
191
+ await sleep(options.every ?? EVERY, signal)
192
+ if (signal.aborted) return
193
+ }
194
+ }
package/sha1.js ADDED
@@ -0,0 +1,115 @@
1
+ // SHA-1 with its state kept between chunks — the one digest in this package
2
+ // that is not `crypto.subtle`'s.
3
+ //
4
+ // A packfile ends in the SHA-1 of every byte before it, and a packfile is a
5
+ // stream: objects arrive one at a time and leave as soon as they are deflated.
6
+ // `crypto.subtle.digest` wants the whole message in one buffer, so computing
7
+ // the trailer with it would mean holding the finished packfile in memory to
8
+ // write twenty bytes at the end — a clone's worth of bytes, for a trailer.
9
+ // Here the digest is fed as the stream goes past, so a packfile of any size
10
+ // costs one 64-byte block.
11
+ //
12
+ // Every byte of a pack passes through it, so it is the runtime's own hash
13
+ // wherever the runtime has one: `node:crypto`, which Deno, Node and workerd
14
+ // (`nodejs_compat`) all supply, reached at run time so a browser bundle needs
15
+ // none. In workerd it is about ten times as fast as script and makes no
16
+ // garbage. A browser falls back to `portableSha1`.
17
+ //
18
+ // SHA-1 is a name here and never a security claim, the same as the object ids
19
+ // in ./oid.ts: it is what the packfile format specifies, so it is what we
20
+ // write.
21
+ /** A digest in progress: bytes in, the twenty bytes that name them out. */ // The runtime's streaming SHA-1, where it has `node:crypto`.
22
+ let hashing = (()=>{
23
+ let process = globalThis.process;
24
+ let crypto = process?.getBuiltinModule?.('node:crypto');
25
+ let create = crypto?.createHash;
26
+ return create && (()=>create('sha1'));
27
+ })();
28
+ let native = (start)=>{
29
+ let hash = start();
30
+ return {
31
+ update: (bytes)=>void hash.update(bytes),
32
+ digest: ()=>new Uint8Array(hash.digest())
33
+ };
34
+ };
35
+ /** Start a SHA-1: the runtime's own where it has one, else
36
+ * {@link portableSha1}. */ export let sha1 = ()=>hashing ? native(hashing) : portableSha1();
37
+ let rotl = (x, n)=>x << n | x >>> 32 - n;
38
+ /** Start a SHA-1 in script, for a runtime without `node:crypto`. The
39
+ * algorithm is FIPS 180-4 section 6.1.2 verbatim, in 32-bit integer
40
+ * arithmetic: the `| 0` after every addition is what keeps a JavaScript number
41
+ * a machine word. */ export let portableSha1 = ()=>{
42
+ let h = new Int32Array([
43
+ 0x67452301,
44
+ 0xefcdab89,
45
+ 0x98badcfe,
46
+ 0x10325476,
47
+ 0xc3d2e1f0
48
+ ]);
49
+ let block = new Uint8Array(64);
50
+ let view = new DataView(block.buffer);
51
+ let w = new Int32Array(80);
52
+ let at = 0 // bytes waiting in the block
53
+ ;
54
+ let len = 0 // bytes seen, all told
55
+ ;
56
+ // One 64-byte block: the message schedule, then eighty rounds of it.
57
+ let round = ()=>{
58
+ for(let i = 0; i < 16; i++)w[i] = view.getInt32(i * 4);
59
+ for(let i = 16; i < 80; i++){
60
+ w[i] = rotl(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
61
+ }
62
+ let a = h[0], b = h[1], c = h[2], d = h[3], e = h[4];
63
+ for(let i = 0; i < 80; i++){
64
+ let f = i < 20 ? b & c | ~b & d : i < 40 ? b ^ c ^ d : i < 60 ? b & c | b & d | c & d : b ^ c ^ d;
65
+ let k = i < 20 ? 0x5a827999 : i < 40 ? 0x6ed9eba1 : i < 60 ? 0x8f1bbcdc : 0xca62c1d6;
66
+ let t = rotl(a, 5) + f + e + k + w[i] | 0;
67
+ e = d;
68
+ d = c;
69
+ c = rotl(b, 30);
70
+ b = a;
71
+ a = t;
72
+ }
73
+ h[0] += a;
74
+ h[1] += b;
75
+ h[2] += c;
76
+ h[3] += d;
77
+ h[4] += e;
78
+ };
79
+ let update = (bytes)=>{
80
+ len += bytes.length;
81
+ for(let i = 0; i < bytes.length;){
82
+ let n = Math.min(64 - at, bytes.length - i);
83
+ block.set(bytes.subarray(i, i + n), at);
84
+ at += n;
85
+ i += n;
86
+ if (at == 64) {
87
+ round();
88
+ at = 0;
89
+ }
90
+ }
91
+ };
92
+ let one = new Uint8Array(1);
93
+ let pad = new Uint8Array(64);
94
+ let digest = ()=>{
95
+ // This is the message's own length, so it is read before the padding adds
96
+ // to it.
97
+ let bits = len * 8;
98
+ one[0] = 0x80;
99
+ update(one);
100
+ update(pad.subarray(0, (56 - at + 64) % 64));
101
+ view.setUint32(56, Math.floor(bits / 0x100000000));
102
+ view.setUint32(60, bits >>> 0);
103
+ round();
104
+ at = 0;
105
+ let out = new Uint8Array(20);
106
+ let name = new DataView(out.buffer);
107
+ h.forEach((x, i)=>name.setUint32(i * 4, x >>> 0));
108
+ return out;
109
+ };
110
+ return {
111
+ update,
112
+ digest
113
+ };
114
+ };
115
+ //# sourceMappingURL=sha1.js.map
package/sha1.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sha1.js","sources":["./sha1.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,iCAAiC;AACjC,EAAE;AACF,4EAA4E;AAC5E,+EAA+E;AAC/E,6EAA6E;AAC7E,4EAA4E;AAC5E,2EAA2E;AAC3E,4EAA4E;AAC5E,2BAA2B;AAC3B,EAAE;AACF,0EAA0E;AAC1E,4EAA4E;AAC5E,8EAA8E;AAC9E,wEAAwE;AACxE,mDAAmD;AACnD,EAAE;AACF,8EAA8E;AAC9E,0EAA0E;AAC1E,SAAS;AAET,yEAAyE,GAWzE,6DAA6D;AAC7D,IAAI,UAAU,CAAC;EACb,IAAI,UAAU,AAAC,WAAsC,O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O,IAAI,SAAS,IAAI,MAAM;IAClC,EAAE,OAAO,CAAC,CAAC,GAAG,IAAM,KAAK,SAAS,CAAC,IAAI,GAAG,MAAM;IAChD,OAAO;EACT;EAEA,OAAO;IAAE;IAAQ;EAAO;AAC1B,EAAC"}
package/sha1.ts ADDED
@@ -0,0 +1,146 @@
1
+ // SHA-1 with its state kept between chunks — the one digest in this package
2
+ // that is not `crypto.subtle`'s.
3
+ //
4
+ // A packfile ends in the SHA-1 of every byte before it, and a packfile is a
5
+ // stream: objects arrive one at a time and leave as soon as they are deflated.
6
+ // `crypto.subtle.digest` wants the whole message in one buffer, so computing
7
+ // the trailer with it would mean holding the finished packfile in memory to
8
+ // write twenty bytes at the end — a clone's worth of bytes, for a trailer.
9
+ // Here the digest is fed as the stream goes past, so a packfile of any size
10
+ // costs one 64-byte block.
11
+ //
12
+ // Every byte of a pack passes through it, so it is the runtime's own hash
13
+ // wherever the runtime has one: `node:crypto`, which Deno, Node and workerd
14
+ // (`nodejs_compat`) all supply, reached at run time so a browser bundle needs
15
+ // none. In workerd it is about ten times as fast as script and makes no
16
+ // garbage. A browser falls back to `portableSha1`.
17
+ //
18
+ // SHA-1 is a name here and never a security claim, the same as the object ids
19
+ // in ./oid.ts: it is what the packfile format specifies, so it is what we
20
+ // write.
21
+
22
+ /** A digest in progress: bytes in, the twenty bytes that name them out. */
23
+ export type Sha1 = {
24
+ /** fold another run of bytes in */
25
+ update: (bytes: Uint8Array) => void
26
+ /** pad, finish, and return the twenty bytes — once */
27
+ digest: () => Uint8Array<ArrayBuffer>
28
+ }
29
+
30
+ type Hash = { update: (bytes: Uint8Array) => unknown; digest: () => Uint8Array }
31
+ type Builtins = { getBuiltinModule?: (id: string) => unknown }
32
+
33
+ // The runtime's streaming SHA-1, where it has `node:crypto`.
34
+ let hashing = (() => {
35
+ let process = (globalThis as { process?: Builtins }).process
36
+ let crypto = process?.getBuiltinModule?.('node:crypto') as
37
+ | { createHash?: (algorithm: string) => Hash }
38
+ | undefined
39
+ let create = crypto?.createHash
40
+ return create && (() => create('sha1'))
41
+ })()
42
+
43
+ let native = (start: () => Hash): Sha1 => {
44
+ let hash = start()
45
+ return {
46
+ update: (bytes) => void hash.update(bytes),
47
+ digest: () => new Uint8Array(hash.digest()),
48
+ }
49
+ }
50
+
51
+ /** Start a SHA-1: the runtime's own where it has one, else
52
+ * {@link portableSha1}. */
53
+ export let sha1 = (): Sha1 => hashing ? native(hashing) : portableSha1()
54
+
55
+ let rotl = (x: number, n: number): number => (x << n) | (x >>> (32 - n))
56
+
57
+ /** Start a SHA-1 in script, for a runtime without `node:crypto`. The
58
+ * algorithm is FIPS 180-4 section 6.1.2 verbatim, in 32-bit integer
59
+ * arithmetic: the `| 0` after every addition is what keeps a JavaScript number
60
+ * a machine word. */
61
+ export let portableSha1 = (): Sha1 => {
62
+ let h = new Int32Array([
63
+ 0x67452301,
64
+ 0xefcdab89,
65
+ 0x98badcfe,
66
+ 0x10325476,
67
+ 0xc3d2e1f0,
68
+ ])
69
+ let block = new Uint8Array(64)
70
+ let view = new DataView(block.buffer)
71
+ let w = new Int32Array(80)
72
+ let at = 0 // bytes waiting in the block
73
+ let len = 0 // bytes seen, all told
74
+
75
+ // One 64-byte block: the message schedule, then eighty rounds of it.
76
+ let round = () => {
77
+ for (let i = 0; i < 16; i++) w[i] = view.getInt32(i * 4)
78
+ for (let i = 16; i < 80; i++) {
79
+ w[i] = rotl(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1)
80
+ }
81
+ let a = h[0], b = h[1], c = h[2], d = h[3], e = h[4]
82
+ for (let i = 0; i < 80; i++) {
83
+ let f = i < 20
84
+ ? (b & c) | (~b & d)
85
+ : i < 40
86
+ ? b ^ c ^ d
87
+ : i < 60
88
+ ? (b & c) | (b & d) | (c & d)
89
+ : b ^ c ^ d
90
+ let k = i < 20
91
+ ? 0x5a827999
92
+ : i < 40
93
+ ? 0x6ed9eba1
94
+ : i < 60
95
+ ? 0x8f1bbcdc
96
+ : 0xca62c1d6
97
+ let t = (rotl(a, 5) + f + e + k + w[i]) | 0
98
+ e = d
99
+ d = c
100
+ c = rotl(b, 30)
101
+ b = a
102
+ a = t
103
+ }
104
+ h[0] += a
105
+ h[1] += b
106
+ h[2] += c
107
+ h[3] += d
108
+ h[4] += e
109
+ }
110
+
111
+ let update = (bytes: Uint8Array) => {
112
+ len += bytes.length
113
+ for (let i = 0; i < bytes.length;) {
114
+ let n = Math.min(64 - at, bytes.length - i)
115
+ block.set(bytes.subarray(i, i + n), at)
116
+ at += n
117
+ i += n
118
+ if (at == 64) {
119
+ round()
120
+ at = 0
121
+ }
122
+ }
123
+ }
124
+
125
+ let one = new Uint8Array(1)
126
+ let pad = new Uint8Array(64)
127
+
128
+ let digest = (): Uint8Array<ArrayBuffer> => {
129
+ // This is the message's own length, so it is read before the padding adds
130
+ // to it.
131
+ let bits = len * 8
132
+ one[0] = 0x80
133
+ update(one)
134
+ update(pad.subarray(0, (56 - at + 64) % 64))
135
+ view.setUint32(56, Math.floor(bits / 0x100000000))
136
+ view.setUint32(60, bits >>> 0)
137
+ round()
138
+ at = 0
139
+ let out = new Uint8Array(20)
140
+ let name = new DataView(out.buffer)
141
+ h.forEach((x, i) => name.setUint32(i * 4, x >>> 0))
142
+ return out
143
+ }
144
+
145
+ return { update, digest }
146
+ }
package/source.js ADDED
@@ -0,0 +1,100 @@
1
+ // Read a commit's regular-file blobs, never the checkout's mutable files.
2
+ // Paths and revisions are data, restricted before they reach Git's arguments.
3
+ import { run } from "./land.js";
4
+ // deno-lint-ignore no-control-regex
5
+ let unsafe = /[\\\x00-\x1f\x7f]/;
6
+ export let repoPath = (path)=>!!path && !path.startsWith('/') && !unsafe.test(path) && path.split('/').every((part)=>!!part && part != '.' && part != '..');
7
+ // What a commit holds at each path, or undefined where Git could not say.
8
+ let listed = async (cwd, commit, paths, ask)=>{
9
+ let got = await ask([
10
+ '--literal-pathspecs',
11
+ 'ls-tree',
12
+ '-z',
13
+ `${commit}^{commit}`,
14
+ '--',
15
+ ...paths
16
+ ], cwd);
17
+ if (!got.ok) return;
18
+ let out = new Map();
19
+ for (let entry of got.out.split('\0')){
20
+ let match = entry.match(/^100(?:644|755) blob ([a-f0-9]+)\t(.+)$/);
21
+ if (match && paths.includes(match[2])) out.set(match[2], match[1]);
22
+ }
23
+ return out;
24
+ };
25
+ /** A commit's regular-file blobs: `(commit, paths)` maps each path the commit
26
+ * holds as a regular file to its blob id. A commit never changes, so each path
27
+ * is asked of Git once while its commit is among the `keep` read most
28
+ * recently, however many callers ask at once; a read Git refused is asked
29
+ * again next time. */ export let trees = (cwd, { keep = 16, ask = run } = {})=>{
30
+ let commits = new Map();
31
+ return async (commit, paths)=>{
32
+ let out = new Map();
33
+ if (!/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/.test(commit)) return out;
34
+ let safe = [
35
+ ...new Set(paths.filter(repoPath))
36
+ ];
37
+ if (!safe.length) return out;
38
+ let seen = commits.get(commit) ?? new Map();
39
+ commits.delete(commit);
40
+ commits.set(commit, seen);
41
+ for (let old of commits.keys()){
42
+ if (commits.size <= keep) break;
43
+ commits.delete(old);
44
+ }
45
+ let missing = safe.filter((p)=>!seen.has(p));
46
+ if (missing.length) {
47
+ let asked = listed(cwd, commit, missing, ask);
48
+ for (let path of missing){
49
+ let blob = asked.then((got)=>{
50
+ if (!got && seen.get(path) == blob) seen.delete(path);
51
+ return got?.get(path);
52
+ });
53
+ seen.set(path, blob);
54
+ }
55
+ }
56
+ for (let path of safe){
57
+ let blob = await seen.get(path);
58
+ if (blob) out.set(path, blob);
59
+ }
60
+ return out;
61
+ };
62
+ };
63
+ /// await commitsOf('.', 'zz') -> []
64
+ /** Every commit an abbreviated or full sha names in the repository at `cwd`,
65
+ * by its full sha, as Git reads one: the one it names, none, or each of
66
+ * several that start that way, which Git refuses to choose between. */ export let commitsOf = async (cwd, sha, ask = run)=>{
67
+ if (!/^[0-9a-f]{4,64}$/.test(sha)) return [];
68
+ let commit = (id)=>ask([
69
+ 'rev-parse',
70
+ '--verify',
71
+ '--quiet',
72
+ `${id}^{commit}`
73
+ ], cwd);
74
+ let one = await commit(sha);
75
+ if (one.ok) return [
76
+ one.out.trim()
77
+ ];
78
+ // A prefix Git would not read lists every object it starts, of any type;
79
+ // the commits among them are the ones it names.
80
+ let all = await ask([
81
+ 'rev-parse',
82
+ `--disambiguate=${sha}`
83
+ ], cwd);
84
+ if (!all.ok) return [];
85
+ let peeled = await Promise.all(all.out.split('\n').filter(Boolean).map(commit));
86
+ return [
87
+ ...new Set(peeled.filter((p)=>p.ok).map((p)=>p.out.trim()))
88
+ ];
89
+ };
90
+ /** All checkout roots, including the primary; observation never prunes Git. */ export let rootsOf = async (common, ask = run)=>{
91
+ let got = await ask([
92
+ '--git-dir=' + common,
93
+ 'worktree',
94
+ 'list',
95
+ '--porcelain'
96
+ ], common);
97
+ if (!got.ok) throw Error(got.err.trim());
98
+ return got.out.split('\n').filter((l)=>l.startsWith('worktree ')).map((l)=>l.slice('worktree '.length));
99
+ };
100
+ //# sourceMappingURL=source.js.map