-
Notifications
You must be signed in to change notification settings - Fork 36
/
validate-rebase.ts
53 lines (48 loc) · 1.66 KB
/
validate-rebase.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type * as dag from '../dag/mod';
import * as db from '../db/mod';
import * as sync from './mod';
import type {ReadonlyJSONValue} from '../json';
import type {Hash} from '../hash';
export type RebaseOpts = {
basis: Hash;
original: Hash;
};
export async function validateRebase(
opts: RebaseOpts,
dagRead: dag.Read,
mutatorName: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_args: ReadonlyJSONValue | undefined,
): Promise<void> {
// Ensure the rebase commit is going on top of the current sync head.
const syncHeadHash = await dagRead.getHead(sync.SYNC_HEAD_NAME);
if (syncHeadHash !== opts.basis) {
throw new Error(
`WrongSyncHeadJSLogInfo: sync head is ${syncHeadHash}, transaction basis is ${opts.basis}`,
);
}
// Ensure rebase and original commit mutator names match.
const [, original] = await db.readCommit(
db.whenceHash(opts.original),
dagRead,
);
if (original.isLocal()) {
const lm = original.meta;
if (lm.mutatorName !== mutatorName) {
throw new Error(
`Inconsistent mutator: original: ${lm.mutatorName}, request: ${mutatorName}`,
);
}
} else {
throw new Error('Internal programmer error: Commit is not a local commit');
}
// Ensure rebase and original commit mutation ids names match.
const [, basis] = await db.readCommit(db.whenceHash(opts.basis), dagRead);
if (basis.nextMutationID !== original.mutationID) {
throw new Error(
`Inconsistent mutation ID: original: ${original.mutationID}, next: ${basis.nextMutationID}`,
);
}
// TODO: temporarily skipping check that args are the same.
// https://github.com/rocicorp/repc/issues/151
}