@git-stunts/git-cas 6.2.0 → 6.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ARCHITECTURE.md +16 -8
- package/CHANGELOG.md +37 -0
- package/README.md +13 -4
- package/UPGRADING.md +32 -0
- package/build-info.json +2 -2
- package/docs/API.md +124 -8
- package/docs/releases/v6.3.0.md +96 -0
- package/index.d.ts +95 -2
- package/package.json +2 -1
- package/src/domain/errors/Codes.js +4 -0
- package/src/domain/services/CacheAcquisitionInventory.js +123 -0
- package/src/domain/services/CacheAcquisitionRegistry.js +188 -0
- package/src/domain/services/CacheSet.js +56 -5
- package/src/domain/services/CacheSetRegistry.js +7 -0
- package/src/domain/services/RepositoryDoctor.js +30 -3
- package/src/domain/value-objects/CacheAcquisition.js +88 -0
- package/src/domain/value-objects/CacheAcquisitionRef.js +129 -0
- package/src/domain/value-objects/RetentionWitness.js +6 -1
- package/src/infrastructure/adapters/GitRefAdapter.js +312 -2
- package/src/infrastructure/adapters/GitRepositoryInspectionAdapter.js +28 -10
- package/src/package-version.js +1 -1
- package/src/ports/GitRefPort.js +45 -0
- package/src/ports/RepositoryInspectionPort.js +5 -2
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import createCasError from '../errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../errors/index.js';
|
|
3
|
+
import assertCanonicalTimestamp from '../helpers/assertCanonicalTimestamp.js';
|
|
4
|
+
import CollectionNamespace from './CollectionNamespace.js';
|
|
5
|
+
|
|
6
|
+
export const CACHE_ACQUISITION_REF_PREFIX = 'refs/cas/cache-acquisitions/';
|
|
7
|
+
|
|
8
|
+
const ID_PATTERN = /^v1-(\d{13})-([0-9a-f]{64})-([0-9a-f]{32})$/u;
|
|
9
|
+
|
|
10
|
+
/** Immutable identity for one scoped cache-generation anchor. */
|
|
11
|
+
export default class CacheAcquisitionRef {
|
|
12
|
+
#acquiredAt;
|
|
13
|
+
#id;
|
|
14
|
+
#keyDigest;
|
|
15
|
+
#namespace;
|
|
16
|
+
#value;
|
|
17
|
+
|
|
18
|
+
constructor(value) {
|
|
19
|
+
if (typeof value !== 'string' || !value.startsWith(CACHE_ACQUISITION_REF_PREFIX)) {
|
|
20
|
+
throw invalid('Cache acquisition ref is outside its managed namespace', { ref: value });
|
|
21
|
+
}
|
|
22
|
+
const parts = value.slice(CACHE_ACQUISITION_REF_PREFIX.length).split('/');
|
|
23
|
+
if (parts.length !== 2) {
|
|
24
|
+
throw invalid('Cache acquisition ref must include a cache namespace and ID', { ref: value });
|
|
25
|
+
}
|
|
26
|
+
const [encodedNamespace, id] = parts;
|
|
27
|
+
const match = ID_PATTERN.exec(id);
|
|
28
|
+
if (!match) {
|
|
29
|
+
throw invalid('Cache acquisition ID is invalid', { ref: value, id });
|
|
30
|
+
}
|
|
31
|
+
const acquiredAt = new Date(Number(match[1])).toISOString();
|
|
32
|
+
this.#namespace = decodeNamespaceSegment(encodedNamespace, { ref: value, id });
|
|
33
|
+
this.#id = id;
|
|
34
|
+
this.#acquiredAt = acquiredAt;
|
|
35
|
+
this.#keyDigest = match[2];
|
|
36
|
+
this.#value = `${CacheAcquisitionRef.prefixForNamespace(this.#namespace)}${id}`;
|
|
37
|
+
Object.freeze(this);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static create({ namespace, keyDigest, acquiredAt, nonce }) {
|
|
41
|
+
assertCanonicalTimestamp(acquiredAt, {
|
|
42
|
+
invalid,
|
|
43
|
+
message: 'Cache acquisition time must be a canonical UTC timestamp',
|
|
44
|
+
});
|
|
45
|
+
if (typeof keyDigest !== 'string' || !/^[0-9a-f]{64}$/u.test(keyDigest)) {
|
|
46
|
+
throw invalid('Cache acquisition key digest is invalid', { keyDigest });
|
|
47
|
+
}
|
|
48
|
+
if (typeof nonce !== 'string' || !/^[0-9a-f]{32}$/u.test(nonce)) {
|
|
49
|
+
throw invalid('Cache acquisition nonce is invalid', { nonce });
|
|
50
|
+
}
|
|
51
|
+
const epoch = String(Date.parse(acquiredAt)).padStart(13, '0');
|
|
52
|
+
return new CacheAcquisitionRef(
|
|
53
|
+
`${CacheAcquisitionRef.prefixForNamespace(namespace)}v1-${epoch}-${keyDigest}-${nonce}`,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static from(value) {
|
|
58
|
+
return value instanceof CacheAcquisitionRef ? value : new CacheAcquisitionRef(value);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
static forId({ namespace, id }) {
|
|
62
|
+
if (typeof id !== 'string' || !ID_PATTERN.test(id)) {
|
|
63
|
+
throw invalid('Cache acquisition ID is invalid', { namespace, id });
|
|
64
|
+
}
|
|
65
|
+
return new CacheAcquisitionRef(`${CacheAcquisitionRef.prefixForNamespace(namespace)}${id}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static prefixForNamespace(namespace) {
|
|
69
|
+
const canonical = acquisitionNamespace(namespace, { namespace }).toString();
|
|
70
|
+
return `${CACHE_ACQUISITION_REF_PREFIX}${encodeURIComponent(canonical)}/`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
get acquiredAt() {
|
|
74
|
+
return this.#acquiredAt;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
get id() {
|
|
78
|
+
return this.#id;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
get keyDigest() {
|
|
82
|
+
return this.#keyDigest;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get namespace() {
|
|
86
|
+
return this.#namespace.toString();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
toString() {
|
|
90
|
+
return this.#value;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function invalid(message, meta) {
|
|
95
|
+
return createCasError(message, ErrorCodes.CACHE_ACQUISITION_INVALID, meta);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function acquisitionNamespace(value, meta) {
|
|
99
|
+
try {
|
|
100
|
+
return CollectionNamespace.from(value);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
throw invalid('Cache acquisition namespace is invalid', {
|
|
103
|
+
...meta,
|
|
104
|
+
namespace: value,
|
|
105
|
+
originalError: error,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function decodeNamespaceSegment(value, meta) {
|
|
111
|
+
let decoded;
|
|
112
|
+
try {
|
|
113
|
+
decoded = decodeURIComponent(value);
|
|
114
|
+
} catch (error) {
|
|
115
|
+
throw invalid('Cache acquisition namespace encoding is invalid', {
|
|
116
|
+
...meta,
|
|
117
|
+
namespace: value,
|
|
118
|
+
originalError: error,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
const namespace = acquisitionNamespace(decoded, meta);
|
|
122
|
+
if (encodeURIComponent(namespace.toString()) !== value) {
|
|
123
|
+
throw invalid('Cache acquisition namespace encoding is not canonical', {
|
|
124
|
+
...meta,
|
|
125
|
+
namespace: value,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return namespace;
|
|
129
|
+
}
|
|
@@ -6,7 +6,12 @@ import Oid from './Oid.js';
|
|
|
6
6
|
|
|
7
7
|
const POLICIES = Object.freeze(['pinned', 'evictable']);
|
|
8
8
|
const REACHABILITY = Object.freeze(['anchored', 'orphaned', 'volatile']);
|
|
9
|
-
const ROOT_KINDS = Object.freeze([
|
|
9
|
+
const ROOT_KINDS = Object.freeze([
|
|
10
|
+
'root-set',
|
|
11
|
+
'publication',
|
|
12
|
+
'cache-set',
|
|
13
|
+
'expiring-set',
|
|
14
|
+
]);
|
|
10
15
|
|
|
11
16
|
/**
|
|
12
17
|
* Immutable evidence that one observed Git generation retained a handle.
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Policy } from '@git-stunts/alfred';
|
|
2
2
|
import GitRefPort from '../../ports/GitRefPort.js';
|
|
3
3
|
import { CasError, ErrorCodes } from '../../domain/errors/index.js';
|
|
4
|
-
import { isGitMissingRefError } from '../../domain/helpers/gitRefErrors.js';
|
|
4
|
+
import { errorDetailsText, isGitMissingRefError } from '../../domain/helpers/gitRefErrors.js';
|
|
5
|
+
import Oid from '../../domain/value-objects/Oid.js';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Default resilience policy: 30 s timeout (no retry).
|
|
@@ -12,6 +13,8 @@ import { isGitMissingRefError } from '../../domain/helpers/gitRefErrors.js';
|
|
|
12
13
|
* an unref'd timer that allows Node to exit before the next attempt starts.
|
|
13
14
|
*/
|
|
14
15
|
const DEFAULT_POLICY = Policy.timeout(30_000);
|
|
16
|
+
const FORBIDDEN_REF_CHARACTERS = new Set(['~', '^', ':', '?', '*', '[', '\\']);
|
|
17
|
+
const MAX_REF_ITERATION_LIMIT = 1001;
|
|
15
18
|
|
|
16
19
|
/**
|
|
17
20
|
* {@link GitRefPort} implementation backed by `@git-stunts/plumbing`.
|
|
@@ -112,7 +115,17 @@ export default class GitRefAdapter extends GitRefPort {
|
|
|
112
115
|
* @returns {Promise<void>}
|
|
113
116
|
*/
|
|
114
117
|
async updateRef({ ref, newOid, expectedOldOid }) {
|
|
115
|
-
|
|
118
|
+
assertRefName(ref);
|
|
119
|
+
const symbolicTarget = await this.#resolveSymbolicRef(ref);
|
|
120
|
+
if (symbolicTarget !== null) {
|
|
121
|
+
throw refConflict({
|
|
122
|
+
ref,
|
|
123
|
+
expectedOldOid: expectedOldOid ?? null,
|
|
124
|
+
actualOldOid: null,
|
|
125
|
+
actualSymref: symbolicTarget,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const args = ['update-ref', '--no-deref', ref, newOid];
|
|
116
129
|
if (expectedOldOid !== undefined) {
|
|
117
130
|
args.push(expectedOldOid ?? '0'.repeat(newOid.length));
|
|
118
131
|
}
|
|
@@ -120,4 +133,301 @@ export default class GitRefAdapter extends GitRefPort {
|
|
|
120
133
|
this.plumbing.execute({ args }),
|
|
121
134
|
);
|
|
122
135
|
}
|
|
136
|
+
|
|
137
|
+
/** @override */
|
|
138
|
+
async anchorRef({ sourceRef, expectedSourceOid, targetRef }) {
|
|
139
|
+
assertRefName(sourceRef);
|
|
140
|
+
assertRefName(targetRef);
|
|
141
|
+
const generation = Oid.from(expectedSourceOid).toString();
|
|
142
|
+
// Git 2.43 cannot atomically assert ref type. Preflight rejects observed
|
|
143
|
+
// symrefs; --no-deref contains any subsequent race to the managed names.
|
|
144
|
+
if (
|
|
145
|
+
await this.#resolveSymbolicRef(sourceRef) !== null ||
|
|
146
|
+
await this.#resolveSymbolicRef(targetRef) !== null
|
|
147
|
+
) {
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
const input = [
|
|
151
|
+
'start',
|
|
152
|
+
`verify ${sourceRef} ${generation}`,
|
|
153
|
+
`create ${targetRef} ${generation}`,
|
|
154
|
+
'prepare',
|
|
155
|
+
'commit',
|
|
156
|
+
'',
|
|
157
|
+
].join('\n');
|
|
158
|
+
try {
|
|
159
|
+
await this.policy.execute(() =>
|
|
160
|
+
this.plumbing.execute({ args: ['update-ref', '--no-deref', '--stdin'], input }),
|
|
161
|
+
);
|
|
162
|
+
return true;
|
|
163
|
+
} catch (error) {
|
|
164
|
+
if (isUpdateRefConflict(error, [sourceRef, targetRef])) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** @override */
|
|
172
|
+
async deleteRef({ ref, expectedOldOid }) {
|
|
173
|
+
assertRefName(ref);
|
|
174
|
+
const expectedGeneration = Oid.from(expectedOldOid).toString();
|
|
175
|
+
// A post-probe type race may delete this managed symref name, never its
|
|
176
|
+
// referent. The expected OID still gates the no-dereference mutation.
|
|
177
|
+
const symbolicTarget = await this.#resolveSymbolicRef(ref);
|
|
178
|
+
if (symbolicTarget !== null) {
|
|
179
|
+
throw refConflict({
|
|
180
|
+
ref,
|
|
181
|
+
expectedOldOid: expectedGeneration,
|
|
182
|
+
actualOldOid: null,
|
|
183
|
+
actualSymref: symbolicTarget,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
try {
|
|
187
|
+
await this.policy.execute(() =>
|
|
188
|
+
this.plumbing.execute({
|
|
189
|
+
args: ['update-ref', '--no-deref', '-d', ref, expectedGeneration],
|
|
190
|
+
}),
|
|
191
|
+
);
|
|
192
|
+
} catch (error) {
|
|
193
|
+
if (!isUpdateRefConflict(error, [ref])) {
|
|
194
|
+
throw error;
|
|
195
|
+
}
|
|
196
|
+
const racedSymbolicTarget = await this.#resolveSymbolicRef(ref);
|
|
197
|
+
if (racedSymbolicTarget !== null) {
|
|
198
|
+
throw refConflict({
|
|
199
|
+
ref,
|
|
200
|
+
expectedOldOid: expectedGeneration,
|
|
201
|
+
actualOldOid: null,
|
|
202
|
+
actualSymref: racedSymbolicTarget,
|
|
203
|
+
originalError: error,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
const actual = await this.#inspectDirectRef(ref);
|
|
207
|
+
throw refConflict({
|
|
208
|
+
ref,
|
|
209
|
+
expectedOldOid: expectedGeneration,
|
|
210
|
+
actualOldOid: actual?.oid ?? null,
|
|
211
|
+
actualSymref: actual?.symref ?? null,
|
|
212
|
+
originalError: error,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** @override */
|
|
219
|
+
async *iterateRefs({ prefix = 'refs/', limit } = {}) {
|
|
220
|
+
assertRefPrefix(prefix);
|
|
221
|
+
assertRefIterationLimit(limit);
|
|
222
|
+
if (typeof this.plumbing?.executeStream !== 'function') {
|
|
223
|
+
throw new CasError(
|
|
224
|
+
'Git ref inventory requires streaming plumbing',
|
|
225
|
+
ErrorCodes.GIT_ERROR,
|
|
226
|
+
{ prefix },
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
const stream = await this.policy.execute(() =>
|
|
230
|
+
this.plumbing.executeStream({
|
|
231
|
+
args: [
|
|
232
|
+
'for-each-ref',
|
|
233
|
+
'--format=%(refname)%09%(objectname)%09%(symref)',
|
|
234
|
+
`--count=${limit}`,
|
|
235
|
+
prefix,
|
|
236
|
+
],
|
|
237
|
+
}),
|
|
238
|
+
);
|
|
239
|
+
for await (const line of consumeRefLines(stream)) {
|
|
240
|
+
yield parseIteratedRef(prefix, line);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async #inspectDirectRef(ref) {
|
|
245
|
+
const output = await this.policy.execute(() =>
|
|
246
|
+
this.plumbing.execute({
|
|
247
|
+
args: [
|
|
248
|
+
'for-each-ref',
|
|
249
|
+
'--format=%(refname)%09%(objectname)%09%(symref)',
|
|
250
|
+
'--count=1',
|
|
251
|
+
ref,
|
|
252
|
+
],
|
|
253
|
+
}),
|
|
254
|
+
);
|
|
255
|
+
const line = String(output).replace(/\r?\n$/u, '');
|
|
256
|
+
if (line.length === 0) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
const fields = line.split('\t');
|
|
260
|
+
if (
|
|
261
|
+
(fields.length !== 2 && fields.length !== 3) ||
|
|
262
|
+
fields[0] !== ref ||
|
|
263
|
+
!Oid.isValid(fields[1]) ||
|
|
264
|
+
(fields[2] !== undefined && fields[2] !== '' && !isValidRefSyntax(fields[2], false))
|
|
265
|
+
) {
|
|
266
|
+
throw invalidRefInventory(ref, line);
|
|
267
|
+
}
|
|
268
|
+
return Object.freeze({
|
|
269
|
+
ref,
|
|
270
|
+
oid: Oid.from(fields[1]).toString(),
|
|
271
|
+
symref: fields[2] || null,
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async #resolveSymbolicRef(ref) {
|
|
276
|
+
try {
|
|
277
|
+
const output = await this.policy.execute(() =>
|
|
278
|
+
this.plumbing.execute({ args: ['symbolic-ref', '--quiet', ref] }),
|
|
279
|
+
);
|
|
280
|
+
const target = String(output).trim();
|
|
281
|
+
return target.length > 0 ? target : null;
|
|
282
|
+
} catch (error) {
|
|
283
|
+
if (isQuietSymbolicRefMiss(error, ref)) {
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
throw error;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function assertRefPrefix(prefix) {
|
|
292
|
+
assertRefSyntax(prefix, { allowTrailingSlash: true });
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function assertRefName(ref) {
|
|
296
|
+
assertRefSyntax(ref, { allowTrailingSlash: false });
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function assertRefIterationLimit(limit) {
|
|
300
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > MAX_REF_ITERATION_LIMIT) {
|
|
301
|
+
throw new CasError(
|
|
302
|
+
`Git ref inventory limit must be between 1 and ${MAX_REF_ITERATION_LIMIT}`,
|
|
303
|
+
ErrorCodes.GIT_ERROR,
|
|
304
|
+
{ limit },
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function assertRefSyntax(value, { allowTrailingSlash }) {
|
|
310
|
+
if (typeof value !== 'string' || !isValidRefSyntax(value, allowTrailingSlash)) {
|
|
311
|
+
throw invalidRefName(value);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function isValidRefSyntax(value, allowTrailingSlash) {
|
|
316
|
+
if (!value.startsWith('refs/') || (!allowTrailingSlash && value.endsWith('/'))) {
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
if (['//', '..', '@{'].some((pattern) => value.includes(pattern))) {
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
if (value.endsWith('.') || hasForbiddenRefCharacter(value)) {
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
const trimmed = allowTrailingSlash && value.endsWith('/') ? value.slice(0, -1) : value;
|
|
326
|
+
return !trimmed.split('/').some(isInvalidRefSegment);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function isInvalidRefSegment(part) {
|
|
330
|
+
return part.length === 0 || part.startsWith('.') || part.endsWith('.lock');
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function hasForbiddenRefCharacter(value) {
|
|
334
|
+
return typeof value === 'string' && [...value].some((character) => {
|
|
335
|
+
const code = character.codePointAt(0);
|
|
336
|
+
return code <= 0x20 || code === 0x7f || FORBIDDEN_REF_CHARACTERS.has(character);
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
async function* consumeRefLines(stream) {
|
|
341
|
+
const decoder = new globalThis.TextDecoder();
|
|
342
|
+
let pending = '';
|
|
343
|
+
for await (const chunk of stream) {
|
|
344
|
+
pending += typeof chunk === 'string' ? chunk : decoder.decode(chunk, { stream: true });
|
|
345
|
+
let newline = pending.indexOf('\n');
|
|
346
|
+
while (newline !== -1) {
|
|
347
|
+
const line = pending.slice(0, newline).replace(/\r$/u, '');
|
|
348
|
+
pending = pending.slice(newline + 1);
|
|
349
|
+
if (line.length > 0) {
|
|
350
|
+
yield line;
|
|
351
|
+
}
|
|
352
|
+
newline = pending.indexOf('\n');
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
pending += decoder.decode();
|
|
356
|
+
if (pending.length > 0) {
|
|
357
|
+
yield pending.replace(/\r$/u, '');
|
|
358
|
+
}
|
|
359
|
+
const result = await stream.finished;
|
|
360
|
+
if (result?.code !== 0) {
|
|
361
|
+
throw new CasError('Git ref inventory failed', ErrorCodes.GIT_ERROR, {
|
|
362
|
+
stderr: result?.stderr,
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function invalidRefInventory(prefix, output) {
|
|
368
|
+
return new CasError(
|
|
369
|
+
'Git ref inventory returned invalid structured output',
|
|
370
|
+
ErrorCodes.GIT_ERROR,
|
|
371
|
+
{ prefix, output: String(output) },
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function parseIteratedRef(prefix, line) {
|
|
376
|
+
const fields = line.split('\t');
|
|
377
|
+
if (!isValidIteratedRef(fields, prefix)) {
|
|
378
|
+
throw invalidRefInventory(prefix, line);
|
|
379
|
+
}
|
|
380
|
+
return Object.freeze({
|
|
381
|
+
ref: fields[0],
|
|
382
|
+
oid: Oid.from(fields[1]).toString(),
|
|
383
|
+
symref: fields[2] || null,
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function isValidIteratedRef(fields, prefix) {
|
|
388
|
+
return fields.length === 3
|
|
389
|
+
&& fields[0].startsWith(prefix)
|
|
390
|
+
&& Oid.isValid(fields[1])
|
|
391
|
+
&& (fields[2] === '' || isValidRefSyntax(fields[2], false));
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function invalidRefName(ref) {
|
|
395
|
+
return new CasError('Git ref name or prefix is invalid', ErrorCodes.GIT_ERROR, { ref });
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
function isUpdateRefConflict(error, refs) {
|
|
399
|
+
const text = errorDetailsText(error).toLowerCase();
|
|
400
|
+
return refs.some((ref) => text.includes(ref.toLowerCase()))
|
|
401
|
+
&& (
|
|
402
|
+
text.includes('cannot lock ref') ||
|
|
403
|
+
text.includes('but expected') ||
|
|
404
|
+
text.includes('reference already exists') ||
|
|
405
|
+
text.includes('unable to resolve reference') ||
|
|
406
|
+
text.includes('zero <oldvalue>') ||
|
|
407
|
+
text.includes('is a symref')
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function isQuietSymbolicRefMiss(error, ref) {
|
|
412
|
+
const details = error instanceof Error && error.details && typeof error.details === 'object'
|
|
413
|
+
? error.details
|
|
414
|
+
: {};
|
|
415
|
+
return details.code === 1
|
|
416
|
+
&& Array.isArray(details.args)
|
|
417
|
+
&& details.args[0] === 'symbolic-ref'
|
|
418
|
+
&& details.args.at(-1) === ref;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function refConflict({
|
|
422
|
+
ref,
|
|
423
|
+
expectedOldOid,
|
|
424
|
+
actualOldOid,
|
|
425
|
+
actualSymref = null,
|
|
426
|
+
originalError,
|
|
427
|
+
}) {
|
|
428
|
+
return new CasError(
|
|
429
|
+
`Git ref changed before checked mutation: ${ref}`,
|
|
430
|
+
ErrorCodes.GIT_REF_CONFLICT,
|
|
431
|
+
{ ref, expectedOldOid, actualOldOid, actualSymref, originalError },
|
|
432
|
+
);
|
|
123
433
|
}
|
|
@@ -72,19 +72,19 @@ export default class GitRepositoryInspectionAdapter extends RepositoryInspection
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
async *iterateRefs() {
|
|
75
|
+
async *iterateRefs({ prefix = 'refs/' } = {}) {
|
|
76
|
+
if (typeof prefix !== 'string' || !prefix.startsWith('refs/') || prefix.includes('..')) {
|
|
77
|
+
throw new CasError(
|
|
78
|
+
'Git ref inventory prefix is invalid',
|
|
79
|
+
ErrorCodes.REPOSITORY_INSPECTION_INVALID,
|
|
80
|
+
{ prefix },
|
|
81
|
+
);
|
|
82
|
+
}
|
|
76
83
|
const stream = await this.#stream({
|
|
77
|
-
args: ['for-each-ref', '--format=%(refname)%09%(objectname)',
|
|
84
|
+
args: ['for-each-ref', '--format=%(refname)%09%(objectname)%09%(symref)', prefix],
|
|
78
85
|
});
|
|
79
86
|
for await (const line of consumeLines(stream, 'ref inventory')) {
|
|
80
|
-
|
|
81
|
-
if (fields.length !== 2 || !fields[0].startsWith('refs/')) {
|
|
82
|
-
throw invalidOutput('ref inventory', line);
|
|
83
|
-
}
|
|
84
|
-
yield Object.freeze({
|
|
85
|
-
ref: fields[0],
|
|
86
|
-
oid: parseOid(fields[1], 'ref inventory', line),
|
|
87
|
-
});
|
|
87
|
+
yield parseRefRecord(line);
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
|
|
@@ -137,6 +137,24 @@ function parseOid(value, operation, output) {
|
|
|
137
137
|
return Oid.from(value).toString();
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
+
function parseRefRecord(line) {
|
|
141
|
+
const fields = line.split('\t');
|
|
142
|
+
if (!isValidRefRecord(fields)) {
|
|
143
|
+
throw invalidOutput('ref inventory', line);
|
|
144
|
+
}
|
|
145
|
+
return Object.freeze({
|
|
146
|
+
ref: fields[0],
|
|
147
|
+
oid: parseOid(fields[1], 'ref inventory', line),
|
|
148
|
+
symref: fields[2] || null,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function isValidRefRecord(fields) {
|
|
153
|
+
return fields.length === 3
|
|
154
|
+
&& fields[0].startsWith('refs/')
|
|
155
|
+
&& (fields[2] === '' || fields[2].startsWith('refs/'));
|
|
156
|
+
}
|
|
157
|
+
|
|
140
158
|
function parseType(value, operation, output) {
|
|
141
159
|
if (!OBJECT_TYPES.has(value)) {
|
|
142
160
|
throw invalidOutput(operation, output);
|
package/src/package-version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const PACKAGE_VERSION = '6.
|
|
1
|
+
export const PACKAGE_VERSION = '6.3.0';
|
package/src/ports/GitRefPort.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import createCasError from '../domain/errors/createCasError.js';
|
|
2
|
+
import { ErrorCodes } from '../domain/errors/index.js';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Abstract port for Git ref and commit operations.
|
|
3
6
|
* @abstract
|
|
@@ -55,4 +58,46 @@ export default class GitRefPort {
|
|
|
55
58
|
async updateRef(_options) {
|
|
56
59
|
throw new Error('Not implemented');
|
|
57
60
|
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Atomically verifies one source ref and creates a new ref to its generation.
|
|
64
|
+
* @param {Object} _options
|
|
65
|
+
* @param {string} _options.sourceRef
|
|
66
|
+
* @param {string} _options.expectedSourceOid
|
|
67
|
+
* @param {string} _options.targetRef
|
|
68
|
+
* @returns {Promise<boolean>} Whether the source generation was anchored.
|
|
69
|
+
*/
|
|
70
|
+
async anchorRef(_options) {
|
|
71
|
+
throw unsupportedAcquisitionCapability('anchorRef');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Deletes a ref only when it still names the expected object.
|
|
76
|
+
* @param {Object} _options
|
|
77
|
+
* @param {string} _options.ref
|
|
78
|
+
* @param {string} _options.expectedOldOid
|
|
79
|
+
* @returns {Promise<boolean>} Whether the ref existed and was deleted.
|
|
80
|
+
*/
|
|
81
|
+
async deleteRef(_options) {
|
|
82
|
+
throw unsupportedAcquisitionCapability('deleteRef');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Streams refs below a canonical Git ref prefix.
|
|
87
|
+
* @param {Object} _options
|
|
88
|
+
* @param {string} [_options.prefix]
|
|
89
|
+
* @param {number} _options.limit Maximum records to request from Git.
|
|
90
|
+
* @returns {AsyncIterable<{ref: string, oid: string, symref: string|null}>}
|
|
91
|
+
*/
|
|
92
|
+
iterateRefs(_options) {
|
|
93
|
+
throw unsupportedAcquisitionCapability('iterateRefs');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function unsupportedAcquisitionCapability(capability) {
|
|
98
|
+
return createCasError(
|
|
99
|
+
`Git ref adapter does not implement cache acquisition capability: ${capability}`,
|
|
100
|
+
ErrorCodes.CACHE_ACQUISITION_INVALID,
|
|
101
|
+
{ capability },
|
|
102
|
+
);
|
|
58
103
|
}
|
|
@@ -21,8 +21,11 @@ export default class RepositoryInspectionPort {
|
|
|
21
21
|
throw new Error('Not implemented');
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
/**
|
|
25
|
-
|
|
24
|
+
/**
|
|
25
|
+
* @param {{ prefix?: string }} [_options]
|
|
26
|
+
* @returns {AsyncIterable<{ ref: string, oid: string, symref: string|null }>}
|
|
27
|
+
*/
|
|
28
|
+
iterateRefs(_options) {
|
|
26
29
|
throw new Error('Not implemented');
|
|
27
30
|
}
|
|
28
31
|
|