@leed-ai/git-gateway-contract 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +50 -2
- package/dist/behaviour/assertions.d.ts +23 -0
- package/dist/behaviour/assertions.js +56 -0
- package/dist/behaviour/assertions.js.map +1 -0
- package/dist/behaviour/behaviour-case.d.ts +19 -0
- package/dist/behaviour/behaviour-case.js +12 -0
- package/dist/behaviour/behaviour-case.js.map +1 -0
- package/dist/behaviour/behaviour-suite.d.ts +30 -0
- package/dist/behaviour/behaviour-suite.js +50 -0
- package/dist/behaviour/behaviour-suite.js.map +1 -0
- package/dist/behaviour/commit-action-cases.d.ts +4 -0
- package/dist/behaviour/commit-action-cases.js +342 -0
- package/dist/behaviour/commit-action-cases.js.map +1 -0
- package/dist/behaviour/commit-write-cases.d.ts +4 -0
- package/dist/behaviour/commit-write-cases.js +234 -0
- package/dist/behaviour/commit-write-cases.js.map +1 -0
- package/dist/behaviour/fixtures.d.ts +29 -0
- package/dist/behaviour/fixtures.js +83 -0
- package/dist/behaviour/fixtures.js.map +1 -0
- package/dist/behaviour/lifecycle-cases.d.ts +4 -0
- package/dist/behaviour/lifecycle-cases.js +158 -0
- package/dist/behaviour/lifecycle-cases.js.map +1 -0
- package/dist/behaviour/promote-cases.d.ts +4 -0
- package/dist/behaviour/promote-cases.js +122 -0
- package/dist/behaviour/promote-cases.js.map +1 -0
- package/dist/behaviour/read-cases.d.ts +4 -0
- package/dist/behaviour/read-cases.js +248 -0
- package/dist/behaviour/read-cases.js.map +1 -0
- package/dist/behaviour/sync-cases.d.ts +4 -0
- package/dist/behaviour/sync-cases.js +205 -0
- package/dist/behaviour/sync-cases.js.map +1 -0
- package/dist/clock-B3MlpmVY.d.ts +10 -0
- package/dist/commit-BEZmeAOl.d.ts +23 -0
- package/dist/file-map-CxUveLWz.d.ts +18 -0
- package/dist/gateway-config-oVsdTEeK.d.ts +39 -0
- package/dist/gateway-error-body-BWzZFAQI.d.ts +214 -0
- package/dist/gateway-error.d.ts +30 -0
- package/dist/gateway-error.js +63 -0
- package/dist/gateway-error.js.map +1 -0
- package/dist/git-gateway-VA9Tc5tn.d.ts +358 -0
- package/dist/git-gateway.d.ts +2 -0
- package/dist/git-gateway.js +65 -0
- package/dist/git-gateway.js.map +1 -0
- package/dist/health.d.ts +9 -0
- package/dist/health.js +18 -0
- package/dist/health.js.map +1 -0
- package/dist/history-CqvHaUE-.d.ts +20 -0
- package/dist/package-name.d.ts +3 -0
- package/dist/package-name.js +6 -0
- package/dist/package-name.js.map +1 -0
- package/dist/proxy/audit-event.d.ts +61 -0
- package/dist/proxy/audit-event.js +97 -0
- package/dist/proxy/audit-event.js.map +1 -0
- package/dist/proxy/branch-rule.d.ts +72 -0
- package/dist/proxy/branch-rule.js +106 -0
- package/dist/proxy/branch-rule.js.map +1 -0
- package/dist/proxy/gateway-hooks.d.ts +58 -0
- package/dist/proxy/gateway-hooks.js +0 -0
- package/dist/proxy/git-operation.d.ts +17 -0
- package/dist/proxy/git-operation.js +32 -0
- package/dist/proxy/git-operation.js.map +1 -0
- package/dist/proxy/policy-result.d.ts +51 -0
- package/dist/proxy/policy-result.js +90 -0
- package/dist/proxy/policy-result.js.map +1 -0
- package/dist/proxy/principal.d.ts +18 -0
- package/dist/proxy/principal.js +32 -0
- package/dist/proxy/principal.js.map +1 -0
- package/dist/proxy/push-policy-result.d.ts +34 -0
- package/dist/proxy/push-policy-result.js +48 -0
- package/dist/proxy/push-policy-result.js.map +1 -0
- package/dist/proxy/push-policy.d.ts +146 -0
- package/dist/proxy/push-policy.js +112 -0
- package/dist/proxy/push-policy.js.map +1 -0
- package/dist/proxy/request-credential.d.ts +22 -0
- package/dist/proxy/request-credential.js +46 -0
- package/dist/proxy/request-credential.js.map +1 -0
- package/dist/proxy/smart-http-config.d.ts +22 -0
- package/dist/proxy/smart-http-config.js +40 -0
- package/dist/proxy/smart-http-config.js.map +1 -0
- package/dist/reference/apply-actions.d.ts +9 -0
- package/dist/reference/apply-actions.js +84 -0
- package/dist/reference/apply-actions.js.map +1 -0
- package/dist/reference/branch-methods.d.ts +7 -0
- package/dist/reference/branch-methods.js +105 -0
- package/dist/reference/branch-methods.js.map +1 -0
- package/dist/reference/bytes.d.ts +22 -0
- package/dist/reference/bytes.js +62 -0
- package/dist/reference/bytes.js.map +1 -0
- package/dist/reference/clock.d.ts +2 -0
- package/dist/reference/clock.js +18 -0
- package/dist/reference/clock.js.map +1 -0
- package/dist/reference/commit-method.d.ts +5 -0
- package/dist/reference/commit-method.js +83 -0
- package/dist/reference/commit-method.js.map +1 -0
- package/dist/reference/file-map.d.ts +2 -0
- package/dist/reference/file-map.js +24 -0
- package/dist/reference/file-map.js.map +1 -0
- package/dist/reference/hash.d.ts +10 -0
- package/dist/reference/hash.js +25 -0
- package/dist/reference/hash.js.map +1 -0
- package/dist/reference/history.d.ts +2 -0
- package/dist/reference/history.js +65 -0
- package/dist/reference/history.js.map +1 -0
- package/dist/reference/lifecycle-methods.d.ts +9 -0
- package/dist/reference/lifecycle-methods.js +48 -0
- package/dist/reference/lifecycle-methods.js.map +1 -0
- package/dist/reference/read-methods.d.ts +13 -0
- package/dist/reference/read-methods.js +68 -0
- package/dist/reference/read-methods.js.map +1 -0
- package/dist/reference/reference-gateway.d.ts +18 -0
- package/dist/reference/reference-gateway.js +35 -0
- package/dist/reference/reference-gateway.js.map +1 -0
- package/dist/reference/repo-state.d.ts +43 -0
- package/dist/reference/repo-state.js +45 -0
- package/dist/reference/repo-state.js.map +1 -0
- package/dist/reference/run-method.d.ts +13 -0
- package/dist/reference/run-method.js +15 -0
- package/dist/reference/run-method.js.map +1 -0
- package/dist/reference/three-way-merge.d.ts +16 -0
- package/dist/reference/three-way-merge.js +45 -0
- package/dist/reference/three-way-merge.js.map +1 -0
- package/dist/reference/trailer.d.ts +7 -0
- package/dist/reference/trailer.js +16 -0
- package/dist/reference/trailer.js.map +1 -0
- package/dist/reference/tree-index.d.ts +2 -0
- package/dist/reference/tree-index.js +66 -0
- package/dist/reference/tree-index.js.map +1 -0
- package/dist/reference/write-commit.d.ts +15 -0
- package/dist/reference/write-commit.js +44 -0
- package/dist/reference/write-commit.js.map +1 -0
- package/dist/rpc/commit-exists.d.ts +16 -0
- package/dist/rpc/commit-exists.js +30 -0
- package/dist/rpc/commit-exists.js.map +1 -0
- package/dist/rpc/commit.d.ts +172 -0
- package/dist/rpc/commit.js +115 -0
- package/dist/rpc/commit.js.map +1 -0
- package/dist/rpc/list-tree.d.ts +59 -0
- package/dist/rpc/list-tree.js +54 -0
- package/dist/rpc/list-tree.js.map +1 -0
- package/dist/rpc/log.d.ts +35 -0
- package/dist/rpc/log.js +41 -0
- package/dist/rpc/log.js.map +1 -0
- package/dist/rpc/mint-read-token.d.ts +17 -0
- package/dist/rpc/mint-read-token.js +36 -0
- package/dist/rpc/mint-read-token.js.map +1 -0
- package/dist/rpc/promote.d.ts +39 -0
- package/dist/rpc/promote.js +69 -0
- package/dist/rpc/promote.js.map +1 -0
- package/dist/rpc/read-file.d.ts +44 -0
- package/dist/rpc/read-file.js +56 -0
- package/dist/rpc/read-file.js.map +1 -0
- package/dist/rpc/repo-lifecycle.d.ts +39 -0
- package/dist/rpc/repo-lifecycle.js +64 -0
- package/dist/rpc/repo-lifecycle.js.map +1 -0
- package/dist/rpc/resolve-ref.d.ts +15 -0
- package/dist/rpc/resolve-ref.js +28 -0
- package/dist/rpc/resolve-ref.js.map +1 -0
- package/dist/rpc/sync-branch.d.ts +107 -0
- package/dist/rpc/sync-branch.js +114 -0
- package/dist/rpc/sync-branch.js.map +1 -0
- package/dist/schemas/commit-action.d.ts +100 -0
- package/dist/schemas/commit-action.js +138 -0
- package/dist/schemas/commit-action.js.map +1 -0
- package/dist/schemas/commit.d.ts +2 -0
- package/dist/schemas/commit.js +24 -0
- package/dist/schemas/commit.js.map +1 -0
- package/dist/schemas/content-encoding.d.ts +24 -0
- package/dist/schemas/content-encoding.js +41 -0
- package/dist/schemas/content-encoding.js.map +1 -0
- package/dist/schemas/examples.d.ts +38 -0
- package/dist/schemas/examples.js +32 -0
- package/dist/schemas/examples.js.map +1 -0
- package/dist/schemas/gateway-config.d.ts +2 -0
- package/dist/schemas/gateway-config.js +72 -0
- package/dist/schemas/gateway-config.js.map +1 -0
- package/dist/schemas/gateway-error-body.d.ts +2 -0
- package/dist/schemas/gateway-error-body.js +188 -0
- package/dist/schemas/gateway-error-body.js.map +1 -0
- package/dist/schemas/identity.d.ts +21 -0
- package/dist/schemas/identity.js +40 -0
- package/dist/schemas/identity.js.map +1 -0
- package/dist/schemas/names.d.ts +26 -0
- package/dist/schemas/names.js +58 -0
- package/dist/schemas/names.js.map +1 -0
- package/dist/schemas/ref-update.d.ts +10 -0
- package/dist/schemas/ref-update.js +25 -0
- package/dist/schemas/ref-update.js.map +1 -0
- package/dist/schemas/sha.d.ts +8 -0
- package/dist/schemas/sha.js +14 -0
- package/dist/schemas/sha.js.map +1 -0
- package/dist/schemas/tree-entry.d.ts +2 -0
- package/dist/schemas/tree-entry.js +62 -0
- package/dist/schemas/tree-entry.js.map +1 -0
- package/dist/storage/git-remote.d.ts +17 -0
- package/dist/storage/git-remote.js +31 -0
- package/dist/storage/git-remote.js.map +1 -0
- package/dist/storage/storage-backend.d.ts +61 -0
- package/dist/storage/storage-backend.js +0 -0
- package/dist/storage/storage-errors.d.ts +12 -0
- package/dist/storage/storage-errors.js +22 -0
- package/dist/storage/storage-errors.js.map +1 -0
- package/dist/storage/storage-input.d.ts +7 -0
- package/dist/storage/storage-input.js +14 -0
- package/dist/storage/storage-input.js.map +1 -0
- package/dist/storage/storage-read.d.ts +13 -0
- package/dist/storage/storage-read.js +30 -0
- package/dist/storage/storage-read.js.map +1 -0
- package/dist/storage/storage-repo-info.d.ts +16 -0
- package/dist/storage/storage-repo-info.js +30 -0
- package/dist/storage/storage-repo-info.js.map +1 -0
- package/dist/storage/storage-tree-entry.d.ts +21 -0
- package/dist/storage/storage-tree-entry.js +17 -0
- package/dist/storage/storage-tree-entry.js.map +1 -0
- package/dist/tree-entry-BM_1gou1.d.ts +41 -0
- package/dist/tree-index-Bk721Phv.d.ts +14 -0
- package/package.json +401 -4
- package/src/behaviour/assertions.ts +75 -0
- package/src/behaviour/behaviour-case.ts +28 -0
- package/src/behaviour/behaviour-suite.ts +76 -0
- package/src/behaviour/commit-action-cases.ts +253 -0
- package/src/behaviour/commit-write-cases.ts +205 -0
- package/src/behaviour/fixtures.ts +103 -0
- package/src/behaviour/lifecycle-cases.ts +104 -0
- package/src/behaviour/promote-cases.ts +78 -0
- package/src/behaviour/read-cases.ts +176 -0
- package/src/behaviour/sync-cases.ts +156 -0
- package/src/gateway-error.ts +72 -0
- package/src/git-gateway.ts +76 -0
- package/src/health.ts +26 -0
- package/src/package-name.ts +4 -0
- package/src/proxy/audit-event.ts +135 -0
- package/src/proxy/branch-rule.ts +190 -0
- package/src/proxy/gateway-hooks.ts +67 -0
- package/src/proxy/git-operation.ts +36 -0
- package/src/proxy/policy-result.ts +127 -0
- package/src/proxy/principal.ts +81 -0
- package/src/proxy/push-policy-result.ts +86 -0
- package/src/proxy/push-policy.ts +158 -0
- package/src/proxy/request-credential.ts +47 -0
- package/src/proxy/smart-http-config.ts +158 -0
- package/src/reference/apply-actions.ts +96 -0
- package/src/reference/branch-methods.ts +105 -0
- package/src/reference/bytes.ts +66 -0
- package/src/reference/clock.ts +19 -0
- package/src/reference/commit-method.ts +97 -0
- package/src/reference/file-map.ts +35 -0
- package/src/reference/hash.ts +25 -0
- package/src/reference/history.ts +78 -0
- package/src/reference/lifecycle-methods.ts +52 -0
- package/src/reference/read-methods.ts +87 -0
- package/src/reference/reference-gateway.ts +50 -0
- package/src/reference/repo-state.ts +83 -0
- package/src/reference/run-method.ts +25 -0
- package/src/reference/three-way-merge.ts +42 -0
- package/src/reference/trailer.ts +18 -0
- package/src/reference/tree-index.ts +65 -0
- package/src/reference/write-commit.ts +44 -0
- package/src/rpc/commit-exists.ts +46 -0
- package/src/rpc/commit.ts +156 -0
- package/src/rpc/list-tree.ts +78 -0
- package/src/rpc/log.ts +51 -0
- package/src/rpc/mint-read-token.ts +59 -0
- package/src/rpc/promote.ts +72 -0
- package/src/rpc/read-file.ts +75 -0
- package/src/rpc/repo-lifecycle.ts +103 -0
- package/src/rpc/resolve-ref.ts +41 -0
- package/src/rpc/sync-branch.ts +149 -0
- package/src/schemas/commit-action.ts +157 -0
- package/src/schemas/commit.ts +43 -0
- package/src/schemas/content-encoding.ts +58 -0
- package/src/schemas/examples.ts +24 -0
- package/src/schemas/gateway-config.ts +116 -0
- package/src/schemas/gateway-error-body.ts +230 -0
- package/src/schemas/identity.ts +57 -0
- package/src/schemas/names.ts +126 -0
- package/src/schemas/ref-update.ts +38 -0
- package/src/schemas/sha.ts +20 -0
- package/src/schemas/tree-entry.ts +72 -0
- package/src/storage/git-remote.ts +58 -0
- package/src/storage/storage-backend.ts +65 -0
- package/src/storage/storage-errors.ts +24 -0
- package/src/storage/storage-input.ts +14 -0
- package/src/storage/storage-read.ts +45 -0
- package/src/storage/storage-repo-info.ts +46 -0
- package/src/storage/storage-tree-entry.ts +20 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
import { RefDecisionAllow, RefDecisionDeny } from './policy-result.ts';
|
|
5
|
+
|
|
6
|
+
/** What a push policy asks the gateway to compute before it is called. */
|
|
7
|
+
export const PushPolicyNeeds = z
|
|
8
|
+
.object({
|
|
9
|
+
changedPaths: z
|
|
10
|
+
.boolean()
|
|
11
|
+
.describe(
|
|
12
|
+
'true to be given, for every update, the commits it brings with the paths each changes (PushPolicyUpdate.commits). That means buffering and reading the pushed pack and reading trees from storage, bounded by SmartHttpConfig.maxPushPolicyPackBytes, maxPushPolicyObjectBytes, maxPushPolicyReads and maxChangedPaths; a push past a bound is refused as uncheckable. false to decide from the ref updates alone, before the pack is read (`commits` is then null).',
|
|
13
|
+
),
|
|
14
|
+
})
|
|
15
|
+
.strict()
|
|
16
|
+
.meta({
|
|
17
|
+
id: 'PushPolicyNeeds',
|
|
18
|
+
title: 'Push policy needs',
|
|
19
|
+
description:
|
|
20
|
+
'What PushPolicy.needs() returns for a push: whether check() needs the changed paths of every update.',
|
|
21
|
+
examples: [{ changedPaths: true }],
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
/** What a push policy needs, inferred from {@link PushPolicyNeeds}. */
|
|
25
|
+
export type PushPolicyNeeds = z.infer<typeof PushPolicyNeeds>;
|
|
26
|
+
|
|
27
|
+
/** A push policy's verdict on one ref update: allow or deny. */
|
|
28
|
+
export const PushRefDecision = z.discriminatedUnion('decision', [RefDecisionAllow, RefDecisionDeny]).meta({
|
|
29
|
+
id: 'PushRefDecision',
|
|
30
|
+
title: 'Push ref decision',
|
|
31
|
+
description: 'The push policy verdict on one ref update: allow it, or deny it with a reason git shows.',
|
|
32
|
+
examples: [
|
|
33
|
+
{ ref: 'refs/heads/main', decision: 'deny', reason: 'generated/ is written by the build pipeline' },
|
|
34
|
+
],
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
/** A push policy's verdict on one update, inferred from {@link PushRefDecision}. */
|
|
38
|
+
export type PushRefDecision = z.infer<typeof PushRefDecision>;
|
|
39
|
+
|
|
40
|
+
/** What PushPolicy.check() returns. */
|
|
41
|
+
export const PushPolicyResult = z
|
|
42
|
+
.object({
|
|
43
|
+
refs: z
|
|
44
|
+
.array(PushRefDecision)
|
|
45
|
+
.max(10_000)
|
|
46
|
+
.superRefine((decisions, ctx) => {
|
|
47
|
+
const seen = new Set<string>();
|
|
48
|
+
decisions.forEach((decision, index) => {
|
|
49
|
+
if (seen.has(decision.ref)) {
|
|
50
|
+
ctx.addIssue({
|
|
51
|
+
code: 'custom',
|
|
52
|
+
message: `the ref ${decision.ref} has more than one decision; a push policy result names each ref once`,
|
|
53
|
+
path: [index, 'ref'],
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
seen.add(decision.ref);
|
|
57
|
+
});
|
|
58
|
+
})
|
|
59
|
+
.describe(
|
|
60
|
+
'One decision per update the policy was given, each ref named at most once. An update without a decision is refused, and a push is written only when every update is allowed; otherwise each refused ref is reported with its reason and the rest as "not updated: another update in this push was refused".',
|
|
61
|
+
),
|
|
62
|
+
})
|
|
63
|
+
.strict()
|
|
64
|
+
.meta({
|
|
65
|
+
id: 'PushPolicyResult',
|
|
66
|
+
title: 'Push policy result',
|
|
67
|
+
description:
|
|
68
|
+
'The push policy verdict on every ref update of one push. It runs after the branch policy, and the push is written only when both allow every update.',
|
|
69
|
+
examples: [
|
|
70
|
+
{
|
|
71
|
+
refs: [
|
|
72
|
+
{
|
|
73
|
+
ref: 'refs/heads/main',
|
|
74
|
+
decision: 'deny',
|
|
75
|
+
reason: 'files over 10 MiB are not accepted: video.mp4',
|
|
76
|
+
},
|
|
77
|
+
{ ref: 'refs/heads/feature/x', decision: 'allow' },
|
|
78
|
+
],
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
/** A push policy verdict, inferred from {@link PushPolicyResult}. */
|
|
84
|
+
export type PushPolicyResult = z.infer<typeof PushPolicyResult>;
|
|
85
|
+
/** A push policy verdict as a policy may write it, inferred from {@link PushPolicyResult}. */
|
|
86
|
+
export type PushPolicyResultInput = z.input<typeof PushPolicyResult>;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
import { FullRefName, RepoName } from '../schemas/names.ts';
|
|
5
|
+
import { Sha } from '../schemas/sha.ts';
|
|
6
|
+
import { FileMode } from '../schemas/tree-entry.ts';
|
|
7
|
+
import { Principal } from './principal.ts';
|
|
8
|
+
|
|
9
|
+
const EXAMPLE_OLD = '1a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d';
|
|
10
|
+
const EXAMPLE_NEW = '8f3c2a9e1b7d4c6f0a5e2d9b8c7a6f5e4d3c2b1a';
|
|
11
|
+
const EXAMPLE_BLOB = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391';
|
|
12
|
+
|
|
13
|
+
/** One file a pushed commit adds, changes or removes. */
|
|
14
|
+
export const ChangedPath = z
|
|
15
|
+
.object({
|
|
16
|
+
path: z
|
|
17
|
+
.string()
|
|
18
|
+
.min(1)
|
|
19
|
+
.max(4096)
|
|
20
|
+
.regex(/^[^\0]+$/)
|
|
21
|
+
.describe(
|
|
22
|
+
'The file\'s path from the repository root, "/"-separated, exactly as the tree names it (any bytes git allows except NUL, decoded as UTF-8); 1–4096 characters. Always a file, symlink or submodule, never a directory: a directory added or removed appears as each file in it.',
|
|
23
|
+
),
|
|
24
|
+
change: z.enum(['added', 'modified', 'deleted']).meta({
|
|
25
|
+
description: 'What the commit did to the path.',
|
|
26
|
+
enumDescriptions: {
|
|
27
|
+
added: 'The path is new: no entry before (a file replacing a directory is added too).',
|
|
28
|
+
modified: 'The path existed and its content or mode changed.',
|
|
29
|
+
deleted: 'The path is gone (a directory replacing a file deletes the file).',
|
|
30
|
+
},
|
|
31
|
+
}),
|
|
32
|
+
oldMode: FileMode.nullable().describe('The mode before the commit; null when the path was added.'),
|
|
33
|
+
newMode: FileMode.nullable().describe('The mode after the commit; null when the path was deleted.'),
|
|
34
|
+
oldSha: Sha.nullable().describe('The blob (or submodule commit) before the commit; null when added.'),
|
|
35
|
+
newSha: Sha.nullable().describe('The blob (or submodule commit) after the commit; null when deleted.'),
|
|
36
|
+
size: z
|
|
37
|
+
.int()
|
|
38
|
+
.min(0)
|
|
39
|
+
.nullable()
|
|
40
|
+
.describe(
|
|
41
|
+
'Size in bytes of the new blob (for a symlink, of its target text); null when the path was deleted or is a submodule (mode 160000).',
|
|
42
|
+
),
|
|
43
|
+
})
|
|
44
|
+
.strict()
|
|
45
|
+
.meta({
|
|
46
|
+
id: 'ChangedPath',
|
|
47
|
+
title: 'Changed path',
|
|
48
|
+
description:
|
|
49
|
+
"One path a pushed commit changed, from comparing its tree with its parent's: the change, the modes and ids on both sides, and the new blob's size.",
|
|
50
|
+
examples: [
|
|
51
|
+
{
|
|
52
|
+
path: 'content/pages/about.md',
|
|
53
|
+
change: 'modified',
|
|
54
|
+
oldMode: '100644',
|
|
55
|
+
newMode: '100644',
|
|
56
|
+
oldSha: EXAMPLE_BLOB,
|
|
57
|
+
newSha: EXAMPLE_OLD,
|
|
58
|
+
size: 1832,
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
/** A changed path, inferred from {@link ChangedPath}. */
|
|
64
|
+
export type ChangedPath = z.infer<typeof ChangedPath>;
|
|
65
|
+
|
|
66
|
+
/** A commit a push brings that storage did not have, with the paths it changes. */
|
|
67
|
+
export const PushedCommit = z
|
|
68
|
+
.object({
|
|
69
|
+
sha: Sha.describe('The commit id.'),
|
|
70
|
+
parents: z.array(Sha.describe('A parent commit id.')).max(1000).describe('Parent ids in order.'),
|
|
71
|
+
changes: z
|
|
72
|
+
.array(ChangedPath)
|
|
73
|
+
.max(100_000)
|
|
74
|
+
.describe(
|
|
75
|
+
"The paths the commit changes. Against its parent for an ordinary commit and against the empty tree for a root commit; for a merge, only the paths whose result differs from every parent (git's combined diff), since anything matching one parent came from that parent. Sorted by path.",
|
|
76
|
+
),
|
|
77
|
+
})
|
|
78
|
+
.strict()
|
|
79
|
+
.meta({
|
|
80
|
+
id: 'PushedCommit',
|
|
81
|
+
title: 'Pushed commit',
|
|
82
|
+
description:
|
|
83
|
+
'A commit new to the repository that a ref update brings (it is in the pushed pack), with the paths it changes.',
|
|
84
|
+
examples: [
|
|
85
|
+
{
|
|
86
|
+
sha: EXAMPLE_NEW,
|
|
87
|
+
parents: [EXAMPLE_OLD],
|
|
88
|
+
changes: [
|
|
89
|
+
{
|
|
90
|
+
path: 'README.md',
|
|
91
|
+
change: 'added',
|
|
92
|
+
oldMode: null,
|
|
93
|
+
newMode: '100644',
|
|
94
|
+
oldSha: null,
|
|
95
|
+
newSha: EXAMPLE_BLOB,
|
|
96
|
+
size: 0,
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
/** A pushed commit, inferred from {@link PushedCommit}. */
|
|
104
|
+
export type PushedCommit = z.infer<typeof PushedCommit>;
|
|
105
|
+
|
|
106
|
+
/** One ref update of a push, as a push policy sees it. */
|
|
107
|
+
export const PushPolicyUpdate = z
|
|
108
|
+
.object({
|
|
109
|
+
ref: FullRefName.describe('The ref the update names, fully qualified.'),
|
|
110
|
+
oldSha: Sha.describe('Id the ref names now; the all-zero id when the update creates the ref.'),
|
|
111
|
+
newSha: Sha.describe('Id the ref will name; the all-zero id when the update deletes the ref.'),
|
|
112
|
+
commits: z
|
|
113
|
+
.array(PushedCommit)
|
|
114
|
+
.max(100_000)
|
|
115
|
+
.nullable()
|
|
116
|
+
.describe(
|
|
117
|
+
'The commits new to the repository that the update brings — those of the pushed pack reachable from `newSha` — newest first (breadth-first from `newSha`), each with its changed paths. Empty for a deletion, or when `newSha` is a commit storage already has. Null when the policy did not ask for changed paths (PushPolicyNeeds.changedPaths false).',
|
|
118
|
+
),
|
|
119
|
+
})
|
|
120
|
+
.strict()
|
|
121
|
+
.meta({
|
|
122
|
+
id: 'PushPolicyUpdate',
|
|
123
|
+
title: 'Push policy update',
|
|
124
|
+
description: 'One ref update of a push and, when the policy asked for them, the commits it brings.',
|
|
125
|
+
examples: [{ ref: 'refs/heads/main', oldSha: EXAMPLE_OLD, newSha: EXAMPLE_NEW, commits: null }],
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
/** A ref update as a push policy sees it, inferred from {@link PushPolicyUpdate}. */
|
|
129
|
+
export type PushPolicyUpdate = z.infer<typeof PushPolicyUpdate>;
|
|
130
|
+
|
|
131
|
+
/** What a PushPolicy is asked to judge. */
|
|
132
|
+
export const PushPolicyInput = z
|
|
133
|
+
.object({
|
|
134
|
+
principal: Principal.describe('Who is pushing, as the Authenticator established it.'),
|
|
135
|
+
repo: RepoName.describe('The repository pushed to.'),
|
|
136
|
+
updates: z
|
|
137
|
+
.array(PushPolicyUpdate)
|
|
138
|
+
.min(1)
|
|
139
|
+
.max(10_000)
|
|
140
|
+
.describe('Every update of the push, in the order the client sent them; all passed the branch policy.'),
|
|
141
|
+
})
|
|
142
|
+
.strict()
|
|
143
|
+
.meta({
|
|
144
|
+
id: 'PushPolicyInput',
|
|
145
|
+
title: 'Push policy input',
|
|
146
|
+
description:
|
|
147
|
+
'What PushPolicy.check() is given: the principal, the repository and every ref update of a push the branch policy allowed, with changed paths when the policy asked for them.',
|
|
148
|
+
examples: [
|
|
149
|
+
{
|
|
150
|
+
principal: { subject: 'alice', name: null, email: null, roles: ['editor'], expiresAt: null },
|
|
151
|
+
repo: 'acme-site',
|
|
152
|
+
updates: [{ ref: 'refs/heads/main', oldSha: EXAMPLE_OLD, newSha: EXAMPLE_NEW, commits: null }],
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
/** A push policy's input, inferred from {@link PushPolicyInput}. */
|
|
158
|
+
export type PushPolicyInput = z.infer<typeof PushPolicyInput>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
|
|
3
|
+
/** The credential a request carries. */
|
|
4
|
+
export interface RequestCredential {
|
|
5
|
+
/** How it was sent. */
|
|
6
|
+
readonly scheme: 'basic' | 'bearer';
|
|
7
|
+
/** The HTTP Basic user name (git's credential "username"); null for a Bearer token. */
|
|
8
|
+
readonly username: string | null;
|
|
9
|
+
/** The bearer credential: the Bearer token, or the HTTP Basic password. Secret. */
|
|
10
|
+
readonly secret: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The credential in a request's Authorization header. git sends credentials as HTTP Basic, and
|
|
15
|
+
* the password is treated as a bearer credential (an OAuth access token, a JWT, an API key), so a
|
|
16
|
+
* credential helper can supply one; a Bearer header is accepted too. Null when there is none, or
|
|
17
|
+
* the header is malformed or carries an empty secret.
|
|
18
|
+
*/
|
|
19
|
+
export function readCredential(request: Request): RequestCredential | null {
|
|
20
|
+
const header = request.headers.get('Authorization')?.trim();
|
|
21
|
+
if (header === undefined) return null;
|
|
22
|
+
const space = header.indexOf(' ');
|
|
23
|
+
if (space === -1) return null;
|
|
24
|
+
const scheme = header.slice(0, space).toLowerCase();
|
|
25
|
+
const value = header.slice(space + 1).trim();
|
|
26
|
+
if (scheme === 'bearer') return value === '' ? null : { scheme, username: null, secret: value };
|
|
27
|
+
if (scheme !== 'basic') return null;
|
|
28
|
+
let decoded: string;
|
|
29
|
+
try {
|
|
30
|
+
decoded = new TextDecoder().decode(Uint8Array.from(atob(value), (char) => char.codePointAt(0) ?? 0));
|
|
31
|
+
} catch {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
const colon = decoded.indexOf(':');
|
|
35
|
+
if (colon === -1) return null;
|
|
36
|
+
const secret = decoded.slice(colon + 1);
|
|
37
|
+
return secret === '' ? null : { scheme, username: decoded.slice(0, colon), secret };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A SHA-256 fingerprint of a credential (lowercase hex), so caches can key on it without holding
|
|
42
|
+
* the credential.
|
|
43
|
+
*/
|
|
44
|
+
export async function credentialFingerprint(credential: RequestCredential): Promise<string> {
|
|
45
|
+
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(credential.secret));
|
|
46
|
+
return [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, '0')).join('');
|
|
47
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
/** Settings of the smart-HTTP proxy beyond those every gateway shares (GatewayConfig). */
|
|
5
|
+
export const SmartHttpConfig = z
|
|
6
|
+
.object({
|
|
7
|
+
realm: z
|
|
8
|
+
.string()
|
|
9
|
+
.min(1)
|
|
10
|
+
.max(100)
|
|
11
|
+
.regex(/^[^"\\\n\0]+$/)
|
|
12
|
+
.default('git-gateway')
|
|
13
|
+
.describe(
|
|
14
|
+
'Realm named in the WWW-Authenticate: Basic challenge of a 401, which git credential helpers show and key on; 1–100 characters without quotes, backslash, newline or NUL. Default "git-gateway".',
|
|
15
|
+
),
|
|
16
|
+
maxPushHeadBytes: z
|
|
17
|
+
.int()
|
|
18
|
+
.min(4096)
|
|
19
|
+
.max(64 * 1024 * 1024)
|
|
20
|
+
.default(8 * 1024 * 1024)
|
|
21
|
+
.describe(
|
|
22
|
+
'Most bytes of a push read before the pack (update commands, capabilities, push options); a larger head is refused. 4 KiB–64 MiB, default 8 MiB (8388608).',
|
|
23
|
+
),
|
|
24
|
+
maxAncestryReads: z
|
|
25
|
+
.int()
|
|
26
|
+
.min(1)
|
|
27
|
+
.max(100_000)
|
|
28
|
+
.default(2000)
|
|
29
|
+
.describe(
|
|
30
|
+
'Most commits read from the storage backend to prove that a fast-forward-only update is a fast-forward; past it the update is refused as unverifiable. 1–100000, default 2000.',
|
|
31
|
+
),
|
|
32
|
+
maxScannedCommitBytes: z
|
|
33
|
+
.int()
|
|
34
|
+
.min(1024 * 1024)
|
|
35
|
+
.max(256 * 1024 * 1024)
|
|
36
|
+
.default(32 * 1024 * 1024)
|
|
37
|
+
.describe(
|
|
38
|
+
'Most bytes of commit objects kept while scanning a pushed pack for ancestry; a push needing more is refused as unverifiable. 1 MiB–256 MiB, default 32 MiB (33554432).',
|
|
39
|
+
),
|
|
40
|
+
maxVerdictWaitMs: z
|
|
41
|
+
.int()
|
|
42
|
+
.min(0)
|
|
43
|
+
.max(600_000)
|
|
44
|
+
.default(60_000)
|
|
45
|
+
.describe(
|
|
46
|
+
'Most milliseconds a push waits, once the storage host\'s request has failed, for the gateway to finish proving the push from its pack, so that a refusal (which makes the host\'s request fail by design) is reported in-protocol as "ng <ref> <reason>" rather than as a 502. Past it, or when no verdict comes, the push is answered 502 and nothing is written. 0–600000, default 60000 (one minute).',
|
|
47
|
+
),
|
|
48
|
+
maxPushPolicyPackBytes: z
|
|
49
|
+
.int()
|
|
50
|
+
.min(1024 * 1024)
|
|
51
|
+
.max(256 * 1024 * 1024)
|
|
52
|
+
.default(32 * 1024 * 1024)
|
|
53
|
+
.describe(
|
|
54
|
+
'Most bytes of a pushed pack held in memory so the push policy can be given changed paths (only when PushPolicy.needs() asks for them); a larger push is refused as uncheckable before it completes. 1 MiB–256 MiB, default 32 MiB (33554432).',
|
|
55
|
+
),
|
|
56
|
+
maxPushPolicyObjectBytes: z
|
|
57
|
+
.int()
|
|
58
|
+
.min(1024 * 1024)
|
|
59
|
+
.max(512 * 1024 * 1024)
|
|
60
|
+
.default(64 * 1024 * 1024)
|
|
61
|
+
.describe(
|
|
62
|
+
"Most bytes of objects decompressed and rebuilt from the pushed pack (every entry's data plus every delta's result, thin-pack bases read from storage included) to compute changed paths; past it the push is refused as uncheckable. 1 MiB–512 MiB, default 64 MiB (67108864).",
|
|
63
|
+
),
|
|
64
|
+
maxPushPolicyReads: z
|
|
65
|
+
.int()
|
|
66
|
+
.min(1)
|
|
67
|
+
.max(100_000)
|
|
68
|
+
.default(2000)
|
|
69
|
+
.describe(
|
|
70
|
+
'Most storage backend calls made to compute changed paths for the push policy — parent commits, trees and blobs not in the pushed pack (thin-pack delta bases, unchanged subtrees of a moved directory, sizes of blobs storage already has); past it the push is refused as uncheckable. 1–100000, default 2000.',
|
|
71
|
+
),
|
|
72
|
+
maxChangedPaths: z
|
|
73
|
+
.int()
|
|
74
|
+
.min(1)
|
|
75
|
+
.max(100_000)
|
|
76
|
+
.default(10_000)
|
|
77
|
+
.describe(
|
|
78
|
+
'Most changed paths, summed over every commit of a push, given to the push policy; a push changing more is refused as uncheckable. 1–100000, default 10000.',
|
|
79
|
+
),
|
|
80
|
+
decisionCacheEntries: z
|
|
81
|
+
.int()
|
|
82
|
+
.min(0)
|
|
83
|
+
.max(1_000_000)
|
|
84
|
+
.default(10_000)
|
|
85
|
+
.describe(
|
|
86
|
+
'Most authentication and authorization decisions held in the per-isolate cache, oldest evicted first; 0 disables the cache. Default 10000. How long entries live is GatewayConfig.authCacheSeconds.',
|
|
87
|
+
),
|
|
88
|
+
maxAuditReportBytes: z
|
|
89
|
+
.int()
|
|
90
|
+
.min(0)
|
|
91
|
+
.max(16 * 1024 * 1024)
|
|
92
|
+
.default(1024 * 1024)
|
|
93
|
+
.describe(
|
|
94
|
+
'Most bytes of a storage host push response kept to read its per-ref report for the audit event and the push event; past it the refs are audited as "unknown" and no push event is sent. 0 skips reading reports for audit; when an onPush hook is set the report is still read, up to the 1 MiB default. Default 1 MiB (1048576).',
|
|
95
|
+
),
|
|
96
|
+
allowedPushOptions: z
|
|
97
|
+
.array(
|
|
98
|
+
z
|
|
99
|
+
.string()
|
|
100
|
+
.min(1)
|
|
101
|
+
.max(100)
|
|
102
|
+
.regex(/^[^\s\0=]+$/)
|
|
103
|
+
.describe(
|
|
104
|
+
'A push option name, the part of a "git push -o <name>[=<value>]" option before the first "="; 1–100 characters without whitespace, NUL or "=". Matched exactly and case-sensitively.',
|
|
105
|
+
),
|
|
106
|
+
)
|
|
107
|
+
.max(100)
|
|
108
|
+
.default([])
|
|
109
|
+
.describe(
|
|
110
|
+
"Push option names the proxy forwards to the storage host; a push carrying any other option is refused before it reaches storage. The host runs push options as the gateway's own credential (GitLab's merge_request.* and ci.variable, for example), so each one bypasses branch policy unless refused here. At most 100; default none, so every push option is refused.",
|
|
111
|
+
),
|
|
112
|
+
deniedStatus: z
|
|
113
|
+
.union([
|
|
114
|
+
z
|
|
115
|
+
.literal('err')
|
|
116
|
+
.describe(
|
|
117
|
+
'Answer in-protocol: HTTP 200 with an "ERR" packet, which git prints as "remote error: <reason>".',
|
|
118
|
+
),
|
|
119
|
+
z
|
|
120
|
+
.literal(403)
|
|
121
|
+
.describe(
|
|
122
|
+
'Answer HTTP 403 with the reason as a plain-text body, which git prints as "The requested URL returned error: 403".',
|
|
123
|
+
),
|
|
124
|
+
])
|
|
125
|
+
.default('err')
|
|
126
|
+
.describe(
|
|
127
|
+
'How an authorization refusal (the Authorizer denying read or write of a repository) is answered: "err" (default) or 403. Branch-policy refusals of individual refs are always in-protocol ("ng" or "ERR"), so a push to a protected branch keeps its readable reason either way.',
|
|
128
|
+
),
|
|
129
|
+
})
|
|
130
|
+
.strict()
|
|
131
|
+
.meta({
|
|
132
|
+
id: 'SmartHttpConfig',
|
|
133
|
+
title: 'Smart-HTTP proxy configuration',
|
|
134
|
+
description:
|
|
135
|
+
'Limits and names used by the smart-HTTP proxy. Every field has a default, so {} is a complete configuration.',
|
|
136
|
+
examples: [
|
|
137
|
+
{
|
|
138
|
+
realm: 'git-gateway',
|
|
139
|
+
maxPushHeadBytes: 8_388_608,
|
|
140
|
+
maxAncestryReads: 2000,
|
|
141
|
+
maxScannedCommitBytes: 33_554_432,
|
|
142
|
+
maxVerdictWaitMs: 60_000,
|
|
143
|
+
maxPushPolicyPackBytes: 33_554_432,
|
|
144
|
+
maxPushPolicyObjectBytes: 67_108_864,
|
|
145
|
+
maxPushPolicyReads: 2000,
|
|
146
|
+
maxChangedPaths: 10_000,
|
|
147
|
+
decisionCacheEntries: 10_000,
|
|
148
|
+
maxAuditReportBytes: 1_048_576,
|
|
149
|
+
allowedPushOptions: ['ci.skip'],
|
|
150
|
+
deniedStatus: 'err',
|
|
151
|
+
},
|
|
152
|
+
],
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
/** A smart-HTTP configuration as written (defaults optional). */
|
|
156
|
+
export type SmartHttpConfigInput = z.input<typeof SmartHttpConfig>;
|
|
157
|
+
/** A complete smart-HTTP configuration, inferred from {@link SmartHttpConfig}. */
|
|
158
|
+
export type SmartHttpConfig = z.output<typeof SmartHttpConfig>;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { gatewayError } from '../gateway-error.ts';
|
|
3
|
+
import type { CommitAction } from '../schemas/commit-action.ts';
|
|
4
|
+
import { decodeContent } from './bytes.ts';
|
|
5
|
+
import { fileParent, isDirectory, type FileMap, type StoredFile } from './file-map.ts';
|
|
6
|
+
import { blobSha } from './hash.ts';
|
|
7
|
+
|
|
8
|
+
async function storedFile(
|
|
9
|
+
content: string,
|
|
10
|
+
encoding: 'text' | 'base64',
|
|
11
|
+
executable: boolean,
|
|
12
|
+
): Promise<StoredFile> {
|
|
13
|
+
const bytes = decodeContent(content, encoding);
|
|
14
|
+
return { bytes, executable, sha: await blobSha(bytes) };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Throws path_exists or path_conflict unless `path` is free to receive a new file. */
|
|
18
|
+
function requireFree(files: FileMap, path: string): void {
|
|
19
|
+
if (files.has(path) || isDirectory(files, path)) {
|
|
20
|
+
throw gatewayError('path_exists', `Path already exists: ${path}`, { path });
|
|
21
|
+
}
|
|
22
|
+
requireNoFileParent(files, path);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Throws path_conflict when a parent of `path` is a file. */
|
|
26
|
+
function requireNoFileParent(files: FileMap, path: string): void {
|
|
27
|
+
const conflictingPath = fileParent(files, path);
|
|
28
|
+
if (conflictingPath !== undefined) {
|
|
29
|
+
throw gatewayError('path_conflict', `A parent of ${path} is a file: ${conflictingPath}`, {
|
|
30
|
+
path,
|
|
31
|
+
conflictingPath,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The existing file at `path`, or path_not_found. */
|
|
37
|
+
function requireFile(files: FileMap, path: string): StoredFile {
|
|
38
|
+
const file = files.get(path);
|
|
39
|
+
if (file === undefined) throw gatewayError('path_not_found', `File not found: ${path}`, { path });
|
|
40
|
+
return file;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function applyOne(files: Map<string, StoredFile>, action: CommitAction): Promise<void> {
|
|
44
|
+
switch (action.action) {
|
|
45
|
+
case 'create':
|
|
46
|
+
requireFree(files, action.path);
|
|
47
|
+
files.set(action.path, await storedFile(action.content, action.encoding, false));
|
|
48
|
+
return;
|
|
49
|
+
case 'update': {
|
|
50
|
+
const existing = requireFile(files, action.path);
|
|
51
|
+
files.set(action.path, await storedFile(action.content, action.encoding, existing.executable));
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
case 'upsert': {
|
|
55
|
+
const existing = files.get(action.path);
|
|
56
|
+
if (existing === undefined) requireFree(files, action.path);
|
|
57
|
+
files.set(
|
|
58
|
+
action.path,
|
|
59
|
+
await storedFile(action.content, action.encoding, existing?.executable ?? false),
|
|
60
|
+
);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
case 'delete':
|
|
64
|
+
requireFile(files, action.path);
|
|
65
|
+
files.delete(action.path);
|
|
66
|
+
return;
|
|
67
|
+
case 'move': {
|
|
68
|
+
const existing = requireFile(files, action.previousPath);
|
|
69
|
+
// The source leaves first, so a file may move beneath its own old path (a → a/b).
|
|
70
|
+
files.delete(action.previousPath);
|
|
71
|
+
requireFree(files, action.path);
|
|
72
|
+
const moved =
|
|
73
|
+
action.content === undefined
|
|
74
|
+
? existing
|
|
75
|
+
: await storedFile(action.content, action.encoding, existing.executable);
|
|
76
|
+
files.set(action.path, moved);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
case 'chmod': {
|
|
80
|
+
const existing = requireFile(files, action.path);
|
|
81
|
+
files.set(action.path, { ...existing, executable: action.executeFilemode });
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Applies commit actions in order, each to the result of the one before, with the GitLab
|
|
89
|
+
* Commits API rules (plus upsert). Returns the new snapshot; throws a GatewayError on the first
|
|
90
|
+
* action that breaks a rule, leaving `files` untouched.
|
|
91
|
+
*/
|
|
92
|
+
export async function applyActions(files: FileMap, actions: readonly CommitAction[]): Promise<FileMap> {
|
|
93
|
+
const next = new Map(files);
|
|
94
|
+
for (const action of actions) await applyOne(next, action);
|
|
95
|
+
return next;
|
|
96
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { RPC_SCHEMAS, type RpcInput, type RpcOutput, type RpcParsedInput } from '../git-gateway.ts';
|
|
3
|
+
import { gatewayError } from '../gateway-error.ts';
|
|
4
|
+
import { ZERO_SHA } from '../schemas/sha.ts';
|
|
5
|
+
import { contains, mergeBase } from './history.ts';
|
|
6
|
+
import {
|
|
7
|
+
lookupIn,
|
|
8
|
+
requireBranchHead,
|
|
9
|
+
requireRepo,
|
|
10
|
+
withRepoLock,
|
|
11
|
+
type ReferenceContext,
|
|
12
|
+
type RepoState,
|
|
13
|
+
} from './repo-state.ts';
|
|
14
|
+
import { runMethod } from './run-method.ts';
|
|
15
|
+
import { threeWayMerge } from './three-way-merge.ts';
|
|
16
|
+
import { writeCommit } from './write-commit.ts';
|
|
17
|
+
|
|
18
|
+
const branchRef = (branch: string): string => `refs/heads/${branch}`;
|
|
19
|
+
|
|
20
|
+
async function syncLocked(
|
|
21
|
+
context: ReferenceContext,
|
|
22
|
+
repo: RepoState,
|
|
23
|
+
input: RpcParsedInput<'syncBranch'>,
|
|
24
|
+
): Promise<RpcOutput<'syncBranch'>> {
|
|
25
|
+
const fromHead = requireBranchHead(repo, input.from);
|
|
26
|
+
const intoHead = requireBranchHead(repo, input.into);
|
|
27
|
+
const lookup = lookupIn(repo);
|
|
28
|
+
if (contains(lookup, intoHead, fromHead)) return { status: 'up_to_date', head: intoHead };
|
|
29
|
+
const refUpdate = (newSha: string) => ({ ref: branchRef(input.into), oldSha: intoHead, newSha });
|
|
30
|
+
if (contains(lookup, fromHead, intoHead)) {
|
|
31
|
+
repo.branches.set(input.into, fromHead);
|
|
32
|
+
return { status: 'fast_forward', refUpdate: refUpdate(fromHead) };
|
|
33
|
+
}
|
|
34
|
+
const base = mergeBase(lookup, intoHead, fromHead);
|
|
35
|
+
if (base === null) {
|
|
36
|
+
throw gatewayError('unrelated_histories', `${input.from} and ${input.into} share no history`, {
|
|
37
|
+
from: input.from,
|
|
38
|
+
into: input.into,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
const files = (sha: string) => repo.commits.get(sha)?.files ?? new Map();
|
|
42
|
+
const merged = threeWayMerge(files(base), files(intoHead), files(fromHead));
|
|
43
|
+
if (merged.kind === 'conflict') {
|
|
44
|
+
return { status: 'conflict', paths: [...merged.paths], base, fromHead, intoHead };
|
|
45
|
+
}
|
|
46
|
+
const identity = input.author ?? context.config.serviceIdentity;
|
|
47
|
+
const stored = await writeCommit(repo, {
|
|
48
|
+
parents: [intoHead, fromHead],
|
|
49
|
+
files: merged.files,
|
|
50
|
+
author: identity,
|
|
51
|
+
committer: identity,
|
|
52
|
+
message: input.message ?? `Merge branch '${input.from}' into ${input.into}`,
|
|
53
|
+
date: context.now().toISOString(),
|
|
54
|
+
});
|
|
55
|
+
repo.branches.set(input.into, stored.commit.sha);
|
|
56
|
+
return { status: 'merged', commit: stored.commit, refUpdate: refUpdate(stored.commit.sha) };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The reference `syncBranch`: up to date, fast-forward, file-level merge, or conflict with paths. */
|
|
60
|
+
export function referenceSyncBranch(
|
|
61
|
+
context: ReferenceContext,
|
|
62
|
+
input: RpcInput<'syncBranch'>,
|
|
63
|
+
): Promise<RpcOutput<'syncBranch'>> {
|
|
64
|
+
return runMethod(RPC_SCHEMAS.syncBranch, input, async (parsed) => {
|
|
65
|
+
const repo = requireRepo(context, parsed.repo);
|
|
66
|
+
return withRepoLock(repo, () => syncLocked(context, repo, parsed));
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function promoteLocked(repo: RepoState, input: RpcParsedInput<'promote'>): RpcOutput<'promote'> {
|
|
71
|
+
const { from, into, sha } = input;
|
|
72
|
+
const fromHead = requireBranchHead(repo, from);
|
|
73
|
+
if (fromHead !== sha) {
|
|
74
|
+
throw gatewayError('not_head_of_source', `${sha} is not the head of ${from}`, {
|
|
75
|
+
from,
|
|
76
|
+
sha,
|
|
77
|
+
head: fromHead,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
const intoHead = repo.branches.get(into);
|
|
81
|
+
if (intoHead === sha) return { status: 'up_to_date', head: sha };
|
|
82
|
+
if (intoHead !== undefined && !contains(lookupIn(repo), sha, intoHead)) {
|
|
83
|
+
throw gatewayError('not_fast_forward', `${sha} does not contain the head of ${into}`, {
|
|
84
|
+
into,
|
|
85
|
+
sha,
|
|
86
|
+
head: intoHead,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
repo.branches.set(into, sha);
|
|
90
|
+
return {
|
|
91
|
+
status: 'promoted',
|
|
92
|
+
refUpdate: { ref: branchRef(into), oldSha: intoHead ?? ZERO_SHA, newSha: sha },
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The reference `promote`: fast-forward only, from the source head only. */
|
|
97
|
+
export function referencePromote(
|
|
98
|
+
context: ReferenceContext,
|
|
99
|
+
input: RpcInput<'promote'>,
|
|
100
|
+
): Promise<RpcOutput<'promote'>> {
|
|
101
|
+
return runMethod(RPC_SCHEMAS.promote, input, async (parsed) => {
|
|
102
|
+
const repo = requireRepo(context, parsed.repo);
|
|
103
|
+
return withRepoLock(repo, async () => promoteLocked(repo, parsed));
|
|
104
|
+
});
|
|
105
|
+
}
|